Skip to content

Commit 839c659

Browse files
committed
Prevent Repository.Head.IsCurrentRepositoryHead from throwing a NullReferenceException when the Repository is empty
Fix issue libgit2#105
1 parent b765016 commit 839c659

File tree

4 files changed

+6
-18
lines changed

4 files changed

+6
-18
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,11 @@ public void CanCheckoutAnArbitraryCommit(string commitPointer)
316316
detachedHead.CanonicalName.ShouldEqual("(no branch)");
317317
detachedHead.Tip.Sha.ShouldEqual(repo.Lookup(commitPointer).Sha);
318318

319-
detachedHead.IsCurrentRepositoryHead.ShouldBeTrue();
320319
detachedHead.ShouldEqual(repo.Head);
321320

322321
master.IsCurrentRepositoryHead.ShouldBeFalse();
322+
detachedHead.IsCurrentRepositoryHead.ShouldBeTrue();
323+
repo.Head.IsCurrentRepositoryHead.ShouldBeTrue();
323324
}
324325
}
325326

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private static void AssertInitializedRepository(Repository repo)
9999
headRef.ResolveToDirectReference().ShouldBeNull();
100100

101101
repo.Head.ShouldNotBeNull();
102+
repo.Head.IsCurrentRepositoryHead.ShouldBeTrue();
102103
repo.Head.CanonicalName.ShouldEqual(headRef.TargetIdentifier);
103104
repo.Head.Tip.ShouldBeNull();
104105

LibGit2Sharp/Branch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ public int BehindBy
111111
/// Gets a value indicating whether this instance is current branch (HEAD) in the repository.
112112
/// </summary>
113113
/// <value>
114-
/// <c>true</c> if this instance is current branch; otherwise, <c>false</c>.
114+
/// <c>true</c> if this instance is the current branch; otherwise, <c>false</c>.
115115
/// </value>
116-
public virtual bool IsCurrentRepositoryHead
116+
public bool IsCurrentRepositoryHead
117117
{
118-
get { return repo.Refs[CanonicalName].ResolveToDirectReference() == repo.Refs["HEAD"].ResolveToDirectReference(); }
118+
get { return repo.Head == this; }
119119
}
120120

121121
/// <summary>

LibGit2Sharp/DetachedHead.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ internal DetachedHead(Repository repo, Reference reference)
77
{
88
}
99

10-
/// <summary>
11-
/// Gets a value indicating whether this instance is current branch (HEAD) in the repository.
12-
/// </summary>
13-
/// <value>
14-
/// <c>true</c> if this instance is current branch; otherwise, <c>false</c>.
15-
/// </value>
16-
public override bool IsCurrentRepositoryHead
17-
{
18-
get
19-
{
20-
return repo.Head == this;
21-
}
22-
}
23-
2410
protected override string Shorten(string branchName)
2511
{
2612
return branchName;

0 commit comments

Comments
 (0)