Skip to content

Commit

Permalink
Sped up the Normalization by removing an unnecessary O(n^2) loop (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
ni-fgenois authored Feb 14, 2022
1 parent 7fdf3ca commit efddf2f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/GitVersion.Core/Core/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,14 @@ private void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(string remoteName
{
var remoteTrackingReferenceName = remoteTrackingReference.Name.Canonical;
var branchName = remoteTrackingReferenceName.Substring(prefix.Length);
var localCanonicalName = "refs/heads/" + branchName;
var localReferenceName = ReferenceName.FromBranchName(branchName);

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

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

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

var branch = this.repository.Branches[branchName]!;
this.repository.Branches.UpdateTrackedBranch(branch, remoteTrackingReferenceName);
Expand Down
1 change: 1 addition & 0 deletions src/GitVersion.Core/Git/IReferenceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public interface IReferenceCollection : IEnumerable<IReference>
{
IReference? Head { get; }
IReference? this[string name] { get; }
IReference? this[ReferenceName referenceName] { get; }
void Add(string name, string canonicalRefNameOrObjectish, bool allowOverwrite = false);
void UpdateTarget(IReference directRef, IObjectId targetId);
IEnumerable<IReference> FromGlob(string prefix);
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersion.Core/Git/ReferenceName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static ReferenceName Parse(string canonicalName)
}
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
}

public static ReferenceName FromBranchName(string branchName) => Parse(LocalBranchPrefix + branchName);

public string Canonical { get; }
public string Friendly { get; }
public string WithoutRemote { get; }
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public IReference? this[string name]
}
}

public IReference? this[ReferenceName referenceName] => this[referenceName.Canonical];

public IReference? Head => this["HEAD"];

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

0 comments on commit efddf2f

Please sign in to comment.