Skip to content
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
2 changes: 1 addition & 1 deletion src/NerdBank.GitVersioning/LibGit2/LibGit2GitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static IEnumerable<Commit> GetCommitsFromVersion(LibGit2Context context,
where !IsVersionHeightMismatch(version, commitVersionOptions, commit, tracker)
select commit;

return possibleCommits;
return possibleCommits.ToList();
}

/// <summary>
Expand Down
22 changes: 14 additions & 8 deletions src/NerdBank.GitVersioning/VersionOracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ public class VersionOracle

private const bool UseLibGit2 = false;

private readonly GitContext context;

private readonly string? gitCommitId;

private readonly DateTimeOffset? gitCommitDate;

private readonly DateTimeOffset? gitCommitAuthorDate;

private readonly IReadOnlyCollection<string>? tags;

private readonly bool isWorkingTreeDirty;

private readonly string? versionGitCommitId;
Expand All @@ -46,7 +50,9 @@ public class VersionOracle
/// <param name="overrideVersionHeightOffset">An optional value to override the version height offset.</param>
public VersionOracle(GitContext context, ICloudBuild? cloudBuild = null, int? overrideVersionHeightOffset = null)
{
this.context = context;
this.gitCommitDate = context.GitCommitDate;
this.gitCommitAuthorDate = context.GitCommitAuthorDate;
this.tags = context.HeadTags?.ToArray();
string? rawGitCommitId = context.GitCommitId ?? cloudBuild?.GitCommitId;
this.gitCommitId = rawGitCommitId;
this.CommittedVersion = context.VersionFile.GetVersion();
Expand Down Expand Up @@ -116,7 +122,7 @@ public VersionOracle(GitContext context, ICloudBuild? cloudBuild = null, int? ov

// Get it from the git repository if there is a repository present and it is enabled.
string gitCommitIdShort = gitCommitIdShortAutoMinimum > 0
? this.context.GetShortUniqueCommitId(gitCommitIdShortAutoMinimum)
? context.GetShortUniqueCommitId(gitCommitIdShortAutoMinimum)
: rawGitCommitId!.Substring(0, gitCommitIdShortFixedLength);
this.GitCommitIdShort = this.isWorkingTreeDirty ? gitCommitIdShort + "-dirty" : gitCommitIdShort;
}
Expand All @@ -126,7 +132,7 @@ public VersionOracle(GitContext context, ICloudBuild? cloudBuild = null, int? ov
int gitCommitIdShortFixedLength = this.VersionOptions?.GitCommitIdShortFixedLength ?? VersionOptions.DefaultGitCommitIdShortFixedLength;
int gitCommitIdShortAutoMinimum = this.VersionOptions?.GitCommitIdShortAutoMinimum ?? 0;
this.versionGitCommitIdShort = gitCommitIdShortAutoMinimum > 0
? this.context.GetShortUniqueCommitId(this.versionGitCommitId!, gitCommitIdShortAutoMinimum)
? context.GetShortUniqueCommitId(this.versionGitCommitId!, gitCommitIdShortAutoMinimum)
: this.versionGitCommitId!.Substring(0, gitCommitIdShortFixedLength);
}

Expand Down Expand Up @@ -314,12 +320,12 @@ public string PrereleaseVersion
/// <summary>
/// Gets the Git revision control commit date for HEAD (the current source code version).
/// </summary>
public DateTimeOffset? GitCommitDate => this.context.GitCommitDate;
public DateTimeOffset? GitCommitDate => this.gitCommitDate;

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

/// <summary>
/// Gets or sets the number of commits in the longest single path between
Expand Down Expand Up @@ -353,7 +359,7 @@ public string PrereleaseVersion
/// Gets a collection of the tags that reference HEAD.
/// </summary>
[Ignore]
public IReadOnlyCollection<string>? Tags => this.context.HeadTags;
public IReadOnlyCollection<string>? Tags => this.tags;

/// <summary>
/// Gets or sets the version for this project, with up to 4 components.
Expand Down
16 changes: 16 additions & 0 deletions test/Nerdbank.GitVersioning.Tests/LibGit2GitExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ public void GetCommitsFromVersion_MatchesOnEitherEndian()
Assert.Contains(commit, LibGit2GitExtensions.GetCommitsFromVersion(this.Context, swappedEndian));
}

[Fact]
public void GetCommitsFromVersion_MaterializesBeforeContextIsDisposed()
{
this.InitializeSourceControl();
Commit commit = this.WriteVersionFile(new VersionOptions { Version = SemanticVersion.Parse("1.2") });
Version version = new VersionOracle(this.Context).Version;
IEnumerable<Commit> matchingCommits;

using (LibGit2Context context = LibGit2Context.Create(this.RepoPath))
{
matchingCommits = LibGit2GitExtensions.GetCommitsFromVersion(context, version);
}

Assert.Single(matchingCommits, candidate => candidate.Sha == commit.Sha);
}

[Fact]
public void GetIdAsVersion_Roundtrip_WithSubdirectoryVersionFiles()
{
Expand Down
16 changes: 16 additions & 0 deletions test/Nerdbank.GitVersioning.Tests/VersionOracleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public void NotRepo()
Assert.Equal(0, oracle.VersionHeight);
}

[Fact]
public void GetVersionOracle_CapturesContextDataBeforeDisposal()
{
this.WriteVersionFile();
this.InitializeSourceControl();
this.LibGit2Repository.ApplyTag("test");

VersionOracle oracle = this.GetVersionOracle();

Assert.Equal(this.LibGit2Repository.Head.Tip.Committer.When, oracle.GitCommitDate);
Assert.Equal(this.LibGit2Repository.Head.Tip.Author.When, oracle.GitCommitAuthorDate);
Assert.Equal("refs/tags/test", Assert.Single(oracle.Tags));
Assert.Contains("NBGV_GitCommitDate", oracle.CloudBuildAllVars.Keys);
Assert.Contains("NBGV_GitCommitAuthorDate", oracle.CloudBuildAllVars.Keys);
}

[Fact]
public void EmptyRepoWithCloudCommitNotInRepository()
{
Expand Down
1 change: 1 addition & 0 deletions test/Nerdbank.GitVersioning.Tests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
"parallelizeTestCollections": false,
"shadowCopy": false
}
Loading