[PATCH] sequencer: reset saved HEAD OID when picking onto an unborn branch
- From
- Wonseok Oh via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Nov 21, 2026, 06:30 UTC
- Message-ID
- <pull.2317.git.1795242633923.gitgitgadget@gmail.com>
From: Wonseok Oh <marinelay7@gmail.com>
When picking multiple commits, "git cherry-pick" stores the current HEAD in .git/sequencer/head for "--abort" to return to. If the branch is unborn, repo_get_oid() fails and leaves the object ID uninitialized, but sequencer_pick_revisions() saves it anyway instead of clearing it to the null OID that sequencer_rollback() checks for to report "cannot abort from a branch yet to be born".
As a result, "--abort" errors out with "Could not parse object" in my master build, while in Debian's 2.47.3 build the uninitialized memory happened to hold the last picked commit's OID and moved the new branch there.
Clear the object ID to the null OID when HEAD cannot be resolved, and add a test.
Signed-off-by: Wonseok Oh <marinelay7@gmail.com>
---
sequencer: reset saved HEAD OID when picking onto an unborn branchPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2317%2Fmarinelay%2Fcherry-pick-unborn-abort-v1 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2317/marinelay/cherry-pick-unborn-abort-v1 Pull-Request: https://github.com/gitgitgadget/git/pull/2317
sequencer.c | 9 ++++++--- t/t3510-cherry-pick-sequence.sh | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/sequencer.c b/sequencer.c index 4a1c0e70b8..20bfe00ba3 100644 --- a/sequencer.c +++ b/sequencer.c @@ -5788,9 +5788,12 @@ int sequencer_pick_revisions(struct repository *r, goto out; } - if (repo_get_oid(r, "HEAD", &oid) && (opts->action == REPLAY_REVERT)) { - res = error(_("can't revert as initial commit")); - goto out; + if (repo_get_oid(r, "HEAD", &oid)) { + if (opts->action == REPLAY_REVERT) { + res = error(_("can't revert as initial commit")); + goto out; + } + oidclr(&oid, r->hash_algo); } if (save_head(oid_to_hex(&oid))) { diff --git a/t/t3510-cherry-pick-sequence.sh b/t/t3510-cherry-pick-sequence.sh index 9400ddfc45..75004bba0c 100755 --- a/t/t3510-cherry-pick-sequence.sh +++ b/t/t3510-cherry-pick-sequence.sh @@ -269,6 +269,16 @@ test_expect_success 'allow skipping commit but not abort for a new history' ' test_cmp expect advice ' +test_expect_success 'cannot abort a cherry-pick sequence on a new history' ' + pristine_detach initial && + git checkout --orphan new_disconnected_seq && + git reset --hard && + test_must_fail git cherry-pick base..anotherpick && + test_must_fail git cherry-pick --abort 2>advice && + test_grep "cannot abort from a branch yet to be born" advice && + test_must_fail git rev-parse --verify HEAD +' + test_expect_success 'allow skipping stopped cherry-pick because of untracked file modifications' ' test_when_finished "rm unrelated" && pristine_detach initial && base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4
-- gitgitgadget