Skip to content

Commit 3fad05e

Browse files
committed
remove redundant dot checks
1 parent f5ebe41 commit 3fad05e

File tree

6 files changed

+24
-35
lines changed

6 files changed

+24
-35
lines changed

GitVersionCore/GitFlow/BranchFinders/HotfixVersionFinder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class HotfixVersionFinder
88
public SemanticVersion FindVersion(GitVersionContext context)
99
{
1010
var versionString = GetSuffix(context.CurrentBranch);
11-
if (!versionString.Contains("."))
12-
return new SemanticVersion();
1311
var shortVersion = ShortVersionParser.Parse(versionString);
1412

1513
EnsureVersionIsValid(shortVersion, context.CurrentBranch);

GitVersionCore/GitFlow/BranchFinders/ReleaseVersionFinder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class ReleaseVersionFinder
88
public SemanticVersion FindVersion(GitVersionContext context)
99
{
1010
var versionString = GetSuffix(context.CurrentBranch);
11-
if (!versionString.Contains("."))
12-
return new SemanticVersion();
1311
var shortVersion = ShortVersionParser.Parse(versionString);
1412

1513
EnsureVersionIsValid(shortVersion, context.CurrentBranch);

GitVersionCore/GitHubFlow/MergedBranchesWithVersionFinder.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
namespace GitVersion
22
{
3-
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65

76
public class MergedBranchesWithVersionFinder
87
{
9-
Lazy<SemanticVersion> lastMergedBranchWithVersion;
8+
GitVersionContext context;
109

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

16-
public SemanticVersion GetVersion()
17-
{
18-
return lastMergedBranchWithVersion.Value;
19-
}
20-
21-
SemanticVersion GetVersion(GitVersionContext context)
15+
public bool TryGetVersion(out SemanticVersion semanticVersion)
2216
{
2317
var shortVersion = GetAllVersions(context)
2418
.OrderBy(x=>x.Major)
2519
.ThenBy(x=>x.Minor).ThenBy(x=>x.Patch)
2620
.LastOrDefault();
2721
if (shortVersion == null)
2822
{
29-
return null;
23+
semanticVersion = null;
24+
return false;
3025
}
31-
return new SemanticVersion
26+
semanticVersion =new SemanticVersion
3227
{
3328
Major = shortVersion.Major,
3429
Minor = shortVersion.Minor,
3530
Patch = shortVersion.Patch
3631
};
32+
return true;
3733
}
3834

3935
static IEnumerable<ShortVersion> GetAllVersions(GitVersionContext context)

GitVersionCore/GitHubFlow/NextSemverCalculator.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@ public SemanticVersion NextVersion()
3333
{
3434
versions.Add(fileVersion);
3535
}
36-
versions.Add(mergedBranchesWithVersionFinder.GetVersion());
36+
SemanticVersion tryGetVersion;
37+
if (mergedBranchesWithVersionFinder.TryGetVersion(out tryGetVersion))
38+
{
39+
versions.Add(tryGetVersion);
40+
}
3741
var otherBranchVersion = unknownBranchFinder.FindVersion(context);
38-
if (otherBranchVersion != null && otherBranchVersion.PreReleaseTag != null && otherBranchVersion.PreReleaseTag.Name == "release")
42+
if (otherBranchVersion != null)
3943
{
40-
otherBranchVersion.PreReleaseTag.Name = "beta";
44+
if (otherBranchVersion.PreReleaseTag != null && otherBranchVersion.PreReleaseTag.Name == "release")
45+
{
46+
otherBranchVersion.PreReleaseTag.Name = "beta";
47+
}
4148
}
4249
versions.Add(otherBranchVersion);
4350

GitVersionCore/GitHubFlow/OtherBranchVersionFinder.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
11
namespace GitVersion
22
{
3-
using System;
43
using System.Linq;
54
using LibGit2Sharp;
65

76
class OtherBranchVersionFinder
87
{
98
public SemanticVersion FindVersion(GitVersionContext context)
10-
{
11-
try
12-
{
13-
return FindVersion(context, "master");
14-
}
15-
catch (Exception)
16-
{
17-
return new SemanticVersion();
18-
}
19-
}
20-
21-
SemanticVersion FindVersion(GitVersionContext context, string baseBranchName)
229
{
2310
var versionString = GetUnknownBranchSuffix(context.CurrentBranch);
2411
if (!versionString.Contains("."))
12+
{
2513
return new SemanticVersion();
14+
}
2615
var shortVersion = ShortVersionParser.Parse(versionString);
2716

2817
var semanticVersionPreReleaseTag = context.CurrentBranch.Name.Replace("-" + versionString, string.Empty) + ".1";
2918

30-
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Unknown, baseBranchName);
31-
19+
var nbHotfixCommits = BranchCommitDifferenceFinder.NumberOfCommitsInBranchNotKnownFromBaseBranch(context.Repository, context.CurrentBranch, BranchType.Unknown, "master");
20+
3221
var tagVersion = RecentTagVersionExtractor.RetrieveMostRecentOptionalTagVersion(context.Repository, shortVersion, context.CurrentBranch.Commits.Take(nbHotfixCommits + 1));
3322
if (tagVersion != null)
3423
{
3524
semanticVersionPreReleaseTag = tagVersion;
3625
}
26+
3727
return new SemanticVersion
3828
{
3929
Major = shortVersion.Major,
@@ -42,7 +32,6 @@ SemanticVersion FindVersion(GitVersionContext context, string baseBranchName)
4232
PreReleaseTag = semanticVersionPreReleaseTag,
4333
BuildMetaData = new SemanticVersionBuildMetaData(nbHotfixCommits, context.CurrentBranch.Name, context.CurrentCommit.Sha, context.CurrentCommit.When())
4434
};
45-
4635
}
4736

4837
static string GetUnknownBranchSuffix(Branch branch)

GitVersionTask.Tests/GitHubFlow/MergedBranchesWithVersionFinderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public void ShouldFindMergeCommit()
1818
};
1919
var sut = new MergedBranchesWithVersionFinder(new GitVersionContext(null, currentBranch));
2020

21-
var version = sut.GetVersion();
21+
SemanticVersion version;
22+
sut.TryGetVersion(out version);
2223

2324
version.ToString().ShouldBe("2.0.0");
2425
}

0 commit comments

Comments
 (0)