Skip to contentSkip to conversation

sequencer: reset saved HEAD OID when picking onto an unborn branch

PATCH In review Wonseok Oh via GitGitGadget 3 participants 3 messages Nov 21 – Nov 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sequencer: reset saved HEAD OID when picking onto an unborn branch

From: Wonseok Oh <[email protected]>

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.

---
sequencer: reset saved HEAD OID when picking onto an unborn branch

Published-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(-)
sequencer.c +6 -3
index 4a1c0e70b8..20bfe00ba3 100644
@@ -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))) {
t/t3510-cherry-pick-sequence.sh +10 -0
index 9400ddfc45..75004bba0c 100755
@@ -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

Signature · 1 line
gitgitgadget
#1 lore raw

Hi Wonseok

Thanks for working on this. I think that as an immediate fix for the use of an uninitialized variable the patch looks good, but I'm not sure the behavior of cherry-pick and revert onto an unborn branch makes much sense.

Quoted 8 lines · Wonseok Oh via GitGitGadget

From: Wonseok Oh <[email protected]>

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".

I had a hard time parsing the second half of that sentence. sequencer_rollback() expects us to have saved the null OID when HEAD points to an unborn branch, but instead we save some random junk because we haven't initialized the 'head' variable. The use of an uninitialized variable comes from 0f974e2124 (cherry-pick: allow to pick to unborn branches, 2016-06-06). Before that commit we refused to cherry-pick onto an unborn branch and so did not use 'head' unless it was initialized.

It is rather strange that when sequencer_rollback() rejects an unborn branch it refuses to remove the sequencer state so that it is impossible to abort a multi-pick cherry-pick started on an unborn branch. It is also strange that we will try and revert a single commit onto an unborn branch but not multiple commits. 7acaaac275 (revert: allow single-pick in the middle of cherry-pick sequence, 2011-12-10) inserted a separate code path for single picks before the check for an unborn branch, so prior that commit we refused to cherry-pick and revert onto an unborn branch regardless of the number of commits. Going further back in history, the check for an unborn branch was introduced by 04d3d3cfc4 (revert: Save data for continuing after conflict resolution, 2011-08-04). The commit message does not mention a reason for introducing that check and therefore changing the behavior but I assume it was to avoid having to support aborting to an unborn branch.

It would be nice to either remove the check for an unborn branch when reverting multiple commits, or move the check so that it covers single picks as well. It would also be good to allow the user to abort when cherry-picking onto a unborn branch. Those changes don't need to be part of this series though, they can always come later.

I see that rollback_is_safe() expects to see a null OID when picking onto an unborn branch, but update_abort_safety_file() writes an empty file in that case. We should be consistent about the way we represent an unborn branch in the state files. Arguably an empty file is a better way of representing an unborn branch because there is no OID.

Thanks

Phillip

Quoted 63 lines

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.

---
sequencer: reset saved HEAD OID when picking onto an unborn branch

Published-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(-)
sequencer.c +6 -3
index 4a1c0e70b8..20bfe00ba3 100644
@@ -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))) {
t/t3510-cherry-pick-sequence.sh +10 -0
index 9400ddfc45..75004bba0c 100755
@@ -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

#2 lore raw

Hi Phillip!

Quoted 7 lines

I had a hard time parsing the second half of that sentence. sequencer_rollback() expects us to have saved the null OID when HEAD points to an unborn branch, but instead we save some random junk because we haven't initialized the 'head' variable. The use of an uninitialized variable comes from 0f974e2124 (cherry-pick: allow to pick to unborn branches, 2016-06-06). Before that commit we refused to cherry-pick onto an unborn branch and so did not use 'head' unless it was initialized.

Thanks for the review and for digging into the history. I'll reword the commit message along these lines and mention 0f974e2124 in the updated patch.

Quoted 5 lines

It would be nice to either remove the check for an unborn branch when reverting multiple commits, or move the check so that it covers single picks as well. It would also be good to allow the user to abort when cherry-picking onto a unborn branch. Those changes don't need to be part of this series though, they can always come later.

Indeed. I'll think about which direction makes more sense and follow up separately.

Quoted 5 lines

I see that rollback_is_safe() expects to see a null OID when picking onto an unborn branch, but update_abort_safety_file() writes an empty file in that case. We should be consistent about the way we represent an unborn branch in the state files. Arguably an empty file is a better way of representing an unborn branch because there is no OID.

Along with the reworded commit message,
should the updated patch also save an empty ".git/sequencer/head" on an unborn branch,
to align with update_abort_safety_file()?
If so, I'm happy to include that change.

Thanks
Wonseok Oh

2026년 11월 23일 (월) 오후 11:32, Phillip Wood <[email protected]>님이 작성:

Quoted 120 lines

Hi Wonseok

Thanks for working on this. I think that as an immediate fix for the use of an uninitialized variable the patch looks good, but I'm not sure the behavior of cherry-pick and revert onto an unborn branch makes much sense.

Quoted 8 lines · Wonseok Oh via GitGitGadget

From: Wonseok Oh <[email protected]>

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".

I had a hard time parsing the second half of that sentence. sequencer_rollback() expects us to have saved the null OID when HEAD points to an unborn branch, but instead we save some random junk because we haven't initialized the 'head' variable. The use of an uninitialized variable comes from 0f974e2124 (cherry-pick: allow to pick to unborn branches, 2016-06-06). Before that commit we refused to cherry-pick onto an unborn branch and so did not use 'head' unless it was initialized.

It is rather strange that when sequencer_rollback() rejects an unborn branch it refuses to remove the sequencer state so that it is impossible to abort a multi-pick cherry-pick started on an unborn branch. It is also strange that we will try and revert a single commit onto an unborn branch but not multiple commits. 7acaaac275 (revert: allow single-pick in the middle of cherry-pick sequence, 2011-12-10) inserted a separate code path for single picks before the check for an unborn branch, so prior that commit we refused to cherry-pick and revert onto an unborn branch regardless of the number of commits. Going further back in history, the check for an unborn branch was introduced by 04d3d3cfc4 (revert: Save data for continuing after conflict resolution, 2011-08-04). The commit message does not mention a reason for introducing that check and therefore changing the behavior but I assume it was to avoid having to support aborting to an unborn branch.

It would be nice to either remove the check for an unborn branch when reverting multiple commits, or move the check so that it covers single picks as well. It would also be good to allow the user to abort when cherry-picking onto a unborn branch. Those changes don't need to be part of this series though, they can always come later.

I see that rollback_is_safe() expects to see a null OID when picking onto an unborn branch, but update_abort_safety_file() writes an empty file in that case. We should be consistent about the way we represent an unborn branch in the state files. Arguably an empty file is a better way of representing an unborn branch because there is no OID.

Thanks

Phillip

Quoted 63 lines

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.

---
sequencer: reset saved HEAD OID when picking onto an unborn branch

Published-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(-)
sequencer.c +6 -3
index 4a1c0e70b8..20bfe00ba3 100644
@@ -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))) {
t/t3510-cherry-pick-sequence.sh +10 -0
index 9400ddfc45..75004bba0c 100755
@@ -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