Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
m0sa committed Apr 12, 2021
1 parent 163380e commit 4a2b7a8
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ protected override void ApplyGlobalProperties(IDictionary<string, string> global
=> globalProperties["NBGV_GitEngine"] = "LibGit2";
}

[Trait("Engine", "Disabled")]
[Collection("Build")] // msbuild sets current directory in the process, so we can't have it be concurrent with other build tests.
public class BuildIntegrationDisabledTests : BuildIntegrationTests
{
public BuildIntegrationDisabledTests(ITestOutputHelper logger)
: base(logger)
{
}

protected override GitContext CreateGitContext(string path, string committish = null)
=> GitContext.Create(path, committish, disabled: true);

protected override void ApplyGlobalProperties(IDictionary<string, string> globalProperties)
=> globalProperties["NBGV_GitEngine"] = "Disabled";
}

public abstract class BuildIntegrationTests : RepoTestBase, IClassFixture<MSBuildFixture>
{
private const string GitVersioningTargetsFileName = "NerdBank.GitVersioning.targets";
Expand Down
36 changes: 36 additions & 0 deletions src/NerdBank.GitVersioning/DisabledGit/DisabledGitContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#nullable enable

using System;
using System.Diagnostics;

namespace Nerdbank.GitVersioning
{
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
internal class DisabledGitContext : GitContext
{
public DisabledGitContext(string workingTreePath)
: base(workingTreePath, null)
{
this.VersionFile = new DisabledGitVersionFile(this);
}

public override VersionFile VersionFile { get; }

public override string? GitCommitId => null;

public override bool IsHead => false;

public override DateTimeOffset? GitCommitDate => null;

public override string? HeadCanonicalName => null;

private string DebuggerDisplay => $"\"{this.WorkingTreePath}\" (no-git)";

public override void ApplyTag(string name) => throw new NotSupportedException();
public override void Stage(string path) => throw new NotSupportedException();
public override string GetShortUniqueCommitId(int minLength) => "nerdbankdisabled";
public override bool TrySelectCommit(string committish) => true;
internal override int CalculateVersionHeight(VersionOptions? committedVersion, VersionOptions? workingVersion) => 0;
internal override Version GetIdAsVersion(VersionOptions? committedVersion, VersionOptions? workingVersion, int versionHeight) => Version0;
}
}
20 changes: 20 additions & 0 deletions src/NerdBank.GitVersioning/DisabledGit/DisabledGitVersionFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Nerdbank.GitVersioning
{
using Validation;

internal class DisabledGitVersionFile : VersionFile
{
public DisabledGitVersionFile(GitContext context)
: base(context)
{
}

protected new DisabledGitContext Context => (DisabledGitContext)base.Context;

protected override VersionOptions GetVersionCore(out string actualDirectory)
{
actualDirectory = null;
return null;
}
}
}
5 changes: 3 additions & 2 deletions src/NerdBank.GitVersioning/GitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ public void Dispose()
/// <param name="path">The path to a directory for which version information is required.</param>
/// <param name="committish">The SHA-1 or ref for a git commit.</param>
/// <param name="writable"><see langword="true"/> if mutating the git repository may be required; <see langword="false" /> otherwise.</param>
/// <param name="disabled"><see langword="true"/> if no git operations should be disabled (implies <paramref name="writable"/> false); <see langword="false" /> otherwise.</param>
/// <returns></returns>
public static GitContext Create(string path, string? committish = null, bool writable = false)
public static GitContext Create(string path, string? committish = null, bool writable = false, bool disabled = false)
{
if (TryFindGitPaths(path, out string? gitDirectory, out string? workingTreeDirectory, out string? workingTreeRelativePath))
{
GitContext result = writable
GitContext result = disabled ? new DisabledGitContext(workingTreeDirectory) : writable
? (GitContext)new LibGit2.LibGit2Context(workingTreeDirectory, gitDirectory, committish)
: new Managed.ManagedGitContext(workingTreeDirectory, gitDirectory, committish);
result.RepoRelativeProjectDirectory = workingTreeRelativePath;
Expand Down
20 changes: 15 additions & 5 deletions src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,30 @@ protected override bool ExecuteInner()
}

bool useLibGit2 = false;
bool disabled = false;
if (!string.IsNullOrWhiteSpace(this.GitEngine))
{
useLibGit2 =
this.GitEngine == "Managed" ? false :
this.GitEngine == "LibGit2" ? true :
throw new ArgumentException("GitEngine property must be set to either \"Managed\" or \"LibGit2\" or left empty.");
switch (this.GitEngine)
{
case "Managed":
break;
case "LibGit2":
useLibGit2 = true;
break;
case "Disabled":
disabled = true;
break;
default:
throw new ArgumentException("GitEngine property must be set to either \"Disabled\", \"Managed\" or \"LibGit2\" or left empty.");
}
}

var cloudBuild = CloudBuild.Active;
var overrideBuildNumberOffset = (this.OverrideBuildNumberOffset == int.MaxValue) ? (int?)null : this.OverrideBuildNumberOffset;
string projectDirectory = this.ProjectPathRelativeToGitRepoRoot is object && this.GitRepoRoot is object
? Path.Combine(this.GitRepoRoot, this.ProjectPathRelativeToGitRepoRoot)
: this.ProjectDirectory;
using var context = GitContext.Create(projectDirectory, writable: useLibGit2);
using var context = GitContext.Create(projectDirectory, writable: useLibGit2, disabled: disabled);
var oracle = new VersionOracle(context, cloudBuild, overrideBuildNumberOffset);
if (!string.IsNullOrEmpty(this.DefaultPublicRelease))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
Returns="@(CloudBuildVersionVars)" />

<Target Name="GetBuildVersionCore">
<PropertyGroup Condition=" '$(NBGV_Disabled)' == 'true' ">
<NBGV_GitEngine>Disabled</NBGV_GitEngine>
</PropertyGroup>
<Nerdbank.GitVersioning.Tasks.GetBuildVersion
BuildingRef="$(_NBGV_BuildingRef)"
BuildMetadata="$(BuildMetadata.Replace(',',';'))"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
ProjectPathRelativeToGitRepoRoot=$(ProjectPathRelativeToGitRepoRoot);
ProjectDirectory=$(GitVersionBaseDirectory);
OverrideBuildNumberOffset=$(OverrideBuildNumberOffset);
NGBV_Disabled=$(NBGV_Disabled);
</NBGV_InnerGlobalProperties>
<NBGV_CoreTargets>$(MSBuildThisFileDirectory)Nerdbank.GitVersioning.Inner.targets</NBGV_CoreTargets>
</PropertyGroup>
Expand Down

0 comments on commit 4a2b7a8

Please sign in to comment.