Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,18 @@ internal static extern StatusEntrySafeHandle git_status_byindex(
internal static extern void git_status_list_free(
IntPtr statusList);

[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,
bool 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,
Expand Down
20 changes: 20 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,26 @@ public static ICollection<TResult> git_submodule_foreach<TResult>(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, bool useGitLink)
{
using (ThreadAffinity())
{
SubmoduleSafeHandle sub;
var res = NativeMethods.git_submodule_add_setup(out sub, repo, url, path, useGitLink);
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())
Expand Down
36 changes: 36 additions & 0 deletions LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +33,41 @@ internal SubmoduleCollection(Repository repo)
this.repo = repo;
}

/// <summary>
/// Adds a new repository, checkout the selected branch and add it to superproject index
/// </summary>
/// <param name="name">The name of the Submodule</param>
/// <param name="url">The url of the remote repository</param>
/// <param name="branch">The remote branch to checkout</param>
/// <param name="relativePath">The path of the submodule inside of the super repository, if none, name is taken.</param>
/// <param name="use_GitLink"></param>
/// <returns></returns>
public 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;

using (SubmoduleSafeHandle handle = Proxy.git_submodule_add_setup(repo.Handle, url, relativePath, useGitLink))
{
string subPath = Path.Combine(repo.Info.WorkingDirectory, relativePath);

Repository.Clone(url, subPath);

if (committish != null)
{
using (Repository subRepo = new Repository(subPath))
subRepo.Checkout(subRepo.Branches[committish]);
}

Proxy.git_submodule_add_finalize(handle);
}

return this[name];
}

/// <summary>
/// Gets the <see cref="LibGit2Sharp.Submodule"/> with the specified name.
/// </summary>
Expand Down