Skip to content

Commit d6d70a8

Browse files
authored
Check git version before running any sparse checkout commands (#5117)
1 parent eda24ce commit d6d70a8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/Agent.Plugins/GitSourceProvider.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ public abstract class GitSourceProvider : ISourceProvider
220220
// Info: https://github.com/git/git/commit/ce81b1da230cf04e231ce337c2946c0671ffb303
221221
protected Version _minGitVersionConfigEnv = new Version(2, 31);
222222

223+
// min git version that supports sparse checkout
224+
protected Version _minGitVersionSupportSparseCheckout = new Version(2, 25);
225+
223226
public abstract bool GitSupportUseAuthHeader(AgentTaskPluginExecutionContext executionContext, GitCliManager gitCommandManager);
224227
public abstract bool GitLfsSupportUseAuthHeader(AgentTaskPluginExecutionContext executionContext, GitCliManager gitCommandManager);
225228
public abstract void RequirementCheck(AgentTaskPluginExecutionContext executionContext, Pipelines.RepositoryResource repository, GitCliManager gitCommandManager);
@@ -704,22 +707,27 @@ public async Task GetSourceAsync(
704707
// Sparse checkout needs to be before any `fetch` task to avoid fetching the excluded trees and blobs, or to not _not_ fetch them if we're disabling a previous sparse checkout.
705708
if (enableSparseCheckout)
706709
{
710+
gitCommandManager.EnsureGitVersion(_minGitVersionSupportSparseCheckout, throwOnNotMatch: true);
711+
707712
// Set up sparse checkout
708713
int exitCode_sparseCheckout = await gitCommandManager.GitSparseCheckout(executionContext, targetPath, sparseCheckoutDirectories, sparseCheckoutPatterns, cancellationToken);
709-
710714
if (exitCode_sparseCheckout != 0)
711715
{
712716
throw new InvalidOperationException($"Git sparse checkout failed with exit code: {exitCode_sparseCheckout}");
713717
}
714718
}
715719
else
716720
{
717-
// Disable sparse checkout in case it was enabled in a previous checkout
718-
int exitCode_sparseCheckoutDisable = await gitCommandManager.GitSparseCheckoutDisable(executionContext, targetPath, cancellationToken);
719-
720-
if (exitCode_sparseCheckoutDisable != 0)
721+
// Only disable if git supports sparse checkout
722+
if (gitCommandManager.EnsureGitVersion(_minGitVersionSupportSparseCheckout, throwOnNotMatch: false))
721723
{
722-
throw new InvalidOperationException($"Git sparse checkout disable failed with exit code: {exitCode_sparseCheckoutDisable}");
724+
// Disable sparse checkout in case it was enabled in a previous checkout
725+
int exitCode_sparseCheckoutDisable = await gitCommandManager.GitSparseCheckoutDisable(executionContext, targetPath, cancellationToken);
726+
727+
if (exitCode_sparseCheckoutDisable != 0)
728+
{
729+
throw new InvalidOperationException($"Git sparse checkout disable failed with exit code: {exitCode_sparseCheckoutDisable}");
730+
}
723731
}
724732
}
725733
}

0 commit comments

Comments
 (0)