From 519916b13047f32e6b336da644a6e6ec1659ee97 Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 19 Jan 2021 14:40:35 +0100 Subject: [PATCH] added ReferenceName.Parse, added ReferenceName.EquivalentTo added ReferenceName.Parse that parse canonical Name into ReferenceName instance --- .../Core/RepositoryExtensionsTests.cs | 4 ++-- .../BranchConfigurationCalculator.cs | 3 +-- src/GitVersionCore/Core/GitPreparer.cs | 21 +++++++++++-------- .../Core/RepositoryMetadataProvider.cs | 16 ++++---------- src/GitVersionCore/Git/ReferenceName.cs | 18 ++++++++++++++++ 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs b/src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs index b5d9cab59d..3e929a5f25 100644 --- a/src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs +++ b/src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using GitVersion; -using GitVersion.Extensions; using GitVersion.Logging; using GitVersionCore.Tests.Helpers; using NSubstitute; @@ -47,7 +46,8 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo, var repoTipId = repoTip.Id; - if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName))) + var referenceName = ReferenceName.Parse(localCanonicalName); + if (repo.Branches.All(b => !b.Name.Equals(referenceName))) { log.Info(isBranch ? $"Creating local branch {localCanonicalName}" : $"Creating local branch {localCanonicalName} pointing at {repoTipId}"); diff --git a/src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs b/src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs index f3a0e14bf7..2e80f9d256 100644 --- a/src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs +++ b/src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs @@ -135,8 +135,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf throw new InvalidOperationException("Could not find a 'develop' or 'master' branch, neither locally nor remotely."); } - var branchName = chosenBranch.Name.Friendly; - log.Warning(errorMessage + System.Environment.NewLine + "Falling back to " + branchName + " branch config"); + log.Warning($"{errorMessage}{System.Environment.NewLine}Falling back to {chosenBranch} branch config"); // To prevent infinite loops, make sure that a new branch was chosen. if (targetBranch.Equals(chosenBranch)) diff --git a/src/GitVersionCore/Core/GitPreparer.cs b/src/GitVersionCore/Core/GitPreparer.cs index 1954b7947c..6d6a0c85e6 100644 --- a/src/GitVersionCore/Core/GitPreparer.cs +++ b/src/GitVersionCore/Core/GitPreparer.cs @@ -216,7 +216,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur const string moveBranchMsg = "Move one of the branches along a commit to remove warning"; log.Warning($"Found more than one local branch pointing at the commit '{headSha}' ({csvNames})."); - var master = localBranchesWhereCommitShaIsHead.SingleOrDefault(n => n.Name.Friendly.IsEquivalentTo(Config.MasterBranchKey)); + var master = localBranchesWhereCommitShaIsHead.SingleOrDefault(n => n.Name.EquivalentTo(Config.MasterBranchKey)); if (master != null) { log.Warning("Because one of the branches is 'master', will build master." + moveBranchMsg); @@ -269,9 +269,10 @@ private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(IGitReposi { var prefix = $"refs/remotes/{remoteName}/"; var remoteHeadCanonicalName = $"{prefix}HEAD"; + var headReferenceName = ReferenceName.Parse(remoteHeadCanonicalName); var remoteTrackingReferences = repo.Refs .FromGlob(prefix + "*") - .Where(r => !r.Name.Canonical.IsEquivalentTo(remoteHeadCanonicalName)); + .Where(r => !r.Name.Equals(headReferenceName)); foreach (var remoteTrackingReference in remoteTrackingReferences) { @@ -279,10 +280,11 @@ private static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(IGitReposi var branchName = remoteTrackingReferenceName.Substring(prefix.Length); var localCanonicalName = "refs/heads/" + branchName; + var referenceName = ReferenceName.Parse(localCanonicalName); // We do not want to touch our current branch - if (branchName.IsEquivalentTo(repo.Head.Name.Friendly)) continue; + if (repo.Head.Name.EquivalentTo(branchName)) continue; - if (repo.Refs.Any(x => x.Name.Canonical.IsEquivalentTo(localCanonicalName))) + if (repo.Refs.Any(x => x.Name.Equals(referenceName))) { var localRef = repo.Refs[localCanonicalName]; if (localRef.DirectReferenceTargetIdentifier == remoteTrackingReference.DirectReferenceTargetIdentifier) @@ -338,16 +340,17 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo, var repoTipId = repoTip.Id; - if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName))) + var referenceName = ReferenceName.Parse(localCanonicalName); + if (repo.Branches.All(b => !b.Name.Equals(referenceName))) { - log.Info(isBranch ? $"Creating local branch {localCanonicalName}" - : $"Creating local branch {localCanonicalName} pointing at {repoTipId}"); + log.Info(isBranch ? $"Creating local branch {referenceName}" + : $"Creating local branch {referenceName} pointing at {repoTipId}"); repo.Refs.Add(localCanonicalName, repoTipId.Sha); } else { - log.Info(isBranch ? $"Updating local branch {localCanonicalName} to point at {repoTip}" - : $"Updating local branch {localCanonicalName} to match ref {currentBranch}"); + log.Info(isBranch ? $"Updating local branch {referenceName} to point at {repoTip}" + : $"Updating local branch {referenceName} to match ref {currentBranch}"); var localRef = repo.Refs[localCanonicalName]; repo.Refs.UpdateTarget(localRef, repoTipId); } diff --git a/src/GitVersionCore/Core/RepositoryMetadataProvider.cs b/src/GitVersionCore/Core/RepositoryMetadataProvider.cs index 874d45cdd9..874c7393f9 100644 --- a/src/GitVersionCore/Core/RepositoryMetadataProvider.cs +++ b/src/GitVersionCore/Core/RepositoryMetadataProvider.cs @@ -4,7 +4,6 @@ using System.Text.RegularExpressions; using GitVersion.Common; using GitVersion.Configuration; -using GitVersion.Extensions; using GitVersion.Logging; using GitVersion.Model.Configuration; using GitVersion.VersionCalculation; @@ -42,7 +41,7 @@ public ICommit FindMergeBase(IBranch branch, IBranch otherBranch) using (log.IndentLog($"Finding merge base between '{branch}' and '{otherBranch}'.")) { - // Otherbranch tip is a forward merge + // Other branch tip is a forward merge var commitToFindCommonBase = otherBranch.Tip; var commit = branch.Tip; if (otherBranch.Tip.Parents.Contains(commit)) @@ -143,7 +142,6 @@ public IBranch GetTargetBranch(string targetBranchName) { // By default, we assume HEAD is pointing to the desired branch var desiredBranch = repository.Head; - var targetBranch = FindBranch(targetBranchName); // Make sure the desired branch has been specified if (!string.IsNullOrEmpty(targetBranchName)) @@ -151,15 +149,13 @@ public IBranch GetTargetBranch(string targetBranchName) // There are some edge cases where HEAD is not pointing to the desired branch. // Therefore it's important to verify if 'currentBranch' is indeed the desired branch. + var targetBranch = FindBranch(targetBranchName); // CanonicalName can be "refs/heads/develop", so we need to check for "/{TargetBranch}" as well if (!desiredBranch.Equals(targetBranch)) { // In the case where HEAD is not the desired branch, try to find the branch with matching name desiredBranch = repository.Branches? - .Where(b => - b.Name.Canonical.IsEquivalentTo(targetBranchName) || - b.Name.Friendly.IsEquivalentTo(targetBranchName) || - b.Name.WithoutRemote.IsEquivalentTo(targetBranchName)) + .Where(b => b.Name.EquivalentTo(targetBranchName)) .OrderBy(b => b.IsRemote) .FirstOrDefault(); @@ -171,11 +167,7 @@ public IBranch GetTargetBranch(string targetBranchName) return desiredBranch; } - public IBranch FindBranch(string branchName) - { - return repository.Branches.FirstOrDefault(x => x.Name.Canonical.IsEquivalentTo(branchName) - || x.Name.WithoutRemote.IsEquivalentTo(branchName)); - } + public IBranch FindBranch(string branchName) => repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName)); public IBranch GetChosenBranch(Config configuration) { diff --git a/src/GitVersionCore/Git/ReferenceName.cs b/src/GitVersionCore/Git/ReferenceName.cs index 7474ffd71c..227d7e340f 100644 --- a/src/GitVersionCore/Git/ReferenceName.cs +++ b/src/GitVersionCore/Git/ReferenceName.cs @@ -18,6 +18,17 @@ public ReferenceName(string canonical) Friendly = Shorten(); WithoutRemote = RemoveRemote(); } + + public static ReferenceName Parse(string canonicalName) + { + if (IsPrefixedBy(canonicalName, LocalBranchPrefix) + || IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix) + || IsPrefixedBy(canonicalName, TagPrefix)) + { + return new ReferenceName(canonicalName); + } + throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name"); + } public string Canonical { get; } public string Friendly { get; } public string WithoutRemote { get; } @@ -28,6 +39,13 @@ public ReferenceName(string canonical) public override int GetHashCode() => equalityHelper.GetHashCode(this); public override string ToString() => Friendly; + public bool EquivalentTo(string name) + { + return Canonical.Equals(name, StringComparison.OrdinalIgnoreCase) + || Friendly.Equals(name, StringComparison.OrdinalIgnoreCase) + || WithoutRemote.Equals(name, StringComparison.OrdinalIgnoreCase); + } + private string Shorten() { if (IsPrefixedBy(Canonical, LocalBranchPrefix))