@@ -30,7 +30,34 @@ steps:
30
30
# 7.2 behavior for command argument passing. Newer behaviors will result
31
31
# in errors from git.exe.
32
32
$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
+
34
61
function SparseCheckout([Array]$paths, [Hashtable]$repository)
35
62
{
36
63
$dir = $repository.WorkingDirectory
@@ -47,23 +74,18 @@ steps:
47
74
Write-Host "Repository $($repository.Name) is being initialized."
48
75
49
76
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) .
52
78
} 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) .
55
80
}
56
81
57
82
# 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
60
84
61
- Write-Host "git sparse-checkout init"
62
- git sparse-checkout init
85
+ Run git sparse-checkout init
63
86
64
87
# Set non-cone mode otherwise path filters will not work in git >= 2.37.0
65
88
# See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits
66
- Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'"
67
89
git sparse-checkout set --no-cone '/*' '!/*/' '/eng'
68
90
}
69
91
@@ -82,10 +104,8 @@ steps:
82
104
$commitish = $repository.Commitish -replace '^refs/heads/', ''
83
105
84
106
# use -- to prevent git from interpreting the commitish as a path
85
- Write-Host "git -c advice.detachedHead=false checkout $commitish --"
86
-
87
107
# 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 --
89
109
} else {
90
110
Write-Host "Skipping checkout as repo has already been initialized"
91
111
}
0 commit comments