Skip to content

Commit 2d9f9b6

Browse files
committed
Generate ReleaseDate attribute
1 parent 62a1587 commit 2d9f9b6

15 files changed

+208
-15
lines changed

GitFlowVersion/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 GitFlowVersion
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+
}

GitFlowVersion/GitFlowVersion.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" />

GitFlowVersion/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+
}

GitFlowVersion/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 GitFlowVersion;
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+
}

GitFlowVersion/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);

GitFlowVersion/VersionForDirectoryFinder.cs

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

55
public class VersionForRepositoryFinder
66
{
7-
public VersionAndBranch GetVersion(Repository repository)
7+
public VersionAndBranchAndDate GetVersion(Repository repository)
88
{
99
var gitFlowVersionFinder = new GitVersionFinder();
10-
return gitFlowVersionFinder.FindVersion(new GitVersionContext
10+
var vab = gitFlowVersionFinder.FindVersion(new GitVersionContext
1111
{
1212
CurrentBranch = repository.Head,
1313
Repository = repository
1414
});
15+
16+
var rd = ReleaseDateFinder.Execute(repository, vab);
17+
18+
return new VersionAndBranchAndDate(vab, rd);
1519
}
1620
}
1721
}

GitFlowVersion/VersionOnMasterFinder.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
namespace GitFlowVersion
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
{
@@ -51,5 +55,88 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
5155
};
5256
}
5357

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+
}
54141
}
55142
}

GitFlowVersionTask/AssemblyInfoBuilder.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class AssemblyInfoBuilder
66
{
77

8-
public VersionAndBranch VersionAndBranch;
8+
public VersionAndBranchAndDate VersionAndBranch;
99
public bool SignAssembly;
1010
public string AssemblyName;
1111

@@ -19,6 +19,7 @@ public string GetAssemblyInfoText()
1919
[assembly: AssemblyFileVersion(""{1}"")]
2020
[assembly: AssemblyInformationalVersion(""{2}"")]
2121
[assembly: {4}.NugetVersion(""{3}"")]
22+
[assembly: {4}.ReleaseDate(""{6}"", ""{7}"")]
2223
2324
namespace {4}
2425
{{
@@ -32,6 +33,19 @@ public NugetVersionAttribute(string version)
3233
3334
public string Version{{get;set;}}
3435
}}
36+
37+
[System.Runtime.CompilerServices.CompilerGenerated]
38+
class ReleaseDateAttribute : System.Attribute
39+
{{
40+
public string OriginalDate {{ get; private set; }}
41+
public string Date {{ get; private set; }}
42+
43+
public ReleaseDateAttribute(string originalDate, string date)
44+
{{
45+
OriginalDate = date;
46+
Date = date;
47+
}}
48+
}}
3549
}}
3650
namespace {4}
3751
{{
@@ -46,7 +60,8 @@ static class GitFlowVersionInformation
4660
}}
4761
}}
4862
49-
", GetAssemblyVersion(), GetAssemblyFileVersion(), VersionAndBranch.ToLongString(), VersionAndBranch.GenerateNugetVersion(), AssemblyName, VersionAndBranch.GenerateSemVer());
63+
", GetAssemblyVersion(), GetAssemblyFileVersion(), VersionAndBranch.ToLongString(), VersionAndBranch.GenerateNugetVersion(), AssemblyName, VersionAndBranch.GenerateSemVer(),
64+
VersionAndBranch.ReleaseDate.OriginalDate.UtcDateTime.ToString("yyyy-MM-dd"), VersionAndBranch.ReleaseDate.Date.UtcDateTime.ToString("yyyy-MM-dd"));
5065

5166
return assemblyInfo;
5267
}

GitFlowVersionTask/GetVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public override bool Execute()
6969
{
7070
try
7171
{
72-
VersionAndBranch versionAndBranch;
72+
VersionAndBranchAndDate versionAndBranch;
7373
if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch))
7474
{
7575
MajorMinorPatch = string.Format("{0}.{1}.{2}", versionAndBranch.Version.Major, versionAndBranch.Version.Minor, versionAndBranch.Version.Patch);

GitFlowVersionTask/UpdateAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void InnerExecute()
6464

6565
InvalidFileChecker.CheckForInvalidFiles(CompileFiles, ProjectFile);
6666

67-
VersionAndBranch versionAndBranch;
67+
VersionAndBranchAndDate versionAndBranch;
6868
if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch))
6969
{
7070
return;
@@ -75,7 +75,7 @@ public void InnerExecute()
7575
}
7676

7777

78-
void CreateTempAssemblyInfo(VersionAndBranch versionAndBranch)
78+
void CreateTempAssemblyInfo(VersionAndBranchAndDate versionAndBranch)
7979
{
8080
var assemblyInfoBuilder = new AssemblyInfoBuilder
8181
{

GitFlowVersionTask/VersionAndBranchFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public static class VersionAndBranchFinder
66
{
77

8-
public static bool TryGetVersion(string directory, out VersionAndBranch versionAndBranch)
8+
public static bool TryGetVersion(string directory, out VersionAndBranchAndDate versionAndBranch)
99
{
1010
var gitDirectory = GitDirFinder.TreeWalkForGitDir(directory);
1111

GitFlowVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override bool Execute()
4747

4848
public void InnerExecute()
4949
{
50-
VersionAndBranch versionAndBranch;
50+
VersionAndBranchAndDate versionAndBranch;
5151
if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch))
5252
{
5353
return;

Tests/AssemblyInfoBuilderTests.VerifyCreatedCode.approved.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using System.Reflection;
66
[assembly: AssemblyFileVersion("1.2.3")]
77
[assembly: AssemblyInformationalVersion("1.2.3-unstable.feature-a682956d Branch:'feature1' Sha:'a682956dc1a2752aa24597a0f5cd939f93614509'")]
88
[assembly: MyAssembly.NugetVersion("1.2.3-Feature-feature1-a682956dc1a2752aa24597a0f5cd939f93614509")]
9+
[assembly: MyAssembly.ReleaseDate("2014-03-01", "2014-03-06")]
910

1011
namespace MyAssembly
1112
{
@@ -19,6 +20,19 @@ namespace MyAssembly
1920

2021
public string Version{get;set;}
2122
}
23+
24+
[System.Runtime.CompilerServices.CompilerGenerated]
25+
class ReleaseDateAttribute : System.Attribute
26+
{
27+
public string OriginalDate { get; private set; }
28+
public string Date { get; private set; }
29+
30+
public ReleaseDateAttribute(string originalDate, string date)
31+
{
32+
OriginalDate = date;
33+
Date = date;
34+
}
35+
}
2236
}
2337
namespace MyAssembly
2438
{

Tests/AssemblyInfoBuilderTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AssemblyInfoBuilderTests
1515
[Test]
1616
public void VerifyCreatedCode()
1717
{
18-
var semanticVersion = new VersionAndBranch
18+
var semanticVersion = new VersionAndBranchAndDate
1919
{
2020
BranchType = BranchType.Feature,
2121
BranchName = "feature1",
@@ -27,6 +27,11 @@ public void VerifyCreatedCode()
2727
Patch = 3,
2828
Tag = "unstable4",
2929
Suffix = "a682956d",
30+
},
31+
ReleaseDate = new ReleaseDate
32+
{
33+
OriginalDate = DateTimeOffset.Parse("2014-03-01 00:00:01Z"),
34+
Date = DateTimeOffset.Parse("2014-03-06 23:59:59Z"),
3035
}
3136
};
3237
var assemblyInfoBuilder = new AssemblyInfoBuilder

0 commit comments

Comments
 (0)