holdenk commented on code in PR #60471:
URL: https://github.com/apache/spark/pull/60471#discussion_r4577031081
##########
dev/merge_spark_pr.py:
##########
@@ -1010,19 +1043,137 @@ class SkipCherryPick(Exception):
"""
-def continue_maybe(prompt, cherry=False):
+def continue_maybe(prompt: str, cherry: bool = False) -> None:
+ """Prompt to continue; on "no" either skip this cherry-pick (`cherry`) or
exit the script.
+
+ `cherry` is the decline semantics, not a statement that a cherry-pick is
mid-flight: the
+ pre-push checks run after the pick is committed, and declining there must
still skip only
+ that branch. So the abort is attempted only when git says one is actually
in progress.
+
+ >>> from contextlib import redirect_stdout
+ >>> from io import StringIO
+ >>> from unittest.mock import call, patch
+
+ Declining with `cherry` while a pick is mid-flight aborts the pick before
skipping --
+ the first git call is the probe, the second the abort:
+
+ >>> with (
+ ... patch("builtins.input", return_value=""),
+ ... patch.object(git, "run", side_effect=["", ""]) as run,
+ ... redirect_stdout(StringIO()),
+ ... ):
+ ... try:
+ ... continue_maybe("Resolved?", cherry=True)
+ ... skipped = False
+ ... except SkipCherryPick:
+ ... skipped = True
+ >>> skipped
+ True
+ >>> run.call_args_list[0]
+ call(['git', 'rev-parse', '--verify', '--quiet', 'CHERRY_PICK_HEAD'])
+ >>> run.call_args_list[1]
+ call('git cherry-pick --abort')
+ >>> run.call_count
+ 2
+
+ After the pick is committed there is nothing to abort. A checkout git
refuses over
+ uncommitted edits waits for the committer to stash them, then the branch
is skipped:
+
+ >>> refused = subprocess.CalledProcessError(1, "git checkout")
+ >>> with (
+ ... patch("builtins.input", side_effect=["", ""]) as inp,
+ ... patch.object(git, "run", side_effect=[refused, refused, ""]) as
run,
+ ... patch.dict(continue_maybe.__globals__, original_head="master"),
+ ... patch.object(sys.modules[__name__], "clean_up") as clean,
+ ... redirect_stdout(StringIO()),
+ ... ):
+ ... try:
+ ... continue_maybe("Push anyway?", cherry=True)
+ ... skipped = False
+ ... except SkipCherryPick:
+ ... skipped = True
+ >>> skipped
+ True
+ >>> [c.args[0] for c in run.call_args_list[1:]]
+ [['git', 'checkout', 'master'], ['git', 'checkout', 'master']]
+ >>> inp.call_count, clean.call_count
+ (2, 1)
+
+ A checkout git keeps refusing, e.g. over a stale index.lock, can be given
up on:
+
+ >>> with (
+ ... patch("builtins.input", side_effect=["", "a"]),
+ ... patch.object(git, "run", side_effect=[refused, refused]),
+ ... patch.dict(continue_maybe.__globals__, original_head="master"),
+ ... redirect_stdout(StringIO()),
+ ... ):
+ ... try:
+ ... continue_maybe("Push anyway?", cherry=True)
+ ... exited = False
+ ... except SystemExit:
+ ... exited = True
+ >>> exited
+ True
+ """
if get_input(f"{prompt} (y/N): ", ["y", "n", ""]) != "y":
if cherry:
- try:
- git.run("git cherry-pick --abort")
- except Exception:
- print_error("Unable to abort and get back to the state before
cherry-pick")
+ if _cherry_pick_in_progress():
+ try:
+ git.run("git cherry-pick --abort")
+ except Exception:
+ print_error("Unable to abort and get back to the state
before cherry-pick")
+ else:
+ _restore_original_head()
print("Skipping this cherry-pick; the merge continues.")
clean_up()
raise SkipCherryPick()
fail("Okay, exiting")
+def _cherry_pick_in_progress() -> bool:
+ """True if the repo is mid-cherry-pick. With --quiet, exit 1 is git's "no
such ref" answer,
+ i.e. no pick in progress. Any other trouble answers "yes", so the caller
still attempts
+ the abort, as if a pick were in progress.
+
+ >>> from unittest.mock import patch
+ >>> answers = []
+ >>> for failure in (None, subprocess.CalledProcessError(1, "g"),
+ ... subprocess.CalledProcessError(128, "g"), OSError("no
git")):
+ ... with patch.object(git, "run", side_effect=failure):
+ ... answers.append(_cherry_pick_in_progress())
+ >>> answers
+ [True, False, True, True]
+ """
+ try:
+ git.run(["git", "rev-parse", "--verify", "--quiet",
"CHERRY_PICK_HEAD"])
+ return True
+ except subprocess.CalledProcessError as e:
+ return e.returncode != 1
+ except Exception:
+ return True
+
+
+def _restore_original_head():
+ """Check out original_head, waiting for the committer while git refuses
over local edits.
+
+ The committed pick is discarded by clean_up() afterwards; the edits are
theirs to keep, and
+ any git carries over to original_head are listed."""
+ if "original_head" not in globals():
+ return
+ while True:
+ try:
+ carried = git.run(["git", "checkout", original_head]).strip()
+ if carried:
+ print("Uncommitted changes carried over to %s:\n%s" %
(original_head, carried))
+ return
+ except subprocess.CalledProcessError:
+ # git already printed why it refused.
+ prompt = "Stash or discard those changes elsewhere, then Enter to
retry, or [a]bort: "
+ if get_input(prompt, ["", "a"]) == "a":
+ print_error("Okay, exiting; local edits and temporary branches
are preserved.")
+ raise _PreserveLocalState(-1)
Review Comment:
Fixed in 35d8b26c500 with the flag you suggested: `[a]bort` sets
`preserve_local_state` and `clean_up()` returns without touching git, so every
route out keeps the pick. That covers the CLI handler, a `fail()` from
`main()`'s `finally` blocks, and a second error there. The flag made
`_PreserveLocalState` and its special case in the entry point redundant, so
both are gone. `abort_entrypoint` now drives an abort straight out, an abort
followed by a `fail()` in a `finally`, and an ordinary exit. It asserts the
first two run no git after the refused checkout, that the ordinary exit still
cleans up, and that the startup self-test leaves the flag off.
---
<sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of
@holdenk.</sub>
<!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk
-->
##########
dev/merge_spark_pr.py:
##########
@@ -1270,6 +1815,9 @@ def _do_cherry_pick(pr_num, merge_hash, pick_ref):
if updated_message != raw_message:
amend_commit_message(updated_message)
+ maybe_binding_policy_warning(pick_ref, pick_head)
Review Comment:
Fixed in 35d8b26c500, in a smaller way than the full repair plan:
`cherry_pick` takes a `record` callback and calls it as each pick returns, and
`main()` builds its refs and commits from those calls. A branch-M.x pick
therefore reaches the merge comment and JIRA even when the branch-M.N half ends
the run. The doctest drives exactly that case. A push whose own cleanup then
fails is still not recorded; I left that out of scope.
---
<sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of
@holdenk.</sub>
<!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk
-->
##########
dev/merge_spark_pr.py:
##########
@@ -1010,19 +1043,137 @@ class SkipCherryPick(Exception):
"""
-def continue_maybe(prompt, cherry=False):
+def continue_maybe(prompt: str, cherry: bool = False) -> None:
+ """Prompt to continue; on "no" either skip this cherry-pick (`cherry`) or
exit the script.
+
+ `cherry` is the decline semantics, not a statement that a cherry-pick is
mid-flight: the
+ pre-push checks run after the pick is committed, and declining there must
still skip only
+ that branch. So the abort is attempted only when git says one is actually
in progress.
Review Comment:
Fixed in 35d8b26c500, taking the suggestion.
---
<sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of
@holdenk.</sub>
<!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk
-->
##########
dev/merge_spark_pr.py:
##########
@@ -1010,19 +1043,137 @@ class SkipCherryPick(Exception):
"""
-def continue_maybe(prompt, cherry=False):
+def continue_maybe(prompt: str, cherry: bool = False) -> None:
+ """Prompt to continue; on "no" either skip this cherry-pick (`cherry`) or
exit the script.
+
+ `cherry` is the decline semantics, not a statement that a cherry-pick is
mid-flight: the
+ pre-push checks run after the pick is committed, and declining there must
still skip only
+ that branch. So the abort is attempted only when git says one is actually
in progress.
+
+ >>> from contextlib import redirect_stdout
+ >>> from io import StringIO
+ >>> from unittest.mock import call, patch
+
+ Declining with `cherry` while a pick is mid-flight aborts the pick before
skipping --
+ the first git call is the probe, the second the abort:
+
+ >>> with (
+ ... patch("builtins.input", return_value=""),
+ ... patch.object(git, "run", side_effect=["", ""]) as run,
+ ... redirect_stdout(StringIO()),
+ ... ):
+ ... try:
+ ... continue_maybe("Resolved?", cherry=True)
+ ... skipped = False
+ ... except SkipCherryPick:
+ ... skipped = True
+ >>> skipped
+ True
+ >>> run.call_args_list[0]
+ call(['git', 'rev-parse', '--verify', '--quiet', 'CHERRY_PICK_HEAD'])
+ >>> run.call_args_list[1]
+ call('git cherry-pick --abort')
+ >>> run.call_count
+ 2
+
+ After the pick is committed there is nothing to abort. A checkout git
refuses over
+ uncommitted edits waits for the committer to stash them, then the branch
is skipped:
+
+ >>> refused = subprocess.CalledProcessError(1, "git checkout")
+ >>> with (
+ ... patch("builtins.input", side_effect=["", ""]) as inp,
+ ... patch.object(git, "run", side_effect=[refused, refused, ""]) as
run,
+ ... patch.dict(continue_maybe.__globals__, original_head="master"),
+ ... patch.object(sys.modules[__name__], "clean_up") as clean,
+ ... redirect_stdout(StringIO()),
+ ... ):
+ ... try:
+ ... continue_maybe("Push anyway?", cherry=True)
+ ... skipped = False
+ ... except SkipCherryPick:
+ ... skipped = True
+ >>> skipped
+ True
+ >>> [c.args[0] for c in run.call_args_list[1:]]
+ [['git', 'checkout', 'master'], ['git', 'checkout', 'master']]
+ >>> inp.call_count, clean.call_count
+ (2, 1)
+
+ A checkout git keeps refusing, e.g. over a stale index.lock, can be given
up on:
+
+ >>> with (
+ ... patch("builtins.input", side_effect=["", "a"]),
+ ... patch.object(git, "run", side_effect=[refused, refused]),
+ ... patch.dict(continue_maybe.__globals__, original_head="master"),
+ ... redirect_stdout(StringIO()),
+ ... ):
+ ... try:
+ ... continue_maybe("Push anyway?", cherry=True)
+ ... exited = False
+ ... except SystemExit:
+ ... exited = True
+ >>> exited
+ True
+ """
if get_input(f"{prompt} (y/N): ", ["y", "n", ""]) != "y":
if cherry:
- try:
- git.run("git cherry-pick --abort")
- except Exception:
- print_error("Unable to abort and get back to the state before
cherry-pick")
+ if _cherry_pick_in_progress():
+ try:
+ git.run("git cherry-pick --abort")
+ except Exception:
+ print_error("Unable to abort and get back to the state
before cherry-pick")
+ else:
+ _restore_original_head()
print("Skipping this cherry-pick; the merge continues.")
clean_up()
raise SkipCherryPick()
fail("Okay, exiting")
+def _cherry_pick_in_progress() -> bool:
+ """True if the repo is mid-cherry-pick. With --quiet, exit 1 is git's "no
such ref" answer,
+ i.e. no pick in progress. Any other trouble answers "yes", so the caller
still attempts
+ the abort, as if a pick were in progress.
+
+ >>> from unittest.mock import patch
+ >>> answers = []
+ >>> for failure in (None, subprocess.CalledProcessError(1, "g"),
+ ... subprocess.CalledProcessError(128, "g"), OSError("no
git")):
+ ... with patch.object(git, "run", side_effect=failure):
+ ... answers.append(_cherry_pick_in_progress())
+ >>> answers
+ [True, False, True, True]
+ """
+ try:
+ git.run(["git", "rev-parse", "--verify", "--quiet",
"CHERRY_PICK_HEAD"])
+ return True
+ except subprocess.CalledProcessError as e:
+ return e.returncode != 1
+ except Exception:
+ return True
+
+
+def _restore_original_head():
+ """Check out original_head, waiting for the committer while git refuses
over local edits.
+
+ The committed pick is discarded by clean_up() afterwards; the edits are
theirs to keep, and
+ any git carries over to original_head are listed."""
+ if "original_head" not in globals():
+ return
+ while True:
+ try:
+ carried = git.run(["git", "checkout", original_head]).strip()
Review Comment:
Fixed in 35d8b26c500: the restore runs `git checkout --quiet` and lists
carried edits from `git status --porcelain --untracked-files=no`. The doctest
pins both commands, and dropping `--quiet` fails it.
---
<sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of
@holdenk.</sub>
<!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]