Skip to content

Commit 2777d74

Browse files
authored
Merge pull request #3479 from kimsey0/bug/3188
Check that repository isn't a shallow clone and show an error if it is
2 parents 0d5c064 + fe0ee42 commit 2777d74

File tree

7 files changed

+46
-0
lines changed

7 files changed

+46
-0
lines changed

docs/input/docs/reference/build-servers/azure-devops.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ either using the Command Line build step or install an extension / custom build
1414
step. The custom build step requires a one-time setup to import the GitVersion
1515
task into your TFS or Azure DevOps Pipeline instance.
1616

17+
:::{.alert .alert-danger}
18+
**Important**
19+
20+
You must disable shallow fetch, either in the pipeline settings UI or by setting `fetchDepth: 0` in your `checkout` step;
21+
without it, Azure DevOps Pipelines will perform a shallow clone, which will cause GitVersion to display an error message.
22+
See [the Azure DevOps documentation](https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-checkout?view=azure-pipelines#shallow-fetch) for more information.
23+
:::
24+
1725
## Executing GitVersion
1826

1927
### Using GitVersion with the MSBuild Task NuGet Package

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,27 @@ public void CalculateVersionVariables_TwoBranchHasSameCommitHeadDetachedAndTagge
544544
version.Sha.ShouldBe(commits.First().Sha);
545545
}
546546

547+
[Test]
548+
public void CalculateVersionVariables_ShallowFetch_ThrowException()
549+
{
550+
// Setup
551+
using var fixture = new RemoteRepositoryFixture();
552+
fixture.LocalRepositoryFixture.MakeShallow();
553+
554+
using var worktreeFixture = new LocalRepositoryFixture(new Repository(fixture.LocalRepositoryFixture.RepositoryPath));
555+
var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture.RepositoryPath };
556+
557+
var environment = new TestEnvironment();
558+
environment.SetEnvironmentVariable(AzurePipelines.EnvironmentVariableName, "true");
559+
560+
this.sp = GetServiceProvider(gitVersionOptions, environment: environment);
561+
var sut = sp.GetRequiredService<IGitVersionCalculateTool>();
562+
563+
// Execute & Verify
564+
var exception = Assert.Throws<WarningException>(() => sut.CalculateVersionVariables());
565+
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.");
566+
}
567+
547568
private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog? logger = null, IGitRepository? repository = null, IFileSystem? fs = null)
548569
{
549570
this.sp = GetServiceProvider(gitVersionOptions, logger, repository, fs);

src/GitVersion.Core/Core/GitPreparer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool
183183

184184
EnsureHeadIsAttachedToBranch(currentBranchName, authentication);
185185
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureHeadIsAttachedToBranch), expectedSha);
186+
187+
if (this.repository.IsShallow)
188+
{
189+
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.");
190+
}
186191
}
187192

188193
private void EnsureRepositoryHeadDuringNormalisation(string occasion, string? expectedSha)

src/GitVersion.Core/Git/IGitRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public interface IGitRepository : IDisposable
55
string Path { get; }
66
string WorkingDirectory { get; }
77
bool IsHeadDetached { get; }
8+
bool IsShallow { get; }
89
IBranch Head { get; }
910
ITagCollection Tags { get; }
1011
IReferenceCollection Refs { get; }

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ GitVersion.IGitRepository.FindMergeBase(GitVersion.ICommit! commit, GitVersion.I
335335
GitVersion.IGitRepository.GetNumberOfUncommittedChanges() -> int
336336
GitVersion.IGitRepository.Head.get -> GitVersion.IBranch!
337337
GitVersion.IGitRepository.IsHeadDetached.get -> bool
338+
GitVersion.IGitRepository.IsShallow.get -> bool
338339
GitVersion.IGitRepository.Path.get -> string!
339340
GitVersion.IGitRepository.Refs.get -> GitVersion.IReferenceCollection!
340341
GitVersion.IGitRepository.Remotes.get -> GitVersion.IRemoteCollection!

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private IRepository RepositoryInstance
2020
public string Path => RepositoryInstance.Info.Path;
2121
public string WorkingDirectory => RepositoryInstance.Info.WorkingDirectory;
2222
public bool IsHeadDetached => RepositoryInstance.Info.IsHeadDetached;
23+
public bool IsShallow => RepositoryInstance.Info.IsShallow;
2324
public IBranch Head => new Branch(RepositoryInstance.Head);
2425
public ITagCollection Tags => new TagCollection(RepositoryInstance.Tags);
2526
public IReferenceCollection Refs => new ReferenceCollection(RepositoryInstance.Refs);

src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ public LocalRepositoryFixture CloneRepository()
127127
return new LocalRepositoryFixture(new Repository(localPath));
128128
}
129129

130+
/// <summary>
131+
/// Pulls with a depth of 1 and prunes all older commits, making the repository shallow.
132+
/// </summary>
133+
public void MakeShallow()
134+
{
135+
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} pull --depth 1");
136+
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} gc --prune=all");
137+
}
138+
130139
public void Fetch(string remote, FetchOptions? options = null)
131140
=> Commands.Fetch(Repository, remote, Array.Empty<string>(), options, null);
132141
}

0 commit comments

Comments
 (0)