Skip to content

Commit 692cfd6

Browse files
authored
Merge pull request #4561 from joaopbnogueira/feature/allow-unshallow-clones
Feature: Support shallow cloning of git repositories
2 parents 9c5ea40 + da29dc5 commit 692cfd6

File tree

13 files changed

+152
-18
lines changed

13 files changed

+152
-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.Tests/HelpWriterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void AllArgsAreInHelp()
4646
{ nameof(Arguments.NoCache), "/nocache" },
4747
{ nameof(Arguments.NoFetch), "/nofetch" },
4848
{ nameof(Arguments.NoNormalize), "/nonormalize" },
49+
{ nameof(Arguments.AllowShallow), "/allowshallow" },
4950
};
5051
var helpText = string.Empty;
5152

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
@@ -25,6 +25,7 @@ internal class Arguments
2525
public bool NoFetch;
2626
public bool NoCache;
2727
public bool NoNormalize;
28+
public bool AllowShallow;
2829

2930
public string? LogFilePath;
3031
public string? ShowVariable;
@@ -77,7 +78,8 @@ public GitVersionOptions ToOptions()
7778
{
7879
NoFetch = NoFetch,
7980
NoCache = NoCache,
80-
NoNormalize = NoNormalize
81+
NoNormalize = NoNormalize,
82+
AllowShallow = AllowShallow,
8183
},
8284

8385
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

0 commit comments

Comments
 (0)