@@ -30,7 +30,34 @@ steps:
3030 # 7.2 behavior for command argument passing. Newer behaviors will result
3131 # in errors from git.exe.
3232 $PSNativeCommandArgumentPassing = 'Legacy'
33-
33+
34+ function Retry()
35+ {
36+ Run 3 @args
37+ }
38+
39+ function Run()
40+ {
41+ $retries, $command, $arguments = $args
42+ if ($retries -isnot [int]) {
43+ $command, $arguments = $args
44+ $retries = 0
45+ }
46+ Write-Host "==>" $command $arguments
47+ $attempt = 0
48+ $sleep = 5
49+
50+ while ($true) {
51+ $attempt++
52+ & $command $arguments
53+ if (!$LASTEXITCODE) { return }
54+ if ($attempt -gt $retries) { exit $LASTEXITCODE }
55+ Write-Warning "Attempt $attempt failed: $_. Trying again in $sleep seconds..."
56+ Start-Sleep -Seconds $sleep
57+ $sleep *= 2
58+ }
59+ }
60+
3461 function SparseCheckout([Array]$paths, [Hashtable]$repository)
3562 {
3663 $dir = $repository.WorkingDirectory
@@ -47,23 +74,18 @@ steps:
4774 Write-Host "Repository $($repository.Name) is being initialized."
4875
4976 if ($repository.Commitish -match '^refs/pull/\d+/merge$') {
50- 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) ."
51- git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) .
77+ Retry git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) .
5278 } else {
53- Write-Host "git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) ."
54- git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
79+ Retry git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
5580 }
5681
5782 # Turn off git GC for sparse checkout. Note: The devops checkout task does this by default
58- Write-Host "git config gc.auto 0"
59- git config gc.auto 0
83+ Run git config gc.auto 0
6084
61- Write-Host "git sparse-checkout init"
62- git sparse-checkout init
85+ Run git sparse-checkout init
6386
6487 # Set non-cone mode otherwise path filters will not work in git >= 2.37.0
6588 # See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits
66- Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'"
6789 git sparse-checkout set --no-cone '/*' '!/*/' '/eng'
6890 }
6991
@@ -82,10 +104,8 @@ steps:
82104 $commitish = $repository.Commitish -replace '^refs/heads/', ''
83105
84106 # use -- to prevent git from interpreting the commitish as a path
85- Write-Host "git -c advice.detachedHead=false checkout $commitish --"
86-
87107 # This will use the default branch if repo.Commitish is empty
88- git -c advice.detachedHead=false checkout $commitish --
108+ Retry git -c advice.detachedHead=false checkout $commitish --
89109 } else {
90110 Write-Host "Skipping checkout as repo has already been initialized"
91111 }
0 commit comments