Skip to content
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

Read and write submodule branch Option #1301 #1915

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions LibGit2Sharp.Tests/SubmoduleFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ public void CanInitSubmodule()
}
}

[Fact]
public void CanChangeSubmoduleBranch()
{
var path = SandboxSubmoduleSmallTestRepo();
string submoduleName = "submodule_target_wd";
string expectedSubmoduleBranch = @"master";

using (var repo = new Repository(path))
{
var submodule = repo.Submodules[submoduleName];

Assert.Null(submodule.Branch);

submodule.Branch = expectedSubmoduleBranch;

Assert.NotNull(submodule.Branch);
Assert.Equal(submodule.Branch, expectedSubmoduleBranch);
}
}

[Fact]
public void UpdatingUninitializedSubmoduleThrows()
{
Expand Down
11 changes: 11 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,17 @@ internal static extern unsafe string git_submodule_path(
internal static extern unsafe string git_submodule_url(
git_submodule* submodule);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
internal static extern unsafe string git_submodule_branch(
git_submodule* submodule);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int git_submodule_set_branch(
git_repository* repo,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string branch);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe git_oid* git_submodule_index_id(
git_submodule* submodule);
Expand Down
11 changes: 11 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3020,6 +3020,17 @@ public static unsafe string git_submodule_url(SubmoduleHandle submodule)
return NativeMethods.git_submodule_url(submodule);
}

public static unsafe string git_submodule_branch(SubmoduleHandle submodule)
{
return NativeMethods.git_submodule_branch(submodule);
}

public static unsafe void git_submodule_set_branch(RepositoryHandle repo, string name, string branch)
{
var res = NativeMethods.git_submodule_set_branch(repo, name, branch);
Ensure.ZeroResult(res);
}

public static unsafe ObjectId git_submodule_index_id(SubmoduleHandle submodule)
{
return ObjectId.BuildFromPtr(NativeMethods.git_submodule_index_id(submodule));
Expand Down
26 changes: 25 additions & 1 deletion LibGit2Sharp/Submodule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Submodule : IEquatable<Submodule>, IBelongToARepository
private readonly string name;
private readonly string path;
private readonly string url;
private string branch;
private readonly ILazy<ObjectId> headCommitId;
private readonly ILazy<ObjectId> indexCommitId;
private readonly ILazy<ObjectId> workdirCommitId;
Expand All @@ -31,12 +32,13 @@ public class Submodule : IEquatable<Submodule>, IBelongToARepository
protected Submodule()
{ }

internal Submodule(Repository repo, string name, string path, string url)
internal Submodule(Repository repo, string name, string path, string url, string branch)
{
this.repo = repo;
this.name = name;
this.path = path;
this.url = url;
this.branch = branch;

var commitIds = new SubmoduleLazyGroup(repo, name);
headCommitId = commitIds.AddLazy(Proxy.git_submodule_head_id);
Expand Down Expand Up @@ -64,6 +66,11 @@ internal Submodule(Repository repo, string name, string path, string url)
/// </summary>
public virtual string Url { get { return url; } }

/// <summary>
/// The name of the remote branch
/// </summary>
public virtual string Branch { get { return branch; } set { SetRemoteBranch(value); } }

/// <summary>
/// The commit ID for this submodule in the current HEAD tree.
/// </summary>
Expand Down Expand Up @@ -152,6 +159,23 @@ private string DebuggerDisplay
}
}

private void SetRemoteBranch(string branch)
{
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
{
if (handle == null)
{
throw new NotFoundException("Submodule lookup failed for '{0}'.",
name);
}

Proxy.git_submodule_set_branch(repo.Handle, name, branch);
Proxy.git_submodule_reload(handle);
this.branch = Proxy.git_submodule_branch(handle);
}

}

IRepository IBelongToARepository.Repository { get { return repo; } }
}
}
3 changes: 2 additions & 1 deletion LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public virtual Submodule this[string name]

return Lookup(name, handle => new Submodule(repo, name,
Proxy.git_submodule_path(handle),
Proxy.git_submodule_url(handle)));
Proxy.git_submodule_url(handle),
Proxy.git_submodule_branch(handle)));
}
}

Expand Down