Skip to content

Commit cdce577

Browse files
authored
Merge pull request #586 from m0sa/disabling
Add NBGV_GitEngine=Disabled option
2 parents b8936b4 + 2bf9b6c commit cdce577

22 files changed

+987
-836
lines changed

src/NerdBank.GitVersioning/Commands/CloudCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void SetBuildVariables(string projectDirectory, IEnumerable<string> metad
9494
activeCloudBuild = CloudBuild.SupportedCloudBuilds[matchingIndex];
9595
}
9696

97-
using var context = GitContext.Create(projectDirectory, writable: alwaysUseLibGit2);
97+
using var context = GitContext.Create(projectDirectory, engine: alwaysUseLibGit2 ? GitContext.Engine.ReadWrite : GitContext.Engine.ReadOnly);
9898
var oracle = new VersionOracle(context, cloudBuild: activeCloudBuild);
9999
if (metadata is not null)
100100
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#nullable enable
5+
6+
using System.Diagnostics;
7+
8+
namespace Nerdbank.GitVersioning;
9+
10+
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
11+
internal class DisabledGitContext : GitContext
12+
{
13+
public DisabledGitContext(string workingTreePath)
14+
: base(workingTreePath, null)
15+
{
16+
this.VersionFile = new DisabledGitVersionFile(this);
17+
}
18+
19+
public override VersionFile VersionFile { get; }
20+
21+
public override string? GitCommitId => null;
22+
23+
public override bool IsHead => false;
24+
25+
public override DateTimeOffset? GitCommitDate => null;
26+
27+
public override string? HeadCanonicalName => null;
28+
29+
private string DebuggerDisplay => $"\"{this.WorkingTreePath}\" (disabled-git)";
30+
31+
public override void ApplyTag(string name) => throw new NotSupportedException();
32+
33+
public override void Stage(string path) => throw new NotSupportedException();
34+
35+
public override string GetShortUniqueCommitId(int minLength) => "nerdbankdisabled";
36+
37+
public override bool TrySelectCommit(string committish) => true;
38+
39+
internal override int CalculateVersionHeight(VersionOptions? committedVersion, VersionOptions? workingVersion) => 0;
40+
41+
internal override Version GetIdAsVersion(VersionOptions? committedVersion, VersionOptions? workingVersion, int versionHeight) => Version0;
42+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#nullable enable
5+
6+
namespace Nerdbank.GitVersioning;
7+
8+
internal class DisabledGitVersionFile : VersionFile
9+
{
10+
public DisabledGitVersionFile(GitContext context)
11+
: base(context)
12+
{
13+
}
14+
15+
protected new DisabledGitContext Context => (DisabledGitContext)base.Context;
16+
17+
protected override VersionOptions? GetVersionCore(out string? actualDirectory)
18+
{
19+
actualDirectory = null;
20+
return null;
21+
}
22+
}

src/NerdBank.GitVersioning/GitContext.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ protected GitContext(string workingTreePath, string? dotGitPath)
4141
this.DotGitPath = dotGitPath;
4242
}
4343

44+
public enum Engine
45+
{
46+
/// <summary>
47+
/// Uses the faster and platform independent managed git implementation.
48+
/// </summary>
49+
ReadOnly,
50+
51+
/// <summary>
52+
/// Uses the LibGit2 engine.
53+
/// </summary>
54+
ReadWrite,
55+
56+
/// <summary>
57+
/// Uses a stubbed out engine that doesn't compute versions at all.
58+
/// </summary>
59+
Disabled,
60+
}
61+
4462
/// <summary>
4563
/// Gets the absolute path to the base directory of the git working tree.
4664
/// </summary>
@@ -110,17 +128,21 @@ public string RepoRelativeProjectDirectory
110128
/// </summary>
111129
/// <param name="path">The path to a directory for which version information is required.</param>
112130
/// <param name="committish">The SHA-1 or ref for a git commit.</param>
113-
/// <param name="writable"><see langword="true"/> if mutating the git repository may be required; <see langword="false" /> otherwise.</param>
131+
/// <param name="engine">The git engine to use.</param>
114132
/// <returns>The newly created <see cref="GitContext"/>.</returns>
115-
public static GitContext Create(string path, string? committish = null, bool writable = false)
133+
public static GitContext Create(string path, string? committish = null, Engine engine = Engine.ReadOnly)
116134
{
117135
Requires.NotNull(path, nameof(path));
118136

119137
if (TryFindGitPaths(path, out string? gitDirectory, out string? workingTreeDirectory, out string? workingTreeRelativePath))
120138
{
121-
GitContext result = writable
122-
? new LibGit2.LibGit2Context(workingTreeDirectory, gitDirectory, committish)
123-
: new Managed.ManagedGitContext(workingTreeDirectory, gitDirectory, committish);
139+
GitContext result = engine switch
140+
{
141+
Engine.Disabled => new DisabledGitContext(workingTreeDirectory),
142+
Engine.ReadWrite => new LibGit2.LibGit2Context(workingTreeDirectory, gitDirectory, committish),
143+
Engine.ReadOnly => new Managed.ManagedGitContext(workingTreeDirectory, gitDirectory, committish),
144+
_ => throw new ArgumentException("Unrecognized value.", nameof(engine)),
145+
};
124146
result.RepoRelativeProjectDirectory = workingTreeRelativePath;
125147
return result;
126148
}

src/NerdBank.GitVersioning/ReleaseManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private LibGit2Context GetRepository(string projectDirectory)
311311
{
312312
// open git repo and use default configuration (in order to commit we need a configured user name and email
313313
// which is most likely configured on a user/system level rather than the repo level.
314-
var context = GitContext.Create(projectDirectory, writable: true);
314+
var context = GitContext.Create(projectDirectory, engine: GitContext.Engine.ReadWrite);
315315
if (!context.IsRepository)
316316
{
317317
this.stderr.WriteLine($"No git repository found above directory '{projectDirectory}'.");

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,21 +219,24 @@ protected override bool ExecuteInner()
219219
Requires.Argument(!containsDotDotSlash, nameof(this.ProjectPathRelativeToGitRepoRoot), "Path must not use ..\\");
220220
}
221221

222-
bool useLibGit2 = false;
222+
GitContext.Engine engine = GitContext.Engine.ReadOnly;
223223
if (!string.IsNullOrWhiteSpace(this.GitEngine))
224224
{
225-
useLibGit2 =
226-
this.GitEngine == "Managed" ? false :
227-
this.GitEngine == "LibGit2" ? true :
228-
throw new ArgumentException("GitEngine property must be set to either \"Managed\" or \"LibGit2\" or left empty.");
225+
engine = this.GitEngine switch
226+
{
227+
"Managed" => GitContext.Engine.ReadOnly,
228+
"LibGit2" => GitContext.Engine.ReadWrite,
229+
"Disabled" => GitContext.Engine.Disabled,
230+
_ => throw new ArgumentException("GitEngine property must be set to either \"Disabled\", \"Managed\" or \"LibGit2\" or left empty."),
231+
};
229232
}
230233

231234
ICloudBuild cloudBuild = CloudBuild.Active;
232235
int? overrideBuildNumberOffset = (this.OverrideBuildNumberOffset == int.MaxValue) ? (int?)null : this.OverrideBuildNumberOffset;
233236
string projectDirectory = this.ProjectPathRelativeToGitRepoRoot is object && this.GitRepoRoot is object
234237
? Path.Combine(this.GitRepoRoot, this.ProjectPathRelativeToGitRepoRoot)
235238
: this.ProjectDirectory;
236-
using var context = GitContext.Create(projectDirectory, writable: useLibGit2);
239+
using var context = GitContext.Create(projectDirectory, engine: engine);
237240
var oracle = new VersionOracle(context, cloudBuild, overrideBuildNumberOffset);
238241
if (!string.IsNullOrEmpty(this.DefaultPublicRelease))
239242
{

src/Nerdbank.GitVersioning.Tasks/build/MSBuildTargetCaching.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<NBGV_InnerGlobalProperties Condition=" '$(GitVersionBaseDirectory)' != '' ">$(NBGV_InnerGlobalProperties)GitVersionBaseDirectory=$(GitVersionBaseDirectory);</NBGV_InnerGlobalProperties>
88
<NBGV_InnerGlobalProperties Condition=" '$(OverrideBuildNumberOffset)' != '' ">$(NBGV_InnerGlobalProperties)OverrideBuildNumberOffset=$(OverrideBuildNumberOffset);</NBGV_InnerGlobalProperties>
99
<NBGV_InnerGlobalProperties Condition=" '$(NBGV_PrivateP2PAuxTargets)' != '' ">$(NBGV_InnerGlobalProperties)NBGV_PrivateP2PAuxTargets=$(NBGV_PrivateP2PAuxTargets);</NBGV_InnerGlobalProperties>
10+
<NBGV_InnerGlobalProperties Condition=" '$(NBGV_GitEngine)' != '' ">$(NBGV_InnerGlobalProperties)NBGV_GitEngine=$(NBGV_GitEngine);</NBGV_InnerGlobalProperties>
1011
</PropertyGroup>
1112

1213
<!-- Compile a list of global properties that may vary when a project builds but that would never influence the result of the GetBuildVersion task. -->

src/nbgv/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ private static async Task<int> OnInstallCommand(string path, string version, str
315315
return (int)ExitCodes.NoGitRepo;
316316
}
317317

318-
using var context = GitContext.Create(searchPath, writable: true);
318+
using var context = GitContext.Create(searchPath, engine: GitContext.Engine.ReadWrite);
319319
if (!context.IsRepository)
320320
{
321321
Console.Error.WriteLine("No git repo found at or above: \"{0}\"", searchPath);
@@ -417,7 +417,7 @@ private static Task<int> OnGetVersionCommand(string project, string[] metadata,
417417

418418
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
419419

420-
using var context = GitContext.Create(searchPath, writable: AlwaysUseLibGit2);
420+
using var context = GitContext.Create(searchPath, engine: AlwaysUseLibGit2 ? GitContext.Engine.ReadWrite : GitContext.Engine.ReadOnly);
421421
if (!context.IsRepository)
422422
{
423423
Console.Error.WriteLine("No git repo found at or above: \"{0}\"", searchPath);
@@ -500,7 +500,7 @@ private static Task<int> OnSetVersionCommand(string project, string version)
500500
};
501501

502502
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
503-
using var context = GitContext.Create(searchPath, writable: true);
503+
using var context = GitContext.Create(searchPath, engine: GitContext.Engine.ReadWrite);
504504
VersionOptions existingOptions = context.VersionFile.GetVersion(out string actualDirectory);
505505
string versionJsonPath;
506506
if (existingOptions is not null)
@@ -540,7 +540,7 @@ private static Task<int> OnTagCommand(string project, string versionOrRef)
540540

541541
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
542542

543-
using var context = (LibGit2Context)GitContext.Create(searchPath, writable: true);
543+
using var context = (LibGit2Context)GitContext.Create(searchPath, engine: GitContext.Engine.ReadWrite);
544544
if (context is null)
545545
{
546546
Console.Error.WriteLine("No git repo found at or above: \"{0}\"", searchPath);
@@ -639,7 +639,7 @@ private static Task<int> OnGetCommitsCommand(string project, bool quiet, string
639639

640640
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
641641

642-
using var context = (LibGit2Context)GitContext.Create(searchPath, writable: true);
642+
using var context = (LibGit2Context)GitContext.Create(searchPath, engine: GitContext.Engine.ReadWrite);
643643
if (!context.IsRepository)
644644
{
645645
Console.Error.WriteLine("No git repo found at or above: \"{0}\"", searchPath);

test/Nerdbank.GitVersioning.Benchmarks/GetVersionBenchmarks.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public class GetVersionBenchmarks
2828
[Benchmark(Baseline = true)]
2929
public void GetVersionLibGit2()
3030
{
31-
using var context = GitContext.Create(GetPath(this.ProjectDirectory), writable: true);
31+
using var context = GitContext.Create(GetPath(this.ProjectDirectory), engine: GitContext.Engine.ReadWrite);
3232
var oracle = new VersionOracle(context, cloudBuild: null);
3333
this.Version = oracle.Version;
3434
}
3535

3636
[Benchmark]
3737
public void GetVersionManaged()
3838
{
39-
using var context = GitContext.Create(GetPath(this.ProjectDirectory), writable: false);
39+
using var context = GitContext.Create(GetPath(this.ProjectDirectory), engine: GitContext.Engine.ReadOnly);
4040
var oracle = new VersionOracle(context, cloudBuild: null);
4141
this.Version = oracle.Version;
4242
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Nerdbank.GitVersioning;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
[Trait("Engine", EngineString)]
9+
[Collection("Build")] // msbuild sets current directory in the process, so we can't have it be concurrent with other build tests.
10+
public class BuildIntegrationDisabledTests : BuildIntegrationTests
11+
{
12+
private const string EngineString = "Disabled";
13+
14+
public BuildIntegrationDisabledTests(ITestOutputHelper logger)
15+
: base(logger)
16+
{
17+
}
18+
19+
protected override GitContext CreateGitContext(string path, string committish = null)
20+
=> GitContext.Create(path, committish, GitContext.Engine.Disabled);
21+
22+
protected override void ApplyGlobalProperties(IDictionary<string, string> globalProperties)
23+
=> globalProperties["NBGV_GitEngine"] = EngineString;
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
[Trait("Engine", EngineString)]
8+
[Collection("Build")] // msbuild sets current directory in the process, so we can't have it be concurrent with other build tests.
9+
public class BuildIntegrationInProjectManagedTests : BuildIntegrationManagedTests
10+
{
11+
public BuildIntegrationInProjectManagedTests(ITestOutputHelper logger)
12+
: base(logger)
13+
{
14+
}
15+
16+
/// <inheritdoc/>
17+
protected override void ApplyGlobalProperties(IDictionary<string, string> globalProperties)
18+
{
19+
base.ApplyGlobalProperties(globalProperties);
20+
globalProperties["NBGV_CacheMode"] = "None";
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Nerdbank.GitVersioning;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
[Trait("Engine", EngineString)]
9+
[Collection("Build")] // msbuild sets current directory in the process, so we can't have it be concurrent with other build tests.
10+
public class BuildIntegrationLibGit2Tests : SomeGitBuildIntegrationTests
11+
{
12+
private const string EngineString = "LibGit2";
13+
14+
public BuildIntegrationLibGit2Tests(ITestOutputHelper logger)
15+
: base(logger)
16+
{
17+
}
18+
19+
protected override GitContext CreateGitContext(string path, string committish = null)
20+
=> GitContext.Create(path, committish, GitContext.Engine.ReadWrite);
21+
22+
protected override void ApplyGlobalProperties(IDictionary<string, string> globalProperties)
23+
=> globalProperties["NBGV_GitEngine"] = EngineString;
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Nerdbank.GitVersioning;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
[Trait("Engine", EngineString)]
9+
[Collection("Build")] // msbuild sets current directory in the process, so we can't have it be concurrent with other build tests.
10+
public class BuildIntegrationManagedTests : SomeGitBuildIntegrationTests
11+
{
12+
protected const string EngineString = "Managed";
13+
14+
public BuildIntegrationManagedTests(ITestOutputHelper logger)
15+
: base(logger)
16+
{
17+
}
18+
19+
protected override GitContext CreateGitContext(string path, string committish = null)
20+
=> GitContext.Create(path, committish, GitContext.Engine.ReadOnly);
21+
22+
protected override void ApplyGlobalProperties(IDictionary<string, string> globalProperties)
23+
=> globalProperties["NBGV_GitEngine"] = EngineString;
24+
}

0 commit comments

Comments
 (0)