rebase -i: stop overwriting ORIG_HEAD buffer

After rebasing, ORIG_HEAD is supposed to point to the old HEAD of the rebased branch. The code used find_unique_abbrev() to obtain the object name of the old HEAD and wrote to both .git/rebase-merge/orig-head (used by `rebase --abort` to go back to the previous state) and to ORIG_HEAD. The buffer find_unique_abbrev() gives back is volatile, unfortunately, and was overwritten after the former file is written but before ORIG_FILE is written, leaving an incorrect object name in it. Avoid relying on the volatile buffer of find_unique_abbrev(), and instead supply our own buffer to keep the object name. I think that all of the users of head_hash should actually be using opts->orig_head instead as passing a string rather than a struct object_id around is a hang over from the scripted implementation. This patch just fixes the immediate bug and adds a regression test based on Caspar's reproduction example[1]. The users will be converted to use struct object_id and head_hash removed in the next few commits. [1] https://lore.kernel.org/git/CAFzd1+7PDg2PZgKw7U0kdepdYuoML9wSN4kofmB_-8NHrbbrHg@mail.gmail.com Reported-by: Caspar Duregger <herr.kaste@gmail.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Nov 4, 2020 at 15:29 UTC e100bea48102dca7e21359993085e89c7f154f9d
2 files changed +16 -5
builtin/rebase.c
+5 -5
@@ -270,15 +270,15 @@ static int edit_todo_file(unsigned flags)
270 }
271
272 static int get_revision_ranges(struct commit *upstream, struct commit *onto,
273 - struct object_id *orig_head, const char **head_hash,
273 + struct object_id *orig_head, char *head_hash,
274 char **revisions, char **shortrevisions)
275 {
276 struct commit *base_rev = upstream ? upstream : onto;
277 const char *shorthead;
278
279 - *head_hash = find_unique_abbrev(orig_head, GIT_MAX_HEXSZ);
279 + find_unique_abbrev_r(head_hash, orig_head, GIT_MAX_HEXSZ);
280 *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid),
281 - *head_hash);
281 + head_hash);
282
283 shorthead = find_unique_abbrev(orig_head, DEFAULT_ABBREV);
284
@@ -327,7 +327,7 @@ static void split_exec_commands(const char *cmd, struct string_list *commands)
327 static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
328 {
329 int ret;
330 - const char *head_hash = NULL;
330 + char head_hash[GIT_MAX_HEXSZ];
331 char *revisions = NULL, *shortrevisions = NULL;
332 struct strvec make_script_args = STRVEC_INIT;
333 struct todo_list todo_list = TODO_LIST_INIT;
@@ -335,7 +335,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
335 struct string_list commands = STRING_LIST_INIT_DUP;
336
337 if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head,
338 - &head_hash, &revisions, &shortrevisions))
338 + head_hash, &revisions, &shortrevisions))
339 return -1;
340
341 if (init_basic_state(&replay,
t/t3404-rebase-interactive.sh
+11
@@ -1797,6 +1797,17 @@ test_expect_success 'todo has correct onto hash' '
1797 test_i18ngrep "^# Rebase ..* onto $onto" actual
1798 '
1799
1800 +test_expect_success 'ORIG_HEAD is updated correctly' '
1801 + test_when_finished "git checkout master && git branch -D test-orig-head" &&
1802 + git checkout -b test-orig-head A &&
1803 + git commit --allow-empty -m A1 &&
1804 + git commit --allow-empty -m A2 &&
1805 + git commit --allow-empty -m A3 &&
1806 + git commit --allow-empty -m A4 &&
1807 + git rebase master &&
1808 + test_cmp_rev ORIG_HEAD test-orig-head@{1}
1809 +'
1810 +
1811 # This must be the last test in this file
1812 test_expect_success '$EDITOR and friends are unchanged' '
1813 test_editor_unchanged