File: sparse-checkout.yml

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (88 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
parameters:
  - name: Paths
    type: object
    default: []
  - name: Repositories
    type: object
    default:
      - Name: $(Build.Repository.Name)
        Commitish: $(Build.SourceVersion)
        WorkingDirectory: $(System.DefaultWorkingDirectory)
  - name: SkipCheckoutNone
    type: boolean
    default: false

steps:
  - ${{ if not(parameters.SkipCheckoutNone) }}:
      - checkout: none

  - task: PowerShell@2
    displayName: 'Sparse checkout repositories'
    inputs:
      targetType: inline
      # Define this inline, because of the chicken/egg problem with loading a script when nothing
      # has been checked out yet.
      script: |
        function SparseCheckout([Array]$paths, [Hashtable]$repository)
        {
            $dir = $repository.WorkingDirectory
            if (!$dir) {
              $dir = "./$($repository.Name)"
            }
            New-Item $dir -ItemType Directory -Force
            Push-Location $dir

            if (Test-Path .git/info/sparse-checkout) {
              $hasInitialized = $true
              Write-Host "Repository $($repository.Name) has already been initialized. Skipping this step."
            } else {
              Write-Host "Repository $($repository.Name) is being initialized."

              if ($repository.Commitish -match '^refs/pull/\d+/merge$') {
                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) ."
                git clone --no-checkout --filter=tree:0 -c remote.origin.fetch=''+$($repository.Commitish):refs/remotes/origin/$($repository.Commitish)'' https://github.com/$($repository.Name) .
              } else {
                Write-Host "git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) ."
                git clone --no-checkout --filter=tree:0 https://github.com/$($repository.Name) .
              }

              Write-Host "git sparse-checkout init"
              git sparse-checkout init

              # Set non-cone mode otherwise path filters will not work in git >= 2.37.0
              # See https://github.blog/2022-06-27-highlights-from-git-2-37/#tidbits
              Write-Host "git sparse-checkout set --no-cone '/*' '!/*/' '/eng'"
              git sparse-checkout set --no-cone '/*' '!/*/' '/eng'
            }

            # Prevent wildcard expansion in Invoke-Expression (e.g. for checkout path '/*')
            $quotedPaths = $paths | ForEach-Object { "'$_'" }
            $gitsparsecmd = "git sparse-checkout add $quotedPaths"
            Write-Host $gitsparsecmd
            Invoke-Expression -Command $gitsparsecmd

            Write-Host "Set sparse checkout paths to:"
            Get-Content .git/info/sparse-checkout

            # sparse-checkout commands after initial checkout will auto-checkout again
            if (!$hasInitialized) {
              Write-Host "git -c advice.detachedHead=false checkout $($repository.Commitish)"
              # This will use the default branch if repo.Commitish is empty
              git -c advice.detachedHead=false checkout $($repository.Commitish)
            } else {
              Write-Host "Skipping checkout as repo has already been initialized"
            }

            Pop-Location
        }

        # Paths may be sourced as a yaml object literal OR a dynamically generated variable json string.
        # If the latter, convertToJson will wrap the 'string' in quotes, so remove them.
        $paths = '${{ convertToJson(parameters.Paths) }}'.Trim('"') | ConvertFrom-Json
        # Replace windows backslash paths, as Azure Pipelines default directories are sometimes formatted like 'D:\a\1\s'
        $repositories = '${{ convertToJson(parameters.Repositories) }}' -replace '\\', '/' | ConvertFrom-Json -AsHashtable
        foreach ($repo in $Repositories) {
          SparseCheckout $paths $repo
        }
      pwsh: true
      workingDirectory: $(System.DefaultWorkingDirectory)