Skip to content

Repository.Submodules.Add created #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -1347,6 +1347,18 @@ internal static extern void git_status_list_free(
internal static extern void git_strarray_free(
ref GitStrArray array);

[DllImport(libgit2)]
internal static extern int git_submodule_add_setup(
out SubmoduleSafeHandle reference,
RepositorySafeHandle repo,
[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)]
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 @@ -2705,6 +2705,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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is init-ing an empty repo at path, but at L57 here, you're attempting to clone into that directory. In order to clone into a directory, it must be empty.

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
34 changes: 34 additions & 0 deletions LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using LibGit2Sharp.Handlers;

namespace LibGit2Sharp
{
Expand All @@ -32,6 +34,38 @@ 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="relativePath">The path of the submodule inside of the super repository, if none, name is taken.</param>
/// <param name="useGitLink">Should workdir contain a gitlink to the repo in .git/modules vs. repo directly in workdir.</param>
/// <param name="initiRepository">Should workdir contain a gitlink to the repo in .git/modules vs. repo directly in workdir.</param>
/// <returns></returns>
public Submodule Add(string name, string url, string relativePath, bool useGitLink , Action<Repository> initiRepository)
{
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);

using (Repository subRep = new Repository(subPath))
{
initiRepository(subRep);
}

Proxy.git_submodule_add_finalize(handle);
}

return this[name];
}

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