Skip to content

Commit 1298eb9

Browse files
committed
Reroute GitExtensions.GetIdAsVersion calls to VersionOracle
1 parent 174a5f1 commit 1298eb9

File tree

5 files changed

+65
-66
lines changed

5 files changed

+65
-66
lines changed

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public async Task GetBuildVersion_In_Git_But_Head_Lacks_VersionFile()
190190
this.WriteVersionFile("3.4");
191191
Assumes.True(repo.Index[VersionFile.JsonFileName] == null);
192192
var buildResult = await this.BuildAsync();
193-
Assert.Equal("3.4.0." + repo.Head.Tip.GetIdAsVersion().Revision, buildResult.BuildVersion);
193+
Assert.Equal("3.4.0." + GetIdAsVersion(repo, repo.Head.Tip).Revision, buildResult.BuildVersion);
194194
Assert.Equal("3.4.0+" + repo.Head.Tip.Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength), buildResult.AssemblyInformationalVersion);
195195
}
196196

@@ -215,7 +215,7 @@ public async Task GetBuildVersion_In_Git_No_VersionFile_At_All()
215215
var repo = new Repository(this.RepoPath); // do not assign Repo property to avoid commits being generated later
216216
repo.Commit("empty", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
217217
var buildResult = await this.BuildAsync();
218-
Assert.Equal("0.0.0." + repo.Head.Tip.GetIdAsVersion().Revision, buildResult.BuildVersion);
218+
Assert.Equal("0.0.0." + GetIdAsVersion(repo, repo.Head.Tip).Revision, buildResult.BuildVersion);
219219
Assert.Equal("0.0.0+" + repo.Head.Tip.Id.Sha.Substring(0, VersionOptions.DefaultGitCommitIdShortFixedLength), buildResult.AssemblyInformationalVersion);
220220
}
221221

@@ -295,7 +295,7 @@ public async Task GetBuildVersion_StableRelease()
295295
var buildResult = await this.BuildAsync();
296296
this.AssertStandardProperties(VersionOptions.FromVersion(new Version(majorMinorVersion)), buildResult);
297297

298-
Version version = this.Repo.Head.Tip.GetIdAsVersion();
298+
Version version = this.GetIdAsVersion(this.Repo.Head.Tip);
299299
Assert.Equal($"{version.Major}.{version.Minor}.{buildResult.GitVersionHeight}", buildResult.NuGetPackageVersion);
300300
}
301301

@@ -1002,9 +1002,9 @@ private static RestoreEnvironmentVariables ApplyEnvironmentVariables(IReadOnlyDi
10021002
private void AssertStandardProperties(VersionOptions versionOptions, BuildResults buildResult, string relativeProjectDirectory = null)
10031003
{
10041004
int versionHeight = this.GetVersionHeight(relativeProjectDirectory);
1005-
Version idAsVersion = this.Repo.GetIdAsVersion(relativeProjectDirectory);
1005+
Version idAsVersion = this.GetIdAsVersion(relativeProjectDirectory);
10061006
string commitIdShort = this.CommitIdShort;
1007-
Version version = this.Repo.GetIdAsVersion(relativeProjectDirectory);
1007+
Version version = this.GetIdAsVersion(relativeProjectDirectory);
10081008
Version assemblyVersion = GetExpectedAssemblyVersion(versionOptions, version);
10091009
var additionalBuildMetadata = from item in buildResult.BuildResult.ProjectStateAfterBuild.GetItems("BuildMetadata")
10101010
select item.EvaluatedInclude;

src/NerdBank.GitVersioning.Tests/GitExtensionsTests.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ public void GetIdAsVersion_ReadsMajorMinorFromVersionTxt()
521521
this.WriteVersionFile("4.8");
522522
var firstCommit = this.Repo.Commits.First();
523523

524-
Version v1 = firstCommit.GetIdAsVersion();
524+
Version v1 = this.GetIdAsVersion(firstCommit);
525525
Assert.Equal(4, v1.Major);
526526
Assert.Equal(8, v1.Minor);
527527
}
@@ -532,7 +532,7 @@ public void GetIdAsVersion_ReadsMajorMinorFromVersionTxtInSubdirectory()
532532
this.WriteVersionFile("4.8", relativeDirectory: "foo/bar");
533533
var firstCommit = this.Repo.Commits.First();
534534

535-
Version v1 = firstCommit.GetIdAsVersion("foo/bar");
535+
Version v1 = this.GetIdAsVersion(firstCommit, "foo/bar");
536536
Assert.Equal(4, v1.Major);
537537
Assert.Equal(8, v1.Minor);
538538
}
@@ -543,7 +543,7 @@ public void GetIdAsVersion_MissingVersionTxt()
543543
this.AddCommits();
544544
var firstCommit = this.Repo.Commits.First();
545545

546-
Version v1 = firstCommit.GetIdAsVersion();
546+
Version v1 = this.GetIdAsVersion(firstCommit);
547547
Assert.Equal(0, v1.Major);
548548
Assert.Equal(0, v1.Minor);
549549
}
@@ -555,7 +555,7 @@ public void GetIdAsVersion_VersionFileNeverCheckedIn_3Ints()
555555
var expectedVersion = new Version(1, 1, 0);
556556
var unstagedVersionData = VersionOptions.FromVersion(expectedVersion);
557557
string versionFilePath = VersionFile.SetVersion(this.RepoPath, unstagedVersionData);
558-
Version actualVersion = this.Repo.GetIdAsVersion();
558+
Version actualVersion = this.GetIdAsVersion();
559559
Assert.Equal(expectedVersion.Major, actualVersion.Major);
560560
Assert.Equal(expectedVersion.Minor, actualVersion.Minor);
561561
Assert.Equal(expectedVersion.Build, actualVersion.Build);
@@ -572,7 +572,7 @@ public void GetIdAsVersion_VersionFileNeverCheckedIn_2Ints()
572572
var expectedVersion = new Version(1, 1);
573573
var unstagedVersionData = VersionOptions.FromVersion(expectedVersion);
574574
string versionFilePath = VersionFile.SetVersion(this.RepoPath, unstagedVersionData);
575-
Version actualVersion = this.Repo.GetIdAsVersion();
575+
Version actualVersion = this.GetIdAsVersion();
576576
Assert.Equal(expectedVersion.Major, actualVersion.Major);
577577
Assert.Equal(expectedVersion.Minor, actualVersion.Minor);
578578
Assert.Equal(0, actualVersion.Build); // height is 0 since the change hasn't been committed.
@@ -587,7 +587,7 @@ public void GetIdAsVersion_VersionFileChangedOnDisk()
587587
this.AddCommits();
588588

589589
// Verify that we're seeing the original version.
590-
Version actualVersion = this.Repo.GetIdAsVersion();
590+
Version actualVersion = this.GetIdAsVersion();
591591
Assert.Equal(1, actualVersion.Major);
592592
Assert.Equal(2, actualVersion.Minor);
593593
Assert.Equal(2, actualVersion.Build);
@@ -597,15 +597,15 @@ public void GetIdAsVersion_VersionFileChangedOnDisk()
597597
string versionFile = VersionFile.SetVersion(this.RepoPath, new Version("1.3"));
598598

599599
// Verify that HEAD reports whatever is on disk at the time.
600-
actualVersion = this.Repo.GetIdAsVersion();
600+
actualVersion = this.GetIdAsVersion();
601601
Assert.Equal(1, actualVersion.Major);
602602
Assert.Equal(3, actualVersion.Minor);
603603
Assert.Equal(0, actualVersion.Build);
604604
Assert.Equal(this.Repo.Head.Commits.First().GetTruncatedCommitIdAsUInt16(), actualVersion.Revision);
605605

606606
// Now commit it and verify the height advances 0->1
607607
this.CommitVersionFile(versionFile, "1.3");
608-
actualVersion = this.Repo.GetIdAsVersion();
608+
actualVersion = this.GetIdAsVersion();
609609
Assert.Equal(1, actualVersion.Major);
610610
Assert.Equal(3, actualVersion.Minor);
611611
Assert.Equal(1, actualVersion.Build);
@@ -650,7 +650,7 @@ public void GetIdAsVersion_Roundtrip(string version, string assemblyVersion, int
650650
for (int i = 0; i < commits.Length; i++)
651651
{
652652
commits[i] = this.Repo.Commit($"Commit {i + 1}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
653-
versions[i] = commits[i].GetIdAsVersion(repoRelativeSubDirectory);
653+
versions[i] = this.GetIdAsVersion(commits[i], repoRelativeSubDirectory);
654654
this.Logger.WriteLine($"Commit {commits[i].Id.Sha.Substring(0, 8)} as version: {versions[i]}");
655655
}
656656

@@ -689,10 +689,10 @@ public void GetIdAsVersion_Roundtrip_UnstableOffset(int startingOffset, int offs
689689
{
690690
versionOptions.VersionHeightOffset += offsetStepChange;
691691
commits[i] = this.WriteVersionFile(versionOptions);
692-
versions[i] = commits[i].GetIdAsVersion();
692+
versions[i] = this.GetIdAsVersion(commits[i]);
693693

694694
commits[i + 1] = this.Repo.Commit($"Commit {i + 1}", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
695-
versions[i + 1] = commits[i + 1].GetIdAsVersion();
695+
versions[i + 1] = this.GetIdAsVersion(commits[i + 1]);
696696

697697
this.Logger.WriteLine($"Commit {commits[i].Id.Sha.Substring(0, 8)} as version: {versions[i]}");
698698
this.Logger.WriteLine($"Commit {commits[i + 1].Id.Sha.Substring(0, 8)} as version: {versions[i + 1]}");
@@ -731,8 +731,8 @@ public void GetIdAsVersion_Roundtrip_WithSubdirectoryVersionFiles()
731731
this.InitializeSourceControl();
732732

733733
Commit head = this.Repo.Head.Commits.First();
734-
Version rootVersionActual = head.GetIdAsVersion();
735-
Version subPathVersionActual = head.GetIdAsVersion(subPathRelative);
734+
Version rootVersionActual = this.GetIdAsVersion(head);
735+
Version subPathVersionActual = this.GetIdAsVersion(head, subPathRelative);
736736

737737
// Verify that the versions calculated took the path into account.
738738
Assert.Equal(rootVersionExpected.Version.Version.Minor, rootVersionActual?.Minor);
@@ -753,7 +753,7 @@ public void GetIdAsVersion_FitsInsideCompilerConstraints()
753753
this.WriteVersionFile("2.5");
754754
var firstCommit = this.Repo.Commits.First();
755755

756-
Version version = firstCommit.GetIdAsVersion();
756+
Version version = this.GetIdAsVersion(firstCommit);
757757
this.Logger.WriteLine(version.ToString());
758758

759759
// The C# compiler produces a build warning and truncates the version number if it exceeds 0xfffe,
@@ -772,12 +772,12 @@ public void GetIdAsVersion_MigrationFromVersionTxtToJson()
772772
var jsonCommit = this.WriteVersionFile("4.8");
773773
Assert.True(File.Exists(Path.Combine(this.RepoPath, "version.json")));
774774

775-
Version v1 = txtCommit.GetIdAsVersion();
775+
Version v1 = this.GetIdAsVersion(txtCommit);
776776
Assert.Equal(4, v1.Major);
777777
Assert.Equal(8, v1.Minor);
778778
Assert.Equal(1, v1.Build);
779779

780-
Version v2 = jsonCommit.GetIdAsVersion();
780+
Version v2 = this.GetIdAsVersion(jsonCommit);
781781
Assert.Equal(4, v2.Major);
782782
Assert.Equal(8, v2.Minor);
783783
Assert.Equal(2, v2.Build);
@@ -793,7 +793,7 @@ public void TestBiggerRepo()
793793
{
794794
foreach (var commit in this.Repo.Head.Commits)
795795
{
796-
var version = commit.GetIdAsVersion("src");
796+
var version = this.GetIdAsVersion(commit, "src");
797797
this.Logger.WriteLine($"commit {commit.Id} got version {version}");
798798
var backAgain = this.Repo.GetCommitFromVersion(version, "src");
799799
Assert.Equal(commit, backAgain);
@@ -820,7 +820,7 @@ private void VerifyCommitsWithVersion(Commit[] commits)
820820

821821
for (int i = 0; i < commits.Length; i++)
822822
{
823-
Version encodedVersion = commits[i].GetIdAsVersion();
823+
Version encodedVersion = this.GetIdAsVersion(commits[i]);
824824
Assert.Equal(i + 1, encodedVersion.Build);
825825
Assert.Equal(commits[i], this.Repo.GetCommitFromVersion(encodedVersion));
826826
}

src/NerdBank.GitVersioning.Tests/RepoTestBase.Helpers.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,44 @@ protected static int GetVersionHeight(Repository repository, string repoRelative
5151
VersionOracle oracle = new VersionOracle(repoRelativeProjectDirectory == null ? repository.Info.WorkingDirectory : Path.Combine(repository.Info.WorkingDirectory, repoRelativeProjectDirectory), repository, null);
5252
return oracle.VersionHeight;
5353
}
54+
55+
/// <summary>
56+
/// Encodes HEAD (or a modified working copy) from history in a <see cref="Version"/>
57+
/// so that the original commit can be found later.
58+
/// </summary>
59+
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
60+
/// <returns>
61+
/// A version whose <see cref="Version.Build"/> and
62+
/// <see cref="Version.Revision"/> components are calculated based on the commit.
63+
/// </returns>
64+
/// <remarks>
65+
/// In the returned version, the <see cref="Version.Build"/> component is
66+
/// the height of the git commit while the <see cref="Version.Revision"/>
67+
/// component is the first four bytes of the git commit id (forced to be a positive integer).
68+
/// </remarks>
69+
protected System.Version GetIdAsVersion(string repoRelativeProjectDirectory = null)
70+
{
71+
VersionOracle oracle = new VersionOracle(
72+
repoRelativeProjectDirectory == null ? this.Repo.Info.WorkingDirectory : Path.Combine(this.Repo.Info.WorkingDirectory, repoRelativeProjectDirectory),
73+
this.Repo,
74+
null);
75+
76+
return oracle.Version;
77+
}
78+
79+
protected System.Version GetIdAsVersion(Commit commit, string repoRelativeProjectDirectory = null)
80+
{
81+
return GetIdAsVersion(this.Repo, commit, repoRelativeProjectDirectory);
82+
}
83+
84+
protected static System.Version GetIdAsVersion(Repository repository, Commit commit, string repoRelativeProjectDirectory = null)
85+
{
86+
VersionOracle oracle = new VersionOracle(
87+
repoRelativeProjectDirectory == null ? repository.Info.WorkingDirectory : Path.Combine(repository.Info.WorkingDirectory, repoRelativeProjectDirectory),
88+
repository,
89+
commit,
90+
null);
91+
92+
return oracle.Version;
93+
}
5494
}

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void VersionHeightResetsWithVersionSpecChanges(string initial, string nex
112112

113113
foreach (var commit in this.Repo.Head.Commits)
114114
{
115-
var versionFromId = commit.GetIdAsVersion();
115+
var versionFromId = this.GetIdAsVersion(commit);
116116
Assert.Contains(commit, this.Repo.GetCommitsFromVersion(versionFromId));
117117
}
118118
}

src/NerdBank.GitVersioning/GitExtensions.cs

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private static IRepository GetRepository(this IBelongToARepository repositoryMem
212212
/// the height of the git commit while the <see cref="Version.Revision"/>
213213
/// component is the first four bytes of the git commit id (forced to be a positive integer).
214214
/// </remarks>
215-
public static Version GetIdAsVersion(this Commit commit, string repoRelativeProjectDirectory = null, int? versionHeight = null)
215+
internal static Version GetIdAsVersion(this Commit commit, string repoRelativeProjectDirectory = null, int? versionHeight = null)
216216
{
217217
Requires.NotNull(commit, nameof(commit));
218218
Requires.Argument(repoRelativeProjectDirectory == null || !Path.IsPathRooted(repoRelativeProjectDirectory), nameof(repoRelativeProjectDirectory), "Path should be relative to repo root.");
@@ -227,47 +227,6 @@ public static Version GetIdAsVersion(this Commit commit, string repoRelativeProj
227227
return GetIdAsVersionHelper(commit, versionOptions, versionHeight.Value);
228228
}
229229

230-
/// <summary>
231-
/// Encodes HEAD (or a modified working copy) from history in a <see cref="Version"/>
232-
/// so that the original commit can be found later.
233-
/// </summary>
234-
/// <param name="repo">The repo whose ID and position in history is to be encoded.</param>
235-
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
236-
/// <param name="versionHeight">
237-
/// The version height, previously calculated by a call to <see cref="GetVersionHeight(Commit, string, Version)"/>
238-
/// with the same value for <paramref name="repoRelativeProjectDirectory"/>.
239-
/// </param>
240-
/// <returns>
241-
/// A version whose <see cref="Version.Build"/> and
242-
/// <see cref="Version.Revision"/> components are calculated based on the commit.
243-
/// </returns>
244-
/// <remarks>
245-
/// In the returned version, the <see cref="Version.Build"/> component is
246-
/// the height of the git commit while the <see cref="Version.Revision"/>
247-
/// component is the first four bytes of the git commit id (forced to be a positive integer).
248-
/// </remarks>
249-
public static Version GetIdAsVersion(this Repository repo, string repoRelativeProjectDirectory = null, int? versionHeight = null)
250-
{
251-
Requires.NotNull(repo, nameof(repo));
252-
253-
var headCommit = repo.Head.Tip;
254-
VersionOptions workingCopyVersionOptions, committedVersionOptions;
255-
if (IsVersionFileChangedInWorkingCopy(repo, repoRelativeProjectDirectory, out committedVersionOptions, out workingCopyVersionOptions))
256-
{
257-
// Apply ordinary logic, but to the working copy version info.
258-
if (!versionHeight.HasValue)
259-
{
260-
var baseVersion = workingCopyVersionOptions?.Version?.Version;
261-
versionHeight = GetVersionHeight(headCommit, repoRelativeProjectDirectory, baseVersion);
262-
}
263-
264-
Version result = GetIdAsVersionHelper(headCommit, workingCopyVersionOptions, versionHeight.Value);
265-
return result;
266-
}
267-
268-
return GetIdAsVersion(headCommit, repoRelativeProjectDirectory);
269-
}
270-
271230
/// <summary>
272231
/// Looks up the commit that matches a specified version number.
273232
/// </summary>

0 commit comments

Comments
 (0)