Skip to content

Commit 8b2c987

Browse files
committed
Don't load an object just to check its ID
In order to check for equality we just need to compare the ID of the object. We do not need to look up the object but can compare their identifiers directly. This also requires us to modify Branch's check for the current branch, but the new code more accurately reflects the check we do want to perform. Namely whether HEAD points to our reference name.
1 parent 4938414 commit 8b2c987

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

LibGit2Sharp/Branch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public virtual BranchTrackingDetails TrackingDetails
106106
/// </value>
107107
public virtual bool IsCurrentRepositoryHead
108108
{
109-
get { return repo.Head == this; }
109+
get { return repo.Head.reference.TargetIdentifier == this.CanonicalName; }
110110
}
111111

112112
/// <summary>

LibGit2Sharp/ReferenceWrapper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TO
1616
/// The repository.
1717
/// </summary>
1818
protected readonly Repository repo;
19+
protected readonly Reference reference;
1920
private readonly Lazy<TObject> objectBuilder;
2021

2122
private static readonly LambdaEqualityHelper<ReferenceWrapper<TObject>> equalityHelper =
22-
new LambdaEqualityHelper<ReferenceWrapper<TObject>>(x => x.CanonicalName, x => x.TargetObject);
23+
new LambdaEqualityHelper<ReferenceWrapper<TObject>>(x => x.CanonicalName, x => x.reference.TargetIdentifier);
2324

2425
private readonly string canonicalName;
2526

@@ -40,6 +41,7 @@ protected internal ReferenceWrapper(Repository repo, Reference reference, Func<R
4041

4142
this.repo = repo;
4243
canonicalName = canonicalNameSelector(reference);
44+
this.reference = reference;
4345
objectBuilder = new Lazy<TObject>(() => RetrieveTargetObject(reference));
4446
}
4547

0 commit comments

Comments
 (0)