Skip to content

Commit dd231ff

Browse files
committed
clean up NextSemverCalculator
1 parent 0ed4a77 commit dd231ff

File tree

4 files changed

+64
-45
lines changed

4 files changed

+64
-45
lines changed

GitVersionCore/GitHubFlow/BuildNumberCalculator.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public BuildNumberCalculator(NextSemverCalculator nextSemverCalculator, LastTagg
1919

2020
public SemanticVersion GetBuildNumber(GitVersionContext context)
2121
{
22-
var commit = lastTaggedReleaseFinder.GetVersion().Commit;
23-
var commitsSinceLastRelease = NumberOfCommitsOnBranchSinceCommit(context, commit);
22+
var commitsSinceLastRelease = GetCommitsSinceLastRelease(context);
2423
var semanticVersion = nextSemverCalculator.NextVersion();
2524

2625
// TODO Need a way of setting this in a cross cutting way
@@ -36,6 +35,21 @@ public SemanticVersion GetBuildNumber(GitVersionContext context)
3635
return semanticVersion;
3736
}
3837

38+
int GetCommitsSinceLastRelease(GitVersionContext context)
39+
{
40+
int commitsSinceLastRelease;
41+
VersionTaggedCommit versionTaggedCommit;
42+
if (lastTaggedReleaseFinder.GetVersion(out versionTaggedCommit))
43+
{
44+
commitsSinceLastRelease = NumberOfCommitsOnBranchSinceCommit(context, versionTaggedCommit.Commit);
45+
}
46+
else
47+
{
48+
commitsSinceLastRelease = NumberOfCommitsOnBranchSinceCommit(context, context.CurrentBranch.Commits.Last());
49+
}
50+
return commitsSinceLastRelease;
51+
}
52+
3953
int NumberOfCommitsOnBranchSinceCommit(GitVersionContext context, Commit commit)
4054
{
4155
var qf = new CommitFilter
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
namespace GitVersion
22
{
3-
using System;
43
using System.Linq;
54
using LibGit2Sharp;
65

76
public class LastTaggedReleaseFinder
87
{
9-
Lazy<VersionTaggedCommit> lastTaggedRelease;
8+
GitVersionContext context;
109

1110
public LastTaggedReleaseFinder(GitVersionContext context)
1211
{
13-
lastTaggedRelease = new Lazy<VersionTaggedCommit>(() => GetVersion(context));
12+
this.context = context;
1413
}
1514

16-
public VersionTaggedCommit GetVersion()
17-
{
18-
return lastTaggedRelease.Value;
19-
}
20-
21-
VersionTaggedCommit GetVersion(GitVersionContext context)
15+
public bool GetVersion(out VersionTaggedCommit versionTaggedCommit)
2216
{
2317
var tags = context.Repository.Tags.Select(t =>
2418
{
@@ -36,10 +30,13 @@ VersionTaggedCommit GetVersion(GitVersionContext context)
3630
context.CurrentBranch.Commits.FirstOrDefault(c => c.When() <= olderThan && tags.Any(a => a.Commit == c));
3731

3832
if (lastTaggedCommit != null)
39-
return tags.Last(a => a.Commit.Sha == lastTaggedCommit.Sha);
33+
{
34+
versionTaggedCommit = tags.Last(a => a.Commit.Sha == lastTaggedCommit.Sha);
35+
return true;
36+
}
4037

41-
var commit = context.CurrentBranch.Commits.Last();
42-
return new VersionTaggedCommit(commit, new SemanticVersion());
38+
versionTaggedCommit = null;
39+
return false;
4340
}
4441
}
4542
}

GitVersionCore/GitHubFlow/NextSemverCalculator.cs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,55 @@ public NextSemverCalculator(
2525

2626
public SemanticVersion NextVersion()
2727
{
28-
var versionZero = new SemanticVersion();
29-
var lastRelease = lastTaggedReleaseFinder.GetVersion();
30-
var versions = new List<SemanticVersion>();
31-
SemanticVersion fileVersion;
32-
if(nextVersionTxtFileFinder.TryGetNextVersion(out fileVersion))
28+
var versions = GetPossibleVersions().ToList();
29+
30+
if (versions.Any())
3331
{
34-
versions.Add(fileVersion);
32+
return versions.Max();
3533
}
36-
SemanticVersion tryGetVersion;
37-
if (mergedBranchesWithVersionFinder.TryGetVersion(out tryGetVersion))
34+
return new SemanticVersion
3835
{
39-
versions.Add(tryGetVersion);
40-
}
41-
42-
var otherBranchVersion = unknownBranchFinder.FindVersion(context);
43-
versions.Add(otherBranchVersion);
36+
Minor = 1
37+
};
38+
}
4439

45-
var maxCalculated = versions.Max();
40+
public IEnumerable<SemanticVersion> GetPossibleVersions()
41+
{
4642

47-
if (lastRelease.SemVer == versionZero && maxCalculated == versionZero)
43+
VersionTaggedCommit lastTaggedRelease;
44+
if (lastTaggedReleaseFinder.GetVersion(out lastTaggedRelease))
4845
{
49-
return new SemanticVersion
46+
//If the exact commit is tagged then just return that commit
47+
if (context.CurrentCommit.Sha == lastTaggedRelease.Commit.Sha)
5048
{
51-
Minor = 1
52-
};
49+
yield return lastTaggedRelease.SemVer;
50+
yield break;
51+
}
52+
yield return new SemanticVersion
53+
{
54+
Major = lastTaggedRelease.SemVer.Major,
55+
Minor = lastTaggedRelease.SemVer.Minor,
56+
Patch = lastTaggedRelease.SemVer.Patch + 1
57+
};
5358
}
5459

55-
if (context.CurrentCommit.Sha == lastRelease.Commit.Sha)
60+
SemanticVersion fileVersion;
61+
if (nextVersionTxtFileFinder.TryGetNextVersion(out fileVersion))
5662
{
57-
return lastRelease.SemVer;
63+
yield return fileVersion;
64+
}
65+
SemanticVersion tryGetVersion;
66+
if (mergedBranchesWithVersionFinder.TryGetVersion(out tryGetVersion))
67+
{
68+
yield return tryGetVersion;
5869
}
5970

60-
if (maxCalculated <= lastRelease.SemVer)
71+
SemanticVersion otherBranchVersion;
72+
if (unknownBranchFinder.FindVersion(context, out otherBranchVersion))
6173
{
62-
return new SemanticVersion
63-
{
64-
Major = lastRelease.SemVer.Major,
65-
Minor = lastRelease.SemVer.Minor,
66-
Patch = lastRelease.SemVer.Patch + 1
67-
};
74+
yield return otherBranchVersion;
6875
}
6976

70-
return maxCalculated;
7177
}
7278
}
7379
}

GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
class OtherBranchVersionFinder
77
{
8-
public SemanticVersion FindVersion(GitVersionContext context)
8+
public bool FindVersion(GitVersionContext context, out SemanticVersion semanticVersion)
99
{
1010
var versionString = GetUnknownBranchSuffix(context.CurrentBranch);
1111
if (!versionString.Contains("."))
1212
{
13-
return new SemanticVersion();
13+
semanticVersion = null;
14+
return false;
1415
}
1516
var shortVersion = ShortVersionParser.Parse(versionString);
1617

@@ -29,14 +30,15 @@ public SemanticVersion FindVersion(GitVersionContext context)
2930
semanticVersionPreReleaseTag.Name = "beta";
3031
}
3132

32-
return new SemanticVersion
33+
semanticVersion = new SemanticVersion
3334
{
3435
Major = shortVersion.Major,
3536
Minor = shortVersion.Minor,
3637
Patch = shortVersion.Patch,
3738
PreReleaseTag = semanticVersionPreReleaseTag,
3839
BuildMetaData = new SemanticVersionBuildMetaData(nbHotfixCommits, context.CurrentBranch.Name, context.CurrentCommit.Sha, context.CurrentCommit.When())
3940
};
41+
return true;
4042
}
4143

4244
static string GetUnknownBranchSuffix(Branch branch)

0 commit comments

Comments
 (0)