Skip to contentSkip to conversation

cmd given to "rebase -x cmd" cannot see the original HEAD?

BUG In review Junio C Hamano 3 participants 8 messages Mar 11, 2021 – Mar 12, 2021

lore
Share & tools
lore
#0 lore raw

I was trying to figure out why I am losing the amlog note that maps each commit on topics to the message ID after amending them with extra trailers (e.g. Reviewed-by). I use a script that runs "git commit --amend" to do this and give it to "rebase -x".

It seems that when the command given to "rebase -x" is run, HEAD is already updated to a commit with tree and log message identical to the original, with updated committer information. However, the note attached to the original commit is *not* copied to this intermediate HEAD that is shown to the external command.

Hence, even if I try to read the existing note from HEAD that is given to the script spawned via "rebase -x cmd" mechanism, the script cannot copy the note to the resulting commit after it adds extra trailers. Also, there is no mechansim for the external command to learn where the original HEAD was.

I see a few possible avenues to fix this.

1. Before "rebase -x" mechanism spawns an external command, make sure that the "copy notes upon rewriting" logic has triggered, so that the HEAD observable by the command has the notes from the original. It seems that the "copy notes" logic is only used at the end, but it does not seem to take the possibility into account that the external command might move HEAD to a different commit (i.e. it starts from HEAD0, creates HEAD1 that is different from HEAD0 only for committer info, hands HEAD1 to external command that may move HEAD to HEAD2, but then copies the note from HEAD0 to HEAD1 because it is unaware of HEAD2), so it only annotates HEAD1 that is immediately lost (nothing other than HEAD's reflog would be pointing at it). [*]

2. Make sure the "copy notes" logic copies the notes taken from the original HEAD to the HEAD _after_ "rebase -x" external command returns.

The first approach would still make the external command responsible for copying notes from HEAD it sees (i.e. HEAD1) to the new commit it creates and points HEAD at (i.e. HEAD2). It _could_ be used as a mechanism to allow the external command to modify/remove notes.

The second approach would forbid the external command from mucking with notes at all. "rebase" would first remember the original HEAD (i.e. HEAD0), does its thing (i.e. creates HEAD1 and points HEAD at it, and then lets the external command create HEAD2 and point HEAD at it), and then figure out the latest HEAD and copy notes from the original HEAD. It is simpler, works with scripts that uses things like "commit --amend" that do not copy notes, but less flexible.

Implementation-wise, it seems that the latter would require more significant surgery to do_pick_commit() codepath, but somebody more familiar with the sequencer codepath should research to find the best approach.

I suspect that "post-commit" hook that is run from try_to_commit() may have the same issue, here in sequencer.c

1539: run_commit_hook(0, r->index_file, "post-commit", NULL);
1540: if (flags & AMEND_MSG)
1541: commit_post_rewrite(r, current_head, oid);

where oid is not adjusted by re-reading what the post-commit hook did to our HEAD.

But the codepath to reach do_exec() (which is what spawns the external command for "rebase -x cmd") is quite away from any call to commit_post_rewrite(),

[footnote]

I have a 6-commit series with notes/amlog record on each commit, and running:

$ git rebase -x '
	git rev-parse HEAD
	git notes --ref=notes/amlog show HEAD || :
' master $(git rev-parse ab/read-tree)

yields the following:

Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
669950d7def2e849cb1ee6e7b3a1beac5c45ce1c
error: no note found for object 669950d7def2e849cb1ee6e7b3a1beac5c45ce1c.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
0545e0a567f96eff14dfb93b44aecf9683b44803
error: no note found for object 0545e0a567f96eff14dfb93b44aecf9683b44803.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
d9b4e77efe5e65952e05533ebf500b559408d436
error: no note found for object d9b4e77efe5e65952e05533ebf500b559408d436.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
6d8735372c0c9b9d3793959157456cd445d025fa
error: no note found for object 6d8735372c0c9b9d3793959157456cd445d025fa.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
b86c1305258a11f42927f924498a94b3ffb19472
error: no note found for object b86c1305258a11f42927f924498a94b3ffb19472.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
7da85bae9f11ab14d616bcade1fb2a7b434aa716
error: no note found for object 7da85bae9f11ab14d616bcade1fb2a7b434aa716. Successfully rebased and updated detached HEAD.

We can see each commit on ab/read-tree has notes/amlog entry that records where it came from:

$ git log --notes=notes/amlog --oneline --reverse master..ab/read-tree
2e3e38a4ad ls-files tests: add meaningful --with-tree tests
Notes (amlog):
    Message-Id: <[email protected]>

2371fda438 tree.c API: move read_tree() into builtin/ls-files.c Notes (amlog):

    Message-Id: <[email protected]>

32c718db53 ls-files: don't needlessly pass around stage variable Notes (amlog):

    Message-Id: <[email protected]>

2ebabd63f7 ls-files: refactor away read_tree()
Notes (amlog):

    Message-Id: <[email protected]>

e98fb6ddc7 tree.h API: remove support for starting at prefix != "" Notes (amlog):

    Message-Id: <[email protected]>

8e88702431 tree.h API: remove "stage" parameter from read_tree_recursive() Notes (amlog):

    Message-Id: <[email protected]>

In the above transcript, 669950d7def2e8 (the first commit that the "rebase -x" command saw) corresponds to 2e3e38a4ad (the first commit in the series), and we can see the only difference between them is the committer info. As we already saw in the above, the original has a note that is not copied to the new one when "rebase -x" command runs.

$ git show --pretty=fuller 2e3e38a4ad >old
$ git show --pretty=fuller 669950d7def2e8 >new
$ diff -u old new
--- old	2021-03-11 12:06:33.259434213 -0800
+++ new	2021-03-11 12:06:33.259434213 -0800
@@ -1,8 +1,8 @@
-commit 2e3e38a4ad6d9d78381e90348289277acb4c6f8b
+commit 669950d7def2e849cb1ee6e7b3a1beac5c45ce1c
 Author:     Ævar Arnfjörð Bjarmason <[email protected]>
 AuthorDate: Mon Mar 8 03:21:33 2021 +0100
 Commit:     Junio C Hamano <[email protected]>
-CommitDate: Mon Mar 8 10:06:35 2021 -0800
+CommitDate: Thu Mar 11 12:01:11 2021 -0800
     ls-files tests: add meaningful --with-tree tests

HOWEVER, after that failed "rebase -x" session, the note is copied to these "intermediate" commits. Using 7da85bae9f1 which is at the tip of the history fed to "rebase -x" external command, we can see:

$ git log --notes=notes/amlog --oneline --reverse master..7da85bae9f1

669950d7de ls-files tests: add meaningful --with-tree tests Notes (amlog):

    Message-Id: <[email protected]>

0545e0a567 tree.c API: move read_tree() into builtin/ls-files.c Notes (amlog):

    Message-Id: <[email protected]>

d9b4e77efe ls-files: don't needlessly pass around stage variable Notes (amlog):

    Message-Id: <[email protected]>

6d8735372c ls-files: refactor away read_tree()
Notes (amlog):

    Message-Id: <[email protected]>

b86c130525 tree.h API: remove support for starting at prefix != "" Notes (amlog):

    Message-Id: <[email protected]>

7da85bae9f tree.h API: remove "stage" parameter from read_tree_recursive() Notes (amlog):

    Message-Id: <[email protected]>

It is only that they seem to be added _after_ the external command finishes, which is not a very useful behaviour.

#1 lore raw

cover "rebase -x cmd" loses notes

So here is a semi-failed attempt to fix the issue I encountered earlier, cf. https://lore.kernel.org/git/[email protected]/
where "git rebase" carries forward notes attached to the commits in the original history to their equivalents in the rewritten history, but "git rebase -x cmd" does not when cmd modifies the given HEAD commit.

Junio C Hamano (2):
sequencer.c: make commit_post_rewrite() take two object names [WIP] sequencer.c: carry forward notes on HEAD across "rebase -x"

 builtin/commit.c              |  6 +++---
 sequencer.c                   | 19 +++++++++++++------
 sequencer.h                   |  2 +-
 t/t3404-rebase-interactive.sh | 18 ++++++++++++++++++
 4 files changed, 35 insertions(+), 10 deletions(-)
Signature · 1 line
2.31.0-rc2-175-g3820f1c72e
#2 lore raw

PATCH 1/2 sequencer.c: make commit_post_rewrite() take two object names

The helper function is to copy notes between two commits by taking the original one and a new one that is supposed to be a rewrite of the original one. It somehow takes "struct commit *" for the former but "struct object_id *" for the latter, but what it does does not need access to the in-core commit object.

Change the function to take two "struct object_id *" instead.

As we require "struct object_id *" for the original commit now, the comment on the old "struct commit *" that must be non-NULL which made taking the address of its object.oid member safe no longer is relevant to this function (it is the caller's concern now), so remove it. Two callers we have are of course safe, and there is no reason to call this "as we rewrote A to B, please copy notes on A to B, too" helper if the caller knows A is NULL, so the comment would have very little value even if it were kept.

---
 builtin/commit.c | 6 +++---
 sequencer.c      | 9 ++++-----
 sequencer.h      | 2 +-
 3 files changed, 8 insertions(+), 9 deletions(-)
builtin/commit.c +3 -3
index 739110c5a7..f2fbef053e 100644
@@ -1702,9 +1702,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	repo_rerere(the_repository, 0);
 	run_auto_maintenance(quiet);
 	run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
-	if (amend && !no_post_rewrite) {
-		commit_post_rewrite(the_repository, current_head, &oid);
-	}
+	if (amend && !no_post_rewrite)
+		commit_post_rewrite(the_repository, &current_head->object.oid, &oid);
+
 	if (!quiet) {
 		unsigned int flags = 0;
 
sequencer.c +4 -5
index d2332d3e17..92a4871997 100644
@@ -1175,18 +1175,17 @@ static int run_rewrite_hook(const struct object_id *oldoid,
 }
 
 void commit_post_rewrite(struct repository *r,
-			 const struct commit *old_head,
+			 const struct object_id *old_head,
 			 const struct object_id *new_head)
 {
 	struct notes_rewrite_cfg *cfg;
 
 	cfg = init_copy_notes_for_rewrite("amend");
 	if (cfg) {
-		/* we are amending, so old_head is not NULL */
-		copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
+		copy_note_for_rewrite(cfg, old_head, new_head);
 		finish_copy_notes_for_rewrite(r, cfg, "Notes added by 'git commit --amend'");
 	}
-	run_rewrite_hook(&old_head->object.oid, new_head);
+	run_rewrite_hook(old_head, new_head);
 }
 
 static int run_prepare_commit_msg_hook(struct repository *r,
@@ -1538,7 +1537,7 @@ static int try_to_commit(struct repository *r,
 
 	run_commit_hook(0, r->index_file, "post-commit", NULL);
 	if (flags & AMEND_MSG)
-		commit_post_rewrite(r, current_head, oid);
+		commit_post_rewrite(r, &current_head->object.oid, oid);
 
 out:
 	free_commit_extra_headers(extra);
sequencer.h +1 -1
index f8b2e4ab85..79eed55c4b 100644
@@ -192,7 +192,7 @@ int update_head_with_reflog(const struct commit *old_head,
 			    const char *action, const struct strbuf *msg,
 			    struct strbuf *err);
 void commit_post_rewrite(struct repository *r,
-			 const struct commit *current_head,
+			 const struct object_id *old_head,
 			 const struct object_id *new_head);
 
 void create_autostash(struct repository *r, const char *path,
Signature · 1 line
2.31.0-rc2-175-g3820f1c72e
#3 lore raw

PATCH 2/2 sequencer.c: carry forward notes on HEAD across "rebase -x"

In reply to Junio C Hamano

When the external command invoked by "rebase -x" replaces the HEAD, we seem to lose the notes attached to the original HEAD, which is why we have been losing the amlog mappings in the git core project.

Here is a half-successful attempt to fix it. For whatever reason, when the external command is "git commit --amend --no-edit", the updated code carries notes forward correctly, but when the command is changed to "git commit --amend -m tweak", it fails to do so, and I do not have more time to work on this, so I'd stop here with an expected failure in the test.

Help is appreciated.

---
 sequencer.c                   | 10 +++++++++-
 t/t3404-rebase-interactive.sh | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
sequencer.c +9 -1
index 92a4871997..e0bdc39e4d 100644
@@ -3276,7 +3276,10 @@ static int do_exec(struct repository *r, const char *command_line)
 {
 	struct strvec child_env = STRVEC_INIT;
 	const char *child_argv[] = { NULL, NULL };
-	int dirty, status;
+	int dirty, status, bad_head;
+	struct object_id old_head_oid, new_head_oid;
+
+	bad_head = get_oid("HEAD", &old_head_oid);
 
 	fprintf(stderr, _("Executing: %s\n"), command_line);
 	child_argv[0] = command_line;
@@ -3286,6 +3289,11 @@ static int do_exec(struct repository *r, const char *command_line)
 	status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL,
 					  child_env.v);
 
+	bad_head |= get_oid("HEAD", &new_head_oid);
+
+	if (!bad_head && !oideq(&old_head_oid, &new_head_oid))
+		commit_post_rewrite(r, &old_head_oid, &new_head_oid);
+
 	/* force re-reading of the cache */
 	if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
 		return error(_("could not read index"));
t/t3404-rebase-interactive.sh +18 -0
index 66bcbbf952..3222c594ab 100755
@@ -155,6 +155,8 @@ test_expect_success 'rebase -i with the exec command checks tree cleanness' '
 	git rebase --continue
 '
 
+# NEEDSWORK: Fix c762aada1ab3a2c428c with s/@/HEAD/;
+
 test_expect_success 'rebase -x with empty command fails' '
 	test_when_finished "git rebase --abort ||:" &&
 	test_must_fail env git rebase -x "" @ 2>actual &&
@@ -867,6 +869,22 @@ test_expect_success 'rebase -i can copy notes over a fixup' '
 	test_cmp expect output
 '
 
+test_expect_success 'notes are copied even rebase -x changes HEAD' '
+	git reset --hard n3 &&
+	git rebase -x "git commit --amend --no-edit" n1^1 &&
+	git log --format="%s <%N>" n1^1..n3 >expect &&
+	git log --format="%s <%N>" n1^1..HEAD >actual &&
+	test_cmp expect actual
+'
+
+test_expect_failure 'notes are copied even rebase -x changes HEAD' '
+	git reset --hard n3 &&
+	git rebase -x "git commit --amend -m tweak" n1^1 &&
+	git log --format="tweak <%N>" n1^1..n3 >expect &&
+	git log --format="%s <%N>" n1^1..HEAD >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'rebase while detaching HEAD' '
 	git symbolic-ref HEAD &&
 	grandparent=$(git rev-parse HEAD~2) &&
Signature · 1 line
2.31.0-rc2-175-g3820f1c72e
#6 lore raw
In reply to Junio C Hamano

Hi Junio

Quoted 10 lines · Junio C Hamano

I was trying to figure out why I am losing the amlog note that maps each commit on topics to the message ID after amending them with extra trailers (e.g. Reviewed-by). I use a script that runs "git commit --amend" to do this and give it to "rebase -x".

It seems that when the command given to "rebase -x" is run, HEAD is already updated to a commit with tree and log message identical to the original, with updated committer information. However, the note attached to the original commit is *not* copied to this intermediate HEAD that is shown to the external command.

Yes this is annoying I've sometimes wanted to edit the notes when the rebase is stopped for for 'edit' or 'break' commands

Quoted 6 lines

Hence, even if I try to read the existing note from HEAD that is given to the script spawned via "rebase -x cmd" mechanism, the script cannot copy the note to the resulting commit after it adds extra trailers. Also, there is no mechansim for the external command to learn where the original HEAD was.

Quoted 6 lines

I see a few possible avenues to fix this.

1. Before "rebase -x" mechanism spawns an external command, make sure that the "copy notes upon rewriting" logic has triggered, so that the HEAD observable by the command has the notes from the original.

That would be my preferred way to fix this, but we should also copy the notes before stopping for an 'edit' or 'break' and maybe 'reword' so that the notes are always available when a rebase stops for some reason other than conflict resolution.

Quoted 9 lines

It seems that the "copy notes" logic is only used at
the end, but it does not seem to take the possibility into account that the external command might move HEAD to a different commit (i.e. it starts from HEAD0, creates HEAD1 that is different from HEAD0 only for committer info, hands HEAD1 to external command that may move HEAD to HEAD2, but then copies the note from HEAD0 to HEAD1 because it is unaware of HEAD2), so it only annotates HEAD1 that is immediately lost (nothing other than HEAD's reflog would be pointing at it). [*]

I think the root cause is that 'git commit --amend' does not update the list of rewritten commits when run during a rebase, which messes up the post-rewrite hooks (as you suspected) and note copying which is essentially a built-in post-rewrite hook. I've got a half finished solution but there are some corner cases that I've never got round to addressing. It would fix your problem but not the problem of making notes available to the user during the rebase.

Best Wishes

Phillip

Quoted 157 lines

2. Make sure the "copy notes" logic copies the notes taken from the original HEAD to the HEAD _after_ "rebase -x" external command returns.

The first approach would still make the external command responsible for copying notes from HEAD it sees (i.e. HEAD1) to the new commit it creates and points HEAD at (i.e. HEAD2). It _could_ be used as a mechanism to allow the external command to modify/remove notes.

The second approach would forbid the external command from mucking with notes at all. "rebase" would first remember the original HEAD (i.e. HEAD0), does its thing (i.e. creates HEAD1 and points HEAD at it, and then lets the external command create HEAD2 and point HEAD at it), and then figure out the latest HEAD and copy notes from the original HEAD. It is simpler, works with scripts that uses things like "commit --amend" that do not copy notes, but less flexible.

Implementation-wise, it seems that the latter would require more significant surgery to do_pick_commit() codepath, but somebody more familiar with the sequencer codepath should research to find the best approach.

I suspect that "post-commit" hook that is run from try_to_commit() may have the same issue, here in sequencer.c

1539: run_commit_hook(0, r->index_file, "post-commit", NULL);
1540: if (flags & AMEND_MSG)
1541: commit_post_rewrite(r, current_head, oid);

where oid is not adjusted by re-reading what the post-commit hook did to our HEAD.

But the codepath to reach do_exec() (which is what spawns the external command for "rebase -x cmd") is quite away from any call to commit_post_rewrite(),

[footnote]

I have a 6-commit series with notes/amlog record on each commit, and running:

$ git rebase -x '
	git rev-parse HEAD
	git notes --ref=notes/amlog show HEAD || :
' master $(git rev-parse ab/read-tree)

yields the following:

Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
669950d7def2e849cb1ee6e7b3a1beac5c45ce1c
error: no note found for object 669950d7def2e849cb1ee6e7b3a1beac5c45ce1c.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
0545e0a567f96eff14dfb93b44aecf9683b44803
error: no note found for object 0545e0a567f96eff14dfb93b44aecf9683b44803.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
d9b4e77efe5e65952e05533ebf500b559408d436
error: no note found for object d9b4e77efe5e65952e05533ebf500b559408d436.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
6d8735372c0c9b9d3793959157456cd445d025fa
error: no note found for object 6d8735372c0c9b9d3793959157456cd445d025fa.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
b86c1305258a11f42927f924498a94b3ffb19472
error: no note found for object b86c1305258a11f42927f924498a94b3ffb19472.
Executing: git rev-parse HEAD; git notes --ref=notes/amlog show HEAD || :
7da85bae9f11ab14d616bcade1fb2a7b434aa716
error: no note found for object 7da85bae9f11ab14d616bcade1fb2a7b434aa716. Successfully rebased and updated detached HEAD.

We can see each commit on ab/read-tree has notes/amlog entry that records where it came from:

$ git log --notes=notes/amlog --oneline --reverse master..ab/read-tree
2e3e38a4ad ls-files tests: add meaningful --with-tree tests
Notes (amlog):
     Message-Id: <[email protected]>

2371fda438 tree.c API: move read_tree() into builtin/ls-files.c Notes (amlog):

     Message-Id: <[email protected]>

32c718db53 ls-files: don't needlessly pass around stage variable Notes (amlog):

     Message-Id: <[email protected]>

2ebabd63f7 ls-files: refactor away read_tree()
Notes (amlog):

     Message-Id: <[email protected]>

e98fb6ddc7 tree.h API: remove support for starting at prefix != "" Notes (amlog):

     Message-Id: <[email protected]>

8e88702431 tree.h API: remove "stage" parameter from read_tree_recursive() Notes (amlog):

     Message-Id: <[email protected]>

In the above transcript, 669950d7def2e8 (the first commit that the "rebase -x" command saw) corresponds to 2e3e38a4ad (the first commit in the series), and we can see the only difference between them is the committer info. As we already saw in the above, the original has a note that is not copied to the new one when "rebase -x" command runs.

$ git show --pretty=fuller 2e3e38a4ad >old
$ git show --pretty=fuller 669950d7def2e8 >new
$ diff -u old new
new renamed from old +2 -2
@@ -1,8 +1,8 @@
-commit 2e3e38a4ad6d9d78381e90348289277acb4c6f8b
+commit 669950d7def2e849cb1ee6e7b3a1beac5c45ce1c
  Author:     Ævar Arnfjörð Bjarmason <[email protected]>
  AuthorDate: Mon Mar 8 03:21:33 2021 +0100
  Commit:     Junio C Hamano <[email protected]>
-CommitDate: Mon Mar 8 10:06:35 2021 -0800
+CommitDate: Thu Mar 11 12:01:11 2021 -0800
 
      ls-files tests: add meaningful --with-tree tests
 

HOWEVER, after that failed "rebase -x" session, the note is copied to these "intermediate" commits. Using 7da85bae9f1 which is at the tip of the history fed to "rebase -x" external command, we can see:

$ git log --notes=notes/amlog --oneline --reverse master..7da85bae9f1

669950d7de ls-files tests: add meaningful --with-tree tests Notes (amlog):

     Message-Id: <[email protected]>

0545e0a567 tree.c API: move read_tree() into builtin/ls-files.c Notes (amlog):

     Message-Id: <[email protected]>

d9b4e77efe ls-files: don't needlessly pass around stage variable Notes (amlog):

     Message-Id: <[email protected]>

6d8735372c ls-files: refactor away read_tree()
Notes (amlog):

     Message-Id: <[email protected]>

b86c130525 tree.h API: remove support for starting at prefix != "" Notes (amlog):

     Message-Id: <[email protected]>

7da85bae9f tree.h API: remove "stage" parameter from read_tree_recursive() Notes (amlog):

     Message-Id: <[email protected]>

It is only that they seem to be added _after_ the external command finishes, which is not a very useful behaviour.

#4 lore raw

Re: [PATCH 2/2] [WIP] sequencer.c: carry forward notes on HEAD across "rebase -x"

In reply to Junio C Hamano
Quoted 36 lines · Junio C Hamano

When the external command invoked by "rebase -x" replaces the HEAD, we seem to lose the notes attached to the original HEAD, which is why we have been losing the amlog mappings in the git core project.

Here is a half-successful attempt to fix it. For whatever reason, when the external command is "git commit --amend --no-edit", the updated code carries notes forward correctly, but when the command is changed to "git commit --amend -m tweak", it fails to do so, and I do not have more time to work on this, so I'd stop here with an expected failure in the test.

Help is appreciated.

---
  sequencer.c                   | 10 +++++++++-
  t/t3404-rebase-interactive.sh | 18 ++++++++++++++++++
  2 files changed, 27 insertions(+), 1 deletion(-)
sequencer.c +4 -1
index 92a4871997..e0bdc39e4d 100644
@@ -3276,7 +3276,10 @@ static int do_exec(struct repository *r, const char *command_line)
  {
  	struct strvec child_env = STRVEC_INIT;
  	const char *child_argv[] = { NULL, NULL };
-	int dirty, status;
+	int dirty, status, bad_head;
+	struct object_id old_head_oid, new_head_oid;
+
+	bad_head = get_oid("HEAD", &old_head_oid);
 
  	fprintf(stderr, _("Executing: %s\n"), command_line);
  	child_argv[0] = command_line;

sequencer.c static int do_exec(struct repository *r, const char *command_line) · line 3295

Quoted 7 lines
sequencer.c +4 -0
@@ -3286,6 +3289,11 @@ static int do_exec(struct repository *r, const char *command_line)
  	status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL,
  					  child_env.v);
 
+	bad_head |= get_oid("HEAD", &new_head_oid);
+
+	if (!bad_head && !oideq(&old_head_oid, &new_head_oid))
+		commit_post_rewrite(r, &old_head_oid, &new_head_oid);

If HEAD has changed then we copy the notes - but we never copied the notes from the original commit to the rewritten commit before running the exec command so this does not do what we want. I think this is why the second test below fails. I think the first test passes because the author and committer identities and dates of the amended commit are unchanged from the rebased commit so we end up copying the notes at the end of the rebase because the oid is unchanged.

Blindly copying the notes here would mean that the notes would be copied if an exec command created a new commit on top of HEAD rather than amending HEAD which is not what we want.

Best Wishes

Phillip

Quoted 40 lines
sequencer.c +0 -0
  	/* force re-reading of the cache */
  	if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
  		return error(_("could not read index"));
t/t3404-rebase-interactive.sh +18 -0
index 66bcbbf952..3222c594ab 100755
@@ -155,6 +155,8 @@ test_expect_success 'rebase -i with the exec command checks tree cleanness' '
  	git rebase --continue
  '
 
+# NEEDSWORK: Fix c762aada1ab3a2c428c with s/@/HEAD/;
+
  test_expect_success 'rebase -x with empty command fails' '
  	test_when_finished "git rebase --abort ||:" &&
  	test_must_fail env git rebase -x "" @ 2>actual &&
@@ -867,6 +869,22 @@ test_expect_success 'rebase -i can copy notes over a fixup' '
  	test_cmp expect output
  '
 
+test_expect_success 'notes are copied even rebase -x changes HEAD' '
+	git reset --hard n3 &&
+	git rebase -x "git commit --amend --no-edit" n1^1 &&
+	git log --format="%s <%N>" n1^1..n3 >expect &&
+	git log --format="%s <%N>" n1^1..HEAD >actual &&
+	test_cmp expect actual
+'
+
+test_expect_failure 'notes are copied even rebase -x changes HEAD' '
+	git reset --hard n3 &&
+	git rebase -x "git commit --amend -m tweak" n1^1 &&
+	git log --format="tweak <%N>" n1^1..n3 >expect &&
+	git log --format="%s <%N>" n1^1..HEAD >actual &&
+	test_cmp expect actual
+'
+
  test_expect_success 'rebase while detaching HEAD' '
  	git symbolic-ref HEAD &&
  	grandparent=$(git rev-parse HEAD~2) &&
#7 lore raw

PATCH 1/1 rebase: update the list of rewritten commits when amending pick

In reply to Junio C Hamano

From: Phillip Wood <[email protected]>

If HEAD is amended during an exec command then the amended commit is missing from the list of rewritten commits. The commonest way for this to happen is if a commit is amended to fix a test failure when running git rebase --exec "make test" but it can also happen if the exec command calls git commit --amend directly. Amending commits with exec commands was discussed on the mailing list recently where someone wanted to reset the author before submitting patches upstream[1].

[1] https://public-inbox.org/git/CABPp-BEYRmhrb4Tx3bGzkx8y53T_0BYhLE5J0cEmxj18WtZs9A@mail.gmail.com/#t

---

This is what I've been using for a couple of years to update the list of rewritten commits when running "git commit --amend" during a rebase. I think it changes which commit gets used as the rewritten one if you split a commit with 'edit', the patch is so old I cannot remember if there were any other corner cases.

 builtin/commit.c             |   2 +-
 sequencer.c                  | 119 +++++++++++++++++++++++++++++------
 sequencer.h                  |   6 +-
 t/lib-rebase.sh              |   2 +-
 t/t5407-post-rewrite-hook.sh |  70 ++++++++++++++++++++-
 5 files changed, 176 insertions(+), 23 deletions(-)
builtin/commit.c +1 -1
index ae7aaf6dc6..9b6f3d8b6b 100644
@@ -1697,7 +1697,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
 	run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
 	if (amend && !no_post_rewrite) {
-		commit_post_rewrite(the_repository, current_head, &oid);
+		commit_post_rewrite(the_repository, current_head, &oid, NULL);
 	}
 	if (!quiet) {
 		unsigned int flags = 0;
sequencer.c +100 -19

290 lines not on this page · raw message

sequencer.h +5 -1
index 574260f621..91834cfe1c 100644
@@ -63,6 +63,9 @@ struct replay_opts {
 	struct object_id squash_onto;
 	int have_squash_onto;
 
+	/* Used to update rewritten-list */
+	struct object_id rewritten_head;
+
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
 };
@@ -188,7 +191,8 @@ int update_head_with_reflog(const struct commit *old_head,
 			    struct strbuf *err);
 void commit_post_rewrite(struct repository *r,
 			 const struct commit *current_head,
-			 const struct object_id *new_head);
+			 const struct object_id *new_head,
+			 struct object_id *rewritten_head);
 
 int prepare_branch_to_be_rebased(struct repository *r, struct replay_opts *opts,
 				 const char *commit);
t/lib-rebase.sh +1 -1
index 6d87961e41..9c0016848d 100644
@@ -49,7 +49,7 @@ set_fake_editor () {
 		case $line in
 		pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
 			action="$line";;
-		exec_*|x_*|break|b)
+		exec_*|x_*|break|b|reset_*|t_*)
 			echo "$line" | sed 's/_/ /g' >> "$1";;
 		"#")
 			echo '# comment' >> "$1";;
t/t5407-post-rewrite-hook.sh +69 -1

80 lines not on this page · raw message

Signature · 1 line
2.30.1
#5 lore raw

Re: [PATCH 2/2] [WIP] sequencer.c: carry forward notes on HEAD across "rebase -x"

In reply to Junio C Hamano

t/t3404-rebase-interactive.sh test_expect_success 'rebase -i can copy notes over a fixup' ' · line 870

Quoted 8 lines · Junio C Hamano
t/t3404-rebase-interactive.sh +2 -0
+# NEEDSWORK: Fix c762aada1ab3a2c428c with s/@/HEAD/;
+
 test_expect_success 'rebase -x with empty command fails' '
 	test_when_finished "git rebase --abort ||:" &&
 	test_must_fail env git rebase -x "" @ 2>actual &&
@@ -867,6 +869,22 @@ test_expect_success 'rebase -i can copy notes over a fixup' '
 	test_cmp expect output
 '

I eyeballed c762aada1ab (rebase -x: sanity check command, 2019-01-29) for a bit and still don't quite know what this HEAD v.s. @ is about in that context, seems this is a stray FIXME comment for an unrelated test.

Maybe it would be better to have test_expect_failure etc. here as appropriate?

Quoted 19 lines
t/t3404-rebase-interactive.sh +16 -0
+test_expect_success 'notes are copied even rebase -x changes HEAD' '
+	git reset --hard n3 &&
+	git rebase -x "git commit --amend --no-edit" n1^1 &&
+	git log --format="%s <%N>" n1^1..n3 >expect &&
+	git log --format="%s <%N>" n1^1..HEAD >actual &&
+	test_cmp expect actual
+'
+
+test_expect_failure 'notes are copied even rebase -x changes HEAD' '
+	git reset --hard n3 &&
+	git rebase -x "git commit --amend -m tweak" n1^1 &&
+	git log --format="tweak <%N>" n1^1..n3 >expect &&
+	git log --format="%s <%N>" n1^1..HEAD >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'rebase while detaching HEAD' '
 	git symbolic-ref HEAD &&
 	grandparent=$(git rev-parse HEAD~2) &&