Skip to content

Cover ReleaseDate underlying changes with tests #98

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
May 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ protected SemanticVersion FindVersion(
PreReleaseTag = "unstable0",
BuildMetaData = new SemanticVersionBuildMetaData(
numberOfCommitsOnBranchSinceCommit,
context.CurrentBranch.Name, sha,
releaseDate.OriginalDate, releaseDate.Date)
context.CurrentBranch.Name, releaseDate)
};

return semanticVersion;
Expand Down
3 changes: 1 addition & 2 deletions GitVersionCore/GitFlow/BranchFinders/DevelopVersionFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public SemanticVersion FindVersion(GitVersionContext context)
Minor = versionFromMaster.Minor + 1,
Patch = 0,
PreReleaseTag = "unstable" + numberOfCommitsSinceRelease,
BuildMetaData = new SemanticVersionBuildMetaData(numberOfCommitsSinceRelease, context.CurrentBranch.Name, tip.Sha,
releaseDate.OriginalDate, releaseDate.Date),
BuildMetaData = new SemanticVersionBuildMetaData(numberOfCommitsSinceRelease, context.CurrentBranch.Name, releaseDate),
};
return semanticVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SemanticVersion BuildVersion(IRepository repository, Commit tip, int major, int
Major = major,
Minor = minor,
Patch = patch,
BuildMetaData = new SemanticVersionBuildMetaData(null, "master", tip.Sha, releaseDate.OriginalDate, releaseDate.Date)
BuildMetaData = new SemanticVersionBuildMetaData(null, "master", releaseDate)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ protected SemanticVersion FindVersion(
Patch = version.Patch,
PreReleaseTag = version.PreReleaseTag,
BuildMetaData = new SemanticVersionBuildMetaData(
nbHotfixCommits, context.CurrentBranch.Name, sha,
releaseDate.OriginalDate, releaseDate.Date)
nbHotfixCommits, context.CurrentBranch.Name, releaseDate)
};

if (tagVersion != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SemanticVersion BuildVersion(IRepository repository, Commit tip, int major, int
Major = major,
Minor = minor,
Patch = patch,
BuildMetaData = new SemanticVersionBuildMetaData(null, "support", tip.Sha, releaseDate.OriginalDate, releaseDate.Date)
BuildMetaData = new SemanticVersionBuildMetaData(null, "support", releaseDate)
};
}
}
Expand Down
3 changes: 1 addition & 2 deletions GitVersionCore/GitHubFlow/BuildNumberCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public SemanticVersion GetBuildNumber(GitVersionContext context)

// TODO Need a way of setting this in a cross cutting way
semanticVersion.BuildMetaData = new SemanticVersionBuildMetaData(commitsSinceLastRelease,
context.CurrentBranch.Name, sha,
releaseDate.OriginalDate, releaseDate.Date);
context.CurrentBranch.Name, releaseDate);
if (context.CurrentBranch.IsPullRequest())
{
EnsurePullBranchShareACommonAncestorWithMaster(gitRepo, gitRepo.Head);
Expand Down
1 change: 1 addition & 0 deletions GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="GitVersionContext.cs" />
<Compile Include="GitVersionFinder.cs" />
<Compile Include="Helpers\DeleteHelper.cs" />
<Compile Include="LambdaEqualityHelper.cs" />
<Compile Include="LibGitExtensions.cs" />
<Compile Include="Logger.cs" />
<Compile Include="MissingBranchException.cs" />
Expand Down
62 changes: 62 additions & 0 deletions GitVersionCore/LambdaEqualityHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace GitVersion
{
using System;

// From the LibGit2Sharp project (ligit2sharp.com)
// MIT License - Copyright (c) 2011-2014 LibGit2Sharp contributors
// see https://github.com/libgit2/libgit2sharp/blob/7af5c60f22f9bd6064204f84467cfa62bedd1147/LibGit2Sharp/Core/LambdaEqualityHelper.cs

internal class LambdaEqualityHelper<T>
{
private readonly Func<T, object>[] equalityContributorAccessors;

public LambdaEqualityHelper(params Func<T, object>[] equalityContributorAccessors)
{
this.equalityContributorAccessors = equalityContributorAccessors;
}

public bool Equals(T instance, T other)
{
if (ReferenceEquals(null, instance) || ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(instance, other))
{
return true;
}

if (instance.GetType() != other.GetType())
{
return false;
}

foreach (Func<T, object> accessor in equalityContributorAccessors)
{
if (!Equals(accessor(instance), accessor(other)))
{
return false;
}
}

return true;
}

public int GetHashCode(T instance)
{
int hashCode = GetType().GetHashCode();

unchecked
{
foreach (Func<T, object> accessor in equalityContributorAccessors)
{
object item = accessor(instance);
hashCode = (hashCode * 397) ^ (item != null ? item.GetHashCode() : 0);
}
}

return hashCode;
}
}
}
31 changes: 30 additions & 1 deletion GitVersionCore/ReleaseDate.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
using System;
using GitVersion;

public class ReleaseDate
public class ReleaseDate : IEquatable<ReleaseDate>
{
public DateTimeOffset OriginalDate;
public string OriginalCommitSha;
public DateTimeOffset Date;
public string CommitSha;

private static readonly LambdaEqualityHelper<ReleaseDate> equalityHelper =
new LambdaEqualityHelper<ReleaseDate>(x => x.OriginalDate, x => x.OriginalCommitSha, x => x.Date, x => x.CommitSha);

public override bool Equals(object obj)
{
return Equals(obj as ReleaseDate);
}

public bool Equals(ReleaseDate other)
{
return equalityHelper.Equals(this, other);
}

public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}

public static bool operator ==(ReleaseDate left, ReleaseDate right)
{
return Equals(left, right);
}

public static bool operator !=(ReleaseDate left, ReleaseDate right)
{
return !Equals(left, right);
}
}
46 changes: 13 additions & 33 deletions GitVersionCore/SemanticVersionBuildMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,47 @@ namespace GitVersion
using System;
using System.Text.RegularExpressions;

public class SemanticVersionBuildMetaData : IFormattable
public class SemanticVersionBuildMetaData : IFormattable, IEquatable<SemanticVersionBuildMetaData>
{
static readonly Regex ParseRegex = new Regex(
@"(?<BuildNumber>\d+)?(\.?Branch(Name)?\.(?<BranchName>[^\.]+))?(\.?Sha?\.(?<Sha>[^\.]+))?(?<Other>.*)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);

private static readonly LambdaEqualityHelper<SemanticVersionBuildMetaData> equalityHelper =
new LambdaEqualityHelper<SemanticVersionBuildMetaData>(x => x.CommitsSinceTag, x => x.Branch, x => x.Sha, x => x.ReleaseDate);

public int? CommitsSinceTag;
public string Branch;
public ReleaseDate ReleaseDate;
public string Sha;
public DateTimeOffset? OriginalReleaseDate;
public DateTimeOffset? ReleaseDate;
public string OtherMetaData;

public SemanticVersionBuildMetaData()
{
}

public SemanticVersionBuildMetaData(
int? commitsSinceTag, string branch, string sha,
DateTimeOffset? originalReleaseDate, DateTimeOffset? releaseDate)
int? commitsSinceTag, string branch, ReleaseDate releaseDate)
{
ReleaseDate = releaseDate;
OriginalReleaseDate = originalReleaseDate;
Sha = sha;
Sha = releaseDate.CommitSha;
CommitsSinceTag = commitsSinceTag;
Branch = branch;
}

protected bool Equals(SemanticVersionBuildMetaData other)
public override bool Equals(object obj)
{
return CommitsSinceTag == other.CommitsSinceTag && string.Equals(Branch, other.Branch) && string.Equals(Sha, other.Sha) && OriginalReleaseDate.Equals(other.OriginalReleaseDate) && ReleaseDate.Equals(other.ReleaseDate);
return Equals(obj as SemanticVersionBuildMetaData);
}

public override bool Equals(object obj)
public bool Equals(SemanticVersionBuildMetaData other)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((SemanticVersionBuildMetaData) obj);
return equalityHelper.Equals(this, other);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = CommitsSinceTag.GetHashCode();
hashCode = (hashCode*397) ^ (Branch != null ? Branch.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (Sha != null ? Sha.GetHashCode() : 0);
hashCode = (hashCode*397) ^ OriginalReleaseDate.GetHashCode();
hashCode = (hashCode*397) ^ ReleaseDate.GetHashCode();
return hashCode;
}
return equalityHelper.GetHashCode(this);
}

public override string ToString()
Expand Down Expand Up @@ -151,4 +131,4 @@ public static SemanticVersionBuildMetaData Parse(string buildMetaData)
return semanticVersionBuildMetaData;
}
}
}
}
33 changes: 15 additions & 18 deletions GitVersionCore/SemanticVersionPreReleaseTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ namespace GitVersion
using System;
using System.Text.RegularExpressions;

public class SemanticVersionPreReleaseTag : IFormattable, IComparable<SemanticVersionPreReleaseTag>
public class SemanticVersionPreReleaseTag :
IFormattable, IComparable<SemanticVersionPreReleaseTag>, IEquatable<SemanticVersionPreReleaseTag>
{
public string Name;
public int? Number;

private static readonly LambdaEqualityHelper<SemanticVersionPreReleaseTag> equalityHelper =
new LambdaEqualityHelper<SemanticVersionPreReleaseTag>(x => x.Name, x => x.Number);

public SemanticVersionPreReleaseTag()
{
}
Expand All @@ -18,26 +22,19 @@ public SemanticVersionPreReleaseTag(string name, int? number)
Number = number;
}

protected bool Equals(SemanticVersionPreReleaseTag other)
public override bool Equals(object obj)
{
return string.Equals(Name, other.Name) && Number == other.Number;
return Equals(obj as SemanticVersionPreReleaseTag);
}

public override bool Equals(object obj)
public bool Equals(SemanticVersionPreReleaseTag other)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((SemanticVersionPreReleaseTag) obj);
return equalityHelper.Equals(this, other);
}

public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}

public static bool operator ==(SemanticVersionPreReleaseTag left, SemanticVersionPreReleaseTag right)
Expand Down Expand Up @@ -146,4 +143,4 @@ public bool HasTag()
return !string.IsNullOrEmpty(Name);
}
}
}
}
4 changes: 2 additions & 2 deletions GitVersionTask/AssemblyInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ static class GitVersionInformation


", GetAssemblyVersion(), GetAssemblyFileVersion(), SemanticVersion.ToString("i"),
SemanticVersion.BuildMetaData.OriginalReleaseDate.Value.UtcDateTime.ToString("yyyy-MM-dd"),
SemanticVersion.BuildMetaData.ReleaseDate.Value.UtcDateTime.ToString("yyyy-MM-dd"),
SemanticVersion.BuildMetaData.ReleaseDate.OriginalDate.UtcDateTime.ToString("yyyy-MM-dd"),
SemanticVersion.BuildMetaData.ReleaseDate.Date.UtcDateTime.ToString("yyyy-MM-dd"),
GenerateVariableMembers());

return assemblyInfo;
Expand Down
9 changes: 6 additions & 3 deletions Tests/AssemblyInfoBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public void VerifyCreatedCode()
PreReleaseTag = "unstable4",
BuildMetaData = new SemanticVersionBuildMetaData(5,
"feature1",
"a682956dc1a2752aa24597a0f5cd939f93614509",
DateTimeOffset.Parse("2014-03-01 00:00:01Z"),
DateTimeOffset.Parse("2014-03-06 23:59:59Z")),
new ReleaseDate
{
CommitSha = "a682956dc1a2752aa24597a0f5cd939f93614509",
OriginalDate = DateTimeOffset.Parse("2014-03-01 00:00:01Z"),
Date = DateTimeOffset.Parse("2014-03-06 23:59:59Z")
})
};
var assemblyInfoBuilder = new AssemblyInfoBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
"BuildMetaData": {
"CommitsSinceTag": 1,
"Branch": "featureWithOneCommit",
"ReleaseDate": {
"OriginalDate": "<date replaced>",
"OriginalCommitSha": "000000000000000000000000000000000000000",
"Date": "<date replaced>",
"CommitSha": "000000000000000000000000000000000000000"
},
"Sha": "000000000000000000000000000000000000000",
"OriginalReleaseDate": "<date replaced>",
"ReleaseDate": "<date replaced>",
"OtherMetaData": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
"BuildMetaData": {
"CommitsSinceTag": 2,
"Branch": "featureWithOneCommit",
"ReleaseDate": {
"OriginalDate": "<date replaced>",
"OriginalCommitSha": "000000000000000000000000000000000000000",
"Date": "<date replaced>",
"CommitSha": "000000000000000000000000000000000000000"
},
"Sha": "000000000000000000000000000000000000000",
"OriginalReleaseDate": "<date replaced>",
"ReleaseDate": "<date replaced>",
"OtherMetaData": null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
"BuildMetaData": {
"CommitsSinceTag": 2,
"Branch": "featureWithOneCommit",
"ReleaseDate": {
"OriginalDate": "<date replaced>",
"OriginalCommitSha": "000000000000000000000000000000000000000",
"Date": "<date replaced>",
"CommitSha": "000000000000000000000000000000000000000"
},
"Sha": "000000000000000000000000000000000000000",
"OriginalReleaseDate": "<date replaced>",
"ReleaseDate": "<date replaced>",
"OtherMetaData": null
}
}
Loading