Skip to content

Commit 3e367a5

Browse files
krobelusgitster
authored andcommitted
sequencer: avoid dropping fixup commit that targets self via commit-ish
Commit 68d5d03 (rebase: teach --autosquash to match on sha1 in addition to message, 2010-11-04) taught autosquash to recognize subjects like "fixup! 7a235b" where 7a235b is an OID-prefix. It actually did more than advertised: 7a235b can be an arbitrary commit-ish (as long as it's not trailed by spaces). Accidental(?) use of this secret feature revealed a bug where we would silently drop a fixup commit. The bug can also be triggered when using an OID-prefix but that's unlikely in practice. Let the commit with subject "fixup! main" be the tip of the "main" branch. When computing the fixup target for this commit, we find the commit itself. This is wrong because, by definition, a fixup target must be an earlier commit in the todo list. We wrongly find the current commit because we added it to the todo list prematurely. Avoid these fixup-cycles by only adding the current commit to the todo list after we have finished looking for the fixup target. Reported-by: Erik Cervin Edin <[email protected]> Signed-off-by: Johannes Altmanninger <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd59c5b commit 3e367a5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

sequencer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5774,8 +5774,6 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
57745774
return error(_("the script was already rearranged."));
57755775
}
57765776

5777-
*commit_todo_item_at(&commit_todo, item->commit) = item;
5778-
57795777
parse_commit(item->commit);
57805778
commit_buffer = logmsg_reencode(item->commit, NULL, "UTF-8");
57815779
find_commit_subject(commit_buffer, &subject);
@@ -5842,6 +5840,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
58425840
strhash(entry->subject));
58435841
hashmap_put(&subject2item, &entry->entry);
58445842
}
5843+
5844+
*commit_todo_item_at(&commit_todo, item->commit) = item;
58455845
}
58465846

58475847
if (rearranged) {

t/t3415-rebase-autosquash.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ test_expect_success 'auto squash that matches longer sha1' '
232232
test_line_count = 1 actual
233233
'
234234

235+
test_expect_success 'auto squash of fixup commit that matches branch name which points back to fixup commit' '
236+
git reset --hard base &&
237+
git commit --allow-empty -m "fixup! self-cycle" &&
238+
git branch self-cycle &&
239+
GIT_SEQUENCE_EDITOR="cat >tmp" git rebase --autosquash -i HEAD^^ &&
240+
sed -ne "/^[^#]/{s/[0-9a-f]\{7,\}/HASH/g;p;}" tmp >actual &&
241+
cat <<-EOF >expect &&
242+
pick HASH second commit
243+
pick HASH fixup! self-cycle # empty
244+
EOF
245+
test_cmp expect actual
246+
'
247+
235248
test_auto_commit_flags () {
236249
git reset --hard base &&
237250
echo 1 >file1 &&

0 commit comments

Comments
 (0)