Skip to content

Commit ec9f32e

Browse files
authored
Merge pull request #527 from qmfrederik/features/simplify-gitextensions
Re-route calls to GitExtensions.GitIdAsVersion, GetVersionHeight to VersionOracle
2 parents d9c51b8 + ba1bbcb commit ec9f32e

File tree

7 files changed

+178
-177
lines changed

7 files changed

+178
-177
lines changed

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 6 additions & 6 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

@@ -1001,10 +1001,10 @@ private static RestoreEnvironmentVariables ApplyEnvironmentVariables(IReadOnlyDi
10011001

10021002
private void AssertStandardProperties(VersionOptions versionOptions, BuildResults buildResult, string relativeProjectDirectory = null)
10031003
{
1004-
int versionHeight = this.Repo.GetVersionHeight(relativeProjectDirectory);
1005-
Version idAsVersion = this.Repo.GetIdAsVersion(relativeProjectDirectory);
1004+
int versionHeight = this.GetVersionHeight(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: 64 additions & 64 deletions
Large diffs are not rendered by default.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.IO;
3+
using LibGit2Sharp;
4+
using Nerdbank.GitVersioning;
5+
6+
public partial class RepoTestBase
7+
{
8+
/// <summary>
9+
/// Gets the number of commits in the longest single path between
10+
/// the specified commit and the most distant ancestor (inclusive)
11+
/// that set the version to the value at the tip of the <paramref name="branch"/>.
12+
/// </summary>
13+
/// <param name="branch">The branch to measure the height of.</param>
14+
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
15+
/// <returns>The height of the branch till the version is changed.</returns>
16+
protected int GetVersionHeight(Branch branch, string repoRelativeProjectDirectory = null)
17+
{
18+
var commit = branch.Tip ?? throw new InvalidOperationException("No commit exists.");
19+
return this.GetVersionHeight(commit, repoRelativeProjectDirectory);
20+
}
21+
/// <summary>
22+
/// Gets the number of commits in the longest single path between
23+
/// the specified commit and the most distant ancestor (inclusive)
24+
/// that set the version to the value at <paramref name="commit"/>.
25+
/// </summary>
26+
/// <param name="commit">The commit to measure the height of.</param>
27+
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
28+
/// <param name="baseVersion">Optional base version to calculate the height. If not specified, the base version will be calculated by scanning the repository.</param>
29+
/// <returns>The height of the commit. Always a positive integer.</returns>
30+
protected int GetVersionHeight(Commit commit, string repoRelativeProjectDirectory = null)
31+
{
32+
VersionOracle oracle = new VersionOracle(repoRelativeProjectDirectory == null ? this.RepoPath : Path.Combine(this.RepoPath, repoRelativeProjectDirectory), this.Repo, commit, null);
33+
return oracle.VersionHeight;
34+
}
35+
36+
/// <summary>
37+
/// Gets the number of commits in the longest single path between
38+
/// HEAD in a repo and the most distant ancestor (inclusive)
39+
/// that set the version to the value in the working copy
40+
/// (or HEAD for bare repositories).
41+
/// </summary>
42+
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
43+
/// <returns>The height of the repo at HEAD. Always a positive integer.</returns>
44+
protected int GetVersionHeight(string repoRelativeProjectDirectory = null)
45+
{
46+
return GetVersionHeight(this.Repo, repoRelativeProjectDirectory);
47+
}
48+
49+
protected static int GetVersionHeight(Repository repository, string repoRelativeProjectDirectory = null)
50+
{
51+
VersionOracle oracle = new VersionOracle(repoRelativeProjectDirectory == null ? repository.Info.WorkingDirectory : Path.Combine(repository.Info.WorkingDirectory, repoRelativeProjectDirectory), repository, null);
52+
return oracle.VersionHeight;
53+
}
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+
}
94+
}

src/NerdBank.GitVersioning.Tests/RepoTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Validation;
77
using Xunit.Abstractions;
88

9-
public abstract class RepoTestBase : IDisposable
9+
public abstract partial class RepoTestBase : IDisposable
1010
{
1111
private readonly List<string> repoDirectories = new List<string>();
1212

src/NerdBank.GitVersioning.Tests/VersionFileTests.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,9 @@ public void VersionJson_DoNotInheritButNoVersionSpecified()
485485
}
486486

487487
[Theory]
488-
[InlineData(false, false)]
489-
[InlineData(true, false)]
490-
[InlineData(true, true)]
491-
public void VersionJson_Inheritance(bool commitInSourceControl, bool bareRepo)
488+
[InlineData(false)]
489+
[InlineData(true)]
490+
public void VersionJson_Inheritance(bool commitInSourceControl)
492491
{
493492
if (commitInSourceControl)
494493
{
@@ -531,11 +530,6 @@ public void VersionJson_Inheritance(bool commitInSourceControl, bool bareRepo)
531530
"inheritWithVersion");
532531

533532
Repository operatingRepo = this.Repo;
534-
if (bareRepo)
535-
{
536-
operatingRepo = new Repository(
537-
Repository.Clone(this.RepoPath, this.CreateDirectoryForNewRepo(), new CloneOptions { IsBare = true }));
538-
}
539533

540534
using (operatingRepo)
541535
{
@@ -573,13 +567,13 @@ public void VersionJson_Inheritance(bool commitInSourceControl, bool bareRepo)
573567

574568
// The version height should be the same for all those that inherit the version from the base,
575569
// even though the inheriting files were introduced in successive commits.
576-
Assert.Equal(totalCommits, operatingRepo.GetVersionHeight());
577-
Assert.Equal(totalCommits, operatingRepo.GetVersionHeight("foo"));
578-
Assert.Equal(totalCommits, operatingRepo.GetVersionHeight("foo/bar"));
570+
Assert.Equal(totalCommits, GetVersionHeight(operatingRepo));
571+
Assert.Equal(totalCommits, GetVersionHeight(operatingRepo, "foo"));
572+
Assert.Equal(totalCommits, GetVersionHeight(operatingRepo, "foo/bar"));
579573

580574
// These either don't inherit, or inherit but reset versions, so the commits were reset.
581-
Assert.Equal(2, operatingRepo.GetVersionHeight("noInherit"));
582-
Assert.Equal(1, operatingRepo.GetVersionHeight("inheritWithVersion"));
575+
Assert.Equal(2, GetVersionHeight(operatingRepo, "noInherit"));
576+
Assert.Equal(1, GetVersionHeight(operatingRepo, "inheritWithVersion"));
583577
}
584578
}
585579
}

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ public void VersionHeightResetsWithVersionSpecChanges(string initial, string nex
101101

102102
var oracle = VersionOracle.Create(this.RepoPath);
103103
Assert.Equal(11, oracle.VersionHeight);
104-
Assert.Equal(11, this.Repo.Head.GetVersionHeight());
104+
Assert.Equal(11, this.GetVersionHeight(this.Repo.Head));
105105

106106
options.Version = SemanticVersion.Parse(next);
107107

108108
this.WriteVersionFile(options);
109109
oracle = VersionOracle.Create(this.RepoPath);
110110
Assert.Equal(1, oracle.VersionHeight);
111-
Assert.Equal(1, this.Repo.Head.GetVersionHeight());
111+
Assert.Equal(1, this.GetVersionHeight(this.Repo.Head));
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: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static class GitExtensions
4949
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
5050
/// <param name="baseVersion">Optional base version to calculate the height. If not specified, the base version will be calculated by scanning the repository.</param>
5151
/// <returns>The height of the commit. Always a positive integer.</returns>
52-
public static int GetVersionHeight(this Commit commit, string repoRelativeProjectDirectory = null, Version baseVersion = null)
52+
internal static int GetVersionHeight(this Commit commit, string repoRelativeProjectDirectory = null, Version baseVersion = null)
5353
{
5454
Requires.NotNull(commit, nameof(commit));
5555
Requires.Argument(repoRelativeProjectDirectory == null || !Path.IsPathRooted(repoRelativeProjectDirectory), nameof(repoRelativeProjectDirectory), "Path should be relative to repo root.");
@@ -76,52 +76,6 @@ public static int GetVersionHeight(this Commit commit, string repoRelativeProjec
7676
return 0;
7777
}
7878

79-
/// <summary>
80-
/// Gets the number of commits in the longest single path between
81-
/// HEAD in a repo and the most distant ancestor (inclusive)
82-
/// that set the version to the value in the working copy
83-
/// (or HEAD for bare repositories).
84-
/// </summary>
85-
/// <param name="repo">The repo with the working copy / HEAD to measure the height of.</param>
86-
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
87-
/// <returns>The height of the repo at HEAD. Always a positive integer.</returns>
88-
public static int GetVersionHeight(this Repository repo, string repoRelativeProjectDirectory = null)
89-
{
90-
if (repo == null)
91-
{
92-
return 0;
93-
}
94-
95-
VersionOptions workingCopyVersionOptions, committedVersionOptions;
96-
if (IsVersionFileChangedInWorkingCopy(repo, repoRelativeProjectDirectory, out committedVersionOptions, out workingCopyVersionOptions))
97-
{
98-
Version workingCopyVersion = workingCopyVersionOptions?.Version?.Version;
99-
Version headCommitVersion = committedVersionOptions?.Version?.Version ?? Version0;
100-
if (workingCopyVersion == null || !workingCopyVersion.Equals(headCommitVersion))
101-
{
102-
// The working copy has changed the major.minor version.
103-
// So by definition the version height is 0, since no commit represents it yet.
104-
return 0;
105-
}
106-
}
107-
108-
// No special changes in the working directory, so apply regular logic.
109-
return GetVersionHeight(repo.Head, repoRelativeProjectDirectory);
110-
}
111-
112-
/// <summary>
113-
/// Gets the number of commits in the longest single path between
114-
/// the specified commit and the most distant ancestor (inclusive)
115-
/// that set the version to the value at the tip of the <paramref name="branch"/>.
116-
/// </summary>
117-
/// <param name="branch">The branch to measure the height of.</param>
118-
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
119-
/// <returns>The height of the branch till the version is changed.</returns>
120-
public static int GetVersionHeight(this Branch branch, string repoRelativeProjectDirectory = null)
121-
{
122-
return GetVersionHeight(branch.Tip ?? throw new InvalidOperationException("No commit exists."), repoRelativeProjectDirectory);
123-
}
124-
12579
/// <summary>
12680
/// Gets the number of commits in the longest single path between
12781
/// the specified commit and the most distant ancestor (inclusive).
@@ -258,7 +212,7 @@ private static IRepository GetRepository(this IBelongToARepository repositoryMem
258212
/// the height of the git commit while the <see cref="Version.Revision"/>
259213
/// component is the first four bytes of the git commit id (forced to be a positive integer).
260214
/// </remarks>
261-
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)
262216
{
263217
Requires.NotNull(commit, nameof(commit));
264218
Requires.Argument(repoRelativeProjectDirectory == null || !Path.IsPathRooted(repoRelativeProjectDirectory), nameof(repoRelativeProjectDirectory), "Path should be relative to repo root.");
@@ -273,47 +227,6 @@ public static Version GetIdAsVersion(this Commit commit, string repoRelativeProj
273227
return GetIdAsVersionHelper(commit, versionOptions, versionHeight.Value);
274228
}
275229

276-
/// <summary>
277-
/// Encodes HEAD (or a modified working copy) from history in a <see cref="Version"/>
278-
/// so that the original commit can be found later.
279-
/// </summary>
280-
/// <param name="repo">The repo whose ID and position in history is to be encoded.</param>
281-
/// <param name="repoRelativeProjectDirectory">The repo-relative project directory for which to calculate the version.</param>
282-
/// <param name="versionHeight">
283-
/// The version height, previously calculated by a call to <see cref="GetVersionHeight(Commit, string, Version)"/>
284-
/// with the same value for <paramref name="repoRelativeProjectDirectory"/>.
285-
/// </param>
286-
/// <returns>
287-
/// A version whose <see cref="Version.Build"/> and
288-
/// <see cref="Version.Revision"/> components are calculated based on the commit.
289-
/// </returns>
290-
/// <remarks>
291-
/// In the returned version, the <see cref="Version.Build"/> component is
292-
/// the height of the git commit while the <see cref="Version.Revision"/>
293-
/// component is the first four bytes of the git commit id (forced to be a positive integer).
294-
/// </remarks>
295-
public static Version GetIdAsVersion(this Repository repo, string repoRelativeProjectDirectory = null, int? versionHeight = null)
296-
{
297-
Requires.NotNull(repo, nameof(repo));
298-
299-
var headCommit = repo.Head.Tip;
300-
VersionOptions workingCopyVersionOptions, committedVersionOptions;
301-
if (IsVersionFileChangedInWorkingCopy(repo, repoRelativeProjectDirectory, out committedVersionOptions, out workingCopyVersionOptions))
302-
{
303-
// Apply ordinary logic, but to the working copy version info.
304-
if (!versionHeight.HasValue)
305-
{
306-
var baseVersion = workingCopyVersionOptions?.Version?.Version;
307-
versionHeight = GetVersionHeight(headCommit, repoRelativeProjectDirectory, baseVersion);
308-
}
309-
310-
Version result = GetIdAsVersionHelper(headCommit, workingCopyVersionOptions, versionHeight.Value);
311-
return result;
312-
}
313-
314-
return GetIdAsVersion(headCommit, repoRelativeProjectDirectory);
315-
}
316-
317230
/// <summary>
318231
/// Looks up the commit that matches a specified version number.
319232
/// </summary>

0 commit comments

Comments
 (0)