8000 Teach Checkout() to cope with revparse syntax · repo-archive/libgit2sharp@902ed7d · GitHub
[go: up one dir, main page]

Skip to content

Commit 902ed7d

Browse files
committed
Teach Checkout() to cope with revparse syntax
1 parent d5c4a44 commit 902ed7d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

LibGit2Sharp.Tests/CheckoutFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void CanCheckoutAnExistingBranchByName(string branchName)
8080
[Theory]
8181
[InlineData("6dcf9bf")]
8282
[InlineData("refs/tags/lw")]
83+
[InlineData("HEAD~2")]
8384
public void CanCheckoutAnArbitraryCommit(string commitPointer)
8485
{
8586
string path = CloneStandardTestRepo();
@@ -93,10 +94,12 @@ public void CanCheckoutAnArbitraryCommit(string commitPointer)
9394

9495
Assert.False(repo.Index.RetrieveStatus().IsDirty);
9596

97+
var commitSha = repo.Lookup(commitPointer).Sha;
98+
9699
Branch detachedHead = repo.Checkout(commitPointer);
97100

98101
Assert.Equal(repo.Head, detachedHead);
99-
Assert.Equal(repo.Lookup(commitPointer).Sha, detachedHead.Tip.Sha);
102+
Assert.Equal(commitSha, detachedHead.Tip.Sha);
100103
Assert.True(repo.Head.IsCurrentRepositoryHead);
101104
Assert.True(repo.Info.IsHeadDetached);
102105
Assert.False(repo.Index.RetrieveStatus().IsDirty);

LibGit2Sharp/Repository.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public Branch Checkout(string committishOrBranchSpec, CheckoutOptions checkoutOp
579579
{
580580
Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec");
581581

582-
var branch = Branches[committishOrBranchSpec];
582+
Branch branch = TryResolveBranch(committishOrBranchSpec);
583583

584584
if (branch != null)
585585
{
@@ -595,6 +595,18 @@ public Branch Checkout(string committishOrBranchSpec, CheckoutOptions checkoutOp
595595
return Head;
596596
}
597597

598+
private Branch TryResolveBranch(string committishOrBranchSpec)
599+
{
600+
try
601+
{
602+
return Branches[committishOrBranchSpec];
603+
}
604+
catch (InvalidSpecificationException)
605+
{
606+
return null;
607+
}
608+
}
609+
598610
/// <summary>
599611
/// Checkout the tip commit of the specified <see cref = "Branch" /> object. If this commit is the
600612
/// current tip of the branch, will checkout the named branch. Otherwise, will checkout the tip commit

0 commit comments

Comments
 (0)
0