Skip to content

Commit 95626b2

Browse files
committed
use newest tags
also better version parsing on master
1 parent 7e2de65 commit 95626b2

19 files changed

+176
-52
lines changed

GitFlowVersion/BranchClassifier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace GitFlowVersion
22
{
33
using LibGit2Sharp;
44

5-
public static class BranchClassifier
5+
static class BranchClassifier
66
{
77

88
public static bool IsHotfix(this Branch branch)

GitFlowVersion/BranchFinders/MasterVersionFinder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace GitFlowVersion
22
{
3-
using System.Linq;
43
using LibGit2Sharp;
54

65
class MasterVersionFinder
@@ -13,7 +12,7 @@ public VersionAndBranch FindVersion()
1312
int major;
1413
int minor;
1514
int patch;
16-
foreach (var tag in Repository.Tags.Where(tag => tag.Target == Commit).Reverse())
15+
foreach (var tag in Repository.TagsByDate(Commit))
1716
{
1817
if (ShortVersionParser.TryParse(tag.Name, out major, out minor, out patch))
1918
{

GitFlowVersion/CachedVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace GitFlowVersion
22
{
3-
public class CachedVersion
3+
class CachedVersion
44
{
55
public VersionAndBranch VersionAndBranch;
66
public long Timestamp;

GitFlowVersion/DirectoryDateFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using System.IO;
55

6-
public static class DirectoryDateFinder
6+
static class DirectoryDateFinder
77
{
88
public static long GetLastDirectoryWrite(string path)
99
{

GitFlowVersion/LibGitExtensions.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace GitFlowVersion
55
using System.Linq;
66
using LibGit2Sharp;
77

8-
public static class LibGitExtensions
8+
static class LibGitExtensions
99
{
1010

1111
public static DateTimeOffset When(this Commit commit)
@@ -24,23 +24,33 @@ public static string Prefix(this Commit commit)
2424

2525
public static SemanticVersion NewestSemVerTag(this IRepository repository, Commit commit)
2626
{
27-
SemanticVersion highest = null;
28-
foreach (var tag in repository.Tags.Where(tag => tag.Target == commit))
27+
foreach (var tag in repository.TagsByDate(commit))
2928
{
3029
SemanticVersion version;
31-
if (!SemanticVersionParser.TryParse(tag.Name, out version))
30+
if (SemanticVersionParser.TryParse(tag.Name, out version))
3231
{
33-
continue;
32+
return version;
3433
}
34+
}
35+
return null;
36+
}
3537

36-
if (version.CompareTo(highest) > 0)
38+
public static IEnumerable<Tag> TagsByDate(this IRepository repository, Commit commit)
39+
{
40+
return repository.Tags
41+
.Where(tag => tag.Target == commit)
42+
.OrderByDescending(tag =>
3743
{
38-
highest = version;
39-
}
40-
}
41-
return highest;
44+
if (tag.Annotation != null)
45+
{
46+
return tag.Annotation.Tagger.When;
47+
}
48+
//lightweight tags will not have an Annotation
49+
return commit.Committer.When;
50+
});
4251
}
4352

53+
4454
public static IEnumerable<Commit> CommitsPriorToThan(this Branch branch, DateTimeOffset olderThan)
4555
{
4656
return branch.Commits.SkipWhile(c => c.When() > olderThan);

GitFlowVersion/MergeMessageParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace GitFlowVersion
33
using System.Linq;
44
using LibGit2Sharp;
55

6-
public class MergeMessageParser
6+
class MergeMessageParser
77
{
88
public static bool TryParse(Commit mergeCommit, out string versionPart)
99
{
@@ -14,7 +14,7 @@ public static bool TryParse(Commit mergeCommit, out string versionPart)
1414
return false;
1515
}
1616

17-
string message = mergeCommit.Message;
17+
var message = mergeCommit.Message;
1818

1919
string trimmed;
2020
if (message.StartsWith("Merge branch 'hotfix-"))

GitFlowVersion/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitFlowVersion
66
using System.IO;
77
using System.Linq;
88

9-
public class Program
9+
class Program
1010
{
1111
static void Main()
1212
{

GitFlowVersion/SearchPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using System.IO;
55

6-
public class SearchPath
6+
class SearchPath
77
{
88
static bool isPathSet;
99

GitFlowVersion/ShortVersionParser.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace GitFlowVersion
22
{
33
using System;
44

5-
public class ShortVersionParser
5+
class ShortVersionParser
66
{
77

88
public static void Parse(string versionString, out int major, out int minor, out int patch)
@@ -31,14 +31,45 @@ public static void Parse(string versionString, out int major, out int minor, out
3131

3232
public static bool TryParseMajorMinor(string versionString, out int major, out int minor)
3333
{
34-
int patch;
34+
major = 0;
35+
minor = 0;
36+
var strings = versionString.Split('.');
37+
if (strings.Length == 2)
38+
{
39+
if (!int.TryParse(strings[0], out major))
40+
{
41+
return false;
42+
}
43+
44+
if (!int.TryParse(strings[1], out minor))
45+
{
46+
return false;
47+
}
48+
return true;
49+
}
3550

36-
if (!TryParse(versionString, out major, out minor, out patch))
51+
52+
if (strings.Length == 3)
3753
{
38-
return false;
54+
if (!int.TryParse(strings[0], out major))
55+
{
56+
return false;
57+
}
58+
59+
if (!int.TryParse(strings[1], out minor))
60+
{
61+
return false;
62+
}
63+
int patch;
64+
if (!int.TryParse(strings[2], out patch))
65+
{
66+
return false;
67+
}
68+
return patch == 0;
3969
}
4070

41-
return patch == 0;
71+
72+
return false;
4273
}
4374

4475
public static bool TryParse(string versionString, out int major, out int minor, out int patch)
@@ -74,4 +105,5 @@ static void Throw(string versionString)
74105
throw new Exception(string.Format("Could not parse version from '{0}' expected 'major.minor.patch'", versionString));
75106
}
76107
}
108+
77109
}

GitFlowVersion/TeamCityVersionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using System;
44

5-
public class TeamCityVersionBuilder
5+
class TeamCityVersionBuilder
66
{
77
public static string GenerateBuildVersion(VersionAndBranch versionAndBranch)
88
{

0 commit comments

Comments
 (0)