1919Changelog release helper for Apache Solr release managers.
2020
2121Normally invoked by the Release Wizard; see dev-docs/changelog.adoc for details.
22+
23+ Requires git 2.23 or later (git restore).
2224"""
2325
2426import argparse
@@ -78,13 +80,83 @@ def has_staged_changes(git_root, paths=None):
7880 return bool (r .stdout .strip ())
7981
8082
83+ def git_paths (args , git_root ):
84+ """Run a git command that lists paths, NUL-separated so non-ASCII names are not quoted."""
85+ r = subprocess .run (
86+ ["git" ] + args + ["-z" ],
87+ cwd = git_root , capture_output = True , text = True , check = True ,
88+ )
89+ return [p for p in r .stdout .split ("\0 " ) if p ]
90+
91+
8192def commit_touches_unreleased (sha , git_root ):
8293 """Return True if the given commit modifies any file under changelog/unreleased/."""
94+ files = git_paths (["diff-tree" , "--no-commit-id" , "-r" , "--name-only" , sha ], git_root )
95+ return any (f .startswith ("changelog/unreleased/" ) for f in files )
96+
97+
98+ def unmerged_paths (git_root ):
99+ return git_paths (["diff" , "--name-only" , "--diff-filter=U" ], git_root )
100+
101+
102+ def recover_cherry_pick (git_root , version , dry_run = False ):
103+ """Try to finish a cherry-pick that stopped. Return True if it was resolved.
104+
105+ Handles two cases: conflicts confined to changelog/unreleased/ entries that
106+ are being released (a counterpart exists in changelog/v{version}/, so the
107+ unreleased entry is removed), and a pick that is empty because its changes
108+ are already on this branch. Any other conflict is left for the user.
109+ """
110+ if dry_run :
111+ return False
112+ in_progress = subprocess .run (
113+ ["git" , "rev-parse" , "-q" , "--verify" , "CHERRY_PICK_HEAD" ],
114+ cwd = git_root , capture_output = True ,
115+ ).returncode == 0
116+ if not in_progress :
117+ return False
118+ conflicts = unmerged_paths (git_root )
119+ version_dir = git_root / "changelog" / f"v{ version } "
120+ for p in conflicts :
121+ if not p .startswith ("changelog/unreleased/" ):
122+ return False
123+ if not (version_dir / Path (p ).name ).exists ():
124+ print (f" { p } conflicts but has no counterpart in { version_dir .relative_to (git_root )} / — "
125+ f"not removing it." )
126+ return False
127+ if conflicts :
128+ print (f" Removing { len (conflicts )} conflicting unreleased file(s): "
129+ f"{ ', ' .join (Path (p ).name for p in conflicts )} " )
130+ git (["rm" , "--quiet" , "--" ] + conflicts , cwd = git_root )
131+ if has_staged_changes (git_root ):
132+ git (["-c" , "core.editor=true" , "cherry-pick" , "--continue" ], cwd = git_root )
133+ else :
134+ print (" Nothing left to apply — skipping." )
135+ git (["cherry-pick" , "--skip" ], cwd = git_root )
136+ return True
137+
138+
139+ def require_clean_tree (git_root ):
140+ """Exit unless the working tree has no uncommitted changes to tracked files."""
83141 r = subprocess .run (
84- ["git" , "diff-tree " , "--no-commit-id " , "-r" , "--name-only" , sha ],
142+ ["git" , "status " , "--porcelain " , "--untracked-files=no" ],
85143 cwd = git_root , capture_output = True , text = True , check = True ,
86144 )
87- return any (f .startswith ("changelog/unreleased/" ) for f in r .stdout .splitlines ())
145+ if r .stdout .strip ():
146+ print ("Error: the working tree has uncommitted changes. Commit or stash them first:" ,
147+ file = sys .stderr )
148+ print (r .stdout , file = sys .stderr , end = "" )
149+ sys .exit (1 )
150+
151+
152+ def discard_generated_changelog_files (git_root , dry_run = False ):
153+ """Drop what logchangeGenerate left under changelog/ once the wanted files are committed.
154+
155+ Generation rewrites every version's version-summary.md; a modified or
156+ untracked copy for another version would block the next checkout.
157+ """
158+ git (["restore" , "changelog/" ], cwd = git_root , dry_run = dry_run )
159+ git (["clean" , "--force" , "--" , "changelog/*/version-summary.md" ], cwd = git_root , dry_run = dry_run )
88160
89161
90162def strip_unreleased_block (changelog_path : Path , dry_run = False ):
@@ -322,9 +394,14 @@ def cmd_forward_port(args, git_root):
322394 print (" (push disabled — run with --push when ready)" )
323395 print ()
324396
325- # Step 1: checkout release branch and write release-date.txt
326- print (f"[1] Checking out { release_branch } and writing release-date.txt" )
397+ # The script checks out several branches and discards generated files with
398+ # git restore, so refuse to run on top of uncommitted work.
399+ require_clean_tree (git_root )
400+
401+ # Step 1: checkout and update release branch, write release-date.txt
402+ print (f"[1] Checking out and updating { release_branch } , writing release-date.txt" )
327403 git (["checkout" , release_branch ], cwd = git_root , dry_run = dry_run )
404+ git (["pull" , "--ff-only" , args .git_remote , release_branch ], cwd = git_root , dry_run = dry_run )
328405
329406 if not dry_run and not version_dir .exists ():
330407 print (f"Error: version folder { version_dir } does not exist. "
@@ -362,31 +439,41 @@ def cmd_forward_port(args, git_root):
362439 msg_b = f"Regenerate CHANGELOG.md for v{ version } "
363440 print (f" Committing: { msg_b !r} " )
364441 git (["commit" , "-m" , msg_b ], cwd = git_root , dry_run = dry_run )
442+ discard_generated_changelog_files (git_root , dry_run = dry_run )
365443
366444 # Steps 4+5: for each target branch, find commits on release_branch not yet on
367445 # that target, cherry-pick them, then regenerate CHANGELOG.md fresh.
446+ # Only commits touching the version folder are considered: the
447+ # release branch may have diverged from the target long ago (e.g.
448+ # a 9.x release vs main), so other changelog/ commits are feature
449+ # commits that don't belong on the target.
368450 # CHANGELOG.md is never cherry-picked — it is always regenerated so
369451 # each branch gets a correct full-history version (avoids cross-major
370452 # conflicts). version-summary.md is also excluded from cherry-picks
371453 # for the same reason; it is regenerated alongside CHANGELOG.md.
372- # --cherry-pick with the symmetric-difference range (three dots) omits
373- # commits whose patch is already present on the target, making
374- # forward-port idempotent when re-run after an initial no-push run.
454+ # --cherry-pick is deliberately not used: git would compute patch-ids
455+ # for the whole symmetric difference (thousands of commits across
456+ # majors) before path limiting, taking minutes per target. A commit
457+ # already applied to the target instead cherry-picks to an empty
458+ # change, which recover_cherry_pick skips, so re-running after an
459+ # initial no-push run remains idempotent.
375460 targets = [stable_branch , "main" ]
376461 if latest_lts_stable_branch :
377462 targets .append (latest_lts_stable_branch )
378463
464+ version_pathspec = f"changelog/v{ version } /"
379465 for target in targets :
380- print (f"\n [4] Finding changelog/ commits on { release_branch } not yet on { target } " )
466+ print (f"\n [4] Updating { target } and finding { version_pathspec } commits on { release_branch } not yet on it" )
467+ git (["checkout" , target ], cwd = git_root , dry_run = dry_run )
468+ git (["pull" , "--ff-only" , args .git_remote , target ], cwd = git_root , dry_run = dry_run )
381469 if dry_run :
382- print (f" (dry-run) would run: git log --cherry-pick -- right-only { target } ...{ release_branch } -- changelog/ :(exclude)changelog/*/version-summary.md" )
470+ print (f" (dry-run) would run: git log --right-only { target } ...{ release_branch } -- { version_pathspec } :(exclude)changelog/*/version-summary.md" )
383471 commits = []
384472 else :
385473 result = subprocess .run (
386- ["git" , "log" , "--oneline" , "--reverse" ,
387- "--cherry-pick" , "--right-only" ,
474+ ["git" , "log" , "--oneline" , "--reverse" , "--right-only" ,
388475 f"{ target } ...{ release_branch } " ,
389- "--" , "changelog/" , ":(exclude)changelog/*/version-summary.md" ],
476+ "--" , version_pathspec , ":(exclude)changelog/*/version-summary.md" ],
390477 cwd = git_root , capture_output = True , text = True , check = True ,
391478 )
392479 commits = [line .split ()[0 ] for line in result .stdout .strip ().splitlines () if line ]
@@ -397,20 +484,21 @@ def cmd_forward_port(args, git_root):
397484 print (f" Found { len (commits )} commit(s) to cherry-pick: { ', ' .join (commits )} " )
398485
399486 print (f"\n [5] Cherry-picking { len (commits )} commit(s) to { target } " )
400- git (["checkout" , target ], cwd = git_root , dry_run = dry_run )
401487 for sha in commits :
402- # Commits that touch changelog/unreleased/ are deletions of files
403- # that exist under different names on stable/main — use -X ours so
404- # git keeps the target branch's own unreleased entries rather than
405- # trying to delete them. Commits that only add to the version
406- # folder are clean additions and cherry-pick without strategy options .
488+ # Commits that touch changelog/unreleased/ move entries into the
489+ # version folder. -X no-renames makes those plain adds + deletes;
490+ # -X ours settles content conflicts in favour of the target. A
491+ # deletion of an entry that differs on the target still conflicts
492+ # (modify/delete), which recover_cherry_pick resolves .
407493 if commit_touches_unreleased (sha , git_root ):
408494 cp_args = ["cherry-pick" , "-X" , "ours" , "-X" , "no-renames" , sha ]
409495 else :
410496 cp_args = ["cherry-pick" , sha ]
411497 try :
412498 git (cp_args , cwd = git_root , dry_run = dry_run )
413499 except subprocess .CalledProcessError :
500+ if recover_cherry_pick (git_root , version , dry_run = dry_run ):
501+ continue
414502 print (f"\n Error: cherry-pick of { sha } failed on { target } ." ,
415503 file = sys .stderr )
416504 print (" Resolve the conflict, then run: git cherry-pick --continue" ,
@@ -427,8 +515,7 @@ def cmd_forward_port(args, git_root):
427515 cwd = git_root , dry_run = dry_run )
428516
429517 # Remove any v{version} YAML files that still linger in unreleased/ on this
430- # target branch. Cherry-pick uses -X ours, so a "we modified / they deleted"
431- # conflict silently keeps the local copy; this explicit pass fixes that.
518+ # target branch, e.g. entries the release branch never had in unreleased/.
432519 version_dir = git_root / f"changelog/v{ version } "
433520 stale = (
434521 [
@@ -455,6 +542,7 @@ def cmd_forward_port(args, git_root):
455542 if not dry_run and has_staged_changes (git_root , ["CHANGELOG.md" , version_summary ]):
456543 git (["commit" , "-m" , f"Regenerate CHANGELOG.md with v{ version } entries on { target } " ],
457544 cwd = git_root , dry_run = dry_run )
545+ discard_generated_changelog_files (git_root , dry_run = dry_run )
458546
459547 # Step 6: push (optional)
460548 if do_push :
@@ -522,7 +610,8 @@ def build_parser():
522610 help = "Set release date and forward-port changelog post-vote" ,
523611 description = "Run once after a successful vote.\n "
524612 "Writes the release date to the version folder and regenerates CHANGELOG.md.\n "
525- "Then cherry-picks all changelog commits from the release branch to the stable branch and main.\n "
613+ "Then cherry-picks the commits touching changelog/v<version>/ from the release branch to the stable branch and main.\n "
614+ "Requires a clean working tree; each local branch is fast-forwarded from the remote before committing.\n "
526615 "By default nothing is pushed; review first and pass --push when satisfied." ,
527616 formatter_class = fmt )
528617 fp .add_argument ("--stable-branch" , required = True ,
0 commit comments