]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rebase -i: fix possibly wrong onto hash in todo
authorAntti Keränen <detegr@rbx.email>
Thu, 13 Aug 2020 17:42:57 +0000 (20:42 +0300)
committerJunio C Hamano <gitster@pobox.com>
Thu, 13 Aug 2020 18:32:34 +0000 (11:32 -0700)
'todo_list_write_to_file' may overwrite the static buffer, originating
from 'find_unique_abbrev', that was used to store the short commit hash
'c' for "# Rebase a..b onto c" message in the todo editor. This is
because the buffer that is returned from 'find_unique_abbrev' is valid
until 4 more calls to `find_unique_abbrev` are made.

As 'todo_list_write_to_file' calls 'find_unique_abbrev' for each rebased
commit, the hash for 'c' is overwritten if there are 4 or more commits
in the rebase. This behavior has been broken since its introduction.

Fix by storing the short onto commit hash in a different buffer that
remains valid, before calling 'todo_list_write_to_file'.

Found-by: Jussi Keränen <jussike@gmail.com>
Signed-off-by: Antti Keränen <detegr@rbx.email>
Acked-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sequencer.c
t/t3404-rebase-interactive.sh

index fd7701c88a8643c424d4f61fd0cc86fde3097d7d..e2007dbb8ccb73d2772d6e155b6684cb2994f883 100644 (file)
@@ -5178,13 +5178,14 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
                    struct string_list *commands, unsigned autosquash,
                    struct todo_list *todo_list)
 {
-       const char *shortonto, *todo_file = rebase_path_todo();
+       char shortonto[GIT_MAX_HEXSZ + 1];
+       const char *todo_file = rebase_path_todo();
        struct todo_list new_todo = TODO_LIST_INIT;
        struct strbuf *buf = &todo_list->buf, buf2 = STRBUF_INIT;
        struct object_id oid = onto->object.oid;
        int res;
 
-       shortonto = find_unique_abbrev(&oid, DEFAULT_ABBREV);
+       find_unique_abbrev_r(shortonto, &oid, DEFAULT_ABBREV);
 
        if (buf->len == 0) {
                struct todo_item *item = append_new_todo(todo_list);
index 4a7d21f898f7361ffcdf988b2458e52089b758ba..1b4fa0843e1ce380e07c7847388e5c9035c91ad0 100755 (executable)
@@ -1760,6 +1760,12 @@ test_expect_success 'correct error message for commit --amend after empty pick'
        test_i18ngrep "middle of a rebase -- cannot amend." err
 '
 
+test_expect_success 'todo has correct onto hash' '
+       GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
+       onto=$(git rev-parse --short HEAD~4) &&
+       test_i18ngrep "^# Rebase ..* onto $onto" actual
+'
+
 # This must be the last test in this file
 test_expect_success '$EDITOR and friends are unchanged' '
        test_editor_unchanged