Skip to content

Commit

Permalink
removed *Name from ReferenceName properties, implemented ToString()
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Jan 19, 2021
1 parent 46ce1d4 commit 6c05548
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 112 deletions.
9 changes: 5 additions & 4 deletions src/GitVersion.LibGit2Sharp/Git/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace GitVersion
{
internal sealed class Branch : IBranch
{
private static readonly LambdaEqualityHelper<IBranch> equalityHelper = new(x => x.Name.CanonicalName);
private static readonly LambdaKeyComparer<IBranch, string> comparerHelper = new(x => x.Name.CanonicalName);
private static readonly LambdaEqualityHelper<IBranch> equalityHelper = new(x => x.Name.Canonical);
private static readonly LambdaKeyComparer<IBranch, string> comparerHelper = new(x => x.Name.Canonical);

private readonly LibGit2Sharp.Branch innerBranch;

Expand All @@ -18,9 +18,10 @@ internal Branch(LibGit2Sharp.Branch branch)
public ReferenceName Name { get; }

public int CompareTo(IBranch other) => comparerHelper.Compare(this, other);
public override bool Equals(object obj) => Equals((obj as IBranch)!);
public bool Equals(IBranch other) => equalityHelper.Equals(this, other);
public override bool Equals(object obj) => Equals((obj as IBranch)!);
public override int GetHashCode() => equalityHelper.GetHashCode(this);
public override string ToString() => Name.ToString();
public static implicit operator LibGit2Sharp.Branch(Branch d) => d.innerBranch;

public ICommit? Tip
Expand All @@ -41,7 +42,7 @@ internal Branch(LibGit2Sharp.Branch branch)
}
}

public bool IsDetachedHead => Name.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
public bool IsDetachedHead => Name.Canonical.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);

public bool IsRemote => innerBranch.IsRemote;
public bool IsTracking => innerBranch.IsTracking;
Expand Down
7 changes: 5 additions & 2 deletions src/GitVersion.LibGit2Sharp/Git/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ internal sealed class Commit : ICommit
internal Commit(LibGit2Sharp.Commit innerCommit) => this.innerCommit = innerCommit;

public int CompareTo(ICommit other) => comparerHelper.Compare(this, other);
public override bool Equals(object obj) => Equals((obj as ICommit)!);
public bool Equals(ICommit other) => equalityHelper.Equals(this, other);
public override bool Equals(object obj) => Equals((obj as ICommit)!);
public override int GetHashCode() => equalityHelper.GetHashCode(this);

public override string ToString()
{
return $"{Id.ToString(7)} {innerCommit.MessageShort}";
}
public static implicit operator LibGit2Sharp.Commit(Commit d) => d.innerCommit;

public IEnumerable<ICommit> Parents => innerCommit.Parents.Select(parent => new Commit(parent));
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.LibGit2Sharp/Git/ObjectId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public ObjectId(string sha) : this(new LibGit2Sharp.ObjectId(sha))
}

public int CompareTo(IObjectId other) => comparerHelper.Compare(this, other);
public override bool Equals(object obj) => Equals((obj as IObjectId)!);
public bool Equals(IObjectId other) => equalityHelper.Equals(this, other);
public override bool Equals(object obj) => Equals((obj as IObjectId)!);
public override int GetHashCode() => equalityHelper.GetHashCode(this);
public override string ToString() => ToString(7);
public static implicit operator LibGit2Sharp.ObjectId(ObjectId d) => d.innerObjectId;
public string Sha => innerObjectId.Sha;

Expand Down
5 changes: 3 additions & 2 deletions src/GitVersion.LibGit2Sharp/Git/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace GitVersion
{
internal sealed class Reference : IReference
{
private static readonly LambdaEqualityHelper<IReference> equalityHelper = new(x => x.Name.CanonicalName);
private static readonly LambdaKeyComparer<IReference, string> comparerHelper = new(x => x.Name.CanonicalName);
private static readonly LambdaEqualityHelper<IReference> equalityHelper = new(x => x.Name.Canonical);
private static readonly LambdaKeyComparer<IReference, string> comparerHelper = new(x => x.Name.Canonical);

internal readonly LibGit2Sharp.Reference innerReference;
private DirectReference directReference => innerReference.ResolveToDirectReference();
Expand All @@ -21,6 +21,7 @@ internal Reference(LibGit2Sharp.Reference reference)
public override bool Equals(object obj) => Equals((obj as IReference)!);
public bool Equals(IReference other) => equalityHelper.Equals(this, other);
public override int GetHashCode() => equalityHelper.GetHashCode(this);
public override string ToString() => Name.ToString();
public string TargetIdentifier => innerReference.TargetIdentifier;
public string DirectReferenceTargetIdentifier => directReference.TargetIdentifier;
public IObjectId DirectReferenceTargetId => new ObjectId(directReference.Target.Id);
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.LibGit2Sharp/Git/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ internal sealed class Remote : IRemote
internal Remote(LibGit2Sharp.Remote remote) => innerRemote = remote;

public int CompareTo(IRemote other) => comparerHelper.Compare(this, other);
public override bool Equals(object obj) => Equals((obj as IRemote)!);
public bool Equals(IRemote other) => equalityHelper.Equals(this, other);
public override bool Equals(object obj) => Equals((obj as IRemote)!);
public override int GetHashCode() => equalityHelper.GetHashCode(this);
public override string ToString() => Name;
public string Name => innerRemote.Name;
public string RefSpecs => string.Join(", ", innerRemote.FetchRefSpecs.Select(r => r.Specification));
}
Expand Down
7 changes: 4 additions & 3 deletions src/GitVersion.LibGit2Sharp/Git/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace GitVersion
{
internal sealed class Tag : ITag
{
private static readonly LambdaEqualityHelper<ITag> equalityHelper = new(x => x.Name.CanonicalName);
private static readonly LambdaKeyComparer<ITag, string> comparerHelper = new(x => x.Name.CanonicalName);
private static readonly LambdaEqualityHelper<ITag> equalityHelper = new(x => x.Name.Canonical);
private static readonly LambdaKeyComparer<ITag, string> comparerHelper = new(x => x.Name.Canonical);

private readonly LibGit2Sharp.Tag innerTag;
internal Tag(LibGit2Sharp.Tag tag)
Expand All @@ -17,9 +17,10 @@ internal Tag(LibGit2Sharp.Tag tag)
public ReferenceName Name { get; }

public int CompareTo(ITag other) => comparerHelper.Compare(this, other);
public override bool Equals(object obj) => Equals((obj as ITag)!);
public bool Equals(ITag other) => equalityHelper.Equals(this, other);
public override bool Equals(object obj) => Equals((obj as ITag)!);
public override int GetHashCode() => equalityHelper.GetHashCode(this);
public override string ToString() => Name.ToString();
public string TargetSha => innerTag.Target.Sha;

public ICommit? PeeledTargetCommit()
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/Core/RepositoryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,

var repoTipId = repoTip.Id;

if (repo.Branches.All(b => !b.Name.CanonicalName.IsEquivalentTo(localCanonicalName)))
if (repo.Branches.All(b => !b.Name.Canonical.IsEquivalentTo(localCanonicalName)))
{
log.Info(isBranch ? $"Creating local branch {localCanonicalName}"
: $"Creating local branch {localCanonicalName} pointing at {repoTipId}");
Expand Down Expand Up @@ -91,7 +91,7 @@ private static IRemote MockRemote(IGitRepository repository)
branch.Name.Returns(new ReferenceName("refs/heads/feature/feat-test"));

var branches = Substitute.For<IBranchCollection>();
branches[branch.Name.CanonicalName].Returns(branch);
branches[branch.Name.Canonical].Returns(branch);
branches.GetEnumerator().Returns(_ => ((IEnumerable<IBranch>)new[] { branch }).GetEnumerator());

var reference = Substitute.For<IReference>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static IBranch CreateMockBranch(string name, params ICommit[] commits)

public static IBranch FindBranch(this IGitRepository repository, string branchName)
{
return repository.Branches.FirstOrDefault(x => x.Name.NameWithoutRemote == branchName);
return repository.Branches.FirstOrDefault(x => x.Name.WithoutRemote == branchName);
}

public static void DumpGraph(this IGitRepository repository, Action<string> writer = null, int? maxCommits = null)
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersionCore.Tests/Model/GitVersionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
mockRepository.Head.Returns(releaseLatestBranch);
mockRepository.Commits.Returns(releaseLatestBranch.Commits);

var latestContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseLatestBranch.Name.CanonicalName, config);
var latestContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseLatestBranch.Name.Canonical, config);
latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None);

mockRepository.Head.Returns(releaseVersionBranch);
var versionContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseVersionBranch.Name.CanonicalName, config);
var versionContext = GetGitVersionContext(fixture.RepositoryPath, mockRepository, releaseVersionBranch.Name.Canonical, config);
versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch);
}

Expand Down
20 changes: 10 additions & 10 deletions src/GitVersionCore/Configuration/BranchConfigurationCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public BranchConfigurationCalculator(ILog log, IRepositoryMetadataProvider repos
/// </summary>
public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList<IBranch> excludedInheritBranches = null)
{
var matchingBranches = configuration.GetConfigForBranch(targetBranch.Name.NameWithoutRemote);
var matchingBranches = configuration.GetConfigForBranch(targetBranch.Name.WithoutRemote);

if (matchingBranches == null)
{
log.Info($"No branch configuration found for branch {targetBranch.Name.FriendlyName}, falling back to default configuration");
log.Info($"No branch configuration found for branch {targetBranch.Name.Friendly}, falling back to default configuration");

matchingBranches = BranchConfig.CreateDefaultBranchConfig(FallbackConfigName)
.Apply(new BranchConfig
Expand Down Expand Up @@ -103,7 +103,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
}
}

log.Info("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.Name.FriendlyName)));
log.Info("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.Name.Friendly)));

if (possibleParents.Count == 1)
{
Expand All @@ -125,7 +125,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
// if develop exists and master if not
var errorMessage = possibleParents.Count == 0
? "Failed to inherit Increment branch configuration, no branches found."
: "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.Name.FriendlyName));
: "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.Name.Friendly));

var chosenBranch = repositoryMetadataProvider.GetChosenBranch(configuration);
if (chosenBranch == null)
Expand All @@ -135,7 +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.FriendlyName;
var branchName = chosenBranch.Name.Friendly;
log.Warning(errorMessage + System.Environment.NewLine + "Falling back to " + branchName + " branch config");

// To prevent infinite loops, make sure that a new branch was chosen.
Expand Down Expand Up @@ -189,22 +189,22 @@ private IBranch[] CalculateWhenMultipleParents(ICommit currentCommit, ref IBranc
}
else if (branches.Count > 1)
{
currentBranch = branches.FirstOrDefault(b => b.Name.NameWithoutRemote == Config.MasterBranchKey) ?? branches.First();
currentBranch = branches.FirstOrDefault(b => b.Name.WithoutRemote == Config.MasterBranchKey) ?? branches.First();
}
else
{
var possibleTargetBranches = repositoryMetadataProvider.GetBranchesForCommit(parents[0]).ToList();
if (possibleTargetBranches.Count > 1)
{
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.Name.NameWithoutRemote == Config.MasterBranchKey) ?? possibleTargetBranches.First();
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.Name.WithoutRemote == Config.MasterBranchKey) ?? possibleTargetBranches.First();
}
else
{
currentBranch = possibleTargetBranches.FirstOrDefault() ?? currentBranch;
}
}

log.Info("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name.FriendlyName + " as base");
log.Info("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name.Friendly + " as base");

return excludedBranches;
}
Expand All @@ -216,7 +216,7 @@ private static BranchConfig ChooseMasterOrDevelopIncrementStrategyIfTheChosenBra
BranchConfig masterOrDevelopConfig = null;
var developBranchRegex = config.Branches[Config.DevelopBranchKey].Regex;
var masterBranchRegex = config.Branches[Config.MasterBranchKey].Regex;
if (Regex.IsMatch(chosenBranch.Name.FriendlyName, developBranchRegex, RegexOptions.IgnoreCase))
if (Regex.IsMatch(chosenBranch.Name.Friendly, developBranchRegex, RegexOptions.IgnoreCase))
{
// Normally we would not expect this to happen but for safety we add a check
if (config.Branches[Config.DevelopBranchKey].Increment !=
Expand All @@ -228,7 +228,7 @@ private static BranchConfig ChooseMasterOrDevelopIncrementStrategyIfTheChosenBra
};
}
}
else if (Regex.IsMatch(chosenBranch.Name.FriendlyName, masterBranchRegex, RegexOptions.IgnoreCase))
else if (Regex.IsMatch(chosenBranch.Name.Friendly, masterBranchRegex, RegexOptions.IgnoreCase))
{
// Normally we would not expect this to happen but for safety we add a check
if (config.Branches[Config.MasterBranchKey].Increment !=
Expand Down
Loading

0 comments on commit 6c05548

Please sign in to comment.