Files
reims-vgpu/packaging/windows-runtime/scripts/apply-patch-series.ps1

103 lines
4.2 KiB
PowerShell

<#
.SYNOPSIS
Apply the patch queue in patches/series.txt to the checked-out upstream sources.
.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 -UtilRepo $env:RUNNER_TEMP\reims-util -Metal2VulkanRepo $env:RUNNER_TEMP\metal2vulkan -ResultFile patches.json
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ReimsRepo,
[string]$ResultFile = '',
[string]$UtilRepo = '',
[string]$Metal2VulkanRepo = ''
)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
$patchesDir = Join-Path $repoRoot 'patches'
$repositories = @{
'reims-vgpu' = $ReimsRepo
'vendor/qemu' = Join-Path $ReimsRepo 'vendor\qemu'
'reims-util' = $UtilRepo
'metal2vulkan' = $Metal2VulkanRepo
}
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 -not $repositories.ContainsKey($parts[0])) {
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))
}
}
$series = @(Read-PatchSeries)
if (-not $UtilRepo -and $series.Target -contains 'reims-util') {
throw 'Pass -UtilRepo with a checkout at the reims-util revision pinned by reims-ctl.'
}
if (-not $Metal2VulkanRepo -and $series.Target -contains 'metal2vulkan') {
throw 'Pass -Metal2VulkanRepo with a checkout at the metal2vulkan revision pinned by reims-vgpu.'
}
foreach ($p in $series) {
$file = Join-Path $patchesDir $p.Name
if (-not (Test-Path -LiteralPath $file)) { throw "Patch listed in series.txt is missing: $file" }
$dir = $repositories[$p.Target]
# 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 for $($p.Target)"
}
& 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