From 1215dfb99045463039bc3b0cbcbdf818b3c4b900 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Sun, 9 Jul 2017 11:45:01 +0200 Subject: [PATCH 1/2] add Commands.AddSubmodule --- LibGit2Sharp/Commands/AddSubmodule.cs | 48 +++++++++++++++++++++++++++ LibGit2Sharp/Core/NativeMethods.cs | 14 +++++++- LibGit2Sharp/Core/Proxy.cs | 14 ++++++++ LibGit2Sharp/LibGit2Sharp.csproj | 5 --- 4 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 LibGit2Sharp/Commands/AddSubmodule.cs diff --git a/LibGit2Sharp/Commands/AddSubmodule.cs b/LibGit2Sharp/Commands/AddSubmodule.cs new file mode 100644 index 000000000..f69deaddf --- /dev/null +++ b/LibGit2Sharp/Commands/AddSubmodule.cs @@ -0,0 +1,48 @@ +using System; +using LibGit2Sharp; +using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; +using System.IO; + +namespace LibGit2Sharp +{ + /// + /// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD. + /// + public static partial class Commands + { + /// + /// Adds a new repository, checkout the selected branch and add it to superproject index + /// + /// The repository to add the Submodule to + /// The name of the Submodule + /// The url of the remote repository + /// 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 static Submodule AddSubmodule(IRepository repository, string name, string url, string relativePath, bool useGitLink, Action initiRepository) + { + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + + Ensure.ArgumentNotNullOrEmptyString(url, "url"); + + relativePath = relativePath ?? name; + + using (SubmoduleHandle handle = Proxy.git_submodule_add_setup(((Repository)repository).Handle, url, relativePath, useGitLink ? 1 : 0)) + { + string subPath = Path.Combine(repository.Info.WorkingDirectory, relativePath); + + using (Repository subRep = new Repository(subPath)) + { + initiRepository(subRep); + } + + Proxy.git_submodule_add_finalize(handle); + } + + return repository.Submodules[name]; + } + } +} + diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index b3e7f1297..c04a89c9a 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -24,7 +24,7 @@ internal static partial class NativeMethods #pragma warning disable 0414 private static readonly NativeShutdownObject shutdownObject; #pragma warning restore 0414 - + static NativeMethods() { if (Platform.OperatingSystem == OperatingSystemType.Windows) @@ -1601,6 +1601,18 @@ internal static extern unsafe void git_status_list_free( internal static extern void git_strarray_free( ref GitStrArray array); + [DllImport(libgit2)] + static extern unsafe int git_submodule_add_setup( + out git_submodule* reference, + git_repository* repo, + [CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* url, + [CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path, + int use_gitlink); + + [DllImport(libgit2)] + internal static extern unsafe int git_submodule_add_finalize( + git_submodule* reference); + [DllImport(libgit2)] private static extern unsafe int git_submodule_lookup( out git_submodule* reference, diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index c46d5fc1e..7b1c5b27b 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -2992,6 +2992,20 @@ public static unsafe ICollection git_submodule_foreach(Reposit return git_foreach(resultSelector, c => NativeMethods.git_submodule_foreach(repo, (x, y, p) => c(x, y, p), IntPtr.Zero)); } + public static unsafe SubmoduleHandle git_submodule_add_setup(RepositoryHandle repo, string url, FilePath path, int use_gitlink) + { + git_submodule* submodule; + var res = NativeMethods.git_submodule_add_setup(out submodule, repo, url, path, use_gitlink); + Ensure.ZeroResult(res); + return new SubmoduleHandle(submodule, true); + } + + public static unsafe void git_submodule_add_finalize(SubmoduleHandle submodule) + { + var res = NativeMethods.git_submodule_add_finalize(submodule); + Ensure.ZeroResult(res); + } + public static unsafe void git_submodule_add_to_index(SubmoduleHandle submodule, bool write_index) { var res = NativeMethods.git_submodule_add_to_index(submodule, write_index); diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index f3fb18896..07e75491c 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -17,15 +17,10 @@ TextTemplatingFileGenerator - Objects.cs - - - Objects.tt - From d23614437957174992b8f93a71b0d66a54221b79 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Wed, 6 Sep 2017 20:00:55 +0200 Subject: [PATCH 2/2] remove netstandard2.0 to make it compile --- LibGit2Sharp/LibGit2Sharp.csproj | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 5ed49775d..051abb677 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -1,7 +1,7 @@  - net40;netstandard2.0 + net40 true LibGit2Sharp brings all the might and speed of libgit2, a native Git implementation, to the managed world of .Net and Mono. LibGit2Sharp contributors @@ -19,17 +19,22 @@ false + + 2 + TextTemplatingFileGenerator + Objects.cs +