From 5ec438e5e6991a1556aea8a5125e55274930c905 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Wed, 7 Aug 2013 20:55:46 +0200 Subject: [PATCH 1/4] Repository.Submodules.Add created --- LibGit2Sharp/Core/NativeMethods.cs | 12 +++++++++++ LibGit2Sharp/Core/Proxy.cs | 20 +++++++++++++++++++ LibGit2Sharp/SubmoduleCollection.cs | 31 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 5a4a08735..751c9deed 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -1036,6 +1036,18 @@ internal delegate int git_status_cb( [DllImport(libgit2)] internal static extern int git_status_foreach(RepositorySafeHandle repo, git_status_cb cb, IntPtr payload); + [DllImport(libgit2)] + internal static extern int git_submodule_add_setup( + out SubmoduleSafeHandle reference, + RepositorySafeHandle repo, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(Utf8Marshaler))] string url, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath path, + int use_gitlink); + + [DllImport(libgit2)] + internal static extern int git_submodule_add_finalize( + SubmoduleSafeHandle submodule); + [DllImport(libgit2)] internal static extern int git_submodule_lookup( out SubmoduleSafeHandle reference, diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 691136931..4ffe746b1 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -2010,6 +2010,26 @@ public static ICollection git_submodule_foreach(RepositorySafe return git_foreach(resultSelector, c => NativeMethods.git_submodule_foreach(repo, (x, y, p) => c(x, y, p), IntPtr.Zero)); } + public static SubmoduleSafeHandle git_submodule_add_setup(RepositorySafeHandle repo, string url, FilePath path, int use_gitlink) + { + using (ThreadAffinity()) + { + SubmoduleSafeHandle sub; + var res = NativeMethods.git_submodule_add_setup(out sub, repo, url, path, use_gitlink); + Ensure.ZeroResult(res); + return sub; + } + } + + public static void git_submodule_add_finalize(SubmoduleSafeHandle submodule) + { + using (ThreadAffinity()) + { + var res = NativeMethods.git_submodule_add_finalize(submodule); + Ensure.ZeroResult(res); + } + } + public static void git_submodule_add_to_index(SubmoduleSafeHandle submodule, bool write_index) { using (ThreadAffinity()) diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index 18649c764..0996b87fa 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.IO; using System.Linq; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Handles; @@ -32,6 +33,36 @@ internal SubmoduleCollection(Repository repo) this.repo = repo; } + /// + /// Adds a new repository, checkout the selected branch and add it to superproject index + /// + /// The name of the Submodule + /// The url of the remote repository + /// The remote branch to checkout + /// The path of the submodule inside of the super repository, if none, name is taken. + /// + /// + public Submodule Add(string name, string url, string branch = "master", string relativePath = null, int use_GitLink = 0) + { + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + + Ensure.ArgumentNotNullOrEmptyString(url, "url"); + + relativePath = relativePath ?? name; + + SubmoduleSafeHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, relativePath, use_GitLink); + + using (Repository subRepo = new Repository(Path.Combine(repo.Info.WorkingDirectory, relativePath))) + { + subRepo.Fetch("origin"); + subRepo.Checkout(subRepo.Branches["origin/" + branch]); + } + + Proxy.git_submodule_add_finalize(handle); + + return this[name]; + } + /// /// Gets the with the specified name. /// From 362f6242da94363cd2802096a396b1502c8ef67d Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Thu, 8 Aug 2013 00:55:01 +0200 Subject: [PATCH 2/4] Submodules.Add: use_gitlink as bool, and use clone instead of fetch --- LibGit2Sharp/Core/NativeMethods.cs | 2 +- LibGit2Sharp/Core/Proxy.cs | 4 ++-- LibGit2Sharp/SubmoduleCollection.cs | 21 +++++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 751c9deed..0ad2cda6a 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -1042,7 +1042,7 @@ internal static extern int git_submodule_add_setup( RepositorySafeHandle repo, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(Utf8Marshaler))] string url, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath path, - int use_gitlink); + bool use_gitlink); [DllImport(libgit2)] internal static extern int git_submodule_add_finalize( diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 4ffe746b1..8f6837522 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -2010,12 +2010,12 @@ public static ICollection git_submodule_foreach(RepositorySafe return git_foreach(resultSelector, c => NativeMethods.git_submodule_foreach(repo, (x, y, p) => c(x, y, p), IntPtr.Zero)); } - public static SubmoduleSafeHandle git_submodule_add_setup(RepositorySafeHandle repo, string url, FilePath path, int use_gitlink) + public static SubmoduleSafeHandle git_submodule_add_setup(RepositorySafeHandle repo, string url, FilePath path, bool useGitLink) { using (ThreadAffinity()) { SubmoduleSafeHandle sub; - var res = NativeMethods.git_submodule_add_setup(out sub, repo, url, path, use_gitlink); + var res = NativeMethods.git_submodule_add_setup(out sub, repo, url, path, useGitLink); Ensure.ZeroResult(res); return sub; } diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index 0996b87fa..18543cee1 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -42,7 +42,7 @@ internal SubmoduleCollection(Repository repo) /// The path of the submodule inside of the super repository, if none, name is taken. /// /// - public Submodule Add(string name, string url, string branch = "master", string relativePath = null, int use_GitLink = 0) + public Submodule Add(string name, string url, string committish = null, string relativePath = null, bool useGitLink = true) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); @@ -50,15 +50,20 @@ public Submodule Add(string name, string url, string branch = "master", string relativePath = relativePath ?? name; - SubmoduleSafeHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, relativePath, use_GitLink); - - using (Repository subRepo = new Repository(Path.Combine(repo.Info.WorkingDirectory, relativePath))) + using (SubmoduleSafeHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, relativePath, useGitLink)) { - subRepo.Fetch("origin"); - subRepo.Checkout(subRepo.Branches["origin/" + branch]); - } + string subPath = Path.Combine(repo.Info.WorkingDirectory, relativePath); + + Repository.Clone(url, subPath); - Proxy.git_submodule_add_finalize(handle); + if (committish != null) + { + using (Repository subRepo = new Repository(subPath)) + subRepo.Checkout(subRepo.Branches[committish]); + } + + Proxy.git_submodule_add_finalize(handle); + } return this[name]; } From d4d093b068c9d01350bece19939d33674a1f8dcc Mon Sep 17 00:00:00 2001 From: Paul Saunders Date: Tue, 22 Apr 2014 17:51:16 +0100 Subject: [PATCH 3/4] add Submodule Add functionality with tests --- LibGit2Sharp.Tests/SubmoduleFixture.cs | 17 +++++++++++++++++ LibGit2Sharp/Core/NativeMethods.cs | 4 ++-- LibGit2Sharp/SubmoduleCollection.cs | 23 +++++++++++++---------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/LibGit2Sharp.Tests/SubmoduleFixture.cs b/LibGit2Sharp.Tests/SubmoduleFixture.cs index e1b378b71..e5ded9ebf 100644 --- a/LibGit2Sharp.Tests/SubmoduleFixture.cs +++ b/LibGit2Sharp.Tests/SubmoduleFixture.cs @@ -150,5 +150,22 @@ public void CanStageChangeInSubmoduleViaIndexStageWithOtherPaths(string submodul Assert.Equal(SubmoduleStatus.IndexModified, statusAfter & SubmoduleStatus.IndexModified); } } + + [Theory] + [InlineData("https://github.com/libgit2/TestGitRepository", false)] + [InlineData("https://github.com/libgit2/TestGitRepository", true)] + [InlineData("git://github.com/libgit2/TestGitRepository", true)] + public void CanAddASubmodule(string url, bool useGitLink) + { + var path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + var submodule = repo.Submodules.Add("test_submodule", url, null, "test_submodule", useGitLink); + Assert.NotNull(submodule); + + Assert.True(Directory.Exists(repo.Info.WorkingDirectory + Path.DirectorySeparatorChar + "test_submodule")); + Assert.True(File.Exists(repo.Info.WorkingDirectory + Path.DirectorySeparatorChar + @"test_submodule" + Path.DirectorySeparatorChar + "master.txt")); + } + } } } diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 52d69efd8..03eea4a8f 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -1291,8 +1291,8 @@ internal static extern void git_status_list_free( internal static extern int git_submodule_add_setup( out SubmoduleSafeHandle reference, RepositorySafeHandle repo, - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(Utf8Marshaler))] string url, - [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath path, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path, bool use_gitlink); [DllImport(libgit2)] diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index e5b44d26e..743a72fdb 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -36,16 +36,20 @@ internal SubmoduleCollection(Repository repo) /// /// Adds a new repository, checkout the selected branch and add it to superproject index /// + /// New style: sub-repo goes in <repo-dir>/modules/<name>/ with a + /// gitlink in the sub-repo workdir directory to that repository + /// + /// Old style: sub-repo goes directly into repo/<name>/.git/ + /// /// The name of the Submodule /// The url of the remote repository - /// The remote branch to checkout + /// A revparse spec for the submodule. /// The path of the submodule inside of the super repository, if none, name is taken. - /// + /// Use new style git subrepos or oldstyle. /// - public Submodule Add(string name, string url, string committish = null, string relativePath = null, bool useGitLink = true) + public virtual Submodule Add(string name, string url, string committish = null, string relativePath = null, bool useGitLink = true) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); - Ensure.ArgumentNotNullOrEmptyString(url, "url"); relativePath = relativePath ?? name; @@ -54,14 +58,13 @@ public Submodule Add(string name, string url, string committish = null, string r { string subPath = Path.Combine(repo.Info.WorkingDirectory, relativePath); - Repository.Clone(url, subPath); - - if (committish != null) + string branch = committish ?? "refs/remotes/origin/master"; + using (var subRepo = new Repository(subPath)) { - using (Repository subRepo = new Repository(subPath)) - subRepo.Checkout(subRepo.Branches[committish]); + subRepo.Fetch("origin"); + subRepo.Checkout(branch); } - + Proxy.git_submodule_add_finalize(handle); } From 30391274690d321b68516b211c9023fb644266df Mon Sep 17 00:00:00 2001 From: Paul Saunders Date: Sat, 26 Apr 2014 09:11:37 +0100 Subject: [PATCH 4/4] modified submodule add after feedback --- LibGit2Sharp.Tests/SubmoduleFixture.cs | 3 ++- LibGit2Sharp/SubmoduleCollection.cs | 23 +++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/LibGit2Sharp.Tests/SubmoduleFixture.cs b/LibGit2Sharp.Tests/SubmoduleFixture.cs index e5ded9ebf..1a890757d 100644 --- a/LibGit2Sharp.Tests/SubmoduleFixture.cs +++ b/LibGit2Sharp.Tests/SubmoduleFixture.cs @@ -155,12 +155,13 @@ public void CanStageChangeInSubmoduleViaIndexStageWithOtherPaths(string submodul [InlineData("https://github.com/libgit2/TestGitRepository", false)] [InlineData("https://github.com/libgit2/TestGitRepository", true)] [InlineData("git://github.com/libgit2/TestGitRepository", true)] +// [InlineData("git://github.com/libgit2/libgit2sharp.git", true)] public void CanAddASubmodule(string url, bool useGitLink) { var path = CloneStandardTestRepo(); using (var repo = new Repository(path)) { - var submodule = repo.Submodules.Add("test_submodule", url, null, "test_submodule", useGitLink); + var submodule = repo.Submodules.Add("test_submodule", url, null, useGitLink); Assert.NotNull(submodule); Assert.True(Directory.Exists(repo.Info.WorkingDirectory + Path.DirectorySeparatorChar + "test_submodule")); diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index 743a72fdb..bf77308cc 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -41,34 +41,33 @@ internal SubmoduleCollection(Repository repo) /// /// Old style: sub-repo goes directly into repo/<name>/.git/ /// - /// The name of the Submodule + /// The path of the submodule inside of the super repository, if none, name is taken. /// The url of the remote repository /// A revparse spec for the submodule. - /// The path of the submodule inside of the super repository, if none, name is taken. /// Use new style git subrepos or oldstyle. /// - public virtual Submodule Add(string name, string url, string committish = null, string relativePath = null, bool useGitLink = true) + public virtual Submodule Add(string relativePath, string url, string committish = null, bool useGitLink = true) { - Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(relativePath, "name"); Ensure.ArgumentNotNullOrEmptyString(url, "url"); - relativePath = relativePath ?? name; + string subPath = Path.Combine(repo.Info.WorkingDirectory, relativePath); + Repository.Clone(url, subPath); using (SubmoduleSafeHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, relativePath, useGitLink)) { - string subPath = Path.Combine(repo.Info.WorkingDirectory, relativePath); - - string branch = committish ?? "refs/remotes/origin/master"; - using (var subRepo = new Repository(subPath)) + if (committish != null) { - subRepo.Fetch("origin"); - subRepo.Checkout(branch); + using (var subRepo = new Repository(subPath)) + { + subRepo.Checkout(committish); + } } Proxy.git_submodule_add_finalize(handle); } - return this[name]; + return this[relativePath]; } ///