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 cbb850b16..b48a012ef 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -22,7 +22,7 @@ internal static class NativeMethods // This will handle initialization and shutdown of the underlying // native library. private static NativeShutdownObject shutdownObject; - + static NativeMethods() { if (Platform.IsRunningOnNetFramework() || Platform.IsRunningOnNetCore()) @@ -1808,6 +1808,19 @@ internal static extern unsafe int git_submodule_lookup( git_repository* repo, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name); + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern unsafe int git_submodule_add_setup( + out git_submodule* reference, + git_repository* repo, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath name, + int use_gitlink); + + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern unsafe int git_submodule_add_finalize( + git_submodule* reference); + + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_submodule_resolve_url( GitBuf buf, diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 83d35e22c..afccc218c 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -3002,6 +3002,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 1c4abef7b..51a847034 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -27,6 +27,9 @@ true + + 2 +