Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions eng/common/pipelines/templates/steps/sparse-checkout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,34 @@ steps:
# 7.2 behavior for command argument passing. Newer behaviors will result
# in errors from git.exe.
$PSNativeCommandArgumentPassing = 'Legacy'


function Retry()
{
Run 3 @args
}

function Run()
{
$retries, $command, $arguments = $args
if ($retries -isnot [int]) {
$command, $arguments = $args
$retries = 0
}
Write-Host "==>" $command $arguments
$attempt = 0
$sleep = 5

while ($true) {
$attempt++
& $command $arguments
if (!$LASTEXITCODE) { return }
if ($attempt -gt $retries) { exit $LASTEXITCODE }
Write-Warning "Attempt $attempt failed: $_. Trying again in $sleep seconds..."
Start-Sleep -Seconds $sleep
$sleep *= 2
}
}

function SparseCheckout([Array]$paths, [Hashtable]$repository)
{
$dir = $repository.WorkingDirectory
Expand All @@ -47,23 +74,18 @@ steps:
Write-Host "Repository $($repository.Name) is being initialized."

if ($repository.Commitish -match '^refs/pull/\d+/merge$') {
Write-Host "git clone --no-checkout --filter=tree:0 -c remote.origin.fetch='+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)' https://github.com/$($repository.Name) ."
git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) .
Retry git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) .
} else {
Write-Host "git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) ."
git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
Retry git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
}

# Turn off git GC for sparse checkout. Note: The devops checkout task does this by default
Write-Host "git config gc.auto 0"
git config gc.auto 0
Run git config gc.auto 0

Write-Host "git sparse-checkout init"
git sparse-checkout init
Run git sparse-checkout init

# Set non-cone mode otherwise path filters will not work in git >= 2.37.0
# See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits
Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'"
git sparse-checkout set --no-cone '/*' '!/*/' '/eng'
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember why we used Invoke-Expression in the lines below? If not needed we could use the Run there as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a lot of issues with commands that contained any sort of glob/wildcard when sourcing those values dynamically. We may be able to improve upon this by using invoke expression everywhere, I will have to test it out.

Expand All @@ -82,10 +104,8 @@ steps:
$commitish = $repository.Commitish -replace '^refs/heads/', ''

# use -- to prevent git from interpreting the commitish as a path
Write-Host "git -c advice.detachedHead=false checkout $commitish --"

# This will use the default branch if repo.Commitish is empty
git -c advice.detachedHead=false checkout $commitish --
Retry git -c advice.detachedHead=false checkout $commitish --
} else {
Write-Host "Skipping checkout as repo has already been initialized"
}
Expand Down