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/5] 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/5] 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 3c15f3e0df0529ca8af1505b77e23fac1d8ebdc4 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Sat, 10 Aug 2013 12:39:45 +0200 Subject: [PATCH 3/5] Submodule.Add back to fetch and add optional parameters --- LibGit2Sharp/SubmoduleCollection.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index 18543cee1..4f4738499 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -7,6 +7,7 @@ using System.Linq; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Handles; +using LibGit2Sharp.Handlers; namespace LibGit2Sharp { @@ -38,11 +39,19 @@ internal SubmoduleCollection(Repository repo) /// /// The name of the Submodule /// The url of the remote repository - /// The remote branch to checkout + /// The remote branch or commit to checkout /// The path of the submodule inside of the super repository, if none, name is taken. - /// + /// Should workdir contain a gitlink to the repo in .git/modules vs. repo directly in workdir. /// - public Submodule Add(string name, string url, string committish = null, string relativePath = null, bool useGitLink = true) + public Submodule Add(string name, string url, string commitOrBranchSpec, string relativePath = null, bool useGitLink = true, + TagFetchMode tagFetchMode = TagFetchMode.Auto, + ProgressHandler onProgress = null, + CompletionHandler onCompletion = null, + UpdateTipsHandler onUpdateTips = null, + TransferProgressHandler onTransferProgress = null, + Credentials credentials = null, + CheckoutProgressHandler onCheckoutProgress = null, + CheckoutNotificationOptions checkoutNotificationOptions = null) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); @@ -54,12 +63,10 @@ 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) + using (Repository subRep = new Repository(subPath)) { - using (Repository subRepo = new Repository(subPath)) - subRepo.Checkout(subRepo.Branches[committish]); + subRep.Fetch("origin", tagFetchMode, onProgress, onCompletion, onUpdateTips, onTransferProgress, credentials); + subRep.Checkout(commitOrBranchSpec, CheckoutModifiers.None, onCheckoutProgress, checkoutNotificationOptions); } Proxy.git_submodule_add_finalize(handle); From c67a8fa139708922539142020ceec256da785389 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Mon, 28 Jul 2014 11:38:30 +0200 Subject: [PATCH 4/5] SubmoduleCollection.Add simplified with ActioninitiRepository --- LibGit2Sharp/SubmoduleCollection.cs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/LibGit2Sharp/SubmoduleCollection.cs b/LibGit2Sharp/SubmoduleCollection.cs index 4f4738499..3a55a6bf9 100644 --- a/LibGit2Sharp/SubmoduleCollection.cs +++ b/LibGit2Sharp/SubmoduleCollection.cs @@ -39,19 +39,11 @@ internal SubmoduleCollection(Repository repo) /// /// The name of the Submodule /// The url of the remote repository - /// The remote branch or commit to checkout /// The path of the submodule inside of the super repository, if none, name is taken. /// Should workdir contain a gitlink to the repo in .git/modules vs. repo directly in workdir. + /// Should workdir contain a gitlink to the repo in .git/modules vs. repo directly in workdir. /// - public Submodule Add(string name, string url, string commitOrBranchSpec, string relativePath = null, bool useGitLink = true, - TagFetchMode tagFetchMode = TagFetchMode.Auto, - ProgressHandler onProgress = null, - CompletionHandler onCompletion = null, - UpdateTipsHandler onUpdateTips = null, - TransferProgressHandler onTransferProgress = null, - Credentials credentials = null, - CheckoutProgressHandler onCheckoutProgress = null, - CheckoutNotificationOptions checkoutNotificationOptions = null) + public Submodule Add(string name, string url, string relativePath, bool useGitLink , Action initiRepository) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); @@ -65,8 +57,7 @@ public Submodule Add(string name, string url, string commitOrBranchSpec, string using (Repository subRep = new Repository(subPath)) { - subRep.Fetch("origin", tagFetchMode, onProgress, onCompletion, onUpdateTips, onTransferProgress, credentials); - subRep.Checkout(commitOrBranchSpec, CheckoutModifiers.None, onCheckoutProgress, checkoutNotificationOptions); + initiRepository(subRep); } Proxy.git_submodule_add_finalize(handle); From b5e4bf58bdede1d4185ba6f2dd8b6946fd7a1d2a Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Tue, 29 Jul 2014 09:22:47 +0200 Subject: [PATCH 5/5] fix git_submodule_add_setup --- LibGit2Sharp/Core/NativeMethods.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 80af37136..2a9a9cb14 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -1351,8 +1351,8 @@ internal static extern void git_strarray_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)]