Teach the sequencer to act as rebase -i's backend
PATCH v3 11/38 sequencer (rebase -i): implement the short commands
In reply to Johannes SchindelinFor users' convenience, most rebase commands can be abbreviated, e.g. 'p' instead of 'pick' and 'x' instead of 'exec'. Let's teach the sequencer to handle those abbreviated commands just fine.
--- sequencer.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
sequencer.c +21 -14
@@ -678,20 +678,23 @@ enum todo_command { TODO_NOOP }; -static const char *todo_command_strings[] = { -"pick" , -"revert" , -"edit" , -"fixup" , -"squash" , -"exec" , -"noop" +static struct { +char c; +const char *str; +} todo_command_info[] = { + {'p' ,"pick" }, + {0 ,"revert" }, + {'e' ,"edit" }, + {'f' ,"fixup" }, + {'s' ,"squash" }, + {'x' ,"exec" }, + {0 ,"noop" } };static const char *command_to_string (const enum todo_command command) { -if ((size_t )command <ARRAY_SIZE (todo_command_strings)) -return todo_command_strings[command]; +if ((size_t )command <ARRAY_SIZE (todo_command_info)) +return todo_command_info[command].str;die ("Unknown command: %d" , command); }
@@ -1087,12 +1090,16 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)return 0 ; } -for (i =0 ; i <ARRAY_SIZE (todo_command_strings); i++) -if (skip_prefix (bol, todo_command_strings[i], &bol)) { +for (i =0 ; i <ARRAY_SIZE (todo_command_info); i++) +if (skip_prefix (bol, todo_command_info[i].str, &bol)) { + item->command = i; +break ; + }else if (bol[1 ] ==' ' && *bol == todo_command_info[i].c) { + bol++; item->command = i;break ; } -if (i >=ARRAY_SIZE (todo_command_strings)) +if (i >=ARRAY_SIZE (todo_command_info))return -1 ;if (item->command ==TODO_NOOP ) {
@@ -1287,7 +1294,7 @@ static int walk_revs_populate_todo(struct todo_list *todo_list, {enum todo_command command = opts->action ==REPLAY_PICK ?TODO_PICK :TODO_REVERT ; -const char *command_string = todo_command_strings[command]; +const char *command_string = todo_command_info[command].str;struct commit *commit;if (prepare_revs (opts))
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 12/38 sequencer (rebase -i): write an author-script file
In reply to Johannes SchindelinWhen the interactive rebase aborts, it writes out an author-script file to record the author information for the current commit. As we are about to teach the sequencer how to perform the actions behind an interactive rebase, it needs to write those author-script files, too.
--- sequencer.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-)
sequencer.c +49 -1
@@ -483,6 +483,52 @@ static int is_index_unchanged(void)return !hashcmp (active_cache_tree->sha1, head_commit->tree->object.oid.hash); } +static int write_author_script (const char *message) +{ +struct strbuf buf =STRBUF_INIT ; +const char *eol; +int res; + +for (;;) +if (!*message ||starts_with (message,"\n" )) { +missing_author: +/* Missing 'author' line? */ +unlink (rebase_path_author_script ()); +return 0 ; + }else if (skip_prefix (message,"author " , &message)) +break ; +else if ((eol =strchr (message,'\n' ))) + message = eol +1 ; +else + goto missing_author; + +strbuf_addstr (&buf,"GIT_AUTHOR_NAME='" ); +while (*message && *message !='\n' && *message !='\r' ) +if (skip_prefix (message," <" , &message)) +break ; +else if (*message !='\'' ) +strbuf_addch (&buf, *(message++)); +else +strbuf_addf (&buf,"'\\\\%c'" , *(message++)); +strbuf_addstr (&buf,"'\nGIT_AUTHOR_EMAIL='" ); +while (*message && *message !='\n' && *message !='\r' ) +if (skip_prefix (message,"> " , &message)) +break ; +else if (*message !='\'' ) +strbuf_addch (&buf, *(message++)); +else +strbuf_addf (&buf,"'\\\\%c'" , *(message++)); +strbuf_addstr (&buf,"'\nGIT_AUTHOR_DATE='@" ); +while (*message && *message !='\n' && *message !='\r' ) +if (*message !='\'' ) +strbuf_addch (&buf, *(message++)); +else +strbuf_addf (&buf,"'\\\\%c'" , *(message++)); + res =write_message (buf.buf, buf.len,rebase_path_author_script (),1 ); +strbuf_release (&buf); +return res; +} +/* * Read the author-script file into an environment block, ready for use in * run_command(), that can be free()d afterwards.
@@ -935,7 +981,9 @@ static int do_pick_commit(enum todo_command command, struct commit *commit, } } -if (!opts->strategy || !strcmp (opts->strategy,"recursive" ) || command ==TODO_REVERT ) { +if (is_rebase_i (opts) &&write_author_script (msg.message) <0 ) + res =-1 ; +else if (!opts->strategy || !strcmp (opts->strategy,"recursive" ) || command ==TODO_REVERT ) { res =do_recursive_merge (base, next, base_label, next_label, head, &msgbuf, opts);if (res <0 )
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 13/38 sequencer (rebase -i): allow continuing with staged changes
In reply to Johannes SchindelinWhen an interactive rebase is interrupted, the user may stage changes before continuing, and we need to commit those changes in that case.
Please note that the nested "if" added to the sequencer_continue() is not combined into a single "if" because it will be extended with an "else" clause in a later patch in this patch series.
--- sequencer.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
sequencer.c +40 -0
@@ -1826,6 +1826,42 @@ static int continue_single_pick(void)return run_command_v_opt (argv,RUN_GIT_CMD ); } +static int commit_staged_changes (struct replay_opts *opts) +{ +int amend =0 ; + +if (has_unstaged_changes (1 )) +return error (_ ("cannot rebase: You have unstaged changes." )); +if (!has_uncommitted_changes (0 )) +return 0 ; + +if (file_exists (rebase_path_amend ())) { +struct strbuf rev =STRBUF_INIT ; +unsigned char head[20 ], to_amend[20 ]; + +if (get_sha1 ("HEAD" , head)) +return error (_ ("cannot amend non-existing commit" )); +if (!read_oneliner (&rev,rebase_path_amend (),0 )) +return error (_ ("invalid file: '%s'" ),rebase_path_amend ()); +if (get_sha1_hex (rev.buf, to_amend)) +return error (_ ("invalid contents: '%s'" ), +rebase_path_amend ()); +if (hashcmp (head, to_amend)) +return error (_ ("\nYou have uncommitted changes in your " +"working tree. Please, commit them\n" +"first and then run 'git rebase " +"--continue' again." )); + +strbuf_release (&rev); + amend =1 ; + } + +if (run_git_commit (rebase_path_message (), opts,1 ,1 , amend,0 )) +return error (_ ("could not commit staged changes." )); +unlink (rebase_path_amend ()); +return 0 ; +} +int sequencer_continue (struct replay_opts *opts) {struct todo_list todo_list =TODO_LIST_INIT ;
@@ -1834,6 +1870,10 @@ int sequencer_continue(struct replay_opts *opts)if (read_and_refresh_cache (opts))return -1 ; +if (is_rebase_i (opts)) { +if (commit_staged_changes (opts)) +return -1 ; + }if (!file_exists (get_todo_path (opts)))return continue_single_pick ();if (read_populate_opts (opts))
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 14/38 sequencer (rebase -i): remove CHERRY_PICK_HEAD when no longer needed
In reply to Johannes SchindelinThe scripted version of the interactive rebase already does that.
--- sequencer.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
sequencer.c +6 -1
@@ -1832,8 +1832,13 @@ static int commit_staged_changes(struct replay_opts *opts)if (has_unstaged_changes (1 ))return error (_ ("cannot rebase: You have unstaged changes." )); -if (!has_uncommitted_changes (0 )) +if (!has_uncommitted_changes (0 )) { +const char *cherry_pick_head =git_path ("CHERRY_PICK_HEAD" ); + +if (file_exists (cherry_pick_head) &&unlink (cherry_pick_head)) +return error (_ ("could not remove CHERRY_PICK_HEAD" ));return 0 ; + }if (file_exists (rebase_path_amend ())) {struct strbuf rev =STRBUF_INIT ;
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 15/38 sequencer (rebase -i): skip some revert/cherry-pick specific code path
In reply to Johannes SchindelinWhen a cherry-pick continues without a "todo script", the intention is simply to pick a single commit.
However, when an interactive rebase is continued without a "todo script", it means that the last command has been completed and that we now need to clean up.
This commit guards the revert/cherry-pick specific steps so that they are not executed in rebase -i mode.
--- sequencer.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-)
sequencer.c +14 -12
@@ -1878,26 +1878,28 @@ int sequencer_continue(struct replay_opts *opts)if (is_rebase_i (opts)) {if (commit_staged_changes (opts))return -1 ; - } -if (!file_exists (get_todo_path (opts))) + }else if (!file_exists (get_todo_path (opts)))return continue_single_pick ();if (read_populate_opts (opts))return -1 ;if ((res =read_populate_todo (&todo_list, opts))) goto release_todo_list; -/* Verify that the conflict has been resolved */ -if (file_exists (git_path_cherry_pick_head ()) || -file_exists (git_path_revert_head ())) { - res =continue_single_pick (); -if (res) +if (!is_rebase_i (opts)) { +/* Verify that the conflict has been resolved */ +if (file_exists (git_path_cherry_pick_head ()) || +file_exists (git_path_revert_head ())) { + res =continue_single_pick (); +if (res) + goto release_todo_list; + } +if (index_differs_from ("HEAD" ,0 ,0 )) { + res =error_dirty_index (opts); goto release_todo_list; + } + todo_list.current++; } -if (index_differs_from ("HEAD" ,0 ,0 )) { - res =error_dirty_index (opts); - goto release_todo_list; - } - todo_list.current++; + res =pick_commits (&todo_list, opts); release_todo_list:todo_list_release (&todo_list);
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 16/38 sequencer (rebase -i): the todo can be empty when continuing
In reply to Johannes SchindelinWhen the last command of an interactive rebase fails, the user needs to resolve the problem and then continue the interactive rebase. Naturally, the todo script is empty by then. So let's not complain about that!
To that end, let's move that test out of the function that parses the todo script, and into the more high-level function read_populate_todo(). This is also necessary by now because the lower-level parse_insn_buffer() has no idea whether we are performing an interactive rebase or not.
--- sequencer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
sequencer.c +5 -2
@@ -1215,8 +1215,7 @@ static int parse_insn_buffer(char *buf, struct todo_list *todo_list)else if (!is_noop (item->command)) fixup_okay =1 ; } -if (!todo_list->nr) -return error (_ ("no commits parsed." )); +return res; }
@@ -1240,6 +1239,10 @@ static int read_populate_todo(struct todo_list *todo_list,if (res)return error (_ ("unusable instruction sheet: '%s'" ), todo_file); +if (!todo_list->nr && + (!is_rebase_i (opts) || !file_exists (rebase_path_done ()))) +return error (_ ("no commits parsed." )); +if (!is_rebase_i (opts)) {enum todo_command valid = opts->action ==REPLAY_PICK ?TODO_PICK :TODO_REVERT ;
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 17/38 sequencer (rebase -i): update refs after a successful rebase
In reply to Johannes SchindelinAn interactive rebase operates on a detached HEAD (to keep the reflog of the original branch relatively clean), and updates the branch only at the end.
Now that the sequencer learns to perform interactive rebases, it also needs to learn the trick to update the branch before removing the directory containing the state of the interactive rebase.
We introduce a new head_ref variable in a wider scope than necessary at the moment, to allow for a later patch that prints out "Successfully rebased and updated <ref>".
--- sequencer.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)
sequencer.c +45 -1
@@ -102,6 +102,8 @@ static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") +static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") +static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") static inline int is_rebase_i(const struct replay_opts *opts) {
@@ -1784,12 +1786,53 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) }if (is_rebase_i (opts)) { -struct strbuf buf =STRBUF_INIT ; +struct strbuf head_ref =STRBUF_INIT , buf =STRBUF_INIT ;/* Stopped in the middle, as planned? */ if (todo_list->current < todo_list->nr)return 0 ; +if (read_oneliner (&head_ref,rebase_path_head_name (),0 ) && +starts_with (head_ref.buf,"refs/" )) { +unsigned char head[20 ], orig[20 ]; +int res; + +if (get_sha1 ("HEAD" , head)) { + res =error (_ ("cannot read HEAD" )); +cleanup_head_ref: +strbuf_release (&head_ref); +strbuf_release (&buf); +return res; + } +if (!read_oneliner (&buf,rebase_path_orig_head (),0 ) || +get_sha1_hex (buf.buf, orig)) { + res =error (_ ("could not read orig-head" )); + goto cleanup_head_ref; + } +strbuf_addf (&buf,"rebase -i (finish): %s onto " , + head_ref.buf); +if (!read_oneliner (&buf,rebase_path_onto (),0 )) { + res =error (_ ("could not read 'onto'" )); + goto cleanup_head_ref; + } +if (update_ref (buf.buf, head_ref.buf, head, orig, +REF_NODEREF ,UPDATE_REFS_MSG_ON_ERR )) { + res =error (_ ("could not update %s" ), + head_ref.buf); + goto cleanup_head_ref; + } +strbuf_reset (&buf); +strbuf_addf (&buf, +"rebase -i (finish): returning to %s" , + head_ref.buf); +if (create_symref ("HEAD" , head_ref.buf, buf.buf)) { + res =error (_ ("could not update HEAD to %s" ), + head_ref.buf); + goto cleanup_head_ref; + } +strbuf_reset (&buf); + } +if (opts->verbose) {struct rev_info log_tree_opt;struct object_id orig, head;
@@ -1810,6 +1853,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) } }strbuf_release (&buf); +strbuf_release (&head_ref); }/*
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 18/38 sequencer (rebase -i): leave a patch upon error
In reply to Johannes SchindelinWhen doing an interactive rebase, we want to leave a 'patch' file for further inspection by the user (even if we never tried to actually apply that patch, since we're cherry-picking instead).
--- sequencer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
sequencer.c +3 -1
@@ -1769,7 +1769,9 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)intend_to_amend ();return error_failed_squash (item->commit, opts, item->arg_len, item->arg); - } + }else if (res &&is_rebase_i (opts)) +return res |error_with_patch (item->commit, + item->arg, item->arg_len, opts, res,0 ); }else if (item->command ==TODO_EXEC ) {char *end_of_arg = (char *)(item->arg + item->arg_len);int saved = *end_of_arg;
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 19/38 sequencer (rebase -i): implement the 'reword' command
In reply to Johannes SchindelinThis is now trivial, as all the building blocks are in place: all we need to do is to flip the "edit" switch when committing.
--- sequencer.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
sequencer.c +7 -2
@@ -718,6 +718,7 @@ enum todo_command {TODO_PICK =0 ,TODO_REVERT ,TODO_EDIT , +TODO_REWORD ,TODO_FIXUP , TODO_SQUASH,/* commands that do something else than handling a single commit */
@@ -733,6 +734,7 @@ static struct { { 'p', "pick" }, { 0, "revert" }, { 'e', "edit" }, + { 'r', "reword" }, { 'f', "fixup" }, { 's', "squash" }, { 'x', "exec" },
@@ -962,7 +964,9 @@ static int do_pick_commit(enum todo_command command, struct commit *commit, } } -if (is_fixup (command)) { +if (command ==TODO_REWORD ) + edit =1 ; +else if (is_fixup (command)) {if (update_squash_messages (command, commit, opts))return -1 ; amend =1 ;
@@ -1771,7 +1775,8 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) item->arg_len, item->arg); }else if (res &&is_rebase_i (opts))return res |error_with_patch (item->commit, - item->arg, item->arg_len, opts, res,0 ); + item->arg, item->arg_len, opts, res, + item->command ==TODO_REWORD ); }else if (item->command ==TODO_EXEC ) {char *end_of_arg = (char *)(item->arg + item->arg_len);int saved = *end_of_arg;
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 20/38 sequencer (rebase -i): allow fast-forwarding for edit/reword
In reply to Johannes SchindelinThe sequencer already knew how to fast-forward instead of cherry-picking, if possible.
We want to continue to do this, of course, but in case of the 'reword' command, we will need to call git commit after fast-forwarding.
--- sequencer.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)
sequencer.c +17 -8
@@ -860,7 +860,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,const char *base_label, *next_label;struct commit_message msg = {NULL ,NULL ,NULL ,NULL };struct strbuf msgbuf =STRBUF_INIT ; -int res, unborn =0 , amend =0 , allow; +int res, unborn =0 , amend =0 , allow =0 ;if (opts->no_commit) {/*
@@ -905,11 +905,23 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,else parent = commit->parents->item; +if (get_message (commit, &msg) !=0 ) +return error (_ ("cannot get commit message for %s" ), +oid_to_hex (&commit->object.oid)); +if (opts->allow_ff && !is_fixup (command) && ((parent && !hashcmp (parent->object.oid.hash, head)) || - (!parent && unborn))) -return fast_forward_to (commit->object.oid.hash, head, unborn, opts); - + (!parent && unborn))) { +if (is_rebase_i (opts)) +write_author_script (msg.message); + res =fast_forward_to (commit->object.oid.hash, head, unborn, + opts); +if (res || command !=TODO_REWORD ) + goto leave; + edit = amend =1 ; + msg_file =NULL ; + goto fast_forward_edit; + }if (parent &&parse_commit (parent) <0 )/* TRANSLATORS: The first %s will be a "todo" command like "revert" or "pick", the second %s a SHA1. */
@@ -917,10 +929,6 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,command_to_string (command), oid_to_hex(&parent->object.oid)); -if (get_message (commit, &msg) !=0 ) -return error (_ ("cannot get commit message for %s" ), -oid_to_hex (&commit->object.oid)); -/* * "commit" is an existing commit. We would want to apply * the difference it introduces since its first parent "prev"
@@ -1044,6 +1052,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit, goto leave; }if (!opts->no_commit) +fast_forward_edit: res =run_git_commit (msg_file, opts, allow, edit, amend, cleanup_commit_message);
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 21/38 sequencer (rebase -i): refactor setting the reflog message
In reply to Johannes SchindelinThis makes the code DRYer, with the obvious benefit that we can enhance the code further in a single place.
We can also reuse the functionality elsewhere by calling this new function.
--- sequencer.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-)
sequencer.c +26 -7
@@ -1743,6 +1743,26 @@ static int is_final_fixup(struct todo_list *todo_list)return 1 ; } +static const char *reflog_message (struct replay_opts *opts, +const char *sub_action,const char *fmt, ...) +{ +va_list ap; +static struct strbuf buf =STRBUF_INIT ; + +va_start (ap, fmt); +strbuf_reset (&buf); +strbuf_addstr (&buf,action_name (opts)); +if (sub_action) +strbuf_addf (&buf," (%s)" , sub_action); +if (fmt) { +strbuf_addstr (&buf,": " ); +strbuf_vaddf (&buf, fmt, ap); + } +va_end (ap); + +return buf.buf; +} +static int pick_commits (struct todo_list *todo_list,struct replay_opts *opts) {int res =0 ;
@@ -1810,6 +1830,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)if (read_oneliner (&head_ref,rebase_path_head_name (),0 ) &&starts_with (head_ref.buf,"refs/" )) { +const char *msg;unsigned char head[20 ], orig[20 ];int res;
@@ -1825,23 +1846,21 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) res =error (_ ("could not read orig-head" )); goto cleanup_head_ref; } -strbuf_addf (&buf,"rebase -i (finish): %s onto " , - head_ref.buf);if (!read_oneliner (&buf,rebase_path_onto (),0 )) { res =error (_ ("could not read 'onto'" )); goto cleanup_head_ref; } -if (update_ref (buf.buf, head_ref.buf, head, orig, + msg =reflog_message (opts,"finish" ,"%s onto %s" , + head_ref.buf, buf.buf); +if (update_ref (msg, head_ref.buf, head, orig,REF_NODEREF ,UPDATE_REFS_MSG_ON_ERR )) { res =error (_ ("could not update %s" ), head_ref.buf); goto cleanup_head_ref; } -strbuf_reset (&buf); -strbuf_addf (&buf, -"rebase -i (finish): returning to %s" , + msg =reflog_message (opts,"finish" ,"returning to %s" , head_ref.buf); -if (create_symref ("HEAD" , head_ref.buf, buf.buf)) { +if (create_symref ("HEAD" , head_ref.buf, msg)) { res =error (_ ("could not update HEAD to %s" ), head_ref.buf); goto cleanup_head_ref;
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 22/38 sequencer (rebase -i): set the reflog message consistently
In reply to Johannes SchindelinWe already used the same reflog message as the scripted version of rebase -i when finishing. With this commit, we do that also for all the commands before that.
--- sequencer.c | 4 ++++ 1 file changed, 4 insertions(+)
sequencer.c +4 -0
@@ -1785,6 +1785,10 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)unlink (rebase_path_amend ()); }if (item->command <=TODO_SQUASH ) { +if (is_rebase_i (opts)) +setenv ("GIT_REFLOG_ACTION" ,reflog_message (opts, +command_to_string (item->command),NULL ), +1 ); res =do_pick_commit (item->command, item->commit, opts,is_final_fixup (todo_list));if (item->command ==TODO_EDIT ) {
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 23/38 sequencer (rebase -i): copy commit notes at end
In reply to Johannes SchindelinWhen rebasing commits that have commit notes attached, the interactive rebase rewrites those notes faithfully at the end. The sequencer must do this, too, if it wishes to do interactive rebase's job.
--- sequencer.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
sequencer.c +76 -0
@@ -96,6 +96,15 @@ static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")*/ static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")/* +* For the post-rewrite hook, we make a list of rewritten commits and +* their new sha1s. The rewritten-pending list keeps the sha1s of +* commits that have been processed, but not committed yet, +* e.g. because they are waiting for a 'squash' command. + */ +static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") +static GIT_PATH_FUNC(rebase_path_rewritten_pending, + "rebase-merge/rewritten-pending") +/* * The following files are written by git-rebase just after parsing the * command-line (and are only consumed, not modified, by the sequencer). */
@@ -850,6 +859,44 @@ static int update_squash_messages(enum todo_command command,return res; } +static void flush_rewritten_pending (void ) { +struct strbuf buf =STRBUF_INIT ; +unsigned char newsha1[20 ]; +FILE *out; + +if (strbuf_read_file (&buf,rebase_path_rewritten_pending (),82 ) >0 && + !get_sha1 ("HEAD" , newsha1) && + (out =fopen (rebase_path_rewritten_list (),"a" ))) { +char *bol = buf.buf, *eol; + +while (*bol) { + eol =strchrnul (bol,'\n' ); +fprintf (out,"%.*s %s\n" , (int )(eol - bol), + bol,sha1_to_hex (newsha1)); +if (!*eol) +break ; + bol = eol +1 ; + } +fclose (out); +unlink (rebase_path_rewritten_pending ()); + } +strbuf_release (&buf); +} + +static void record_in_rewritten (struct object_id *oid, +enum todo_command next_command) { +FILE *out =fopen (rebase_path_rewritten_pending (),"a" ); + +if (!out) +return ; + +fprintf (out,"%s\n" ,oid_to_hex (oid)); +fclose (out); + +if (!is_fixup (next_command)) +flush_rewritten_pending (); +} +static int do_pick_commit (enum todo_command command,struct commit *commit,struct replay_opts *opts,int final_fixup) {
@@ -1743,6 +1790,17 @@ static int is_final_fixup(struct todo_list *todo_list)return 1 ; } +static enum todo_command peek_command (struct todo_list *todo_list,int offset) +{ +int i; + +for (i = todo_list->current + offset; i < todo_list->nr; i++) +if (!is_noop (todo_list->items[i].command)) +return todo_list->items[i].command; + +return -1 ; +} +static const char *reflog_message (struct replay_opts *opts,const char *sub_action,const char *fmt, ...) {
@@ -1801,6 +1859,9 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) item->arg, item->arg_len, opts, res, !res); } +if (is_rebase_i (opts) && !res) +record_in_rewritten (&item->commit->object.oid, +peek_command (todo_list,1 ));if (res &&is_fixup (item->command)) {if (res ==1 )intend_to_amend ();
@@ -1827,6 +1888,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)if (is_rebase_i (opts)) {struct strbuf head_ref =STRBUF_INIT , buf =STRBUF_INIT ; +struct stat st;/* Stopped in the middle, as planned? */ if (todo_list->current < todo_list->nr)
@@ -1891,6 +1953,20 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)log_tree_diff_flush (&log_tree_opt); } } +flush_rewritten_pending (); +if (!stat (rebase_path_rewritten_list (), &st) && + st.st_size >0 ) { +struct child_process child =CHILD_PROCESS_INIT ; + + child.in =open (rebase_path_rewritten_list (),O_RDONLY ); + child.git_cmd =1 ; +argv_array_push (&child.args,"notes" ); +argv_array_push (&child.args,"copy" ); +argv_array_push (&child.args,"--for-rewrite=rebase" ); +/* we don't care if this copying failed */ +run_command (&child); + } +strbuf_release (&buf);strbuf_release (&head_ref); }
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 24/38 sequencer (rebase -i): record interrupted commits in rewritten, too
In reply to Johannes SchindelinWhen continuing after a pick command failed, we want that commit to show up in the rewritten-list (and its notes to be rewritten), too.
--- sequencer.c | 8 ++++++++ 1 file changed, 8 insertions(+)
sequencer.c +8 -0
@@ -2060,6 +2060,14 @@ int sequencer_continue(struct replay_opts *opts) goto release_todo_list; } todo_list.current++; + }else if (file_exists (rebase_path_stopped_sha ())) { +struct strbuf buf =STRBUF_INIT ; +struct object_id oid; + +if (read_oneliner (&buf,rebase_path_stopped_sha (),1 ) && + !get_sha1_committish (buf.buf, oid.hash)) +record_in_rewritten (&oid,peek_command (&todo_list,0 )); +strbuf_release (&buf); } res =pick_commits (&todo_list, opts);
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 25/38 sequencer (rebase -i): run the post-rewrite hook, if needed
In reply to Johannes Schindelin--- sequencer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
sequencer.c +14 -0
@@ -1957,6 +1957,8 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)if (!stat (rebase_path_rewritten_list (), &st) && st.st_size >0 ) {struct child_process child =CHILD_PROCESS_INIT ; +const char *post_rewrite_hook = +find_hook ("post-rewrite" ); child.in =open (rebase_path_rewritten_list (),O_RDONLY ); child.git_cmd =1 ;
@@ -1965,6 +1967,18 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)argv_array_push (&child.args,"--for-rewrite=rebase" );/* we don't care if this copying failed */ run_command (&child); + +if (post_rewrite_hook) { +struct child_process hook =CHILD_PROCESS_INIT ; + + hook.in =open (rebase_path_rewritten_list (), +O_RDONLY ); + hook.stdout_to_stderr =1 ; +argv_array_push (&hook.args, post_rewrite_hook); +argv_array_push (&hook.args,"rebase" ); +/* we don't care if this hook failed */ +run_command (&hook); + } }strbuf_release (&buf);
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 26/38 sequencer (rebase -i): respect the rebase.autostash setting
In reply to Johannes SchindelinGit's rebase command inspects the rebase.autostash config setting to determine whether it should stash any uncommitted changes before rebasing and re-apply them afterwards.
As we introduce more bits and pieces to let the sequencer act as interactive rebase's backend, here is the part that adds support for the autostash feature.
--- sequencer.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
sequencer.c +43 -0
@@ -113,6 +113,7 @@ static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") +static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") static inline int is_rebase_i(const struct replay_opts *opts) {
@@ -1801,6 +1802,47 @@ static enum todo_command peek_command(struct todo_list *todo_list, int offset)return -1 ; } +static int apply_autostash (struct replay_opts *opts) +{ +struct strbuf stash_sha1 =STRBUF_INIT ; +struct child_process child =CHILD_PROCESS_INIT ; +int ret =0 ; + +if (!read_oneliner (&stash_sha1,rebase_path_autostash (),1 )) { +strbuf_release (&stash_sha1); +return 0 ; + } +strbuf_trim (&stash_sha1); + + child.git_cmd =1 ; +argv_array_push (&child.args,"stash" ); +argv_array_push (&child.args,"apply" ); +argv_array_push (&child.args, stash_sha1.buf); +if (!run_command (&child)) +printf (_ ("Applied autostash." )); +else { +struct child_process store =CHILD_PROCESS_INIT ; + + store.git_cmd =1 ; +argv_array_push (&store.args,"stash" ); +argv_array_push (&store.args,"store" ); +argv_array_push (&store.args,"-m" ); +argv_array_push (&store.args,"autostash" ); +argv_array_push (&store.args,"-q" ); +argv_array_push (&store.args, stash_sha1.buf); +if (run_command (&store)) + ret =error (_ ("cannot store %s" ), stash_sha1.buf); +else +printf (_ ("Applying autostash resulted in conflicts.\n" +"Your changes are safe in the stash.\n" +"You can run \"git stash pop\" or" +" \"git stash drop\" at any time.\n" )); + } + +strbuf_release (&stash_sha1); +return ret; +} +static const char *reflog_message (struct replay_opts *opts,const char *sub_action,const char *fmt, ...) {
@@ -1980,6 +2022,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)run_command (&hook); } } +apply_autostash (opts);strbuf_release (&buf);strbuf_release (&head_ref);
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 27/38 sequencer (rebase -i): respect strategy/strategy_opts settings
In reply to Johannes SchindelinThe sequencer already has an idea about using different merge strategies. We just piggy-back on top of that, using rebase -i's own settings, when running the sequencer in interactive rebase mode.
--- sequencer.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
sequencer.c +25 -1
@@ -114,6 +114,8 @@ static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") +static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") +static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") static inline int is_rebase_i(const struct replay_opts *opts) {
@@ -1368,6 +1370,26 @@ static int populate_opts_cb(const char *key, const char *value, void *data)return 0 ; } +static void read_strategy_opts (struct replay_opts *opts,struct strbuf *buf) +{ +int i; + +strbuf_reset (buf); +if (!read_oneliner (buf,rebase_path_strategy (),0 )) +return ; + opts->strategy =strbuf_detach (buf,NULL ); +if (!read_oneliner (buf,rebase_path_strategy_opts (),0 )) +return ; + + opts->xopts_nr =split_cmdline (buf->buf, (const char ***)&opts->xopts); +for (i =0 ; i < opts->xopts_nr; i++) { +const char *arg = opts->xopts[i]; + +skip_prefix (arg,"--" , &arg); + opts->xopts[i] =xstrdup (arg); + } +} +static int read_populate_opts (struct replay_opts *opts) {if (is_rebase_i (opts)) {
@@ -1381,11 +1403,13 @@ static int read_populate_opts(struct replay_opts *opts) opts->gpg_sign =xstrdup (buf.buf +2 ); } } -strbuf_release (&buf);if (file_exists (rebase_path_verbose ())) opts->verbose =1 ; +read_strategy_opts (opts, &buf); +strbuf_release (&buf); +return 0 ; }
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 28/38 sequencer (rebase -i): allow rescheduling commands
In reply to Johannes SchindelinThe interactive rebase has the very special magic that a cherry-pick that exits with a status different from 0 and 1 signifies a failure to even record that a cherry-pick was started.
This can happen e.g. when a fast-forward fails because it would overwrite untracked files.
In that case, we must reschedule the command that we thought we already had at least started successfully.
--- sequencer.c | 6 ++++++ 1 file changed, 6 insertions(+)
sequencer.c +6 -0
@@ -1915,6 +1915,12 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) 1); res =do_pick_commit (item->command, item->commit, opts,is_final_fixup (todo_list)); +if (is_rebase_i (opts) && res <0 ) { +/* Reschedule */ + todo_list->current--; +if (save_todo (todo_list, opts)) +return -1 ; + }if (item->command ==TODO_EDIT ) {struct commit *commit = item->commit;if (!res)
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 29/38 sequencer (rebase -i): implement the 'drop' command
In reply to Johannes SchindelinThe parsing part of a 'drop' command is almost identical to parsing a 'pick', while the operation is the same as that of a 'noop'.
--- sequencer.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
sequencer.c +5 -3
@@ -736,7 +736,8 @@ enum todo_command {/* commands that do something else than handling a single commit */ TODO_EXEC ,/* commands that do nothing but are counted for reporting progress */ -TODO_NOOP +TODO_NOOP , +TODO_DROP }; static struct {
@@ -750,7 +751,8 @@ static struct { { 'f', "fixup" }, { 's', "squash" }, { 'x', "exec" }, - { 0, "noop" } + { 0, "noop" }, + { 'd', "drop" } };static const char *command_to_string (const enum todo_command command)
@@ -762,7 +764,7 @@ static const char *command_to_string(const enum todo_command command)static int is_noop (const enum todo_command command) { -return TODO_NOOP <= (size_t )command; +return TODO_NOOP <= command; }static int is_fixup (enum todo_command command)
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 30/38 sequencer (rebase -i): differentiate between comments and 'noop'
In reply to Johannes SchindelinIn the upcoming patch, we will support rebase -i's progress reporting. The progress skips comments but counts 'noop's.
--- sequencer.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
sequencer.c +9 -6
@@ -737,7 +737,9 @@ enum todo_command {TODO_EXEC ,/* commands that do nothing but are counted for reporting progress */ TODO_NOOP , -TODO_DROP +TODO_DROP , +/* comments (not counted for reporting progress) */ +TODO_COMMENT }; static struct {
@@ -752,12 +754,13 @@ static struct { { 's', "squash" }, { 'x', "exec" }, { 0, "noop" }, - { 'd', "drop" } + { 'd', "drop" }, + { 0, NULL } };static const char *command_to_string (const enum todo_command command) { -if ((size_t )command <ARRAY_SIZE (todo_command_info)) +if (command <TODO_COMMENT )return todo_command_info[command].str;die ("Unknown command: %d" , command); }
@@ -1198,14 +1201,14 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) bol +=strspn (bol," \t" );if (bol == eol || *bol =='\r' || *bol == comment_line_char) { - item->command =TODO_NOOP ; + item->command =TODO_COMMENT ; item->commit =NULL ; item->arg = bol; item->arg_len = eol - bol;return 0 ; } -for (i =0 ; i <ARRAY_SIZE (todo_command_info); i++) +for (i =0 ; i <TODO_COMMENT ; i++)if (skip_prefix (bol, todo_command_info[i].str, &bol)) { item->command = i;break ;
@@ -1214,7 +1217,7 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) item->command = i;break ; } -if (i >=ARRAY_SIZE (todo_command_info)) +if (i >=TODO_COMMENT )return -1 ;if (item->command ==TODO_NOOP ) {
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 31/38 sequencer: make reading author-script more elegant
In reply to Johannes SchindelinRather than abusing a strbuf to come up with an environment block, let's just use the argv_array structure which serves the same purpose much better.
While at it, rename the function to reflect the fact that it does not really care exactly what environment variables are defined in said file.
--- sequencer.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-)
sequencer.c +11 -21
@@ -544,18 +544,17 @@ static int write_author_script(const char *message) }/* -* Read the author-script file into an environment block, ready for use in -* run_command(), that can be free()d afterwards. +* Read a list of environment variable assignments (such as the author-script +* file) into an environment block. Returns -1 on error, 0 otherwise. */ -static char **read_author_script (void ) +static int read_env_script (struct argv_array *env) {struct strbuf script =STRBUF_INIT ;int i, count =0 ; -char *p, *p2, **env; -size_t env_size; +char *p, *p2;if (strbuf_read_file (&script,rebase_path_author_script (),256 ) <=0 ) -return NULL ; +return -1 ;for (p = script.buf; *p; p++)if (skip_prefix (p,"'\\\\''" , (const char **)&p2))
@@ -567,19 +566,12 @@ static char **read_author_script(void) count++; } - env_size = (count +1 ) *sizeof (*env); -strbuf_grow (&script, env_size); -memmove (script.buf + env_size, script.buf, script.len); - p = script.buf + env_size; - env = (char **)strbuf_detach (&script,NULL ); -for (i =0 ; i < count; i++) { - env[i] = p; +argv_array_push (env, p); p +=strlen (p) +1 ; } - env[count] =NULL ; -return env; +return 0 ; } static const char staged_changes_advice[] =
@@ -612,14 +604,12 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts, int allow_empty, int edit, int amend, int cleanup_commit_message) { - char **env = NULL; -struct argv_array array; + struct argv_array env = ARGV_ARRAY_INIT, array;int rc;const char *value;if (is_rebase_i (opts)) { - env =read_author_script (); -if (!env) { +if (!read_env_script (&env)) {const char *gpg_opt =gpg_sign_opt_quoted (opts); return error(_(staged_changes_advice),
@@ -655,9 +645,9 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,argv_array_push (&array,"--allow-empty-message" ); rc =run_command_v_opt_cd_env (array.argv,RUN_GIT_CMD ,NULL , - (const char *const *)env); + (const char *const *)env.argv);argv_array_clear (&array); -free (env); +argv_array_clear (&env);return rc; }
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 32/38 sequencer: use run_command() directly
In reply to Johannes SchindelinInstead of using the convenience function run_command_v_opt_cd_env(), we now use the run_command() function. The former function is simply a wrapper of the latter, trying to make it more convenient to use.
However, we already have to construct the argv and the env parameters, and we will need even finer control e.g. over the output of the command, so let's just stop using the convenience function.
Based on patches and suggestions by Johannes Sixt and Jeff King.
--- sequencer.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-)
sequencer.c +16 -20
@@ -604,12 +604,13 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts, int allow_empty, int edit, int amend, int cleanup_commit_message) { - struct argv_array env = ARGV_ARRAY_INIT, array; -int rc; + struct child_process cmd = CHILD_PROCESS_INIT;const char *value; + cmd.git_cmd =1 ; +if (is_rebase_i (opts)) { -if (!read_env_script (&env)) { +if (read_env_script (&cmd.env_array)) {const char *gpg_opt =gpg_sign_opt_quoted (opts); return error(_(staged_changes_advice),
@@ -617,39 +618,34 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts, } } -argv_array_init (&array); -argv_array_push (&array,"commit" ); -argv_array_push (&array,"-n" ); +argv_array_push (&cmd.args,"commit" ); +argv_array_push (&cmd.args,"-n" );if (amend) -argv_array_push (&array,"--amend" ); +argv_array_push (&cmd.args,"--amend" );if (opts->gpg_sign) -argv_array_pushf (&array,"-S%s" , opts->gpg_sign); +argv_array_pushf (&cmd.args,"-S%s" , opts->gpg_sign);if (opts->signoff) -argv_array_push (&array,"-s" ); +argv_array_push (&cmd.args,"-s" );if (defmsg) -argv_array_pushl (&array,"-F" , defmsg,NULL ); +argv_array_pushl (&cmd.args,"-F" , defmsg,NULL );if (cleanup_commit_message) -argv_array_push (&array,"--cleanup=strip" ); +argv_array_push (&cmd.args,"--cleanup=strip" );if (edit) -argv_array_push (&array,"-e" ); +argv_array_push (&cmd.args,"-e" );else if (!cleanup_commit_message && !opts->signoff && !opts->record_origin &&git_config_get_value ("commit.cleanup" , &value)) -argv_array_push (&array,"--cleanup=verbatim" ); +argv_array_push (&cmd.args,"--cleanup=verbatim" );if (allow_empty) -argv_array_push (&array,"--allow-empty" ); +argv_array_push (&cmd.args,"--allow-empty" );if (opts->allow_empty_message) -argv_array_push (&array,"--allow-empty-message" ); +argv_array_push (&cmd.args,"--allow-empty-message" ); - rc =run_command_v_opt_cd_env (array.argv,RUN_GIT_CMD ,NULL , - (const char *const *)env.argv); -argv_array_clear (&array); -argv_array_clear (&env); -return rc; +return run_command (&cmd); }static int is_original_commit_empty (struct commit *commit)
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 33/38 sequencer (rebase -i): show only failed `git commit`'s output
In reply to Johannes SchindelinThis is the behavior of the shell script version of the interactive rebase, by using the output function defined in git-rebase.sh.
--- sequencer.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
sequencer.c +18 -0
@@ -610,6 +610,11 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts, cmd.git_cmd =1 ;if (is_rebase_i (opts)) { +if (!edit) { + cmd.stdout_to_stderr =1 ; + cmd.err =-1 ; + } +if (read_env_script (&cmd.env_array)) {const char *gpg_opt =gpg_sign_opt_quoted (opts);
@@ -644,6 +649,19 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,if (opts->allow_empty_message)argv_array_push (&cmd.args,"--allow-empty-message" ); +if (cmd.err ==-1 ) { +/* hide stderr on success */ +struct strbuf buf =STRBUF_INIT ; +int rc =pipe_command (&cmd, +NULL ,0 , +/* stdout is already redirected */ +NULL ,0 , + &buf,0 ); +if (rc) +fputs (buf.buf, stderr); +strbuf_release (&buf); +return rc; + }return run_command (&cmd); }
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 34/38 sequencer (rebase -i): show only failed cherry-picks' output
In reply to Johannes SchindelinThis is the behavior of the shell script version of the interactive rebase, by using the output function defined in git-rebase.sh.
--- sequencer.c | 4 ++++ 1 file changed, 4 insertions(+)
sequencer.c +4 -0
@@ -433,6 +433,8 @@ static int do_recursive_merge(struct commit *base, struct commit *next, o.ancestor = base ? base_label :"(empty tree)" ; o.branch1 ="HEAD" ; o.branch2 = next ? next_label :"(empty tree)" ; +if (is_rebase_i (opts)) + o.buffer_output =2 ; head_tree =parse_tree_indirect (head); next_tree = next ? next->tree :empty_tree ();
@@ -444,6 +446,8 @@ static int do_recursive_merge(struct commit *base, struct commit *next, clean =merge_trees (&o, head_tree, next_tree, base_tree, &result); +if (is_rebase_i (opts) && clean <=0 ) +fputs (o.obuf.buf, stdout);strbuf_release (&o.obuf);if (clean <0 )return clean;
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 35/38 sequencer (rebase -i): suggest --edit-todo upon unknown command
In reply to Johannes SchindelinThis is the same behavior as known from git rebase -i.
--- sequencer.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
sequencer.c +5 -1
@@ -1314,8 +1314,12 @@ static int read_populate_todo(struct todo_list *todo_list,close (fd); res =parse_insn_buffer (todo_list->buf.buf, todo_list); -if (res) +if (res) { +if (is_rebase_i (opts)) +return error (_ ("please fix this using " +"'git rebase --edit-todo'." ));return error (_ ("unusable instruction sheet: '%s'" ), todo_file); + }if (!todo_list->nr && (!is_rebase_i (opts) || !file_exists (rebase_path_done ())))
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 36/38 sequencer (rebase -i): show the progress
In reply to Johannes SchindelinThe interactive rebase keeps the user informed about its progress. If the sequencer wants to do the grunt work of the interactive rebase, it also needs to show that progress.
--- sequencer.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
sequencer.c +32 -0
@@ -1181,6 +1181,7 @@ struct todo_list {struct strbuf buf;struct todo_item *items;int nr, alloc, current; +int done_nr, total_nr; };#define TODO_LIST_INIT { STRBUF_INIT }
@@ -1297,6 +1298,17 @@ static int parse_insn_buffer(char *buf, struct todo_list *todo_list)return res; } +static int count_commands (struct todo_list *todo_list) +{ +int count =0 , i; + +for (i =0 ; i < todo_list->nr; i++) +if (todo_list->items[i].command !=TODO_COMMENT ) + count++; + +return count; +} +static int read_populate_todo (struct todo_list *todo_list,struct replay_opts *opts) {
@@ -1339,6 +1351,21 @@ static int read_populate_todo(struct todo_list *todo_list,return error (_ ("cannot revert during a cherry-pick." )); } +if (is_rebase_i (opts)) { +struct todo_list done =TODO_LIST_INIT ; + +if (strbuf_read_file (&done.buf,rebase_path_done (),0 ) >0 && + !parse_insn_buffer (done.buf.buf, &done)) + todo_list->done_nr =count_commands (&done); +else + todo_list->done_nr =0 ; + + todo_list->total_nr = todo_list->done_nr + +count_commands (todo_list); + +todo_list_release (&done); + } +return 0 ; }
@@ -1920,6 +1947,11 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)if (save_todo (todo_list, opts))return -1 ;if (is_rebase_i (opts)) { +if (item->command !=TODO_COMMENT ) +fprintf (stderr,"Rebasing (%d/%d)%s" , + ++(todo_list->done_nr), + todo_list->total_nr, + opts->verbose ?"\n" :"\r" );unlink (rebase_path_message ());unlink (rebase_path_author_script ());unlink (rebase_path_stopped_sha ());
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 37/38 sequencer (rebase -i): write the progress into files
In reply to Johannes SchindelinFor the benefit of e.g. the shell prompt, the interactive rebase not only displays the progress for the user to see, but also writes it into the msgnum/end files in the state directory.
Teach the sequencer this new trick.
--- sequencer.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-)
sequencer.c +27 -3
@@ -47,6 +47,16 @@ static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")*/ static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done")/* +* The file to keep track of how many commands were already processed (e.g. +* for the prompt). + */ +staticGIT_PATH_FUNC (rebase_path_msgnum,"rebase-merge/msgnum" ); +/* +* The file to keep track of how many commands are to be processed in total +* (e.g. for the prompt). +*/ +static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end"); +/* * The commit message that is planned to be used for any changes that * need to be committed following a user interaction. */
@@ -1353,6 +1363,7 @@ static int read_populate_todo(struct todo_list *todo_list,if (is_rebase_i (opts)) {struct todo_list done =TODO_LIST_INIT ; +FILE *f =fopen (rebase_path_msgtotal (),"w" );if (strbuf_read_file (&done.buf,rebase_path_done (),0 ) >0 && !parse_insn_buffer (done.buf.buf, &done))
@@ -1362,8 +1373,12 @@ static int read_populate_todo(struct todo_list *todo_list, todo_list->total_nr = todo_list->done_nr +count_commands (todo_list); -todo_list_release (&done); + +if (f) { +fprintf (f,"%d\n" , todo_list->total_nr); +fclose (f); + } }return 0 ;
@@ -1947,11 +1962,20 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)if (save_todo (todo_list, opts))return -1 ;if (is_rebase_i (opts)) { -if (item->command !=TODO_COMMENT ) +if (item->command !=TODO_COMMENT ) { +FILE *f =fopen (rebase_path_msgnum (),"w" ); + + todo_list->done_nr++; + +if (f) { +fprintf (f,"%d\n" , todo_list->done_nr); +fclose (f); + }fprintf (stderr,"Rebasing (%d/%d)%s" , - ++(todo_list->done_nr), + todo_list->done_nr, todo_list->total_nr, opts->verbose ?"\n" :"\r" ); + }unlink (rebase_path_message ());unlink (rebase_path_author_script ());unlink (rebase_path_stopped_sha ());
Signature · 1 line
2.11.0.rc3.windows.1
PATCH v3 38/38 sequencer (rebase -i): write out the final message
In reply to Johannes SchindelinThe shell script version of the interactive rebase has a very specific final message. Teach the sequencer to print the same.
--- sequencer.c | 3 +++ 1 file changed, 3 insertions(+)
sequencer.c +3 -0
@@ -2127,6 +2127,9 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) }apply_autostash (opts); +fprintf (stderr,"Successfully rebased and updated %s.\n" , + head_ref.buf); +strbuf_release (&buf);strbuf_release (&head_ref); }
Signature · 1 line
2.11.0.rc3.windows.1
Quoted 6 lines · Johannes Schindelin
Changes since v2:
- fixed a TRANSLATORS: comment
...
- replaced a spawneddiff-treecommand by code using the diff functions directly
I just finished skimming the interdiff (the difference between the result of merging the v2 into 'master' and the result of applying this series on 'master'). I didn't cross reference it with all the review comments that I saw on the list for the previous round, I wouldn't have noticed even if there are issues that still haven't been addressed. But what I saw in the interdiff were mostly good changes.
I however think that the renaming of read_author_script() is totally backwards from maintainability's point of view.
Both "am" and "rebase" use an external file whose name is the same "author-script" (because they are meant to serve the same purpose), and introduction of new code that is different from what "am" uses to read it in v2 was bad enough. If the rename of the function in this round really means "author-script of 'am' can hold only author information, but that of 'rebase' can hold anything the end user desires", it makes things even worse. If 'rebase' side extends the file to hold other environment variables (in its own code), users would wonder why the same extension will not be available in 'am' side (and vice versa).
I ran out of time for the day and haven't looked at individual patches yet. I may find other issues, but from the interdiff, that was the only thing that I found even worse than the previous round, and everything else was either the same or has improved.
Thanks.
Quoted 4 lines · Junio C Hamano
I ran out of time for the day and haven't looked at individual patches yet. I may find other issues, but from the interdiff, that was the only thing that I found even worse than the previous round, and everything else was either the same or has improved.
... I forgot to say a few things:
* In the meantime, I think this is overall better than what we had for the past few weeks; I'll try to replace the copy in my tree with this round, even though this may not be the final one.
* There were a few style issues [*1*] pointed out by the checkpatch.pl script borrowed from the kernel land.
* And finally, you wrote "This marks the count down to '3': two more patch series after this (really tiny ones) and we have a faster rebase -i." This is seriously an exciting news. Thanks.
I probably am still forgetting to say a few more things, but I'd send this message out in this shape for now. Thanks.
[Footnote]
*1*
Subject: [PATCH v3 10/38] sequencer (rebase -i): add support for the 'fixup' and 'squash' commands
ERROR: do not use assignment in if condition
#118: FILE: sequencer.c:725:
+ if (buf.buf[0] != comment_line_char ||
ERROR: do not use assignment in if condition
#142: FILE: sequencer.c:749:
+ if (!(head_commit = lookup_commit_reference(head)))
ERROR: do not use assignment in if condition
#144: FILE: sequencer.c:751:
+ if (!(head_message = get_commit_buffer(head_commit, NULL)))
ERROR: do not use assignment in if condition
#167: FILE: sequencer.c:774:
+ if (!(message = get_commit_buffer(commit, NULL)))
total: 4 errors, 1 warnings, 320 lines checked
------------------------------------------------
Subject: [PATCH v3 12/38] sequencer (rebase -i): write an author-script file
ERROR: do not use assignment in if condition
#35: FILE: sequencer.c:500:
+ else if ((eol = strchr(message, '\n')))
total: 1 errors, 0 warnings, 62 lines checked
------------------------------------------------
Subject: [PATCH v3 23/38] sequencer (rebase -i): copy commit notes at end
ERROR: open brace '{' following function declarations go on the next line #36: FILE: sequencer.c:862:
+static void flush_rewritten_pending(void) {
ERROR: do not use assignment in if condition
#41: FILE: sequencer.c:867:
+ if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 &&
total: 2 errors, 0 warnings, 112 lines checked
Hi Junio,
Quoted 4 lines · Junio C Hamano
Quoted 2 lines · Johannes Schindelin
Changes since v2:
Quoted 8 lines
Quoted 4 lines
- fixed a TRANSLATORS: comment
...
- replaced a spawneddiff-treecommand by code using the diff functions directlyI just finished skimming the interdiff (the difference between the result of merging the v2 into 'master' and the result of applying this series on 'master').
I wish you would not have skimmed it, but provided a thorough review. There was a rather serious bug in this (not the first problem introduced into one of my patch series *because of* code review, unhidden-git and mmap-regexec are also very recent examples, I really should learn to resist the prodding to replace well-tested code with code of unknown correctness).
The problem in this instance was that the authorship is no longer retained when continuing after resolving a conflict. Let me stress again that this has not been a problem with v1 of sequencer-i, nor with v2. The regression was caused by changes required by the code review.
In case you wonder: Yes, I am upset by this.
The required fixup patch is:
-- snipsnap --
Subject: [PATCH] fixup! sequencer: make reading author-script more elegant
An unfortunate regression of formerly battle-tested code sadly crept into Git for Windows v2.11.0(2): authorship was not retained in case of conflicts during picks.
--- sequencer.c | 2 +- t/t3404-rebase-interactive.sh | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
sequencer.c +1 -1
@@ -612,7 +612,7 @@ static int read_env_script(struct argv_array *env) count++; } -for (i =0 ; i < count; i++) { +for (i =0 , p = script.buf; i < count; i++) {argv_array_push (env, p); p +=strlen (p) +1 ; }
t/t3404-rebase-interactive.sh +16 -0
@@ -237,6 +237,22 @@ test_expect_success 'retain authorship' ' git show HEAD | grep "^Author: Twerp Snog" ' +test_expect_success 'retain authorship w/ conflicts' ' + git reset --hard twerp && + test_commit a conflict a conflict-a && + git reset --hard twerp && + GIT_AUTHOR_NAME=AttributeMe \ + test_commit b conflict b conflict-b && + set_fake_editor && + test_must_fail git rebase -i conflict-a && + echo resolved >conflict && + git add conflict && + git rebase --continue && + test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) && + git show >out && + grep AttributeMe out +' + test_expect_success 'squash' ' git reset --hard twerp && echo B > file7 &&
Signature · 1 line
2.11.0.windows.3
Quoted 9 lines · Johannes Schindelin
The problem in this instance was that the authorship is no longer retained when continuing after resolving a conflict. Let me stress again that this has not been a problem with v1 of sequencer-i, nor with v2. The regression was caused by changes required by the code review.
In case you wonder: Yes, I am upset by this.
-- snipsnap --
Subject: [PATCH] fixup! sequencer: make reading author-script more elegant
I do not think anybody asked to make the code "more elegant". Quite frankly, I do not expect elegance in your code (or any of the code in our codebase, for that matter). What we want is readable code that does not make the overall codebase less maintainable that is correct. Not reinveting a new codepath when there is already code that does the thing is one of the things that we may need to do, but that was not done between these rerolls.
Of course, when trying to share code, the existing code we have that the new codepath needs to borrow would have to be refactored and extended, and a new bug can sneak in during the process. If that were what happened, I would be a bit more sympathetic, but I suspect that this "more elegant" thing that needed fix-up is not that.
You may be upset, but I cannot quite bring myself to feel sympathy in this particular case.
Hi Junio,
Quoted 2 lines · Junio C Hamano
I however think that the renaming of
read_author_script()is totally backwards from maintainability's point of view.
You stated elsewhere that converting a script into a builtin should focus on a faithful conversion.
The original code is:
. "$author_script"
Granted, this *cannot* be converted faithfully without reimplementing a shell interpreter. So I did the next best thing: I converted it into code that reads a block of environment variable settings.
What you asked for is totally unreasonable: you ask me to make this conversion *even less faithful*.
What is worse: you argue from the "maintainability point of view", when it is pretty obvious that *adding validation that was not there before* can in no way make the code more maintainable, as it *adds new logic*.
And what is the worst: over all these discussions about a nothingburger (you simply cannot convince me that I should introduce validating code that has not been there before in the same patch series that simply tries to recreate existing functionality), the most important part of a code review was forgotten: to make sure that the changes are correct.
The worst direction a code review can take is to introduce regressions, and that is exactly what happened.
Ciao,
Johannes
Quoted 10 lines · Johannes Schindelin
You stated elsewhere that converting a script into a builtin should focus on a faithful conversion.
The original code is:
. "$author_script"Granted, this *cannot* be converted faithfully without reimplementing a shell interpreter. So I did the next best thing: I converted it into code that reads a block of environment variable settings.
It is unfortunate that you took "faithful" too literally. While I do appreciate it if your conversion aims to faithfully replicate the original, even to be bug-to-bug compatible, but obviously we cannot replicate everything the above original _could_ do.
By "a faithful conversion", I meant that the behaviour of the reimplementation should be as faithful to the original's intent as possible. Nothing more.
The intent of the above original is to read back what we wrote to preserve the author identity we learned earlier when we wrote the file. We read the "author" line from the commit object header, and write assignments to GIT_AUTHOR_{NAME,EMAIL,DATE} variables. Nothing more is intended.
The end-users COULD abuse the original code to cause it to do a lot more than that, e.g. by adding "export FOO=BAR" at the end and have the value of the new environment variable propagate throughout the code and even down to subprocesses. They can even assign to some variables we use internally for bookkeeping and break "rebase -i" machinery. But that is outside the intent of the original code--we do not need to or want to replicate that faithfully.
I also need to react to the "environment variable settings" at the end of the quoted paragraph.
If the code in the sequencer.c reads things other than the three variables we ourselves set, and make them into environment variables and propagate to subprocesses (hooks and editors), it would be a bug. The original did not intend to do that (the dot-sourcing is overly loose than reading three known variables and nothing else, but is OK because we do not support the case where end users muck with the file). Also, writing FOO=BAR alone (not "export FOO=BAR" or "FOO=BAR; export FOO") to the file wouldn't have exported FOO to subprocesses anyway.
Hi Junio,
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Johannes Schindelin
You stated elsewhere that converting a script into a builtin should focus on a faithful conversion.
The original code is:
. "$author_script"[...]
Quoted 9 lines
If the code in the
sequencer.creads things other than the three variables we ourselves set, and make them into environment variables and propagate to subprocesses (hooks and editors), it would be a bug. The original did not intend to do that (the dot-sourcing is overly loose than reading three known variables and nothing else, but is OK because we do not support the case where end users muck with the file). Also, writing FOO=BAR alone (not "export FOO=BAR" or "FOO=BAR; export FOO") to the file wouldn't have exported FOO to subprocesses anyway.
That analysis cannot be completely correct, as the GIT_AUTHOR_* variables *are* used by the git commit subprocess.
In any case, it is clear that we (I include all reviewers here) messed this patch series up quite a bit. Hence I will be more careful from now on to only act on suggestions that do, in fact, improve the patch series from my point of view.
Ciao,
Johannes
Quoted 16 lines · Johannes Schindelin
Quoted 6 lines
Quoted 3 lines
The original code is:
. "$author_script"[...]
Quoted 9 lines
If the code in the
sequencer.creads things other than the three variables we ourselves set, and make them into environment variables and propagate to subprocesses (hooks and editors), it would be a bug. The original did not intend to do that (the dot-sourcing is overly loose than reading three known variables and nothing else, but is OK because we do not support the case where end users muck with the file). Also, writing FOO=BAR alone (not "export FOO=BAR" or "FOO=BAR; export FOO") to the file wouldn't have exported FOO to subprocesses anyway.
Quoted 2 lines
That analysis cannot be completely correct, as the GIT_AUTHOR_* variables *are* used by the
git commitsubprocess.
In the scripted version, do_with_author shell function explicitly exports GIT_AUTHOR_* variables because they are the only ones we care about that are read from existing commit and carried forward via the author-script mechanism. We do not care about, and in fact we do not intend to support, any other variables or shell commands that appear in the author-script.