Skip to content

[WIP] Add Major.Minor release date to compiled assembly #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions GitFlowVersion/CachedVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
class CachedVersion
{
public VersionAndBranch VersionAndBranch;
public VersionAndBranchAndDate VersionAndBranch;
public long Timestamp;
}
}
}
18 changes: 18 additions & 0 deletions GitFlowVersion/GitFlow/VersionAndBranchAndDate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace GitFlowVersion
{
public class VersionAndBranchAndDate : VersionAndBranch
{
public ReleaseDate ReleaseDate;

public VersionAndBranchAndDate() { }

public VersionAndBranchAndDate(VersionAndBranch vab, ReleaseDate rd)
{
Version = vab.Version;
BranchType = vab.BranchType;
BranchName = vab.BranchName;
Sha = vab.Sha;
ReleaseDate = rd;
}
}
}
3 changes: 3 additions & 0 deletions GitFlowVersion/GitFlowVersion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="GitFlow\BranchFinders\FeatureVersionFinder.cs" />
<Compile Include="GitFlow\BranchFinders\DevelopVersionFinder.cs" />
<Compile Include="GitFlow\BranchFinders\HotfixVersionFinder.cs" />
<Compile Include="GitFlow\VersionAndBranchAndDate.cs" />
<Compile Include="GitHubFlow\BuildNumberCalculator.cs" />
<Compile Include="GitHubFlow\VersionTaggedCommit.cs" />
<Compile Include="GitHubFlow\GitHubFlowVersionFinder.cs" />
Expand All @@ -79,6 +80,8 @@
<Compile Include="HelpWriter.cs" />
<Compile Include="GitFlow\ReleaseInformation.cs" />
<Compile Include="GitFlow\ReleaseInformationCalculator.cs" />
<Compile Include="ReleaseDate.cs" />
<Compile Include="ReleaseDateFinder.cs" />
<Compile Include="SemanticVersionTag.cs" />
<Compile Include="OutputFormatters\JsonOutputFormatter.cs" />
<Compile Include="VersionBuilders\NugetVersionBuilder.cs" />
Expand Down
14 changes: 9 additions & 5 deletions GitFlowVersion/GitVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public VersionAndBranch FindVersion(GitVersionContext context)
{
EnsureMainTopologyConstraints(context);

var hasDevelopBranch = context.Repository.FindBranch("develop") != null;

if (hasDevelopBranch)
if (ShouldGitHubFlowVersioningSchemeApply(context.Repository))
{
return new GitFlowVersionFinder().FindVersion(context);
return new GitHubFlowVersionFinder().FindVersion(context);
}
return new GitHubFlowVersionFinder().FindVersion(context);

return new GitFlowVersionFinder().FindVersion(context);
}

public static bool ShouldGitHubFlowVersioningSchemeApply(IRepository repo)
{
return repo.FindBranch("develop") == null;
}

void EnsureMainTopologyConstraints(GitVersionContext context)
Expand Down
9 changes: 9 additions & 0 deletions GitFlowVersion/ReleaseDate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public class ReleaseDate
{
public DateTimeOffset OriginalDate;
public string OriginalCommitSha;
public DateTimeOffset Date;
public string CommitSha;
}
38 changes: 38 additions & 0 deletions GitFlowVersion/ReleaseDateFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Diagnostics;
using GitFlowVersion;
using LibGit2Sharp;

public class ReleaseDateFinder
{
public static ReleaseDate Execute(IRepository repo, VersionAndBranch vab)
{
var c = repo.Lookup<Commit>(vab.Sha);
Debug.Assert(c != null);

var rd = new ReleaseDate
{
OriginalDate = c.When(),
OriginalCommitSha = c.Sha,
Date = c.When(),
CommitSha = c.Sha,
};

if (GitVersionFinder.ShouldGitHubFlowVersioningSchemeApply(repo))
{
return rd;
}

if (vab.Version.Patch == 0)
{
return rd;
}

var vp = new VersionOnMasterFinder().FindLatestStableTaggedCommitReachableFrom(repo, c);
var latestStable = repo.Lookup<Commit>(vp.CommitSha);
Debug.Assert(latestStable != null);

rd.OriginalDate = latestStable.When();
rd.OriginalCommitSha = vp.CommitSha;
return rd;
}
}
9 changes: 6 additions & 3 deletions GitFlowVersion/ShortVersionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static bool TryParseMajorMinor(string versionString, out int major, out i
{
int patch;

TryParse(versionString, out major, out minor, out patch);
if (!TryParse(versionString, out major, out minor, out patch))
{
return false;
}

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

if (strings.Length >= 3)
if (strings.Length == 3)
{
if (!int.TryParse(strings[2], out patch))
{
Expand Down
6 changes: 3 additions & 3 deletions GitFlowVersion/VersionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class VersionCache
{
static Dictionary<string, CachedVersion> versionCacheVersions = new Dictionary<string, CachedVersion>();

public static VersionAndBranch GetVersion(string gitDirectory)
public static VersionAndBranchAndDate GetVersion(string gitDirectory)
{
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
{
Expand All @@ -19,7 +19,7 @@ public static VersionAndBranch GetVersion(string gitDirectory)
var ticks = DirectoryDateFinder.GetLastDirectoryWrite(gitDirectory);
var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
CachedVersion cachedVersion;
VersionAndBranch versionAndBranch;
VersionAndBranchAndDate versionAndBranch;
if (versionCacheVersions.TryGetValue(key, out cachedVersion))
{
Logger.WriteInfo("Version read from cache.");
Expand Down Expand Up @@ -49,7 +49,7 @@ public static VersionAndBranch GetVersion(string gitDirectory)
}
}

static VersionAndBranch GetSemanticVersion(Repository repository)
static VersionAndBranchAndDate GetSemanticVersion(Repository repository)
{
var versionForRepositoryFinder = new VersionForRepositoryFinder();
return versionForRepositoryFinder.GetVersion(repository);
Expand Down
10 changes: 6 additions & 4 deletions GitFlowVersion/VersionForDirectoryFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ namespace GitFlowVersion

public class VersionForRepositoryFinder
{
public SemanticVersion SemanticVersion;

public VersionAndBranch GetVersion(Repository repository)
public VersionAndBranchAndDate GetVersion(Repository repository)
{
var gitFlowVersionFinder = new GitVersionFinder();
return gitFlowVersionFinder.FindVersion(new GitVersionContext
var vab = gitFlowVersionFinder.FindVersion(new GitVersionContext
{
CurrentBranch = repository.Head,
Repository = repository
});

var rd = ReleaseDateFinder.Execute(repository, vab);

return new VersionAndBranchAndDate(vab, rd);
}
}
}
98 changes: 94 additions & 4 deletions GitFlowVersion/VersionOnMasterFinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace GitFlowVersion
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using LibGit2Sharp;

class VersionOnMasterFinder
{
Expand All @@ -19,7 +23,8 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
{
Major = major,
Minor = minor,
Timestamp = commit.When()
Timestamp = commit.When(),
CommitSha = commit.Sha,
};
}
}
Expand All @@ -34,7 +39,8 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
{
Major = major,
Minor = minor,
Timestamp = commit.When()
Timestamp = commit.When(),
CommitSha = commit.Sha,
};
}
}
Expand All @@ -44,9 +50,93 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
{
Major = 0,
Minor = 1,
Timestamp = DateTimeOffset.MinValue
Timestamp = DateTimeOffset.MinValue,
CommitSha = null,
};
}

public VersionPoint FindLatestStableTaggedCommitReachableFrom(IRepository repo, Commit commit)
{
var masterTip = repo.FindBranch("master").Tip;
var ancestor = repo.Commits.FindCommonAncestor(masterTip, commit);

var allTags = repo.Tags.ToList();

foreach (var c in repo.Commits.QueryBy(new CommitFilter { Since = ancestor.Id }))
{
var vp = RetrieveStableVersionPointFor(allTags, c);

if (vp != null)
{
return vp;
}
}

return null;
}

static VersionPoint RetrieveStableVersionPointFor(IEnumerable<Tag> allTags, Commit c)
{
var tags = allTags
.Where(tag => tag.PeeledTarget() == c)
.Where(tag => IsStableRelease(tag.Name))
.ToList();

if (tags.Count == 0)
{
return null;
}

if (tags.Count > 1)
{
throw new ErrorException(
string.Format("Commit '{0}' bears more than one stable tag: {1}",
c.Id.ToString(7), string.Join(", ", tags.Select(t => t.Name))));
}

var stableTag = tags.Single();
var commit = RetrieveMergeCommit(stableTag);

return BuildFrom(stableTag, commit);
}

static VersionPoint BuildFrom(Tag stableTag, Commit commit)
{
int major;
int minor;

var hasParsed = ShortVersionParser.TryParseMajorMinor(stableTag.Name, out major, out minor);
Debug.Assert(hasParsed);

return new VersionPoint
{
Major = major,
Minor = minor,
CommitSha = commit.Id.Sha,
};
}

static Commit RetrieveMergeCommit(Tag stableTag)
{
var target = stableTag.PeeledTarget();
if (!(target is Commit))
{
throw new ErrorException(
string.Format("Target '{0}' of Tag '{1}' isn't a Commit.",
target.Id.ToString(7), stableTag.Name));
}

var targetCommit = (Commit) target;

return targetCommit;
}

static bool IsStableRelease(string tagName)
{
int major;
int minor;

return ShortVersionParser.TryParseMajorMinor(tagName, out major, out minor);
}
}
}
}
3 changes: 2 additions & 1 deletion GitFlowVersion/VersionPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class VersionPoint
public int Major;
public int Minor;
public DateTimeOffset Timestamp;
public string CommitSha;
}
}
}
19 changes: 17 additions & 2 deletions GitFlowVersionTask/AssemblyInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class AssemblyInfoBuilder
{

public VersionAndBranch VersionAndBranch;
public VersionAndBranchAndDate VersionAndBranch;
public bool SignAssembly;
public string AssemblyName;

Expand All @@ -19,6 +19,7 @@ public string GetAssemblyInfoText()
[assembly: AssemblyFileVersion(""{1}"")]
[assembly: AssemblyInformationalVersion(""{2}"")]
[assembly: {4}.NugetVersion(""{3}"")]
[assembly: {4}.ReleaseDate(""{6}"", ""{7}"")]

namespace {4}
{{
Expand All @@ -32,6 +33,19 @@ public NugetVersionAttribute(string version)

public string Version{{get;set;}}
}}

[System.Runtime.CompilerServices.CompilerGenerated]
class ReleaseDateAttribute : System.Attribute
{{
public string OriginalDate {{ get; private set; }}
public string Date {{ get; private set; }}

public ReleaseDateAttribute(string originalDate, string date)
{{
OriginalDate = date;
Date = date;
}}
}}
}}
namespace {4}
{{
Expand All @@ -46,7 +60,8 @@ static class GitFlowVersionInformation
}}
}}

", GetAssemblyVersion(), GetAssemblyFileVersion(), VersionAndBranch.ToLongString(), VersionAndBranch.GenerateNugetVersion(), AssemblyName, VersionAndBranch.GenerateSemVer());
", GetAssemblyVersion(), GetAssemblyFileVersion(), VersionAndBranch.ToLongString(), VersionAndBranch.GenerateNugetVersion(), AssemblyName, VersionAndBranch.GenerateSemVer(),
VersionAndBranch.ReleaseDate.OriginalDate.UtcDateTime.ToString("yyyy-MM-dd"), VersionAndBranch.ReleaseDate.Date.UtcDateTime.ToString("yyyy-MM-dd"));

return assemblyInfo;
}
Expand Down
2 changes: 1 addition & 1 deletion GitFlowVersionTask/GetVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override bool Execute()
{
try
{
VersionAndBranch versionAndBranch;
VersionAndBranchAndDate versionAndBranch;
if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch))
{
MajorMinorPatch = string.Format("{0}.{1}.{2}", versionAndBranch.Version.Major, versionAndBranch.Version.Minor, versionAndBranch.Version.Patch);
Expand Down
Loading