Skip to content

Commit b699226

Browse files
dschogitster
authored andcommitted
rebase -i: re-fix short SHA-1 collision
In 66ae9a5 (t3404: rebase -i: demonstrate short SHA-1 collision, 2013-08-23), we added a test case that demonstrated how it is possible that a previously unambiguous short commit ID could become ambiguous *during* a rebase. In 75c6976 (rebase -i: fix short SHA-1 collision, 2013-08-23), we fixed that problem simply by writing out the todo list with expanded commit IDs (except *right* before letting the user edit the todo list, in which case we shorten them, but we expand them right after the file was edited). However, the bug resurfaced as a side effect of 393adf7 (sequencer: directly call pick_commits() from complete_action(), 2019-11-24): as of this commit, the sequencer no longer re-reads the todo list after writing it out with expanded commit IDs. The only redeeming factor is that the todo list is already parsed at that stage, including all the commits corresponding to the commands, therefore the sequencer can continue even if the internal todo list has short commit IDs. That does not prevent problems, though: the sequencer writes out the `done` and `git-rebase-todo` files incrementally (i.e. overwriting the todo list with a version that has _short_ commit IDs), and if a merge conflict happens, or if an `edit` or a `break` command is encountered, a subsequent `git rebase --continue` _will_ re-read the todo list, opening an opportunity for the "short SHA-1 collision" bug again. To avoid that, let's make sure that we do expand the commit IDs in the todo list as soon as we have parsed it after letting the user edit it. Additionally, we improve the 'short SHA-1 collide' test case in t3404 to test specifically for the case where the rebase is resumed. We also hard-code the expected colliding short SHA-1s, to document the expectation (and to make it easier on future readers). Note that we specifically test that the short commit ID is used in the `git-rebase-todo.tmp` file: this file is created by the fake editor in the test script and reflects the state that would have been presented to the user to edit. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d859dca commit b699226

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

sequencer.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5076,7 +5076,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
50765076
{
50775077
const char *shortonto, *todo_file = rebase_path_todo();
50785078
struct todo_list new_todo = TODO_LIST_INIT;
5079-
struct strbuf *buf = &todo_list->buf;
5079+
struct strbuf *buf = &todo_list->buf, buf2 = STRBUF_INIT;
50805080
struct object_id oid = onto->object.oid;
50815081
int res;
50825082

@@ -5128,6 +5128,15 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
51285128
return -1;
51295129
}
51305130

5131+
/* Expand the commit IDs */
5132+
todo_list_to_strbuf(r, &new_todo, &buf2, -1, 0);
5133+
strbuf_swap(&new_todo.buf, &buf2);
5134+
strbuf_release(&buf2);
5135+
new_todo.total_nr -= new_todo.nr;
5136+
if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) < 0)
5137+
BUG("invalid todo list after expanding IDs:\n%s",
5138+
new_todo.buf.buf);
5139+
51315140
if (opts->allow_ff && skip_unnecessary_picks(r, &new_todo, &oid)) {
51325141
todo_list_release(&new_todo);
51335142
return error(_("could not skip unnecessary pick commands"));

t/t3404-rebase-interactive.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,13 +1264,24 @@ test_expect_success SHA1 'short SHA-1 setup' '
12641264
test_expect_success SHA1 'short SHA-1 collide' '
12651265
test_when_finished "reset_rebase && git checkout master" &&
12661266
git checkout collide &&
1267+
colliding_sha1=6bcda37 &&
1268+
test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
12671269
(
12681270
unset test_tick &&
12691271
test_tick &&
12701272
set_fake_editor &&
12711273
FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1272-
FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1273-
)
1274+
FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1275+
test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1276+
grep "^pick $colliding_sha1 " \
1277+
.git/rebase-merge/git-rebase-todo.tmp &&
1278+
grep "^pick [0-9a-f]\{40\}" \
1279+
.git/rebase-merge/git-rebase-todo &&
1280+
git rebase --continue
1281+
) &&
1282+
collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1283+
collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1284+
test "$collide2" = "$collide3"
12741285
'
12751286

12761287
test_expect_success 'respect core.abbrev' '

0 commit comments

Comments
 (0)