Skip to content

Commit c6836f2

Browse files
committed
Add optional GitRelativeProjectPath parameter to GetBuildVersion for the case where Project Path may not be in the same tree folder as Git Repo
1 parent 8dd898e commit c6836f2

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,41 @@ public void CanSetSemVer2ForNuGetPackageVersionNonPublicRelease()
242242
oracle.PublicRelease = false;
243243
Assert.Equal($"7.8.9-foo.25.g{this.CommitIdShort}", oracle.NuGetPackageVersion);
244244
}
245+
246+
[Fact]
247+
public void CanUseGitProjectRelativePathWithGitRepoRoot()
248+
{
249+
VersionOptions rootVersion = new VersionOptions
250+
{
251+
Version = SemanticVersion.Parse("1.1"),
252+
};
253+
254+
VersionOptions projectVersion = new VersionOptions
255+
{
256+
Version = SemanticVersion.Parse("2.2"),
257+
};
258+
259+
string childProjectRelativeDir = "ChildProject1";
260+
string childProjectAbsoluteDir = string.Format(@"{0}\{1}", this.RepoPath, childProjectRelativeDir);
261+
this.WriteVersionFile(rootVersion);
262+
this.WriteVersionFile(projectVersion, childProjectRelativeDir);
263+
264+
this.InitializeSourceControl();
265+
266+
// Check Root Version. Root version will be used
267+
var oracle = VersionOracle.Create(this.RepoPath, this.RepoPath, null, null);
268+
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
269+
270+
// Check ChildProject with projectRelativeDir , with Version file. Child Project version will be used.
271+
oracle = VersionOracle.Create(childProjectAbsoluteDir, this.RepoPath, null, null, childProjectRelativeDir);
272+
Assert.Equal("2.2", oracle.MajorMinorVersion.ToString());
273+
274+
// Check ChildProject withOUT projectRelativeDir , with Version file. Child Project version will be used.
275+
oracle = VersionOracle.Create(childProjectAbsoluteDir, this.RepoPath);
276+
Assert.Equal("2.2", oracle.MajorMinorVersion.ToString());
277+
278+
// Check ChildProject withOUT Version file. Root version will be used.
279+
oracle = VersionOracle.Create(string.Format(@"{0}\{1}", this.RepoPath, "otherChildProject"), this.RepoPath, null, null, "otherChildProject");
280+
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
281+
}
245282
}

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public class VersionOracle
3131
/// <param name="gitRepoDirectory"></param>
3232
/// <param name="cloudBuild"></param>
3333
/// <param name="overrideBuildNumberOffset"></param>
34+
/// <param name="gitProjectRelativePath"></param>
3435
/// <returns></returns>
35-
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null)
36+
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null, string gitProjectRelativePath = null)
3637
{
3738
Requires.NotNull(projectDirectory, nameof(projectDirectory));
3839
if (string.IsNullOrEmpty(gitRepoDirectory))
@@ -42,18 +43,20 @@ public static VersionOracle Create(string projectDirectory, string gitRepoDirect
4243

4344
using (var git = OpenGitRepo(gitRepoDirectory))
4445
{
45-
return new VersionOracle(projectDirectory, git, cloudBuild, overrideBuildNumberOffset);
46+
return new VersionOracle(projectDirectory, git, cloudBuild, overrideBuildNumberOffset, gitProjectRelativePath);
4647
}
4748
}
4849

4950
/// <summary>
5051
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
5152
/// </summary>
52-
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null)
53+
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null, string gitProjectRelativePath = null)
5354
{
5455
var repoRoot = repo?.Info?.WorkingDirectory?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
5556
var relativeRepoProjectDirectory = !string.IsNullOrWhiteSpace(repoRoot)
56-
? projectDirectory.Substring(repoRoot.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
57+
? ( !string.IsNullOrEmpty(gitProjectRelativePath)
58+
? gitProjectRelativePath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
59+
: projectDirectory.Substring(repoRoot.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) )
5760
: null;
5861

5962
var commit = repo?.Head.Commits.FirstOrDefault();

src/Nerdbank.GitVersioning.NuGet/build/Nerdbank.GitVersioning.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
BuildMetadata="@(BuildMetadata)"
6666
DefaultPublicRelease="$(PublicRelease)"
6767
GitRepoRoot="$(GitRepoRoot)"
68+
GitProjectRelativePath="$(GitProjectRelativePath)"
6869
OverrideBuildNumberOffset="$(OverrideBuildNumberOffset)"
6970
TargetsPath="$(MSBuildThisFileDirectory)">
7071

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public GetBuildVersion()
4040
/// </summary>
4141
public string GitRepoRoot { get; set; }
4242

43+
/// <summary>
44+
/// Gets or sets the project path relative to Git Root. This is the case where GitRepoRoot might be different from where project folder is getting built
45+
/// If null or empty, default behavior will be to use Environment.CurrentDirectory and substring GitRepoRoot to search the GIT Tree for version.json
46+
/// </summary>
47+
public string GitProjectRelativePath { get; set; }
48+
4349
/// <summary>
4450
/// Gets or sets the optional override build number offset.
4551
/// </summary>
@@ -162,7 +168,7 @@ protected override bool ExecuteInner()
162168
{
163169
var cloudBuild = CloudBuild.Active;
164170
var overrideBuildNumberOffset = (this.OverrideBuildNumberOffset == int.MaxValue) ? (int?)null : this.OverrideBuildNumberOffset;
165-
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild, overrideBuildNumberOffset);
171+
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild, overrideBuildNumberOffset, this.GitProjectRelativePath);
166172
if (!string.IsNullOrEmpty(this.DefaultPublicRelease))
167173
{
168174
oracle.PublicRelease = string.Equals(this.DefaultPublicRelease, "true", StringComparison.OrdinalIgnoreCase);

0 commit comments

Comments
 (0)