-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathCreate-Patch-Mergeback.ps1
More file actions
339 lines (294 loc) · 13.9 KB
/
Create-Patch-Mergeback.ps1
File metadata and controls
339 lines (294 loc) · 13.9 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#Requires -Version 7.0
#Requires -PSEdition Core
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.SYNOPSIS
[Experimental] Automates creation of a patch-release mergeback against the current branch.
.DESCRIPTION
[Experimental] Given a release branch (e.g. release/patch/20260505) that contains a patch
release, this script ports the patch's published-version bumps and changelog
entries back into the current working branch. It performs three steps:
1. version_client.txt: For every line whose dependency-version (middle
column) was bumped on the release branch, copy that bumped value into
the current branch's version_client.txt. The current-version (right
column) on the current branch is preserved as-is, because it usually
reflects in-development beta versions that should not be reverted.
2. CHANGELOG.md: For each library whose dependency-version changed, take
the topmost (newest) entry from the release branch's CHANGELOG and
insert it into the current branch's CHANGELOG. When an existing
"## ... (Unreleased)" section is present, the new entry is inserted
after that entire section (before the next "##" heading). Otherwise,
it is inserted before the first dated entry (immediately after the
"# Release History" heading).
3. Runs `python eng/versioning/update_versions.py --skip-readme` to
propagate the new dependency-versions into all pom.xml files.
This script does NOT commit, push, or open a pull request. Review the
working tree afterwards, then commit and push manually.
.PARAMETER ReleaseBranch
The name of the patch release branch to port edits from
(for example release/patch/20260505). The branch can be local or remote;
the script will use `git fetch` and resolve origin/<branch> if needed.
.PARAMETER RepoRoot
Optional. Path to the azure-sdk-for-java clone. Defaults to the repo root
inferred from the script's location.
.EXAMPLE
.\eng\scripts\Create-Patch-Mergeback.ps1 -ReleaseBranch release/patch/20260505
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ReleaseBranch,
[string]$RepoRoot
)
$ErrorActionPreference = 'Stop'
# Resolve repo root.
if (-not $RepoRoot) {
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot '..\..')
}
$RepoRoot = (Resolve-Path $RepoRoot).Path
Push-Location $RepoRoot
try {
Write-Host "Repo root: $RepoRoot"
$workingTreeStatus = git status --porcelain
if ($LASTEXITCODE -ne 0) {
throw "Failed to check working tree status."
}
if ($workingTreeStatus) {
throw "Working tree is not clean. Commit, stash, or discard local changes before running this script."
}
# ---------------------------------------------------------------------
# Resolve the release branch ref.
# ---------------------------------------------------------------------
Write-Host "Fetching latest refs..."
git fetch --quiet origin 2>$null | Out-Null
$branchRef = $null
foreach ($candidate in @($ReleaseBranch, "origin/$ReleaseBranch")) {
$null = git rev-parse --verify --quiet "$candidate" 2>$null
if ($LASTEXITCODE -eq 0) { $branchRef = $candidate; break }
}
if (-not $branchRef) {
throw "Could not resolve '$ReleaseBranch' (tried local and origin/)."
}
Write-Host "Using release branch ref: $branchRef"
# ---------------------------------------------------------------------
# 1. version_client.txt: port dependency-version bumps.
# ---------------------------------------------------------------------
$vcRelPath = 'eng/versioning/version_client.txt'
$vcPath = Join-Path $RepoRoot $vcRelPath
Write-Host "`n[1/3] Porting dependency-version bumps from $vcRelPath ..."
$releaseVcRaw = git show "${branchRef}:${vcRelPath}"
if ($LASTEXITCODE -ne 0) { throw "Failed to read $vcRelPath from $branchRef." }
$releaseLines = $releaseVcRaw -split "`r?`n"
$localLines = Get-Content -LiteralPath $vcPath
function Parse-VcLine([string]$line) {
# Returns @{ Key=...; Dep=...; Cur=...; Comment=... } or $null
if ([string]::IsNullOrWhiteSpace($line)) { return $null }
$trim = $line.TrimStart()
if ($trim.StartsWith('#')) { return $null }
# Strip optional inline comment.
$body = $line
$note = ''
$hash = $line.IndexOf(' #')
if ($hash -ge 0) {
$body = $line.Substring(0, $hash)
$note = $line.Substring($hash)
}
$parts = $body.Split(';')
if ($parts.Count -lt 3) { return $null }
return [pscustomobject]@{
Key = $parts[0].Trim()
Dep = $parts[1].Trim()
Cur = $parts[2].Trim()
Comment = $note
}
}
$releaseMap = @{}
foreach ($l in $releaseLines) {
$p = Parse-VcLine $l
if ($p) { $releaseMap[$p.Key] = $p }
}
$changedArtifacts = New-Object System.Collections.Generic.List[string]
$newLocal = New-Object System.Collections.Generic.List[string]
foreach ($line in $localLines) {
$p = Parse-VcLine $line
if (-not $p -or -not $releaseMap.ContainsKey($p.Key)) {
$newLocal.Add($line); continue
}
$r = $releaseMap[$p.Key]
if ($r.Dep -eq $p.Dep) {
$newLocal.Add($line); continue
}
# Bump dep-version, keep local current-version.
$newBody = "$($p.Key);$($r.Dep);$($p.Cur)"
$newLocal.Add("$newBody$($p.Comment)")
$changedArtifacts.Add($p.Key) | Out-Null
}
if ($changedArtifacts.Count -eq 0) {
Write-Host " No dependency-version changes found. Nothing to port."
return
}
# Preserve original line endings of the file.
$origBytes = [System.IO.File]::ReadAllBytes($vcPath)
$useCrlf = $false
for ($i = 0; $i -lt [Math]::Min($origBytes.Length, 4096); $i++) {
if ($origBytes[$i] -eq 13) { $useCrlf = $true; break }
}
$eol = if ($useCrlf) { "`r`n" } else { "`n" }
[System.IO.File]::WriteAllText($vcPath, ($newLocal -join $eol) + $eol)
Write-Host " Bumped $($changedArtifacts.Count) artifact(s) in version_client.txt."
# ---------------------------------------------------------------------
# 2. CHANGELOG.md: port the latest entry from the release branch for
# each artifact whose dependency-version changed.
# ---------------------------------------------------------------------
Write-Host "`n[2/3] Porting CHANGELOG.md entries..."
# Build artifactId -> list of CHANGELOG.md paths once.
$changelogIndex = @{}
Get-ChildItem -Path (Join-Path $RepoRoot 'sdk') -Recurse -Filter 'CHANGELOG.md' -File `
| ForEach-Object {
$artifactId = $_.Directory.Name
if (-not $changelogIndex.ContainsKey($artifactId)) {
$changelogIndex[$artifactId] = New-Object System.Collections.Generic.List[string]
}
$changelogIndex[$artifactId].Add($_.FullName) | Out-Null
}
function Resolve-ChangelogPath([string]$artifactId) {
if (-not $changelogIndex.ContainsKey($artifactId)) { return $null }
$paths = $changelogIndex[$artifactId]
if ($paths.Count -eq 1) { return $paths[0] }
# Prefer the v1 (non *-v2) directory, then shortest path as a tiebreaker.
$nonV2 = $paths | Where-Object { $_ -notmatch '[\\/][^\\/]*-v2[\\/]' }
if ($nonV2.Count -eq 1) { return $nonV2[0] }
return ($paths | Sort-Object Length | Select-Object -First 1)
}
function Get-LatestChangelogEntry([string]$relPath) {
# Returns @{ Header=...; Body=... } for the topmost ## entry, or $null.
$raw = git show "${branchRef}:${relPath}" 2>$null
if ($LASTEXITCODE -ne 0 -or -not $raw) { return $null }
$lines = $raw -split "`r?`n"
$startIdx = -1
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match '^##\s+\S') { $startIdx = $i; break }
}
if ($startIdx -lt 0) { return $null }
$endIdx = $lines.Count
for ($i = $startIdx + 1; $i -lt $lines.Count; $i++) {
if ($lines[$i] -match '^##\s+\S') { $endIdx = $i; break }
}
# Trim trailing blank lines.
$block = $lines[$startIdx..($endIdx - 1)]
while ($block.Count -gt 0 -and [string]::IsNullOrWhiteSpace($block[-1])) {
$block = $block[0..($block.Count - 2)]
}
return [pscustomobject]@{
Header = $lines[$startIdx]
Body = ($block -join "`n")
}
}
$portedCount = 0
$skippedNoFile = New-Object System.Collections.Generic.List[string]
$skippedNoEntry = New-Object System.Collections.Generic.List[string]
$alreadyPresent = New-Object System.Collections.Generic.List[string]
foreach ($key in $changedArtifacts) {
$artifactId = $key.Split(':')[1]
$localPath = Resolve-ChangelogPath $artifactId
if (-not $localPath) { $skippedNoFile.Add($artifactId) | Out-Null; continue }
$relLocalPath = (Resolve-Path -LiteralPath $localPath -Relative).TrimStart('.', '\', '/').Replace('\', '/')
$entry = Get-LatestChangelogEntry $relLocalPath
if (-not $entry) { $skippedNoEntry.Add($artifactId) | Out-Null; continue }
$existing = Get-Content -LiteralPath $localPath -Raw
# Skip if the same released-version header is already present.
$headerEscaped = [regex]::Escape($entry.Header)
if ($existing -match "(?m)^$headerEscaped\s*$") {
$alreadyPresent.Add($artifactId) | Out-Null; continue
}
$existingLines = $existing -split "`r?`n"
# Find an Unreleased heading; if none, find the first dated heading.
$unreleasedIdx = -1
$firstDatedIdx = -1
for ($i = 0; $i -lt $existingLines.Count; $i++) {
if ($unreleasedIdx -lt 0 -and $existingLines[$i] -match '^##\s+.*\(Unreleased\)\s*$') {
$unreleasedIdx = $i
} elseif ($firstDatedIdx -lt 0 -and $existingLines[$i] -match '^##\s+\S') {
$firstDatedIdx = $i
}
}
$insertText = $entry.Body.TrimEnd() + "`n"
if ($unreleasedIdx -ge 0) {
# Find end of the Unreleased block (next ## heading or EOF).
$blockEnd = $existingLines.Count
for ($i = $unreleasedIdx + 1; $i -lt $existingLines.Count; $i++) {
if ($existingLines[$i] -match '^##\s+\S') { $blockEnd = $i; break }
}
# Strip trailing blanks inside the unreleased block before inserting.
$tail = $blockEnd
while ($tail -gt $unreleasedIdx + 1 -and [string]::IsNullOrWhiteSpace($existingLines[$tail - 1])) {
$tail--
}
$before = if ($tail -gt 0) { $existingLines[0..($tail - 1)] } else { @() }
$after = if ($blockEnd -lt $existingLines.Count) { $existingLines[$blockEnd..($existingLines.Count - 1)] } else { @() }
$merged = @()
$merged += $before
$merged += ''
$merged += ($insertText -split "`n")
if ($after.Count -gt 0) { $merged += $after }
} elseif ($firstDatedIdx -ge 0) {
$before = $existingLines[0..($firstDatedIdx - 1)]
$after = $existingLines[$firstDatedIdx..($existingLines.Count - 1)]
# Ensure exactly one blank between.
while ($before.Count -gt 0 -and [string]::IsNullOrWhiteSpace($before[-1])) {
$before = $before[0..($before.Count - 2)]
}
$merged = @()
$merged += $before
$merged += ''
$merged += ($insertText -split "`n")
$merged += ''
$merged += $after
} else {
# No headings at all; append.
$merged = $existingLines + @('') + ($insertText -split "`n")
}
# Determine line ending of the existing file.
$crlf = $existing.Contains("`r`n")
$eol2 = if ($crlf) { "`r`n" } else { "`n" }
$finalText = ($merged -join $eol2)
if (-not $finalText.EndsWith($eol2)) { $finalText += $eol2 }
[System.IO.File]::WriteAllText($localPath, $finalText)
$portedCount++
}
Write-Host " Ported $portedCount changelog entr$(if ($portedCount -eq 1){'y'}else{'ies'})."
if ($alreadyPresent.Count -gt 0) {
Write-Host " Already present (skipped): $($alreadyPresent -join ', ')"
}
if ($skippedNoFile.Count -gt 0) {
Write-Warning " No CHANGELOG.md found for: $($skippedNoFile -join ', ')"
}
if ($skippedNoEntry.Count -gt 0) {
Write-Warning " No release entry found on $branchRef for: $($skippedNoEntry -join ', ')"
}
# ---------------------------------------------------------------------
# 3. Propagate dependency-versions into pom.xml files.
# ---------------------------------------------------------------------
Write-Host "`n[3/3] Running update_versions.py --skip-readme ..."
$py = (Get-Command python -ErrorAction SilentlyContinue) ?? (Get-Command python3 -ErrorAction SilentlyContinue)
if (-not $py) { throw "python is not on PATH; cannot run update_versions.py." }
& $py.Source 'eng/versioning/update_versions.py' '--skip-readme'
if ($LASTEXITCODE -ne 0) {
throw "update_versions.py failed with exit code $LASTEXITCODE."
}
# ---------------------------------------------------------------------
# Summary.
# ---------------------------------------------------------------------
$status = git status --porcelain
$clCount = ($status | Where-Object { $_ -match 'CHANGELOG\.md' }).Count
$pomCount = ($status | Where-Object { $_ -match 'pom\.xml' }).Count
Write-Host "`nDone."
Write-Host " version_client.txt artifacts bumped : $($changedArtifacts.Count)"
Write-Host " CHANGELOG.md files modified : $clCount"
Write-Host " pom.xml files modified : $pomCount"
Write-Host "`nReview with 'git diff', then commit and push when ready."
}
finally {
Pop-Location
}