Skip to content

Commit 5f15d60

Browse files
DanielRoseJakeGinnivan
authored andcommitted
Use new struct BranchCommit to lose less information. Use ExcludingBranches extension method.
1 parent cb77f97 commit 5f15d60

File tree

5 files changed

+84
-25
lines changed

5 files changed

+84
-25
lines changed

src/GitVersionCore/BranchCommit.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace GitVersion
2+
{
3+
using LibGit2Sharp;
4+
5+
/// <summary>
6+
/// A commit, together with the branch to which the commit belongs.
7+
/// </summary>
8+
public struct BranchCommit
9+
{
10+
public static readonly BranchCommit Empty = new BranchCommit();
11+
12+
public BranchCommit(Commit commit, Branch branch) : this()
13+
{
14+
Branch = branch;
15+
Commit = commit;
16+
}
17+
18+
public Branch Branch { get; private set; }
19+
public Commit Commit { get; private set; }
20+
21+
public bool Equals(BranchCommit other)
22+
{
23+
return Equals(Branch, other.Branch) && Equals(Commit, other.Commit);
24+
}
25+
26+
public override bool Equals(object obj)
27+
{
28+
if (ReferenceEquals(null, obj))
29+
return false;
30+
return obj is BranchCommit && Equals((BranchCommit)obj);
31+
}
32+
33+
public override int GetHashCode()
34+
{
35+
unchecked
36+
{
37+
return ((Branch != null ? Branch.GetHashCode() : 0) * 397) ^ (Commit != null ? Commit.GetHashCode() : 0);
38+
}
39+
}
40+
41+
public static bool operator ==(BranchCommit left, BranchCommit right)
42+
{
43+
return left.Equals(right);
44+
}
45+
46+
public static bool operator !=(BranchCommit left, BranchCommit right)
47+
{
48+
return !left.Equals(right);
49+
}
50+
}
51+
}

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static BranchConfig InheritBranchConfiguration(bool onlyEvaluateTrackedBranches,
8787

8888
var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, excludedInheritBranches.ToArray());
8989
List<Branch> possibleParents;
90-
if (branchPoint == null)
90+
if (branchPoint == BranchCommit.Empty)
9191
{
9292
possibleParents = currentCommit.GetBranchesContainingCommit(repository, branchesToEvaluate, true)
9393
// It fails to inherit Increment branch configuration if more than 1 parent;
@@ -97,7 +97,7 @@ static BranchConfig InheritBranchConfiguration(bool onlyEvaluateTrackedBranches,
9797
}
9898
else
9999
{
100-
var branches = branchPoint.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
100+
var branches = branchPoint.Commit.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
101101
if (branches.Count > 1)
102102
{
103103
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="AssemblyVersioningScheme.cs" />
7373
<Compile Include="AssemblyVersionsGenerator.cs" />
7474
<Compile Include="Authentication.cs" />
75+
<Compile Include="BranchCommit.cs" />
7576
<Compile Include="BranchConfigurationCalculator.cs" />
7677
<Compile Include="BuildServers\AppVeyor.cs" />
7778
<Compile Include="BuildServers\BuildServerBase.cs" />

src/GitVersionCore/LibGitExtensions.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IEnumerable<SemanticVersion> GetVersionTagsOnBranch(this Branch br
3636
/// Find the commit where the given branch was branched from another branch.
3737
/// If there are multiple such commits and branches, returns the newest commit.
3838
/// </summary>
39-
public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branch, IRepository repository, params Branch[] excludedBranches)
39+
public static BranchCommit FindCommitBranchWasBranchedFrom([NotNull] this Branch branch, IRepository repository, params Branch[] excludedBranches)
4040
{
4141
const string missingTipFormat = "{0} has no tip. Please see http://example.com/docs for information on how to fix this.";
4242

@@ -50,35 +50,25 @@ public static Commit FindCommitBranchWasBranchedFrom([NotNull] this Branch branc
5050
if (branch.Tip == null)
5151
{
5252
Logger.WriteWarning(string.Format(missingTipFormat, branch.FriendlyName));
53-
return null;
53+
return BranchCommit.Empty;
5454
}
5555

5656
var otherBranches = repository.Branches
57-
.Except(excludedBranches)
58-
.Where(b => IsSameBranch(branch, b))
59-
.ToList();
57+
.ExcludingBranches(excludedBranches)
58+
.Where(b => !IsSameBranch(branch, b));
6059
var mergeBases = otherBranches.Select(otherBranch =>
6160
{
6261
if (otherBranch.Tip == null)
6362
{
6463
Logger.WriteWarning(string.Format(missingTipFormat, otherBranch.FriendlyName));
65-
return null;
64+
return BranchCommit.Empty;
6665
}
6766

6867
var findMergeBase = FindMergeBase(branch, otherBranch, repository);
69-
return new
70-
{
71-
mergeBaseCommit = findMergeBase,
72-
branch = otherBranch
73-
};
74-
}).Where(b => b.mergeBaseCommit != null).OrderByDescending(b => b.mergeBaseCommit.Committer.When).ToList();
68+
return new BranchCommit(findMergeBase,otherBranch);
69+
}).Where(b => b.Commit != null).OrderByDescending(b => b.Commit.Committer.When);
7570

76-
var firstOrDefault = mergeBases.FirstOrDefault();
77-
if (firstOrDefault != null)
78-
{
79-
return firstOrDefault.mergeBaseCommit;
80-
}
81-
return null;
71+
return mergeBases.FirstOrDefault();
8272
}
8373
}
8474

@@ -127,11 +117,28 @@ public static Commit FindMergeBase(this Branch branch, Branch otherBranch, IRepo
127117
}
128118
}
129119

130-
static bool IsSameBranch(Branch branch, Branch b)
120+
/// <summary>
121+
/// Checks if the two branch objects refer to the same branch (have the same friendly name).
122+
/// </summary>
123+
public static bool IsSameBranch(Branch branch, Branch otherBranch)
124+
{
125+
// For each branch, fixup the friendly name if the branch is remote.
126+
var otherBranchFriendlyName = otherBranch.IsRemote ?
127+
otherBranch.FriendlyName.Substring(otherBranch.FriendlyName.IndexOf("/", StringComparison.Ordinal) + 1) :
128+
otherBranch.FriendlyName;
129+
var branchFriendlyName = branch.IsRemote ?
130+
branch.FriendlyName.Substring(branch.FriendlyName.IndexOf("/", StringComparison.Ordinal) + 1) :
131+
branch.FriendlyName;
132+
133+
return otherBranchFriendlyName == branchFriendlyName;
134+
}
135+
136+
/// <summary>
137+
/// Exclude the given branches (by value equality according to friendly name).
138+
/// </summary>
139+
public static IEnumerable<Branch> ExcludingBranches([NotNull] this IEnumerable<Branch> branches, [NotNull] IEnumerable<Branch> branchesToExclude)
131140
{
132-
return (b.IsRemote ?
133-
b.FriendlyName.Substring(b.FriendlyName.IndexOf("/", StringComparison.Ordinal) + 1) :
134-
b.FriendlyName) != branch.FriendlyName;
141+
return branches.Where(b => branchesToExclude.All(bte => !IsSameBranch(b, bte)));
135142
}
136143

137144
public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Commit commit, IRepository repository, IList<Branch> branches, bool onlyTrackedBranches)

src/GitVersionCore/VersionCalculation/BaseVersionCalculators/VersionInBranchBaseVersionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public IEnumerable<BaseVersion> GetVersions(GitVersionContext context, string ta
2727
{
2828
var commitBranchWasBranchedFrom = currentBranch.FindCommitBranchWasBranchedFrom(repository);
2929
var branchNameOverride = branchName.RegexReplace("[-/]" + versionInBranch.Item1, string.Empty);
30-
yield return new BaseVersion(context, "Version in branch name", false, versionInBranch.Item2, commitBranchWasBranchedFrom, branchNameOverride);
30+
yield return new BaseVersion(context, "Version in branch name", false, versionInBranch.Item2, commitBranchWasBranchedFrom.Commit, branchNameOverride);
3131
}
3232
}
3333

0 commit comments

Comments
 (0)