Skip to content

Commit 6bee6b7

Browse files
[Checkout] Configure sparse checkouts before fetch (#5106)
This addresses two issues - first, it allows us to avoid fetching files when checking out a clean repo, as if we pre-configure sparse checkouts we won't fetch unneeded trees and blobs. Second, it allows us to avoid not fetching files incorrectly if we're moving a checkout from sparse to non-sparse. Fixes #5091 Co-authored-by: Derek Morris <[email protected]>
1 parent 8602acf commit 6bee6b7

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/Agent.Plugins/GitSourceProvider.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,31 @@ public async Task GetSourceAsync(
699699
}
700700
}
701701

702+
if (AgentKnobs.UseSparseCheckoutInCheckoutTask.GetValue(executionContext).AsBoolean())
703+
{
704+
// 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.
705+
if (enableSparseCheckout)
706+
{
707+
// Set up sparse checkout
708+
int exitCode_sparseCheckout = await gitCommandManager.GitSparseCheckout(executionContext, targetPath, sparseCheckoutDirectories, sparseCheckoutPatterns, cancellationToken);
709+
710+
if (exitCode_sparseCheckout != 0)
711+
{
712+
throw new InvalidOperationException($"Git sparse checkout failed with exit code: {exitCode_sparseCheckout}");
713+
}
714+
}
715+
else
716+
{
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+
{
722+
throw new InvalidOperationException($"Git sparse checkout disable failed with exit code: {exitCode_sparseCheckoutDisable}");
723+
}
724+
}
725+
}
726+
702727
await RunGitStatusIfSystemDebug(executionContext, gitCommandManager, targetPath);
703728

704729
cancellationToken.ThrowIfCancellationRequested();
@@ -993,30 +1018,6 @@ public async Task GetSourceAsync(
9931018
}
9941019
}
9951020

996-
if (AgentKnobs.UseSparseCheckoutInCheckoutTask.GetValue(executionContext).AsBoolean())
997-
{
998-
if (enableSparseCheckout)
999-
{
1000-
// Set up sparse checkout
1001-
int exitCode_sparseCheckout = await gitCommandManager.GitSparseCheckout(executionContext, targetPath, sparseCheckoutDirectories, sparseCheckoutPatterns, cancellationToken);
1002-
1003-
if (exitCode_sparseCheckout != 0)
1004-
{
1005-
throw new InvalidOperationException($"Git sparse checkout failed with exit code: {exitCode_sparseCheckout}");
1006-
}
1007-
}
1008-
else
1009-
{
1010-
// Disable sparse checkout in case it was enabled in a previous checkout
1011-
int exitCode_sparseCheckoutDisable = await gitCommandManager.GitSparseCheckoutDisable(executionContext, targetPath, cancellationToken);
1012-
1013-
if (exitCode_sparseCheckoutDisable != 0)
1014-
{
1015-
throw new InvalidOperationException($"Git sparse checkout disable failed with exit code: {exitCode_sparseCheckoutDisable}");
1016-
}
1017-
}
1018-
}
1019-
10201021
// Finally, checkout the sourcesToBuild (if we didn't find a valid git object this will throw)
10211022
int exitCode_checkout = await gitCommandManager.GitCheckout(executionContext, targetPath, sourcesToBuild, string.Join(" ", additionalCheckoutArgs), cancellationToken);
10221023
if (exitCode_checkout != 0)

0 commit comments

Comments
 (0)