Skip to content

Commit 27ba83d

Browse files
committed
Add test that the right error message is shown for shallow repositories
1 parent 7dd54bb commit 27ba83d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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.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)