Skip to content

Commit efddf2f

Browse files
authored
Sped up the Normalization by removing an unnecessary O(n^2) loop (#2990)
1 parent 7fdf3ca commit efddf2f

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

src/GitVersion.Core/Core/GitPreparer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,14 @@ private void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(string remoteName
314314
{
315315
var remoteTrackingReferenceName = remoteTrackingReference.Name.Canonical;
316316
var branchName = remoteTrackingReferenceName.Substring(prefix.Length);
317-
var localCanonicalName = "refs/heads/" + branchName;
317+
var localReferenceName = ReferenceName.FromBranchName(branchName);
318318

319-
var referenceName = ReferenceName.Parse(localCanonicalName);
320319
// We do not want to touch our current branch
321320
if (this.repository.Head.Name.EquivalentTo(branchName)) continue;
322321

323-
if (this.repository.Refs.Any(x => x.Name.Equals(referenceName)))
322+
if (this.repository.Refs[localReferenceName] is not null)
324323
{
325-
var localRef = this.repository.Refs[localCanonicalName]!;
324+
var localRef = this.repository.Refs[localReferenceName]!;
326325
if (localRef.TargetIdentifier == remoteTrackingReference.TargetIdentifier)
327326
{
328327
this.log.Info($"Skipping update of '{remoteTrackingReference.Name.Canonical}' as it already matches the remote ref.");
@@ -335,7 +334,7 @@ private void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(string remoteName
335334
}
336335

337336
this.log.Info($"Creating local branch from remote tracking '{remoteTrackingReference.Name.Canonical}'.");
338-
this.repository.Refs.Add(localCanonicalName, remoteTrackingReference.TargetIdentifier, true);
337+
this.repository.Refs.Add(localReferenceName.Canonical, remoteTrackingReference.TargetIdentifier, true);
339338

340339
var branch = this.repository.Branches[branchName]!;
341340
this.repository.Branches.UpdateTrackedBranch(branch, remoteTrackingReferenceName);

src/GitVersion.Core/Git/IReferenceCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public interface IReferenceCollection : IEnumerable<IReference>
44
{
55
IReference? Head { get; }
66
IReference? this[string name] { get; }
7+
IReference? this[ReferenceName referenceName] { get; }
78
void Add(string name, string canonicalRefNameOrObjectish, bool allowOverwrite = false);
89
void UpdateTarget(IReference directRef, IObjectId targetId);
910
IEnumerable<IReference> FromGlob(string prefix);

src/GitVersion.Core/Git/ReferenceName.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public static ReferenceName Parse(string canonicalName)
3838
}
3939
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
4040
}
41+
42+
public static ReferenceName FromBranchName(string branchName) => Parse(LocalBranchPrefix + branchName);
43+
4144
public string Canonical { get; }
4245
public string Friendly { get; }
4346
public string WithoutRemote { get; }

src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public IReference? this[string name]
2222
}
2323
}
2424

25+
public IReference? this[ReferenceName referenceName] => this[referenceName.Canonical];
26+
2527
public IReference? Head => this["HEAD"];
2628

2729
public IEnumerable<IReference> FromGlob(string prefix) => this.innerCollection.FromGlob(prefix).Select(reference => new Reference(reference));

0 commit comments

Comments
 (0)