Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit bf464cf

Browse files
ekk8nulltoken
authored andcommitted
Teach Repository.Clone to accept a specific branch to checkout
1 parent ae65478 commit bf464cf

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

LibGit2Sharp.Tests/CloneFixture.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ public void CanClone(string url)
3636
}
3737
}
3838

39+
[Theory]
40+
[InlineData("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f")]
41+
[InlineData("packed", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9")]
42+
[InlineData("test", "e90810b8df3e80c413d903f631643c716887138d")]
43+
public void CanCloneWithCheckoutBranchName(string branchName, string headTipId)
44+
{
45+
var scd = BuildSelfCleaningDirectory();
46+
47+
string clonedRepoPath = Repository.Clone(BareTestRepoPath, scd.DirectoryPath, new CloneOptions { BranchName = branchName });
48+
49+
using (var repo = new Repository(clonedRepoPath))
50+
{
51+
var head = repo.Head;
52+
53+
Assert.Equal(branchName, head.Name);
54+
Assert.True(head.IsTracking);
55+
Assert.Equal(headTipId, head.Tip.Sha);
56+
}
57+
}
58+
3959
private void AssertLocalClone(string url, string path = null, bool isCloningAnEmptyRepository = false)
4060
{
4161
var scd = BuildSelfCleaningDirectory();

LibGit2Sharp/CloneOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public CloneOptions()
2828
/// </summary>
2929
public bool Checkout { get; set; }
3030

31+
/// <summary>
32+
/// The name of the branch to checkout. When unspecified the
33+
/// remote's default branch will be used instead.
34+
/// </summary>
35+
public string BranchName { get; set; }
36+
3137
/// <summary>
3238
/// Handler for checkout progress information.
3339
/// </summary>

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ public static string Clone(string sourceUrl, string workdirPath,
553553
Bare = options.IsBare ? 1 : 0,
554554
CheckoutOpts = gitCheckoutOptions,
555555
RemoteCallbacks = gitRemoteCallbacks,
556+
CheckoutBranch = StrictUtf8Marshaler.FromManaged(options.BranchName)
556557
};
557558

558559
FilePath repoPath;

0 commit comments

Comments
 (0)