@@ -23,79 +23,52 @@ 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 Clone([Hashtable]$repository)
27
- {
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
- }
33
-
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
- }
46
-
47
26
function SparseCheckout([Array]$paths, [Hashtable]$repository)
48
27
{
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
28
+ $dir = $repository.WorkingDirectory
29
+ if (!$dir) {
30
+ $dir = "./$($repository.Name)"
59
31
}
32
+ New-Item $dir -ItemType Directory -Force
33
+ Push-Location $dir
60
34
61
- Write-Host "git sparse-checkout init"
62
- git sparse-checkout init
63
- if ($LASTEXITCODE) {
64
- throw
65
- }
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."
40
+
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) .
66
43
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
44
+ Write-Host "git sparse-checkout init"
45
+ git sparse-checkout init
46
+
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'
73
51
}
74
- }
75
52
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
- }
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
84
58
85
- Write-Host "Set sparse checkout paths to:"
86
- Get-Content .git/info/sparse-checkout
59
+ Write-Host "Set sparse checkout paths to:"
60
+ Get-Content .git/info/sparse-checkout
87
61
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
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"
95
69
}
96
- } else {
97
- Write-Host "Skipping checkout as repo has already been initialized"
98
- }
70
+
71
+ Pop-Location
99
72
}
100
73
101
74
# Paths may be sourced as a yaml object literal OR a dynamically generated variable json string.
@@ -104,30 +77,7 @@ steps:
104
77
# Replace windows backslash paths, as Azure Pipelines default directories are sometimes formatted like 'D:\a\1\s'
105
78
$repositories = '${{ convertToJson(parameters.Repositories) }}' -replace '\\', '/' | ConvertFrom-Json -AsHashtable
106
79
foreach ($repo in $Repositories) {
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
- }
80
+ SparseCheckout $paths $repo
131
81
}
132
82
pwsh : true
133
83
workingDirectory : $(System.DefaultWorkingDirectory)
0 commit comments