Repository: microsoft/terminal
File: tools/ReleaseEngineering/ServicingPipeline.ps1 (PowerShell)

Code before:
# Propagate default values into all PSGitHub cmdlets
$PSDefaultParameterValues['*GitHub*:Owner'] = "microsoft"
$PSDefaultParameterValues['*GitHub*:RepositoryName'] = "terminal"

$Project = Get-GithubProject -Name "$Version Servicing Pipeline"
$AllColumns = Get-GithubProjectColumn -ProjectId $Project.id
$ToPickColumn = $AllColumns | ? Name -Eq $script:TodoColumnName
$DoneColumn = $AllColumns | ? Name -Eq $script:DoneColumnName
$RejectColumn = $AllColumns | ? Name -Eq $script:RejectColumnName
$Cards = Get-GithubProjectCard -ColumnId $ToPickColumn.id

& git fetch --all 2>&1 | Out-Null

$Branch = & git rev-parse --abbrev-ref HEAD
$RemoteForCurrentBranch = & git config "branch.$Branch.remote"
$CommitsBehind = [int](& git rev-list --count "$RemoteForCurrentBranch/$Branch" "^$Branch")

If ($LASTEXITCODE -Ne 0) {
	Write-Error "Failed to determine branch divergence"
	Exit 1
}

If ($CommitsBehind -Gt 0) {
	Write-Error "Local branch $Branch is out of date with $RemoteForCurrentBranch; consider git pull"
	Exit 1
}

$Entries = @(& git log $SourceBranch --first-parent --grep "(#\($($Cards.Number -Join "\|")\))" "--pretty=format:%H%x1C%s"  |
    ConvertFrom-CSV -Delimiter "`u{001C}" -Header CommitID,Subject)

[Array]::Reverse($Entries)

$PickArgs = @()
If ($GpgSign) {
    $PickArgs += , "-S"
}

$CommitPRRegex = [Regex]"\(#(\d+)\)$"
$Entries | ForEach-Object {
    Write-Host "`e[96m`e[1;7mPICK`e[22;27m $($_.CommitID): $($_.Subject)`e[m"
    $PR = $CommitPRRegex.Match($_.Subject).Groups[1].Value
    $Card = $Cards | ? Number -Eq $PR
    $null = & git cherry-pick -x $_.CommitID 2>&1
    $Err = ""
    While ($True) {
        If ($LASTEXITCODE -ne 0) {
            $Err
            Enter-ConflictResolutionShell $_

            If ($Global:Abort) {
                & git cherry-pick --abort

Review comment (bug):
```suggestion
If ($LASTEXITCODE -Ne 0) {
    Write-Error "Failed to determine branch divergence"
    Exit 1
}

If ($CommitsBehind -Gt 0) {
    Write-Error "Local branch $Branch is out of date with $RemoteForCurrentBranch; consider git pull"
    Exit 1
}
```

Code after:
# Propagate default values into all PSGitHub cmdlets
$PSDefaultParameterValues['*GitHub*:Owner'] = "microsoft"
$PSDefaultParameterValues['*GitHub*:RepositoryName'] = "terminal"

$Project = Get-GithubProject -Name "$Version Servicing Pipeline"
$AllColumns = Get-GithubProjectColumn -ProjectId $Project.id
$ToPickColumn = $AllColumns | ? Name -Eq $script:TodoColumnName
$DoneColumn = $AllColumns | ? Name -Eq $script:DoneColumnName
$RejectColumn = $AllColumns | ? Name -Eq $script:RejectColumnName
$Cards = Get-GithubProjectCard -ColumnId $ToPickColumn.id

& git fetch --all 2>&1 | Out-Null

$Branch = & git rev-parse --abbrev-ref HEAD
$RemoteForCurrentBranch = & git config "branch.$Branch.remote"
$CommitsBehind = [int](& git rev-list --count "$RemoteForCurrentBranch/$Branch" "^$Branch")

If ($LASTEXITCODE -Ne 0) {
    Write-Error "Failed to determine branch divergence"
    Exit 1
}

If ($CommitsBehind -Gt 0) {
    Write-Error "Local branch $Branch is out of date with $RemoteForCurrentBranch; consider git pull"
    Exit 1
}

$Entries = @(& git log $SourceBranch --first-parent --grep "(#\($($Cards.Number -Join "\|")\))" "--pretty=format:%H%x1C%s"  |
    ConvertFrom-CSV -Delimiter "`u{001C}" -Header CommitID,Subject)

[Array]::Reverse($Entries)

$PickArgs = @()
If ($GpgSign) {
    $PickArgs += , "-S"
}

$CommitPRRegex = [Regex]"\(#(\d+)\)$"
$Entries | ForEach-Object {
    Write-Host "`e[96m`e[1;7mPICK`e[22;27m $($_.CommitID): $($_.Subject)`e[m"
    $PR = $CommitPRRegex.Match($_.Subject).Groups[1].Value
    $Card = $Cards | ? Number -Eq $PR
    $null = & git cherry-pick -x $_.CommitID 2>&1
    $Err = ""
    While ($True) {
        If ($LASTEXITCODE -ne 0) {
            $Err
            Enter-ConflictResolutionShell $_

            If ($Global:Abort) {
                & git cherry-pick --abort