@@ -23,52 +23,79 @@ steps:
23
23
# Define this inline, because of the chicken/egg problem with loading a script when nothing
24
24
# has been checked out yet.
25
25
script : |
26
- function SparseCheckout([Array]$paths, [Hashtable]$repository)
26
+ function Clone( [Hashtable]$repository)
27
27
{
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
+ }
34
33
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
+ }
40
46
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
+ }
43
60
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
+ }
46
66
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
51
73
}
74
+ }
52
75
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
+ }
58
84
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
61
87
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
69
95
}
70
-
71
- Pop-Location
96
+ } else {
97
+ Write-Host "Skipping checkout as repo has already been initialized"
98
+ }
72
99
}
73
100
74
101
# Paths may be sourced as a yaml object literal OR a dynamically generated variable json string.
@@ -77,7 +104,30 @@ steps:
77
104
# Replace windows backslash paths, as Azure Pipelines default directories are sometimes formatted like 'D:\a\1\s'
78
105
$repositories = '${{ convertToJson(parameters.Repositories) }}' -replace '\\', '/' | ConvertFrom-Json -AsHashtable
79
106
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
+ }
81
131
}
82
132
pwsh : true
83
133
workingDirectory : $(System.DefaultWorkingDirectory)
0 commit comments