<# .SYNOPSIS Apply the patch queue in patches/series.txt to an upstream reims-vgpu checkout. .DESCRIPTION For each patch, in series order: - applies cleanly -> apply it; - already in the tree -> skip it (git apply --reverse --check passes: on a fresh upstream clone this means merged upstream); - neither -> stop with an error. Patches are never edited automatically (AGENTS.md rule 4). Writes the result as JSON to -ResultFile: { "applied": ["a.patch", ...], "merged": ["b.patch", ...], "conflict": "c.patch" | null } so CI can pass the applied list to pack-runtime.ps1 (manifest states only what was really applied, AGENTS.md rule 5) and report merged or conflicting patches. .EXAMPLE pwsh -File scripts/apply-patch-series.ps1 -ReimsRepo $env:RUNNER_TEMP\reims-vgpu -ResultFile patches.json #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$ReimsRepo, [string]$ResultFile = '' ) $ErrorActionPreference = 'Stop' $repoRoot = Split-Path -Parent $PSScriptRoot $patchesDir = Join-Path $repoRoot 'patches' function Read-PatchSeries { $series = Join-Path $patchesDir 'series.txt' foreach ($line in Get-Content -LiteralPath $series) { $t = $line.Trim() if (-not $t -or $t.StartsWith('#')) { continue } $parts = $t -split '\s+' if ($parts.Count -ne 2 -or $parts[0] -notin @('reims-vgpu', 'vendor/qemu')) { throw "Bad line in patches/series.txt: '$line'" } [pscustomobject]@{ Target = $parts[0]; Name = $parts[1] } } } $result = [ordered]@{ applied = @(); merged = @(); conflict = $null } function Save-Result { if ($ResultFile) { [IO.File]::WriteAllText($ResultFile, ($result | ConvertTo-Json -Depth 3), [Text.UTF8Encoding]::new($false)) } } foreach ($p in Read-PatchSeries) { $file = Join-Path $patchesDir $p.Name if (-not (Test-Path -LiteralPath $file)) { throw "Patch listed in series.txt is missing: $file" } $dir = if ($p.Target -eq 'vendor/qemu') { Join-Path $ReimsRepo 'vendor\qemu' } else { $ReimsRepo } # git -C into a missing or uninitialised submodule falls back to the outer # repository, where "git apply" silently skips paths outside the current # directory and reports success. Require $dir to be its own work tree. $top = (& git -C $dir rev-parse --show-toplevel 2>$null) if ($LASTEXITCODE -ne 0 -or -not $top -or [IO.Path]::GetFullPath($top.Trim()).TrimEnd([char[]]'\/') -ne [IO.Path]::GetFullPath($dir).TrimEnd([char[]]'\/')) { throw "$dir is not a checked-out git work tree (is the vendor/qemu submodule initialised?)" } & git -C $dir apply --check $file 2>$null if ($LASTEXITCODE -eq 0) { & git -C $dir apply $file if ($LASTEXITCODE -ne 0) { throw "git apply $($p.Name) failed after a clean --check" } Write-Host "applied $($p.Target) $($p.Name)" $result.applied += $p.Name continue } & git -C $dir apply --reverse --check $file 2>$null if ($LASTEXITCODE -eq 0) { Write-Host "merged $($p.Target) $($p.Name) (already in the tree: merged upstream? retire it per spec section 5 rule 4)" $result.merged += $p.Name continue } $result.conflict = $p.Name Save-Result # Show git's own reason in the log. & git -C $dir apply --check $file throw "$($p.Name) neither applies nor is already upstream in $dir. Stop: don't edit patches automatically (AGENTS.md rule 4)." } $global:LASTEXITCODE = 0 Save-Result