Skip to content

Commit 8b06315

Browse files
author
João Nogueira
committed
feat(preparer): add allowshallow flag
1 parent 388b4a3 commit 8b06315

File tree

12 files changed

+151
-18
lines changed

12 files changed

+151
-18
lines changed

docs/input/docs/reference/requirements.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ build server, needs to adhere to the below requirements.
1414

1515
### Unshallow
1616

17-
The repository needs to be an [unshallow][git-unshallow] clone. This means
18-
that the `fetch-depth` in GitHub Actions needs to be set to `0`, for instance.
17+
The repository should be an [unshallow][git-unshallow] clone. This means
18+
that the `fetch-depth` in GitHub Actions should set to `0`, unless
19+
the `allowshallow` flag is used.
1920
Check with your [build server][build-servers] to see how it can be configured
2021
appropriately.
2122

docs/input/docs/usage/cli/arguments.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ GitVersion [path]
5050
Currently supported config overrides: tag-prefix
5151
/nocache Bypasses the cache, result will not be written to the cache.
5252
/nonormalize Disables normalize step on a build server.
53+
/allowshallow Allows GitVersion to run on a shallow clone.
54+
This is not recommended, but can be used if you are sure
55+
that the shallow clone contains all the information needed
56+
to calculate the version.
5357
/verbosity Specifies the amount of information to be displayed.
5458
(Quiet, Minimal, Normal, Verbose, Diagnostic)
5559
Default is Normal

docs/input/docs/usage/msbuild.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ version information that is compiled into the resulting artifact.
1818
Since version 6.0 only MSBuild running on .NET Core (`dotnet msbuild`) is supported.
1919

2020
Unfortunately, up until at least Visual Studio 2022 17.11, Visual Studio runs all builds
21-
using the .NET Framework version of MSBuild, and therefore **Visual Studio is not supported**.
22-
For more information see [this discussion](https://github.com/GitTools/GitVersion/discussions/4130).
21+
using the .NET Framework version of MSBuild, and therefore **Visual Studio is not supported**.
22+
For more information see [this discussion](https://github.com/GitTools/GitVersion/discussions/4130).
2323

2424
## TL;DR
2525

@@ -261,7 +261,9 @@ There are properties that correspond to certain
261261
In particular, setting `GitVersion_NoFetchEnabled` to `true` disables `git fetch`
262262
during version calculation, setting `GitVersion_NoNormalizeEnabled` to `true` disables
263263
normalize step on a build server, setting `GitVersion_NoCacheEnabled` to `true`
264-
makes GetVersion ignore cache. All the rest command line arguments can be passed via
264+
makes GetVersion ignore cache, setting `GitVersion_AllowShallowEnabled` to `true`
265+
does not mandate a full clone of the repository to determine the version.
266+
All the rest command line arguments can be passed via
265267
`GitVersion_CommandLineArguments` variable.
266268

267269
## My Git repository requires authentication. What should I do?

src/GitVersion.App.Tests/ArgumentParserTests.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,13 @@ public void NoNormalizeTrueWhenDefined()
629629
arguments.NoNormalize.ShouldBe(true);
630630
}
631631

632+
[Test]
633+
public void AllowshallowTrueWhenDefined()
634+
{
635+
var arguments = this.argumentParser.ParseArguments("-allowshallow");
636+
arguments.AllowShallow.ShouldBe(true);
637+
}
638+
632639
[Test]
633640
public void OtherArgumentsCanBeParsedBeforeNofetch()
634641
{
@@ -653,18 +660,45 @@ public void OtherArgumentsCanBeParsedBeforeNocache()
653660
arguments.NoCache.ShouldBe(true);
654661
}
655662

656-
[TestCase("-nofetch -nonormalize -nocache")]
657-
[TestCase("-nofetch -nocache -nonormalize")]
658-
[TestCase("-nocache -nofetch -nonormalize")]
659-
[TestCase("-nocache -nonormalize -nofetch")]
660-
[TestCase("-nonormalize -nocache -nofetch")]
661-
[TestCase("-nonormalize -nofetch -nocache")]
663+
[Test]
664+
public void OtherArgumentsCanBeParsedBeforeAllowshallow()
665+
{
666+
var arguments = this.argumentParser.ParseArguments("targetpath -allowshallow");
667+
arguments.TargetPath.ShouldBe("targetpath");
668+
arguments.AllowShallow.ShouldBe(true);
669+
}
670+
671+
[TestCase("-nofetch -nonormalize -nocache -allowshallow")]
672+
[TestCase("-nofetch -nonormalize -allowshallow -nocache")]
673+
[TestCase("-nofetch -nocache -nonormalize -allowshallow")]
674+
[TestCase("-nofetch -nocache -allowshallow -nonormalize")]
675+
[TestCase("-nofetch -allowshallow -nonormalize -nocache")]
676+
[TestCase("-nofetch -allowshallow -nocache -nonormalize")]
677+
[TestCase("-nonormalize -nofetch -nocache -allowshallow")]
678+
[TestCase("-nonormalize -nofetch -allowshallow -nocache")]
679+
[TestCase("-nonormalize -nocache -nofetch -allowshallow")]
680+
[TestCase("-nonormalize -nocache -allowshallow -nofetch")]
681+
[TestCase("-nonormalize -allowshallow -nofetch -nocache")]
682+
[TestCase("-nonormalize -allowshallow -nocache -nofetch")]
683+
[TestCase("-nocache -nofetch -nonormalize -allowshallow")]
684+
[TestCase("-nocache -nofetch -allowshallow -nonormalize")]
685+
[TestCase("-nocache -nonormalize -nofetch -allowshallow")]
686+
[TestCase("-nocache -nonormalize -allowshallow -nofetch")]
687+
[TestCase("-nocache -allowshallow -nofetch -nonormalize")]
688+
[TestCase("-nocache -allowshallow -nonormalize -nofetch")]
689+
[TestCase("-allowshallow -nofetch -nonormalize -nocache")]
690+
[TestCase("-allowshallow -nofetch -nocache -nonormalize")]
691+
[TestCase("-allowshallow -nonormalize -nofetch -nocache")]
692+
[TestCase("-allowshallow -nonormalize -nocache -nofetch")]
693+
[TestCase("-allowshallow -nocache -nofetch -nonormalize")]
694+
[TestCase("-allowshallow -nocache -nonormalize -nofetch")]
662695
public void SeveralSwitchesCanBeParsed(string commandLineArgs)
663696
{
664697
var arguments = this.argumentParser.ParseArguments(commandLineArgs);
665698
arguments.NoCache.ShouldBe(true);
666699
arguments.NoNormalize.ShouldBe(true);
667700
arguments.NoFetch.ShouldBe(true);
701+
arguments.AllowShallow.ShouldBe(true);
668702
}
669703

670704
[Test]

src/GitVersion.App/ArgumentParser.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ private static bool ParseSwitches(Arguments arguments, string? name, IReadOnlyLi
281281
return true;
282282
}
283283

284+
if (name.IsSwitch("allowshallow"))
285+
{
286+
arguments.AllowShallow = true;
287+
return true;
288+
}
289+
284290
if (name.IsSwitch("verbosity"))
285291
{
286292
ParseVerbosity(arguments, value);

src/GitVersion.App/ArgumentParserExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static bool IsSwitch(this string? value, string switchName)
6969

7070
public static bool ArgumentRequiresValue(this string argument, int argumentIndex)
7171
{
72-
var booleanArguments = new[] { "updateassemblyinfo", "ensureassemblyinfo", "nofetch", "nonormalize", "nocache" };
72+
var booleanArguments = new[] { "updateassemblyinfo", "ensureassemblyinfo", "nofetch", "nonormalize", "nocache", "allowshallow" };
7373

7474
var argumentMightRequireValue = !booleanArguments.Contains(argument[1..], StringComparer.OrdinalIgnoreCase);
7575

src/GitVersion.App/Arguments.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal class Arguments
2727
public bool NoFetch;
2828
public bool NoCache;
2929
public bool NoNormalize;
30+
public bool AllowShallow;
3031

3132
public string? LogFilePath;
3233
public string? ShowVariable;
@@ -78,7 +79,8 @@ public GitVersionOptions ToOptions()
7879
{
7980
NoFetch = NoFetch,
8081
NoCache = NoCache,
81-
NoNormalize = NoNormalize
82+
NoNormalize = NoNormalize,
83+
AllowShallow = AllowShallow,
8284
},
8385

8486
WixInfo =

src/GitVersion.Core.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,79 @@ public void CalculateVersionVariables_ShallowFetch_ThrowException()
597597
exception?.Message.ShouldBe("Repository is a shallow clone. Git repositories must contain the full history. See https://gitversion.net/docs/reference/requirements#unshallow for more info.");
598598
}
599599

600+
[Test]
601+
public void CalculateVersionVariables_ShallowFetch_WithAllowShallow_ShouldNotThrowException()
602+
{
603+
// Setup
604+
using var fixture = new RemoteRepositoryFixture();
605+
fixture.LocalRepositoryFixture.MakeShallow();
606+
607+
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
608+
var gitVersionOptions = new GitVersionOptions
609+
{
610+
WorkingDirectory = worktreeFixture.RepositoryPath,
611+
Settings = { AllowShallow = true }
612+
};
613+
614+
var environment = new TestEnvironment();
615+
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
616+
617+
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
618+
619+
sp.DiscoverRepository();
620+
621+
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
622+
623+
// Execute
624+
var version = sut.CalculateVersionVariables();
625+
626+
// Verify
627+
version.ShouldNotBeNull();
628+
var commits = worktreeFixture.Repository.Head.Commits;
629+
version.Sha.ShouldBe(commits.First().Sha);
630+
}
631+
632+
[Test]
633+
public void CalculateVersionVariables_WithLimitedCloneDepth_AndAllowShallowTrue_ShouldCalculateVersionCorrectly()
634+
{
635+
// Setup
636+
using var fixture = new RemoteRepositoryFixture();
637+
fixture.LocalRepositoryFixture.MakeShallow();
638+
639+
fixture.LocalRepositoryFixture.Repository.MakeACommit("Initial commit");
640+
fixture.LocalRepositoryFixture.Repository.MakeATaggedCommit("1.0.0");
641+
var latestCommit = fixture.LocalRepositoryFixture.Repository.MakeACommit("+semver:major");
642+
643+
using var worktreeFixture = new LocalRepositoryFixture(new(fixture.LocalRepositoryFixture.RepositoryPath));
644+
645+
var gitVersionOptions = new GitVersionOptions
646+
{
647+
WorkingDirectory = worktreeFixture.RepositoryPath,
648+
Settings = { AllowShallow = true }
649+
};
650+
651+
var environment = new TestEnvironment();
652+
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
653+
654+
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
655+
sp.DiscoverRepository();
656+
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
657+
658+
// Execute
659+
var version = sut.CalculateVersionVariables();
660+
661+
// Verify
662+
version.ShouldNotBeNull();
663+
664+
// Verify that the correct commit is used
665+
version.Sha.ShouldBe(latestCommit.Sha);
666+
version.MajorMinorPatch.ShouldBe("2.0.0");
667+
668+
// Verify repository is still recognized as shallow
669+
var repository = this.sp.GetRequiredService<IGitRepository>();
670+
repository.IsShallow.ShouldBeTrue("Repository should still be shallow after version calculation");
671+
}
672+
600673
private string GetWorktreePath(EmptyRepositoryFixture fixture)
601674
{
602675
var worktreePath = FileSystemHelper.Path.Combine(this.fileSystem.Directory.GetParent(fixture.RepositoryPath)?.FullName, Guid.NewGuid().ToString());

src/GitVersion.Core/Core/GitPreparer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,14 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool
190190

191191
if (this.repository.IsShallow)
192192
{
193-
throw new WarningException("Repository is a shallow clone. Git repositories must contain the full history. See https://gitversion.net/docs/reference/requirements#unshallow for more info.");
193+
if (this.options.Value.Settings.AllowShallow)
194+
{
195+
this.log.Info("Repository is a shallow clone. GitVersion will continue, but it is recommended to use a full clone for accurate versioning.");
196+
}
197+
else
198+
{
199+
throw new WarningException("Repository is a shallow clone. Git repositories must contain the full history. See https://gitversion.net/docs/reference/requirements#unshallow for more info.");
200+
}
194201
}
195202
}
196203

src/GitVersion.Core/Options/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public record Settings
66
public bool NoCache;
77
public bool NoNormalize;
88
public bool OnlyTrackedBranches = false;
9+
public bool AllowShallow = false;
910
}

src/GitVersion.Core/PublicAPI.Shipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ GitVersion.Settings.NoCache -> bool
595595
GitVersion.Settings.NoFetch -> bool
596596
GitVersion.Settings.NoNormalize -> bool
597597
GitVersion.Settings.OnlyTrackedBranches -> bool
598+
GitVersion.Settings.AllowShallow -> bool
598599
GitVersion.VersionCalculation.BaseVersion
599600
GitVersion.VersionCalculation.BaseVersion.BaseVersion() -> void
600601
GitVersion.VersionCalculation.BaseVersion.BaseVersion(GitVersion.VersionCalculation.BaseVersionOperand! Operand) -> void

src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.props

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111
<GitVersion_NoFetchEnabled Condition="$(GitVersion_NoFetchEnabled) == ''">false</GitVersion_NoFetchEnabled>
1212
<GitVersion_NoNormalizeEnabled Condition="$(GitVersion_NoNormalizeEnabled) == ''">false</GitVersion_NoNormalizeEnabled>
1313
<GitVersion_NoCacheEnabled Condition="$(GitVersion_NoCacheEnabled) == ''">false</GitVersion_NoCacheEnabled>
14+
<GitVersion_AllowShallowEnabled Condition="$(GitVersion_AllowShallowEnabled) == ''">false</GitVersion_AllowShallowEnabled>
1415

1516
<GitVersion_ToolArgments>$(GitVersion_CommandLineArguments) -output file -outputfile &quot;$(GitVersionOutputFile)&quot;</GitVersion_ToolArgments>
1617
<GitVersion_ToolArgments Condition=" '$(GitVersion_NoFetchEnabled)' == 'true' ">$(GitVersion_ToolArgments) -nofetch</GitVersion_ToolArgments>
1718
<GitVersion_ToolArgments Condition=" '$(GitVersion_NoNormalizeEnabled)' == 'true' ">$(GitVersion_ToolArgments) -nonormalize</GitVersion_ToolArgments>
1819
<GitVersion_ToolArgments Condition=" '$(GitVersion_NoCacheEnabled)' == 'true' ">$(GitVersion_ToolArgments) -nocache</GitVersion_ToolArgments>
20+
<GitVersion_ToolArgments Condition=" '$(GitVersion_AllowShallowEnabled)' == 'true' ">$(GitVersion_ToolArgments) -allowshallow</GitVersion_ToolArgments>
1921
</PropertyGroup>
2022

2123
<PropertyGroup>
22-
<!-- The GitVersion task is explicitly disabled when running on the .NET Framework because it's no longer supported.
23-
If a project that uses GitVersion.MsBuild is opened in Visual Studio,
24-
the task will be turned off because Visual Studio operates on the .NET Framework's version of MSBuild.
24+
<!-- The GitVersion task is explicitly disabled when running on the .NET Framework because it's no longer supported.
25+
If a project that uses GitVersion.MsBuild is opened in Visual Studio,
26+
the task will be turned off because Visual Studio operates on the .NET Framework's version of MSBuild.
2527
However, you can still execute GitVersion.MsBuild as part of the `dotnet build` or `dotnet msbuild` commands. -->
2628
<DisableGitVersionTask Condition=" '$(MSBuildRuntimeType)' != 'Core' ">true</DisableGitVersionTask>
27-
29+
2830
<DisableGitVersionTask Condition=" '$(DisableGitVersionTask)' == '' ">false</DisableGitVersionTask>
2931

3032
<!-- Property that enables WriteVersionInfoToBuildLog -->

0 commit comments

Comments
 (0)