Skip to content

Commit d44aa05

Browse files
azure-sdkbenbp
andauthored
Sync eng/common directory with azure-sdk-tools for PR 3661 (Azure#18677)
* Add full clone fallback to sparse checkout * Improve clone handling and overrides for sparse checkout * Use SkipSparseCheckout variable name Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent fc72bfb commit d44aa05

File tree

1 file changed

+87
-37
lines changed

1 file changed

+87
-37
lines changed

eng/common/pipelines/templates/steps/sparse-checkout.yml

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,79 @@ steps:
2323
# Define this inline, because of the chicken/egg problem with loading a script when nothing
2424
# has been checked out yet.
2525
script: |
26-
function SparseCheckout([Array]$paths, [Hashtable]$repository)
26+
function Clone([Hashtable]$repository)
2727
{
28-
$dir = $repository.WorkingDirectory
29-
if (!$dir) {
30-
$dir = "./$($repository.Name)"
31-
}
32-
New-Item $dir -ItemType Directory -Force
33-
Push-Location $dir
28+
if (Test-Path .git) {
29+
Write-Warning "Deleting existing git repository"
30+
Write-Host "Remove-Item -Force -Recurse ./*"
31+
Remove-Item -Force -Recurse ./*
32+
}
3433
35-
if (Test-Path .git/info/sparse-checkout) {
36-
$hasInitialized = $true
37-
Write-Host "Repository $($repository.Name) has already been initialized. Skipping this step."
38-
} else {
39-
Write-Host "Repository $($repository.Name) is being initialized."
34+
Write-Host "git clone https://github.com/$($repository.Name) ."
35+
git clone https://github.com/$($repository.Name) .
36+
if ($LASTEXITCODE) {
37+
exit $LASTEXITCODE
38+
}
39+
Write-Host "git -c advice.detachedHead=false checkout $($repository.Commitish)"
40+
# This will use the default branch if repo.Commitish is empty
41+
git -c advice.detachedHead=false checkout $($repository.Commitish)
42+
if ($LASTEXITCODE) {
43+
exit $LASTEXITCODE
44+
}
45+
}
4046
41-
Write-Host "git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) ."
42-
git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
47+
function SparseCheckout([Array]$paths, [Hashtable]$repository)
48+
{
49+
if (Test-Path .git/info/sparse-checkout) {
50+
$hasInitialized = $true
51+
Write-Host "Repository $($repository.Name) has already been initialized. Skipping this step."
52+
} else {
53+
Write-Host "Repository $($repository.Name) is being initialized."
54+
55+
Write-Host "git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) ."
56+
git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
57+
if ($LASTEXITCODE) {
58+
throw
59+
}
4360
44-
Write-Host "git sparse-checkout init"
45-
git sparse-checkout init
61+
Write-Host "git sparse-checkout init"
62+
git sparse-checkout init
63+
if ($LASTEXITCODE) {
64+
throw
65+
}
4666
47-
# Set non-cone mode otherwise path filters will not work in git >= 2.37.0
48-
# See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits
49-
Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'"
50-
git sparse-checkout set --no-cone '/*' '!/*/' '/eng'
67+
# Set non-cone mode otherwise path filters will not work in git >= 2.37.0
68+
# See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits
69+
Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'"
70+
git sparse-checkout set --no-cone '/*' '!/*/' '/eng'
71+
if ($LASTEXITCODE) {
72+
throw
5173
}
74+
}
5275
53-
# Prevent wildcard expansion in Invoke-Expression (e.g. for checkout path '/*')
54-
$quotedPaths = $paths | ForEach-Object { "'$_'" }
55-
$gitsparsecmd = "git sparse-checkout add $quotedPaths"
56-
Write-Host $gitsparsecmd
57-
Invoke-Expression -Command $gitsparsecmd
76+
# Prevent wildcard expansion in Invoke-Expression (e.g. for checkout path '/*')
77+
$quotedPaths = $paths | ForEach-Object { "'$_'" }
78+
$gitsparsecmd = "git sparse-checkout add $quotedPaths"
79+
Write-Host $gitsparsecmd
80+
Invoke-Expression -Command $gitsparsecmd
81+
if ($LASTEXITCODE) {
82+
throw
83+
}
5884
59-
Write-Host "Set sparse checkout paths to:"
60-
Get-Content .git/info/sparse-checkout
85+
Write-Host "Set sparse checkout paths to:"
86+
Get-Content .git/info/sparse-checkout
6187
62-
# sparse-checkout commands after initial checkout will auto-checkout again
63-
if (!$hasInitialized) {
64-
Write-Host "git -c advice.detachedHead=false checkout $($repository.Commitish)"
65-
# This will use the default branch if repo.Commitish is empty
66-
git -c advice.detachedHead=false checkout $($repository.Commitish)
67-
} else {
68-
Write-Host "Skipping checkout as repo has already been initialized"
88+
# sparse-checkout commands after initial checkout will auto-checkout again
89+
if (!$hasInitialized) {
90+
Write-Host "git -c advice.detachedHead=false checkout $($repository.Commitish)"
91+
# This will use the default branch if repo.Commitish is empty
92+
git -c advice.detachedHead=false checkout $($repository.Commitish)
93+
if ($LASTEXITCODE) {
94+
throw
6995
}
70-
71-
Pop-Location
96+
} else {
97+
Write-Host "Skipping checkout as repo has already been initialized"
98+
}
7299
}
73100
74101
# Paths may be sourced as a yaml object literal OR a dynamically generated variable json string.
@@ -77,7 +104,30 @@ steps:
77104
# Replace windows backslash paths, as Azure Pipelines default directories are sometimes formatted like 'D:\a\1\s'
78105
$repositories = '${{ convertToJson(parameters.Repositories) }}' -replace '\\', '/' | ConvertFrom-Json -AsHashtable
79106
foreach ($repo in $Repositories) {
80-
SparseCheckout $paths $repo
107+
$dir = $repo.WorkingDirectory
108+
if (!$dir) {
109+
$dir = "./$($repo.Name)"
110+
}
111+
New-Item $dir -ItemType Directory -Force
112+
Push-Location $dir
113+
114+
try {
115+
# Enable global override if there are sparse checkout issues
116+
if ('$(SkipSparseCheckout)' -ne 'true') {
117+
try {
118+
SparseCheckout $paths $repo
119+
} catch {
120+
# Fallback to full clone if sparse checkout is not working properly
121+
Write-Warning "Sparse checkout failed, falling back to full clone"
122+
Clone $repo
123+
}
124+
} else {
125+
Write-Warning "Sparse checkout disabled, performing full clone"
126+
Clone $repo
127+
}
128+
} finally {
129+
Pop-Location
130+
}
81131
}
82132
pwsh: true
83133
workingDirectory: $(System.DefaultWorkingDirectory)

0 commit comments

Comments
 (0)