Skip to content
Open
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
48 changes: 48 additions & 0 deletions LibGit2Sharp/Commands/AddSubmodule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using LibGit2Sharp;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using System.IO;

namespace LibGit2Sharp
{
/// <summary>
/// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD.
/// </summary>
public static partial class Commands
{
/// <summary>
/// Adds a new repository, checkout the selected branch and add it to superproject index
/// </summary>
/// <param name="repository">The repository to add the Submodule to</param>
/// <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 static Submodule AddSubmodule(IRepository repository, string name, string url, string relativePath, bool useGitLink, Action<Repository> 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];
}
}
}

15 changes: 14 additions & 1 deletion LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,20 @@ public static unsafe ICollection<TResult> git_submodule_foreach<TResult>(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);
Expand Down
3 changes: 3 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net40|AnyCPU'">
<WarningLevel>2</WarningLevel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[2.0.323]" PrivateAssets="none" />
Expand Down