From e100bea48102dca7e21359993085e89c7f154f9d Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 4 Nov 2020 15:29:37 +0000 Subject: 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 Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano diff --git a/builtin/rebase.c b/builtin/rebase.c index eeca533..cd101b2 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -270,15 +270,15 @@ static int edit_todo_file(unsigned flags) } static int get_revision_ranges(struct commit *upstream, struct commit *onto, - struct object_id *orig_head, const char **head_hash, + struct object_id *orig_head, char *head_hash, char **revisions, char **shortrevisions) { struct commit *base_rev = upstream ? upstream : onto; const char *shorthead; - *head_hash = find_unique_abbrev(orig_head, GIT_MAX_HEXSZ); + find_unique_abbrev_r(head_hash, orig_head, GIT_MAX_HEXSZ); *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid), - *head_hash); + head_hash); shorthead = find_unique_abbrev(orig_head, DEFAULT_ABBREV); @@ -327,7 +327,7 @@ static void split_exec_commands(const char *cmd, struct string_list *commands) static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) { int ret; - const char *head_hash = NULL; + char head_hash[GIT_MAX_HEXSZ]; char *revisions = NULL, *shortrevisions = NULL; struct strvec make_script_args = STRVEC_INIT; struct todo_list todo_list = TODO_LIST_INIT; @@ -335,7 +335,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) struct string_list commands = STRING_LIST_INIT_DUP; if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head, - &head_hash, &revisions, &shortrevisions)) + head_hash, &revisions, &shortrevisions)) return -1; if (init_basic_state(&replay, diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index 07a1617..1e56696 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -1797,6 +1797,17 @@ test_expect_success 'todo has correct onto hash' ' test_i18ngrep "^# Rebase ..* onto $onto" actual ' +test_expect_success 'ORIG_HEAD is updated correctly' ' + test_when_finished "git checkout master && git branch -D test-orig-head" && + git checkout -b test-orig-head A && + git commit --allow-empty -m A1 && + git commit --allow-empty -m A2 && + git commit --allow-empty -m A3 && + git commit --allow-empty -m A4 && + git rebase master && + test_cmp_rev ORIG_HEAD test-orig-head@{1} +' + # This must be the last test in this file test_expect_success '$EDITOR and friends are unchanged' ' test_editor_unchanged -- cgit v0.10.2-6-g49f6 From f3e27a02d598084a0ef5c8dd6b410bd6a5c5299f Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 4 Nov 2020 15:29:38 +0000 Subject: rebase -i: use struct object_id rather than looking up commit We already have a struct object_id containing the oid that we want to set ORIG_HEAD to so use that rather than converting it to a string and then calling get_oid() on that string. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano diff --git a/builtin/rebase.c b/builtin/rebase.c index cd101b2..4e4a5e7 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -370,8 +370,9 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) split_exec_commands(opts->cmd, &commands); ret = complete_action(the_repository, &replay, flags, - shortrevisions, opts->onto_name, opts->onto, head_hash, - &commands, opts->autosquash, &todo_list); + shortrevisions, opts->onto_name, opts->onto, + &opts->orig_head, &commands, opts->autosquash, + &todo_list); } string_list_clear(&commands, 0); diff --git a/sequencer.c b/sequencer.c index 00acb12..f79c3df 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3965,21 +3965,17 @@ static int run_git_checkout(struct repository *r, struct replay_opts *opts, static int checkout_onto(struct repository *r, struct replay_opts *opts, const char *onto_name, const struct object_id *onto, - const char *orig_head) + const struct object_id *orig_head) { - struct object_id oid; const char *action = reflog_message(opts, "start", "checkout %s", onto_name); - if (get_oid(orig_head, &oid)) - return error(_("%s: not a valid OID"), orig_head); - if (run_git_checkout(r, opts, oid_to_hex(onto), action)) { apply_autostash(rebase_path_autostash()); sequencer_remove_state(opts); return error(_("could not detach HEAD")); } - return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR); + return update_ref(NULL, "ORIG_HEAD", orig_head, NULL, 0, UPDATE_REFS_MSG_ON_ERR); } static int stopped_at_head(struct repository *r) @@ -5314,7 +5310,7 @@ static int skip_unnecessary_picks(struct repository *r, int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags, const char *shortrevisions, const char *onto_name, - struct commit *onto, const char *orig_head, + struct commit *onto, const struct object_id *orig_head, struct string_list *commands, unsigned autosquash, struct todo_list *todo_list) { diff --git a/sequencer.h b/sequencer.h index b2a501e..ea56825 100644 --- a/sequencer.h +++ b/sequencer.h @@ -163,8 +163,9 @@ void todo_list_add_exec_commands(struct todo_list *todo_list, struct string_list *commands); int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags, const char *shortrevisions, const char *onto_name, - struct commit *onto, const char *orig_head, struct string_list *commands, - unsigned autosquash, struct todo_list *todo_list); + struct commit *onto, const struct object_id *orig_head, + struct string_list *commands, unsigned autosquash, + struct todo_list *todo_list); int todo_list_rearrange_squash(struct todo_list *todo_list); /* -- cgit v0.10.2-6-g49f6 From a2bb10d06db8a90920f1f518705a0bb9d39aa1db Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 4 Nov 2020 15:29:39 +0000 Subject: rebase -i: use struct object_id when writing state Rather than passing a string around pass the struct object_id that the string was created from call oid_hex() when we write the file. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano diff --git a/builtin/rebase.c b/builtin/rebase.c index 4e4a5e7..28e7b7f 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -296,7 +296,8 @@ static int get_revision_ranges(struct commit *upstream, struct commit *onto, } static int init_basic_state(struct replay_opts *opts, const char *head_name, - struct commit *onto, const char *orig_head) + struct commit *onto, + const struct object_id *orig_head) { FILE *interactive; @@ -340,7 +341,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) if (init_basic_state(&replay, opts->head_name ? opts->head_name : "detached HEAD", - opts->onto, head_hash)) { + opts->onto, &opts->orig_head)) { free(revisions); free(shortrevisions); diff --git a/sequencer.c b/sequencer.c index f79c3df..2037f0b 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2692,7 +2692,7 @@ static void write_strategy_opts(struct replay_opts *opts) } int write_basic_state(struct replay_opts *opts, const char *head_name, - struct commit *onto, const char *orig_head) + struct commit *onto, const struct object_id *orig_head) { if (head_name) write_file(rebase_path_head_name(), "%s\n", head_name); @@ -2700,7 +2700,8 @@ int write_basic_state(struct replay_opts *opts, const char *head_name, write_file(rebase_path_onto(), "%s\n", oid_to_hex(&onto->object.oid)); if (orig_head) - write_file(rebase_path_orig_head(), "%s\n", orig_head); + write_file(rebase_path_orig_head(), "%s\n", + oid_to_hex(orig_head)); if (opts->quiet) write_file(rebase_path_quiet(), "%s", ""); diff --git a/sequencer.h b/sequencer.h index ea56825..cf201f2 100644 --- a/sequencer.h +++ b/sequencer.h @@ -227,7 +227,7 @@ int read_author_script(const char *path, char **name, char **email, char **date, int allow_missing); void parse_strategy_opts(struct replay_opts *opts, char *raw_opts); int write_basic_state(struct replay_opts *opts, const char *head_name, - struct commit *onto, const char *orig_head); + struct commit *onto, const struct object_id *orig_head); void sequencer_post_commit_cleanup(struct repository *r, int verbose); int sequencer_get_last_command(struct repository* r, enum replay_action *action); -- cgit v0.10.2-6-g49f6 From 8843302307bb7d652f5adde759cd6a3c1a7fb1ea Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 4 Nov 2020 15:29:40 +0000 Subject: rebase -i: simplify get_revision_ranges() Now that all the external users of head_hash have been converted to use a opts->orig_head instead we can stop returning head_hash from get_revision_ranges(). Because we want to pass the full object names back to the caller in `revisions` the find_unique_abbrev_r() call that was used to initialize `head_hash` is replaced with oid_to_hex(). Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano diff --git a/builtin/rebase.c b/builtin/rebase.c index 28e7b7f..17450e9 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -270,15 +270,14 @@ static int edit_todo_file(unsigned flags) } static int get_revision_ranges(struct commit *upstream, struct commit *onto, - struct object_id *orig_head, char *head_hash, - char **revisions, char **shortrevisions) + struct object_id *orig_head, char **revisions, + char **shortrevisions) { struct commit *base_rev = upstream ? upstream : onto; const char *shorthead; - find_unique_abbrev_r(head_hash, orig_head, GIT_MAX_HEXSZ); *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid), - head_hash); + oid_to_hex(orig_head)); shorthead = find_unique_abbrev(orig_head, DEFAULT_ABBREV); @@ -328,7 +327,6 @@ static void split_exec_commands(const char *cmd, struct string_list *commands) static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) { int ret; - char head_hash[GIT_MAX_HEXSZ]; char *revisions = NULL, *shortrevisions = NULL; struct strvec make_script_args = STRVEC_INIT; struct todo_list todo_list = TODO_LIST_INIT; @@ -336,7 +334,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) struct string_list commands = STRING_LIST_INIT_DUP; if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head, - head_hash, &revisions, &shortrevisions)) + &revisions, &shortrevisions)) return -1; if (init_basic_state(&replay, -- cgit v0.10.2-6-g49f6