diff --git a/src/Agent.Plugins/GitSourceProvider.cs b/src/Agent.Plugins/GitSourceProvider.cs index b8144bdf52..0f68614623 100644 --- a/src/Agent.Plugins/GitSourceProvider.cs +++ b/src/Agent.Plugins/GitSourceProvider.cs @@ -680,6 +680,31 @@ public async Task GetSourceAsync( } } + if (AgentKnobs.UseSparseCheckoutInCheckoutTask.GetValue(executionContext).AsBoolean()) + { + // 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. + if (enableSparseCheckout) + { + // Set up sparse checkout + int exitCode_sparseCheckout = await gitCommandManager.GitSparseCheckout(executionContext, targetPath, sparseCheckoutDirectories, sparseCheckoutPatterns, cancellationToken); + + if (exitCode_sparseCheckout != 0) + { + throw new InvalidOperationException($"Git sparse checkout failed with exit code: {exitCode_sparseCheckout}"); + } + } + else + { + // Disable sparse checkout in case it was enabled in a previous checkout + int exitCode_sparseCheckoutDisable = await gitCommandManager.GitSparseCheckoutDisable(executionContext, targetPath, cancellationToken); + + if (exitCode_sparseCheckoutDisable != 0) + { + throw new InvalidOperationException($"Git sparse checkout disable failed with exit code: {exitCode_sparseCheckoutDisable}"); + } + } + } + await RunGitStatusIfSystemDebug(executionContext, gitCommandManager, targetPath); cancellationToken.ThrowIfCancellationRequested(); @@ -974,30 +999,6 @@ public async Task GetSourceAsync( } } - if (AgentKnobs.UseSparseCheckoutInCheckoutTask.GetValue(executionContext).AsBoolean()) - { - if (enableSparseCheckout) - { - // Set up sparse checkout - int exitCode_sparseCheckout = await gitCommandManager.GitSparseCheckout(executionContext, targetPath, sparseCheckoutDirectories, sparseCheckoutPatterns, cancellationToken); - - if (exitCode_sparseCheckout != 0) - { - throw new InvalidOperationException($"Git sparse checkout failed with exit code: {exitCode_sparseCheckout}"); - } - } - else - { - // Disable sparse checkout in case it was enabled in a previous checkout - int exitCode_sparseCheckoutDisable = await gitCommandManager.GitSparseCheckoutDisable(executionContext, targetPath, cancellationToken); - - if (exitCode_sparseCheckoutDisable != 0) - { - throw new InvalidOperationException($"Git sparse checkout disable failed with exit code: {exitCode_sparseCheckoutDisable}"); - } - } - } - // Finally, checkout the sourcesToBuild (if we didn't find a valid git object this will throw) int exitCode_checkout = await gitCommandManager.GitCheckout(executionContext, targetPath, sourcesToBuild, string.Join(" ", additionalCheckoutArgs), cancellationToken); if (exitCode_checkout != 0)