Skip to content

Commit

Permalink
Merge pull request #1103 from DmitryZhelnin/fix827
Browse files Browse the repository at this point in the history
fix GitCommitDate being author date rather than commit date
  • Loading branch information
AArnott authored Oct 9, 2024
2 parents c384a4c + c0cec9d commit 41ee52c
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/NerdBank.GitVersioning/DisabledGit/DisabledGitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public DisabledGitContext(string workingTreePath)

public override DateTimeOffset? GitCommitDate => null;

public override DateTimeOffset? GitCommitAuthorDate => null;

public override string? HeadCanonicalName => null;

public override IReadOnlyCollection<string>? HeadTags => null;
Expand Down
5 changes: 5 additions & 0 deletions src/NerdBank.GitVersioning/GitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public string RepoRelativeProjectDirectory
/// </summary>
public abstract DateTimeOffset? GitCommitDate { get; }

/// <summary>
/// Gets the date that the commit identified by <see cref="GitCommitId"/> was authored.
/// </summary>
public abstract DateTimeOffset? GitCommitAuthorDate { get; }

/// <summary>
/// Gets the canonical name for HEAD's position (e.g. <c>refs/heads/main</c>).
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion src/NerdBank.GitVersioning/LibGit2/LibGit2Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ internal LibGit2Context(string workingTreeDirectory, string dotGitPath, string?
public override bool IsHead => this.Repository.Head?.Tip?.Equals(this.Commit) ?? false;

/// <inheritdoc />
public override DateTimeOffset? GitCommitDate => this.Commit?.Author.When;
public override DateTimeOffset? GitCommitDate => this.Commit?.Committer.When;

/// <inheritdoc />
public override DateTimeOffset? GitCommitAuthorDate => this.Commit?.Author.When;

/// <inheritdoc />
public override string HeadCanonicalName => this.Repository.Head.CanonicalName;
Expand Down
5 changes: 4 additions & 1 deletion src/NerdBank.GitVersioning/Managed/ManagedGitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ internal ManagedGitContext(string workingDirectory, string dotGitPath, string? c
public override bool IsHead => this.Repository.GetHeadCommit().Equals(this.Commit);

/// <inheritdoc />
public override DateTimeOffset? GitCommitDate => this.Commit is { } commit ? (commit.Author?.Date ?? this.Repository.GetCommit(commit.Sha, readAuthor: true).Author?.Date) : null;
public override DateTimeOffset? GitCommitDate => this.Commit is { } commit ? (commit.Committer?.Date ?? this.Repository.GetCommit(commit.Sha, readAuthor: true).Committer?.Date) : null;

/// <inheritdoc />
public override DateTimeOffset? GitCommitAuthorDate => this.Commit is { } commit ? (commit.Author?.Date ?? this.Repository.GetCommit(commit.Sha, readAuthor: true).Author?.Date) : null;

/// <inheritdoc />
public override string HeadCanonicalName => this.Repository.GetHeadAsReferenceOrSha().ToString() ?? throw new InvalidOperationException("Unable to determine the HEAD position.");
Expand Down
5 changes: 5 additions & 0 deletions src/NerdBank.GitVersioning/ManagedGit/GitCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public struct GitCommit : IEquatable<GitCommit>
/// </summary>
public GitSignature? Author { get; set; }

/// <summary>
/// Gets or sets the committer of this commit.
/// </summary>
public GitSignature? Committer { get; set; }

public static bool operator ==(GitCommit left, GitCommit right)
{
return Equals(left, right);
Expand Down
43 changes: 34 additions & 9 deletions src/NerdBank.GitVersioning/ManagedGit/GitCommitReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class GitCommitReader
private static readonly byte[] TreeStart = GitRepository.Encoding.GetBytes("tree ");
private static readonly byte[] ParentStart = GitRepository.Encoding.GetBytes("parent ");
private static readonly byte[] AuthorStart = GitRepository.Encoding.GetBytes("author ");
private static readonly byte[] CommitterStart = GitRepository.Encoding.GetBytes("committer ");

/// <summary>
/// Reads a <see cref="GitCommit"/> object from a <see cref="Stream"/>.
Expand All @@ -30,7 +31,7 @@ public static class GitCommitReader
/// The <see cref="GitObjectId"/> of the commit.
/// </param>
/// <param name="readAuthor">
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> field.
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> and <see cref="GitCommit.Committer"/> fields.
/// </param>
/// <returns>
/// The <see cref="GitCommit"/>.
Expand Down Expand Up @@ -67,7 +68,7 @@ public static GitCommit Read(Stream stream, GitObjectId sha, bool readAuthor = f
/// The <see cref="GitObjectId"/> of the commit.
/// </param>
/// <param name="readAuthor">
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> field.
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> and <see cref="GitCommit.Committer"/> fields.
/// </param>
/// <returns>
/// The <see cref="GitCommit"/>.
Expand Down Expand Up @@ -102,11 +103,22 @@ public static GitCommit Read(ReadOnlySpan<byte> commit, GitObjectId sha, bool re
buffer = buffer.Slice(ParentLineLength);
}

GitSignature signature = default;
GitSignature author = default;
GitSignature committer = default;

if (readAuthor && !TryReadAuthor(buffer, out signature))
if (readAuthor)
{
throw new GitException();
if (!TryReadAuthor(buffer, out author, out int lineLength))
{
throw new GitException();
}

buffer = buffer.Slice(lineLength);

if (!TryReadCommitter(buffer, out committer))
{
throw new GitException();
}
}

return new GitCommit()
Expand All @@ -116,7 +128,8 @@ public static GitCommit Read(ReadOnlySpan<byte> commit, GitObjectId sha, bool re
SecondParent = secondParent,
AdditionalParents = additionalParents,
Tree = tree,
Author = readAuthor ? signature : null,
Author = readAuthor ? author : null,
Committer = readAuthor ? committer : null,
};
}

Expand Down Expand Up @@ -153,16 +166,27 @@ private static bool TryReadParent(ReadOnlySpan<byte> line, out GitObjectId paren
return true;
}

private static bool TryReadAuthor(ReadOnlySpan<byte> line, out GitSignature signature)
private static bool TryReadAuthor(ReadOnlySpan<byte> line, out GitSignature signature, out int lineLength)
{
return TryReadSignature(line, AuthorStart, out signature, out lineLength);
}

private static bool TryReadCommitter(ReadOnlySpan<byte> line, out GitSignature signature)
{
return TryReadSignature(line, CommitterStart, out signature, out _);
}

private static bool TryReadSignature(ReadOnlySpan<byte> line, byte[] signatureStart, out GitSignature signature, out int lineLength)
{
signature = default;
lineLength = default;

if (!line.Slice(0, AuthorStart.Length).SequenceEqual(AuthorStart))
if (!line.Slice(0, signatureStart.Length).SequenceEqual(signatureStart))
{
return false;
}

line = line.Slice(AuthorStart.Length);
line = line.Slice(signatureStart.Length);

int emailStart = line.IndexOf((byte)'<');
int emailEnd = line.IndexOf((byte)'>');
Expand All @@ -179,6 +203,7 @@ private static bool TryReadAuthor(ReadOnlySpan<byte> line, out GitSignature sign
long ticks = long.Parse(GitRepository.GetString(time.Slice(0, offsetStart)));
signature.Date = DateTimeOffset.FromUnixTimeSeconds(ticks);

lineLength = signatureStart.Length + lineEnd + 1;
return true;
}
}
4 changes: 2 additions & 2 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public GitObjectId GetHeadCommitSha()
/// Gets the current HEAD commit, if available.
/// </summary>
/// <param name="readAuthor">
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> field.
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> and <see cref="GitCommit.Committer"/> fields.
/// </param>
/// <returns>
/// The current HEAD commit, or <see langword="null"/> if not available.
Expand All @@ -320,7 +320,7 @@ public GitObjectId GetHeadCommitSha()
/// The Git object Id of the commit.
/// </param>
/// <param name="readAuthor">
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> field.
/// A value indicating whether to populate the <see cref="GitCommit.Author"/> and <see cref="GitCommit.Committer"/> fields.
/// </param>
/// <returns>
/// The requested commit.
Expand Down
2 changes: 2 additions & 0 deletions src/NerdBank.GitVersioning/NoGit/NoGitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public NoGitContext(string workingTreePath)
/// <inheritdoc/>
public override DateTimeOffset? GitCommitDate => null;

public override DateTimeOffset? GitCommitAuthorDate => null;

/// <inheritdoc/>
public override string? HeadCanonicalName => null;

Expand Down
5 changes: 5 additions & 0 deletions src/NerdBank.GitVersioning/VersionOracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ public IEnumerable<string> BuildMetadataWithCommitId
/// </summary>
public DateTimeOffset? GitCommitDate => this.context.GitCommitDate;

/// <summary>
/// Gets the Git revision control commit author date for HEAD (the current source code version).
/// </summary>
public DateTimeOffset? GitCommitAuthorDate => this.context.GitCommitAuthorDate;

/// <summary>
/// Gets or sets the number of commits in the longest single path between
/// the specified commit and the most distant ancestor (inclusive)
Expand Down
7 changes: 7 additions & 0 deletions src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ the code is regenerated.

public string GitCommitDateTicks { get; set; }

public string GitCommitAuthorDateTicks { get; set; }

public bool EmitThisAssemblyClass { get; set; } = true;

/// <summary>
Expand Down Expand Up @@ -440,6 +442,11 @@ private void GenerateAssemblyAttributes()
fields.Add("GitCommitDate", (new DateTime(gitCommitDateTicks, DateTimeKind.Utc), true));
}

if (long.TryParse(this.GitCommitAuthorDateTicks, out long gitCommitAuthorDateTicks))
{
fields.Add("GitCommitAuthorDate", (new DateTime(gitCommitAuthorDateTicks, DateTimeKind.Utc), true));
}

if (this.AdditionalThisAssemblyFields is object)
{
foreach (ITaskItem item in this.AdditionalThisAssemblyFields)
Expand Down
9 changes: 9 additions & 0 deletions src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ public GetBuildVersion()
[Output]
public string GitCommitDateTicks { get; private set; }

/// <summary>
/// Gets the Git revision control commit author date for HEAD (the current source code version), expressed as the number of 100-nanosecond
/// intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.
/// </summary>
[Output]
public string GitCommitAuthorDateTicks { get; private set; }

/// <summary>
/// Gets the number of commits in the longest single path between
/// the specified commit and the most distant ancestor (inclusive)
Expand Down Expand Up @@ -266,6 +273,7 @@ protected override bool ExecuteInner()
this.GitCommitId = oracle.GitCommitId;
this.GitCommitIdShort = oracle.GitCommitIdShort;
this.GitCommitDateTicks = oracle.GitCommitDate is not null ? oracle.GitCommitDate.Value.UtcTicks.ToString(CultureInfo.InvariantCulture) : null;
this.GitCommitAuthorDateTicks = oracle.GitCommitAuthorDate is not null ? oracle.GitCommitAuthorDate.Value.UtcTicks.ToString(CultureInfo.InvariantCulture) : null;
this.GitVersionHeight = oracle.VersionHeight;
this.BuildMetadataFragment = oracle.BuildMetadataFragment;
this.CloudBuildNumber = oracle.CloudBuildNumberEnabled ? oracle.CloudBuildNumber : null;
Expand Down Expand Up @@ -314,6 +322,7 @@ protected override bool ExecuteInner()
{ "GitCommitId", this.GitCommitId },
{ "GitCommitIdShort", this.GitCommitIdShort },
{ "GitCommitDateTicks", this.GitCommitDateTicks },
{ "GitCommitAuthorDateTicks", this.GitCommitAuthorDateTicks },
{ "GitVersionHeight", this.GitVersionHeight.ToString(CultureInfo.InvariantCulture) },
{ "BuildNumber", this.BuildNumber.ToString(CultureInfo.InvariantCulture) },
{ "BuildVersionNumberComponent", this.BuildNumber.ToString(CultureInfo.InvariantCulture) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
PrereleaseVersion="$(PrereleaseVersion)"
GitCommitId="$(GitCommitId)"
GitCommitDateTicks="$(GitCommitDateTicks)"
GitCommitAuthorDateTicks="$(GitCommitAuthorDateTicks)"
EmitNonVersionCustomAttributes="$(NBGV_EmitNonVersionCustomAttributes)"
EmitThisAssemblyClass="$(NBGV_EmitThisAssemblyClass)"
AdditionalThisAssemblyFields="@(AdditionalThisAssemblyFields)"
Expand Down
5 changes: 4 additions & 1 deletion test/Nerdbank.GitVersioning.Tests/BuildIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ protected void AssertStandardProperties(VersionOptions versionOptions, BuildResu
Assert.Equal(idAsVersion.Build.ToString(), buildResult.BuildVersionNumberComponent);
Assert.Equal($"{idAsVersion.Major}.{idAsVersion.Minor}.{idAsVersion.Build}", buildResult.BuildVersionSimple);
Assert.Equal(this.LibGit2Repository.Head.Tip.Id.Sha, buildResult.GitCommitId);
Assert.Equal(this.LibGit2Repository.Head.Tip.Author.When.UtcTicks.ToString(CultureInfo.InvariantCulture), buildResult.GitCommitDateTicks);
Assert.Equal(this.LibGit2Repository.Head.Tip.Committer.When.UtcTicks.ToString(CultureInfo.InvariantCulture), buildResult.GitCommitDateTicks);
Assert.Equal(this.LibGit2Repository.Head.Tip.Author.When.UtcTicks.ToString(CultureInfo.InvariantCulture), buildResult.GitCommitAuthorDateTicks);
Assert.Equal(commitIdShort, buildResult.GitCommitIdShort);
Assert.Equal(versionHeight.ToString(), buildResult.GitVersionHeight);
Assert.Equal($"{version.Major}.{version.Minor}", buildResult.MajorMinorVersion);
Expand Down Expand Up @@ -558,6 +559,8 @@ internal BuildResults(BuildResult buildResult, IReadOnlyList<BuildEventArgs> log

public string GitCommitDateTicks => this.BuildResult.ProjectStateAfterBuild.GetPropertyValue("GitCommitDateTicks");

public string GitCommitAuthorDateTicks => this.BuildResult.ProjectStateAfterBuild.GetPropertyValue("GitCommitAuthorDateTicks");

public string GitVersionHeight => this.BuildResult.ProjectStateAfterBuild.GetPropertyValue("GitVersionHeight");

public string SemVerBuildSuffix => this.BuildResult.ProjectStateAfterBuild.GetPropertyValue("SemVerBuildSuffix");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void ReadTest()
Assert.Equal(new DateTimeOffset(2020, 10, 6, 13, 40, 09, TimeSpan.FromHours(-6)), author.Date);
Assert.Equal("[email protected]", author.Email);

// Committer and commit message are not read
GitSignature committer = commit.Committer.Value;

Assert.Equal("Andrew Arnott", committer.Name);
Assert.Equal(new DateTimeOffset(2020, 10, 6, 14, 40, 09, TimeSpan.FromHours(-6)), committer.Date);
Assert.Equal("[email protected]", committer.Email);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tree f914b48023c7c804a4f3be780d451f31aef74ac1
parent 06cc627f28736c0d13506b0414126580fe37c6f3
author Andrew Arnott <[email protected]> 1602013209 -0600
committer Andrew Arnott <[email protected]> 1602013209 -0600
committer Andrew Arnott <[email protected]> 1602016809 -0600

Set version to '3.4-alpha'
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ tree f914b48023c7c804a4f3be780d451f31aef74ac1
parent 4497b0eaaa89abf0e6d70961ad5f04fd3a49cbc6
parent 0989e8fe0cd0e0900173b26decdfb24bc0cc8232
author Andrew Arnott <[email protected]> 1602013209 -0600
committer Andrew Arnott <[email protected]> 1602013209 -0600
committer Andrew Arnott <[email protected]> 1602016809 -0600

Merge branch 'v3.3'

0 comments on commit 41ee52c

Please sign in to comment.