Skip to content

Commit 62204f7

Browse files
committed
simplify OptionallyTaggedBranchVersionFinderBase
1 parent 7db87ef commit 62204f7

File tree

7 files changed

+112
-115
lines changed

7 files changed

+112
-115
lines changed

GitVersionCore.Tests/ShortVersionParserTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public class ShortVersionParserTests
88
[Test]
99
public void Major_minor_patch()
1010
{
11-
ShortVersion shortVersion;
12-
ShortVersionParser.Parse("1.2.3", out shortVersion);
11+
var shortVersion = ShortVersionParser.Parse("1.2.3");
1312
Assert.AreEqual(1, shortVersion.Major);
1413
Assert.AreEqual(2, shortVersion.Minor);
1514
Assert.AreEqual(3, shortVersion.Patch);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace GitVersion
2+
{
3+
using System.Linq;
4+
using LibGit2Sharp;
5+
6+
static class BranchCommitDifferenceFinder
7+
{
8+
public static int NumberOfCommitsInBranchNotKnownFromBaseBranch(IRepository repo, Branch branch, BranchType branchType, string baseBranchName)
9+
{
10+
var baseTip = repo.FindBranch(baseBranchName).Tip;
11+
if (branch.Tip == baseTip)
12+
{
13+
// The branch bears no additional commit
14+
return 0;
15+
}
16+
17+
var ancestor = repo.Commits.FindMergeBase(
18+
baseTip,
19+
branch.Tip);
20+
21+
if (ancestor == null)
22+
{
23+
var message = string.Format("A {0} branch is expected to branch off of '{1}'. However, branch '{1}' and '{2}' do not share a common ancestor.", branchType, baseBranchName, branch.Name);
24+
throw new WarningException(message);
25+
}
26+
27+
var filter = new CommitFilter
28+
{
29+
Since = branch.Tip,
30+
Until = ancestor,
31+
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
32+
};
33+
34+
return repo.Commits.QueryBy(filter)
35+
.Count();
36+
}
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace GitVersion
2+
{
3+
using System.Collections.Generic;
4+
using LibGit2Sharp;
5+
6+
class RecentTagVersionExtractor
7+
{
8+
internal static SemanticVersionPreReleaseTag RetrieveMostRecentOptionalTagVersion(IRepository repository, ShortVersion matchVersion, IEnumerable<Commit> take)
9+
{
10+
Commit first = null;
11+
foreach (var commit in take)
12+
{
13+
if (first == null)
14+
{
15+
first = commit;
16+
}
17+
foreach (var tag in repository.TagsByDate(commit))
18+
{
19+
SemanticVersion version;
20+
if (!SemanticVersion.TryParse(tag.Name, out version))
21+
{
22+
continue;
23+
}
24+
25+
if (matchVersion.Major == version.Major &&
26+
matchVersion.Minor == version.Minor &&
27+
matchVersion.Patch == version.Patch)
28+
{
29+
var preReleaseTag = version.PreReleaseTag;
30+
31+
//If the tag is on the eact commit then dont bump the PreReleaseTag
32+
if (first != commit)
33+
{
34+
preReleaseTag.Number++;
35+
}
36+
return preReleaseTag;
37+
}
38+
}
39+
}
40+
41+
return null;
42+
}
43+
44+
45+
}
46+
}
Lines changed: 20 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace GitVersion
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.Linq;
65
using LibGit2Sharp;
76

@@ -12,104 +11,46 @@ protected SemanticVersion FindVersion(
1211
BranchType branchType,
1312
string baseBranchName)
1413
{
15-
var nbHotfixCommits = NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, branchType, baseBranchName);
1614

1715
var versionString = context.CurrentBranch.GetSuffix(branchType);
1816
if (!versionString.Contains("."))
1917
return new SemanticVersion();
20-
var version = SemanticVersion.Parse(versionString);
21-
22-
EnsureVersionIsValid(version, context.CurrentBranch, branchType);
18+
var shortVersion = ShortVersionParser.Parse(versionString);
2319

20+
EnsureVersionIsValid(shortVersion, context.CurrentBranch, branchType);
21+
var semanticVersionPreReleaseTag = new SemanticVersionPreReleaseTag();
2422
if (branchType == BranchType.Hotfix)
25-
version.PreReleaseTag = "beta.1";
23+
semanticVersionPreReleaseTag = "beta.1";
2624
if (branchType == BranchType.Release)
27-
version.PreReleaseTag = "beta.1";
25+
semanticVersionPreReleaseTag = "beta.1";
2826
if (branchType == BranchType.Unknown)
29-
version.PreReleaseTag = context.CurrentBranch.Name.Replace("-" + versionString, string.Empty) + ".1";
27+
semanticVersionPreReleaseTag = context.CurrentBranch.Name.Replace("-" + versionString, string.Empty) + ".1";
3028

31-
var tagVersion = RetrieveMostRecentOptionalTagVersion(context.Repository, version, context.CurrentBranch.Commits.Take(nbHotfixCommits + 1));
32-
33-
var semanticVersion = new SemanticVersion
34-
{
35-
Major = version.Major,
36-
Minor = version.Minor,
37-
Patch = version.Patch,
38-
PreReleaseTag = version.PreReleaseTag,
39-
BuildMetaData = new SemanticVersionBuildMetaData(
40-
nbHotfixCommits, context.CurrentBranch.Name, context.CurrentCommit.Sha, context.CurrentCommit.When())
41-
};
4229

30+
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, branchType, baseBranchName);
31+
32+
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context.Repository, shortVersion, context.CurrentBranch.Commits.Take(nbHotfixCommits + 1));
4333
if (tagVersion != null)
4434
{
45-
//If the tag is on the eact commit then dont bump the PreReleaseTag
46-
if (context.CurrentCommit.Sha != tagVersion.Commit.Sha)
47-
{
48-
tagVersion.SemVer.PreReleaseTag.Number++;
49-
}
50-
semanticVersion.PreReleaseTag = tagVersion.SemVer.PreReleaseTag;
35+
semanticVersionPreReleaseTag = tagVersion;
5136
}
52-
53-
return semanticVersion;
54-
}
55-
56-
bool IsMostRecentCommitTagged(GitVersionContext context, out SemanticVersion version)
57-
{
58-
var currentCommit = context.CurrentBranch.Commits.First();
59-
60-
var tags = context.Repository.Tags
61-
.Where(tag => tag.PeeledTarget() == currentCommit)
62-
.ToList();
63-
64-
foreach (var tag in tags)
37+
return new SemanticVersion
6538
{
66-
if (SemanticVersion.TryParse(tag.Name, out version))
67-
{
68-
return true;
69-
}
70-
}
39+
Major = shortVersion.Major,
40+
Minor = shortVersion.Minor,
41+
Patch = shortVersion.Patch,
42+
PreReleaseTag = semanticVersionPreReleaseTag,
43+
BuildMetaData = new SemanticVersionBuildMetaData(nbHotfixCommits, context.CurrentBranch.Name, context.CurrentCommit.Sha, context.CurrentCommit.When())
44+
};
7145

72-
version = null;
73-
return false;
7446
}
7547

76-
VersionTaggedCommit RetrieveMostRecentOptionalTagVersion(
77-
IRepository repository, SemanticVersion branchVersion, IEnumerable<Commit> take)
78-
{
79-
foreach (var commit in take)
80-
{
81-
foreach (var tag in repository.TagsByDate(commit))
82-
{
83-
SemanticVersion version;
84-
if (!SemanticVersion.TryParse(tag.Name, out version))
85-
{
86-
continue;
87-
}
88-
89-
if (branchVersion.Major != version.Major ||
90-
branchVersion.Minor != version.Minor ||
91-
branchVersion.Patch != version.Patch)
92-
{
93-
continue;
94-
}
95-
96-
return new VersionTaggedCommit(commit, version);
97-
}
98-
}
9948

100-
return null;
101-
}
10249

103-
void EnsureVersionIsValid(SemanticVersion version, Branch branch, BranchType branchType)
50+
void EnsureVersionIsValid(ShortVersion version, Branch branch, BranchType branchType)
10451
{
105-
var msg = string.Format("Branch '{0}' doesn't respect the {1} branch naming convention. ",
106-
branch.Name, branchType);
107-
108-
if (version.PreReleaseTag.HasTag())
109-
{
110-
throw new WarningException(msg + string.Format("Supported format is '{0}-Major.Minor.Patch'.", branchType.ToString().ToLowerInvariant()));
111-
}
112-
52+
var msg = string.Format("Branch '{0}' doesn't respect the {1} branch naming convention. ", branch.Name, branchType);
53+
11354
switch (branchType)
11455
{
11556
case BranchType.Hotfix:
@@ -136,37 +77,5 @@ void EnsureVersionIsValid(SemanticVersion version, Branch branch, BranchType bra
13677
}
13778
}
13879

139-
int NumberOfCommitsInBranchNotKnownFromBaseBranch(
140-
IRepository repo,
141-
Branch branch,
142-
BranchType branchType,
143-
string baseBranchName)
144-
{
145-
var baseTip = repo.FindBranch(baseBranchName).Tip;
146-
if (branch.Tip == baseTip)
147-
{
148-
// The branch bears no additional commit
149-
return 0;
150-
}
151-
152-
var ancestor = repo.Commits.FindMergeBase(
153-
baseTip,
154-
branch.Tip);
155-
156-
if (ancestor == null)
157-
{
158-
var message = string.Format("A {0} branch is expected to branch off of '{1}'. However, branch '{1}' and '{2}' do not share a common ancestor.", branchType, baseBranchName, branch.Name);
159-
throw new WarningException(message);
160-
}
161-
162-
var filter = new CommitFilter
163-
{
164-
Since = branch.Tip,
165-
Until = ancestor,
166-
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
167-
};
168-
169-
return repo.Commits.QueryBy(filter).Count();
170-
}
17180
}
17281
}

GitVersionCore/GitVersionCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
<Compile Include="BuildServers\GitHelper.cs" />
6464
<Compile Include="BuildServers\IBuildServer.cs" />
6565
<Compile Include="BuildServers\TeamCity.cs" />
66+
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
67+
<Compile Include="GitFlow\BranchFinders\OptionallyTaggedBranchVersionFinderBase - Copy.cs" />
6668
<Compile Include="LastVersionOnMasterFinder.cs" />
6769
<Compile Include="ShortVersion.cs" />
6870
<Compile Include="SemanticVersionExtensions.cs" />

GitVersionCore/ShortVersionParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ namespace GitVersion
44

55
class ShortVersionParser
66
{
7-
public static void Parse(string versionString, out ShortVersion shortVersion)
7+
public static ShortVersion Parse(string versionString)
88
{
9+
ShortVersion shortVersion;
910
if (!TryParse(versionString, out shortVersion))
1011
{
1112
throw new Exception(string.Format("Could not parse version from '{0}' expected 'major.minor.patch'", versionString));
1213
}
14+
return shortVersion;
1315
}
1416

1517
public static bool TryParseMajorMinor(string versionString, out ShortVersion shortVersion)

GitVersionTask.Tests/BranchFinders/ReleaseTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using GitVersion;
23
using LibGit2Sharp;
34
using NUnit.Framework;
@@ -36,7 +37,7 @@ public void EnsureAReleaseBranchNameDoesNotExposeAStability()
3637

3738
var finder = new ReleaseVersionFinder();
3839

39-
Assert.Throws<WarningException>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch)));
40+
Assert.Throws<Exception>(() => finder.FindVersion(new GitVersionContext(repo, releaseBranch)));
4041
}
4142
}
4243

0 commit comments

Comments
 (0)