Skip to content

Commit 85899c2

Browse files
Conflicts: GitVersion/VersionForDirectoryFinder.cs GitVersionTask/NugetAssets/GitVersionTask.nuspec Tests/AssemblyInfoBuilderTests.VerifyCreatedCode.approved.txt Tests/VersionOnMasterFinderTests.cs
2 parents cf61c53 + 3554bd4 commit 85899c2

26 files changed

+278
-40
lines changed

GitVersion/BuildServers/GitHelper.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace GitVersion
22
{
3+
using System;
34
using System.Linq;
45
using LibGit2Sharp;
56

@@ -14,7 +15,8 @@ public static void NormalizeGitDirectory(string gitDirectory)
1415
Logger.WriteInfo(string.Format("Fetching from remote '{0}' using the following refspecs: {1}.",
1516
remote.Name, string.Join(", ", remote.FetchRefSpecs.Select(r => r.Specification))));
1617

17-
repo.Network.Fetch(remote);
18+
var fetchOptions = BuildFetchOptions();
19+
repo.Network.Fetch(remote, fetchOptions);
1820

1921
CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
2022

@@ -30,6 +32,21 @@ public static void NormalizeGitDirectory(string gitDirectory)
3032
}
3133
}
3234

35+
static FetchOptions BuildFetchOptions()
36+
{
37+
var username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
38+
var password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
39+
40+
var fetchOptions = new FetchOptions();
41+
42+
if (!string.IsNullOrEmpty(username))
43+
{
44+
fetchOptions.Credentials = new Credentials { Username = username, Password = password };
45+
}
46+
47+
return fetchOptions;
48+
}
49+
3350
static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo)
3451
{
3552
var remote = repo.Network.Remotes.Single();

GitVersion/BuildServers/TeamCity.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public string GenerateSetVersionMessage(string versionToUseForBuildNumber)
3636

3737
static string EscapeValue(string value)
3838
{
39+
if (value == null)
40+
{
41+
return null;
42+
}
3943
// List of escape values from http://confluence.jetbrains.com/display/TCD8/Build+Script+Interaction+with+TeamCity
4044

4145
value = value.Replace("|", "||");

GitVersion/CachedVersion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
class CachedVersion
44
{
5-
public VersionAndBranch VersionAndBranch;
5+
public VersionAndBranchAndDate VersionAndBranch;
66
public long Timestamp;
77
}
8-
}
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace GitVersion
2+
{
3+
public class VersionAndBranchAndDate : VersionAndBranch
4+
{
5+
public ReleaseDate ReleaseDate;
6+
7+
public VersionAndBranchAndDate() { }
8+
9+
public VersionAndBranchAndDate(VersionAndBranch vab, ReleaseDate rd)
10+
{
11+
Version = vab.Version;
12+
BranchType = vab.BranchType;
13+
BranchName = vab.BranchName;
14+
Sha = vab.Sha;
15+
ReleaseDate = rd;
16+
}
17+
}
18+
}

GitVersion/GitVersion.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="GitFlow\BranchFinders\FeatureVersionFinder.cs" />
6363
<Compile Include="GitFlow\BranchFinders\DevelopVersionFinder.cs" />
6464
<Compile Include="GitFlow\BranchFinders\HotfixVersionFinder.cs" />
65+
<Compile Include="GitFlow\VersionAndBranchAndDate.cs" />
6566
<Compile Include="GitHubFlow\BuildNumberCalculator.cs" />
6667
<Compile Include="GitHubFlow\VersionTaggedCommit.cs" />
6768
<Compile Include="GitHubFlow\GitHubFlowVersionFinder.cs" />
@@ -79,6 +80,8 @@
7980
<Compile Include="HelpWriter.cs" />
8081
<Compile Include="GitFlow\ReleaseInformation.cs" />
8182
<Compile Include="GitFlow\ReleaseInformationCalculator.cs" />
83+
<Compile Include="ReleaseDate.cs" />
84+
<Compile Include="ReleaseDateFinder.cs" />
8285
<Compile Include="SemanticVersionTag.cs" />
8386
<Compile Include="OutputFormatters\JsonOutputFormatter.cs" />
8487
<Compile Include="VersionBuilders\NugetVersionBuilder.cs" />

GitVersion/GitVersionFinder.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ public VersionAndBranch FindVersion(GitVersionContext context)
1010
{
1111
EnsureMainTopologyConstraints(context);
1212

13-
var hasDevelopBranch = context.Repository.FindBranch("develop") != null;
14-
15-
if (hasDevelopBranch)
13+
if (ShouldGitHubFlowVersioningSchemeApply(context.Repository))
1614
{
17-
return new GitFlowVersionFinder().FindVersion(context);
15+
return new GitHubFlowVersionFinder().FindVersion(context);
1816
}
19-
return new GitHubFlowVersionFinder().FindVersion(context);
17+
18+
return new GitFlowVersionFinder().FindVersion(context);
19+
}
20+
21+
public static bool ShouldGitHubFlowVersioningSchemeApply(IRepository repo)
22+
{
23+
return repo.FindBranch("develop") == null;
2024
}
2125

2226
void EnsureMainTopologyConstraints(GitVersionContext context)

GitVersion/ReleaseDate.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
public class ReleaseDate
4+
{
5+
public DateTimeOffset OriginalDate;
6+
public string OriginalCommitSha;
7+
public DateTimeOffset Date;
8+
public string CommitSha;
9+
}

GitVersion/ReleaseDateFinder.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Diagnostics;
2+
using GitVersion;
3+
using LibGit2Sharp;
4+
5+
public class ReleaseDateFinder
6+
{
7+
public static ReleaseDate Execute(IRepository repo, VersionAndBranch vab)
8+
{
9+
var c = repo.Lookup<Commit>(vab.Sha);
10+
Debug.Assert(c != null);
11+
12+
var rd = new ReleaseDate
13+
{
14+
OriginalDate = c.When(),
15+
OriginalCommitSha = c.Sha,
16+
Date = c.When(),
17+
CommitSha = c.Sha,
18+
};
19+
20+
if (GitVersionFinder.ShouldGitHubFlowVersioningSchemeApply(repo))
21+
{
22+
return rd;
23+
}
24+
25+
if (vab.Version.Patch == 0)
26+
{
27+
return rd;
28+
}
29+
30+
var vp = new VersionOnMasterFinder().FindLatestStableTaggedCommitReachableFrom(repo, c);
31+
var latestStable = repo.Lookup<Commit>(vp.CommitSha);
32+
Debug.Assert(latestStable != null);
33+
34+
rd.OriginalDate = latestStable.When();
35+
rd.OriginalCommitSha = vp.CommitSha;
36+
return rd;
37+
}
38+
}

GitVersion/ShortVersionParser.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public static bool TryParseMajorMinor(string versionString, out int major, out i
1616
{
1717
int patch;
1818

19-
TryParse(versionString, out major, out minor, out patch);
19+
if (!TryParse(versionString, out major, out minor, out patch))
20+
{
21+
return false;
22+
}
2023

2124
// Note: during scanning of master we only want the last major / minor, not the patch, so patch must be zero
2225
return patch == 0;
@@ -28,7 +31,7 @@ public static bool TryParse(string versionString, out int major, out int minor,
2831
minor = 0;
2932
patch = 0;
3033
var strings = versionString.Split('.');
31-
if (strings.Length < 2)
34+
if (strings.Length < 2 || strings.Length > 3)
3235
{
3336
return false;
3437
}
@@ -42,7 +45,7 @@ public static bool TryParse(string versionString, out int major, out int minor,
4245
return false;
4346
}
4447

45-
if (strings.Length >= 3)
48+
if (strings.Length == 3)
4649
{
4750
if (!int.TryParse(strings[2], out patch))
4851
{

GitVersion/VersionCache.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class VersionCache
77
{
88
static Dictionary<string, CachedVersion> versionCacheVersions = new Dictionary<string, CachedVersion>();
99

10-
public static VersionAndBranch GetVersion(string gitDirectory)
10+
public static VersionAndBranchAndDate GetVersion(string gitDirectory)
1111
{
1212
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
1313
{
@@ -19,7 +19,7 @@ public static VersionAndBranch GetVersion(string gitDirectory)
1919
var ticks = DirectoryDateFinder.GetLastDirectoryWrite(gitDirectory);
2020
var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
2121
CachedVersion cachedVersion;
22-
VersionAndBranch versionAndBranch;
22+
VersionAndBranchAndDate versionAndBranch;
2323
if (versionCacheVersions.TryGetValue(key, out cachedVersion))
2424
{
2525
Logger.WriteInfo("Version read from cache.");
@@ -49,7 +49,7 @@ public static VersionAndBranch GetVersion(string gitDirectory)
4949
}
5050
}
5151

52-
static VersionAndBranch GetSemanticVersion(Repository repository)
52+
static VersionAndBranchAndDate GetSemanticVersion(Repository repository)
5353
{
5454
var versionForRepositoryFinder = new VersionForRepositoryFinder();
5555
return versionForRepositoryFinder.GetVersion(repository);

GitVersion/VersionForDirectoryFinder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ namespace GitVersion
44

55
public class VersionForRepositoryFinder
66
{
7-
public SemanticVersion SemanticVersion;
8-
9-
public VersionAndBranch GetVersion(Repository repository)
7+
public VersionAndBranchAndDate GetVersion(Repository repository)
108
{
119
var gitVersionFinder = new GitVersionFinder();
12-
return gitVersionFinder.FindVersion(new GitVersionContext
10+
var vab = gitVersionFinder.FindVersion(new GitVersionContext
1311
{
1412
CurrentBranch = repository.Head,
1513
Repository = repository
1614
});
15+
16+
var rd = ReleaseDateFinder.Execute(repository, vab);
17+
18+
return new VersionAndBranchAndDate(vab, rd);
1719
}
1820
}
1921
}

GitVersion/VersionOnMasterFinder.cs

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
namespace GitVersion
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using LibGit2Sharp;
48

59
class VersionOnMasterFinder
610
{
@@ -19,7 +23,8 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
1923
{
2024
Major = major,
2125
Minor = minor,
22-
Timestamp = commit.When()
26+
Timestamp = commit.When(),
27+
CommitSha = commit.Sha,
2328
};
2429
}
2530
}
@@ -34,7 +39,8 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
3439
{
3540
Major = major,
3641
Minor = minor,
37-
Timestamp = commit.When()
42+
Timestamp = commit.When(),
43+
CommitSha = commit.Sha,
3844
};
3945
}
4046
}
@@ -44,9 +50,93 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
4450
{
4551
Major = 0,
4652
Minor = 1,
47-
Timestamp = DateTimeOffset.MinValue
53+
Timestamp = DateTimeOffset.MinValue,
54+
CommitSha = null,
4855
};
4956
}
5057

58+
public VersionPoint FindLatestStableTaggedCommitReachableFrom(IRepository repo, Commit commit)
59+
{
60+
var masterTip = repo.FindBranch("master").Tip;
61+
var ancestor = repo.Commits.FindCommonAncestor(masterTip, commit);
62+
63+
var allTags = repo.Tags.ToList();
64+
65+
foreach (var c in repo.Commits.QueryBy(new CommitFilter { Since = ancestor.Id }))
66+
{
67+
var vp = RetrieveStableVersionPointFor(allTags, c);
68+
69+
if (vp != null)
70+
{
71+
return vp;
72+
}
73+
}
74+
75+
return null;
76+
}
77+
78+
static VersionPoint RetrieveStableVersionPointFor(IEnumerable<Tag> allTags, Commit c)
79+
{
80+
var tags = allTags
81+
.Where(tag => tag.PeeledTarget() == c)
82+
.Where(tag => IsStableRelease(tag.Name))
83+
.ToList();
84+
85+
if (tags.Count == 0)
86+
{
87+
return null;
88+
}
89+
90+
if (tags.Count > 1)
91+
{
92+
throw new ErrorException(
93+
string.Format("Commit '{0}' bears more than one stable tag: {1}",
94+
c.Id.ToString(7), string.Join(", ", tags.Select(t => t.Name))));
95+
}
96+
97+
var stableTag = tags.Single();
98+
var commit = RetrieveMergeCommit(stableTag);
99+
100+
return BuildFrom(stableTag, commit);
101+
}
102+
103+
static VersionPoint BuildFrom(Tag stableTag, Commit commit)
104+
{
105+
int major;
106+
int minor;
107+
108+
var hasParsed = ShortVersionParser.TryParseMajorMinor(stableTag.Name, out major, out minor);
109+
Debug.Assert(hasParsed);
110+
111+
return new VersionPoint
112+
{
113+
Major = major,
114+
Minor = minor,
115+
CommitSha = commit.Id.Sha,
116+
};
117+
}
118+
119+
static Commit RetrieveMergeCommit(Tag stableTag)
120+
{
121+
var target = stableTag.PeeledTarget();
122+
if (!(target is Commit))
123+
{
124+
throw new ErrorException(
125+
string.Format("Target '{0}' of Tag '{1}' isn't a Commit.",
126+
target.Id.ToString(7), stableTag.Name));
127+
}
128+
129+
var targetCommit = (Commit) target;
130+
131+
return targetCommit;
132+
}
133+
134+
static bool IsStableRelease(string tagName)
135+
{
136+
int major;
137+
int minor;
138+
139+
return ShortVersionParser.TryParseMajorMinor(tagName, out major, out minor);
140+
}
51141
}
52-
}
142+
}

GitVersion/VersionPoint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ class VersionPoint
77
public int Major;
88
public int Minor;
99
public DateTimeOffset Timestamp;
10+
public string CommitSha;
1011
}
11-
}
12+
}

0 commit comments

Comments
 (0)