Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add Commands.AddSubmodule
  • Loading branch information
olmobrutall committed Jul 9, 2017
commit 1215dfb99045463039bc3b0cbcbdf818b3c4b900
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];
}
}
}

14 changes: 13 additions & 1 deletion LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2992,6 +2992,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
5 changes: 0 additions & 5 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
<None Include="Core\Handles\Objects.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Objects.cs</LastGenOutput>
</None>
<None Include="..\README.md" Pack="true" PackagePath="App_Readme\" />
<None Include="..\LICENSE.md" Pack="true" PackagePath="App_Readme\" />
<None Include="..\CHANGES.md" Pack="true" PackagePath="App_Readme\" />

<Compile Update="Core\Handles\Objects.cs">
<DependentUpon>Objects.tt</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeGenerationAttributes\CodeGenerationAttributes.csproj" PrivateAssets="all" />
Expand Down