From 946a9f20b46b6cf8fbfbcc69ed97182f8696bb8d Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Sun, 3 Dec 2017 17:17:13 -0500 Subject: [PATCH 01/10] Documentation: move rebase.* configs to new file Move all rebase.* configuration variables to a separate file in order to remove duplicates, and include it in config.txt and git-rebase.txt. The new descriptions are mostly taken from config.txt as they are more verbose. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- Documentation/config.txt | 31 +------------------------------ Documentation/git-rebase.txt | 19 +------------------ Documentation/rebase-config.txt | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 48 deletions(-) create mode 100644 Documentation/rebase-config.txt diff --git a/Documentation/config.txt b/Documentation/config.txt index 531649cb40..e424b7de90 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2691,36 +2691,7 @@ push.recurseSubmodules:: is retained. You may override this configuration at time of push by specifying '--recurse-submodules=check|on-demand|no'. -rebase.stat:: - Whether to show a diffstat of what changed upstream since the last - rebase. False by default. - -rebase.autoSquash:: - If set to true enable `--autosquash` option by default. - -rebase.autoStash:: - When set to true, automatically create a temporary stash entry - before the operation begins, and apply it after the operation - ends. This means that you can run rebase on a dirty worktree. - However, use with care: the final stash application after a - successful rebase might result in non-trivial conflicts. - Defaults to false. - -rebase.missingCommitsCheck:: - If set to "warn", git rebase -i will print a warning if some - commits are removed (e.g. a line was deleted), however the - rebase will still proceed. If set to "error", it will print - the previous warning and stop the rebase, 'git rebase - --edit-todo' can then be used to correct the error. If set to - "ignore", no checking is done. - To drop a commit without warning or error, use the `drop` - command in the todo-list. - Defaults to "ignore". - -rebase.instructionFormat:: - A format string, as specified in linkgit:git-log[1], to be used for - the instruction list during an interactive rebase. The format will automatically - have the long commit hash prepended to the format. +include::rebase-config.txt[] receive.advertiseAtomic:: By default, git-receive-pack will advertise the atomic push diff --git a/Documentation/git-rebase.txt b/Documentation/git-rebase.txt index 3cedfb0fd2..8a861c1e0d 100644 --- a/Documentation/git-rebase.txt +++ b/Documentation/git-rebase.txt @@ -203,24 +203,7 @@ Alternatively, you can undo the 'git rebase' with CONFIGURATION ------------- -rebase.stat:: - Whether to show a diffstat of what changed upstream since the last - rebase. False by default. - -rebase.autoSquash:: - If set to true enable `--autosquash` option by default. - -rebase.autoStash:: - If set to true enable `--autostash` option by default. - -rebase.missingCommitsCheck:: - If set to "warn", print warnings about removed commits in - interactive mode. If set to "error", print the warnings and - stop the rebase. If set to "ignore", no checking is - done. "ignore" by default. - -rebase.instructionFormat:: - Custom commit list format to use during an `--interactive` rebase. +include::rebase-config.txt[] OPTIONS ------- diff --git a/Documentation/rebase-config.txt b/Documentation/rebase-config.txt new file mode 100644 index 0000000000..dba088d7c6 --- /dev/null +++ b/Documentation/rebase-config.txt @@ -0,0 +1,32 @@ +rebase.stat:: + Whether to show a diffstat of what changed upstream since the last + rebase. False by default. + +rebase.autoSquash:: + If set to true enable `--autosquash` option by default. + +rebase.autoStash:: + When set to true, automatically create a temporary stash entry + before the operation begins, and apply it after the operation + ends. This means that you can run rebase on a dirty worktree. + However, use with care: the final stash application after a + successful rebase might result in non-trivial conflicts. + This option can be overridden by the `--no-autostash` and + `--autostash` options of linkgit:git-rebase[1]. + Defaults to false. + +rebase.missingCommitsCheck:: + If set to "warn", git rebase -i will print a warning if some + commits are removed (e.g. a line was deleted), however the + rebase will still proceed. If set to "error", it will print + the previous warning and stop the rebase, 'git rebase + --edit-todo' can then be used to correct the error. If set to + "ignore", no checking is done. + To drop a commit without warning or error, use the `drop` + command in the todo-list. + Defaults to "ignore". + +rebase.instructionFormat:: + A format string, as specified in linkgit:git-log[1], to be used for the + instruction list during an interactive rebase. The format will + automatically have the long commit hash prepended to the format. From f3b633dad4cc54c7cdaa007e97d00b2c935c9a90 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Sun, 3 Dec 2017 17:17:14 -0500 Subject: [PATCH 02/10] Documentation: use preferred name for the 'todo list' script Use "todo list" instead of "instruction list" or "todo-list" to reduce further confusion regarding the name of this script. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- Documentation/rebase-config.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/rebase-config.txt b/Documentation/rebase-config.txt index dba088d7c6..30ae08cb5a 100644 --- a/Documentation/rebase-config.txt +++ b/Documentation/rebase-config.txt @@ -23,10 +23,10 @@ rebase.missingCommitsCheck:: --edit-todo' can then be used to correct the error. If set to "ignore", no checking is done. To drop a commit without warning or error, use the `drop` - command in the todo-list. + command in the todo list. Defaults to "ignore". rebase.instructionFormat:: A format string, as specified in linkgit:git-log[1], to be used for the - instruction list during an interactive rebase. The format will + todo list during an interactive rebase. The format will automatically have the long commit hash prepended to the format. From 7dcbb3cb6d4f06ff305e5a18dba873261d3fa5d3 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Sun, 3 Dec 2017 17:17:15 -0500 Subject: [PATCH 03/10] rebase -i: set commit to null in exec commands Make sure commit is set to NULL when parsing exec instructions from the todo list. If not, we may try to access an uninitialized address later while updating the todo list. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- sequencer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/sequencer.c b/sequencer.c index fa94ed652d..5033b049d9 100644 --- a/sequencer.c +++ b/sequencer.c @@ -1268,6 +1268,7 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) bol += padding; if (item->command == TODO_EXEC) { + item->commit = NULL; item->arg = bol; item->arg_len = (int)(eol - bol); return 0; From 8dccc7a6b2deb05783ea5d57b53548dab32d99ae Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Tue, 5 Dec 2017 12:52:30 -0500 Subject: [PATCH 04/10] rebase -i: refactor transform_todo_ids The transform_todo_ids function is a little hard to read. Lets try to make it easier by using more of the strbuf API. Also, since we'll soon be adding command abbreviations, let's rename the function so it's name reflects that change. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- builtin/rebase--helper.c | 4 +-- sequencer.c | 69 ++++++++++++++++------------------------ sequencer.h | 2 +- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index f8519363a3..8ad4779d16 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -55,9 +55,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) if (command == MAKE_SCRIPT && argc > 1) return !!sequencer_make_script(keep_empty, stdout, argc, argv); if (command == SHORTEN_SHA1S && argc == 1) - return !!transform_todo_ids(1); + return !!transform_todos(1); if (command == EXPAND_SHA1S && argc == 1) - return !!transform_todo_ids(0); + return !!transform_todos(0); if (command == CHECK_TODO_LIST && argc == 1) return !!check_todo_list(); if (command == SKIP_UNNECESSARY_PICKS && argc == 1) diff --git a/sequencer.c b/sequencer.c index 5033b049d9..c9a661a8c4 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2494,60 +2494,47 @@ int sequencer_make_script(int keep_empty, FILE *out, } -int transform_todo_ids(int shorten_ids) +int transform_todos(int shorten_ids) { const char *todo_file = rebase_path_todo(); struct todo_list todo_list = TODO_LIST_INIT; - int fd, res, i; - FILE *out; + struct strbuf buf = STRBUF_INIT; + struct todo_item *item; + int i; - strbuf_reset(&todo_list.buf); - fd = open(todo_file, O_RDONLY); - if (fd < 0) - return error_errno(_("could not open '%s'"), todo_file); - if (strbuf_read(&todo_list.buf, fd, 0) < 0) { - close(fd); + if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) return error(_("could not read '%s'."), todo_file); - } - close(fd); - res = parse_insn_buffer(todo_list.buf.buf, &todo_list); - if (res) { + if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { todo_list_release(&todo_list); return error(_("unusable todo list: '%s'"), todo_file); } - out = fopen(todo_file, "w"); - if (!out) { - todo_list_release(&todo_list); - return error(_("unable to open '%s' for writing"), todo_file); - } - for (i = 0; i < todo_list.nr; i++) { - struct todo_item *item = todo_list.items + i; - int bol = item->offset_in_buf; - const char *p = todo_list.buf.buf + bol; - int eol = i + 1 < todo_list.nr ? - todo_list.items[i + 1].offset_in_buf : - todo_list.buf.len; - - if (item->command >= TODO_EXEC && item->command != TODO_DROP) - fwrite(p, eol - bol, 1, out); - else { - const char *id = shorten_ids ? - short_commit_name(item->commit) : - oid_to_hex(&item->commit->object.oid); - int len; - - p += strspn(p, " \t"); /* left-trim command */ - len = strcspn(p, " \t"); /* length of command */ - - fprintf(out, "%.*s %s %.*s\n", - len, p, id, item->arg_len, item->arg); + for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { + /* if the item is not a command write it and continue */ + if (item->command >= TODO_COMMENT) { + strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg); + continue; } + + /* add command to the buffer */ + strbuf_addstr(&buf, command_to_string(item->command)); + + /* add commit id */ + if (item->commit) { + const char *oid = shorten_ids ? + short_commit_name(item->commit) : + oid_to_hex(&item->commit->object.oid); + + strbuf_addf(&buf, " %s", oid); + } + /* add all the rest */ + strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg); } - fclose(out); + + i = write_message(buf.buf, buf.len, todo_file, 0); todo_list_release(&todo_list); - return 0; + return i; } enum check_level { diff --git a/sequencer.h b/sequencer.h index 6f3d3df82c..4f7f2c93f8 100644 --- a/sequencer.h +++ b/sequencer.h @@ -48,7 +48,7 @@ int sequencer_remove_state(struct replay_opts *opts); int sequencer_make_script(int keep_empty, FILE *out, int argc, const char **argv); -int transform_todo_ids(int shorten_ids); +int transform_todos(int shorten_ids); int check_todo_list(void); int skip_unnecessary_picks(void); int rearrange_squash(void); From d80fc29367a6cf92792db6e3d1b5ce2ae2d81de8 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Tue, 5 Dec 2017 12:52:31 -0500 Subject: [PATCH 05/10] rebase -i: replace reference to sha1 with oid Since we are trying to abstract the hash function name elsewhere in the code base, lets use OID instead of SHA-1 in the rebase--helper too. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- builtin/rebase--helper.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 8ad4779d16..c3b8e4d401 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -14,7 +14,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) struct replay_opts opts = REPLAY_OPTS_INIT; int keep_empty = 0; enum { - CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S, + CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH } command = 0; struct option options[] = { @@ -27,9 +27,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) OPT_CMDMODE(0, "make-script", &command, N_("make rebase script"), MAKE_SCRIPT), OPT_CMDMODE(0, "shorten-ids", &command, - N_("shorten SHA-1s in the todo list"), SHORTEN_SHA1S), + N_("shorten commit ids in the todo list"), SHORTEN_OIDS), OPT_CMDMODE(0, "expand-ids", &command, - N_("expand SHA-1s in the todo list"), EXPAND_SHA1S), + N_("expand commit ids in the todo list"), EXPAND_OIDS), OPT_CMDMODE(0, "check-todo-list", &command, N_("check the todo list"), CHECK_TODO_LIST), OPT_CMDMODE(0, "skip-unnecessary-picks", &command, @@ -54,9 +54,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!sequencer_remove_state(&opts); if (command == MAKE_SCRIPT && argc > 1) return !!sequencer_make_script(keep_empty, stdout, argc, argv); - if (command == SHORTEN_SHA1S && argc == 1) + if (command == SHORTEN_OIDS && argc == 1) return !!transform_todos(1); - if (command == EXPAND_SHA1S && argc == 1) + if (command == EXPAND_OIDS && argc == 1) return !!transform_todos(0); if (command == CHECK_TODO_LIST && argc == 1) return !!check_todo_list(); From 313a48eaca58ecd170bef9e6a5a55001c7511f08 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Tue, 5 Dec 2017 12:52:32 -0500 Subject: [PATCH 06/10] rebase -i: update functions to use a flags parameter Update functions used in the rebase--helper so that they take a generic 'flags' parameter instead of a growing list of options. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- builtin/rebase--helper.c | 13 +++++++------ sequencer.c | 9 +++++---- sequencer.h | 8 +++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index c3b8e4d401..1102ecb43b 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -12,7 +12,7 @@ static const char * const builtin_rebase_helper_usage[] = { int cmd_rebase__helper(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; - int keep_empty = 0; + unsigned flags = 0, keep_empty = 0; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH @@ -48,16 +48,17 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, NULL, options, builtin_rebase_helper_usage, PARSE_OPT_KEEP_ARGV0); + flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0; + flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0; + if (command == CONTINUE && argc == 1) return !!sequencer_continue(&opts); if (command == ABORT && argc == 1) return !!sequencer_remove_state(&opts); if (command == MAKE_SCRIPT && argc > 1) - return !!sequencer_make_script(keep_empty, stdout, argc, argv); - if (command == SHORTEN_OIDS && argc == 1) - return !!transform_todos(1); - if (command == EXPAND_OIDS && argc == 1) - return !!transform_todos(0); + return !!sequencer_make_script(stdout, argc, argv, flags); + if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1) + return !!transform_todos(flags); if (command == CHECK_TODO_LIST && argc == 1) return !!check_todo_list(); if (command == SKIP_UNNECESSARY_PICKS && argc == 1) diff --git a/sequencer.c b/sequencer.c index c9a661a8c4..8b0dd610c8 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2444,14 +2444,15 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) strbuf_release(&sob); } -int sequencer_make_script(int keep_empty, FILE *out, - int argc, const char **argv) +int sequencer_make_script(FILE *out, int argc, const char **argv, + unsigned flags) { char *format = NULL; struct pretty_print_context pp = {0}; struct strbuf buf = STRBUF_INIT; struct rev_info revs; struct commit *commit; + int keep_empty = flags & TODO_LIST_KEEP_EMPTY; init_revisions(&revs, NULL); revs.verbose_header = 1; @@ -2494,7 +2495,7 @@ int sequencer_make_script(int keep_empty, FILE *out, } -int transform_todos(int shorten_ids) +int transform_todos(unsigned flags) { const char *todo_file = rebase_path_todo(); struct todo_list todo_list = TODO_LIST_INIT; @@ -2522,7 +2523,7 @@ int transform_todos(int shorten_ids) /* add commit id */ if (item->commit) { - const char *oid = shorten_ids ? + const char *oid = flags & TODO_LIST_SHORTEN_IDS ? short_commit_name(item->commit) : oid_to_hex(&item->commit->object.oid); diff --git a/sequencer.h b/sequencer.h index 4f7f2c93f8..68284e9762 100644 --- a/sequencer.h +++ b/sequencer.h @@ -45,10 +45,12 @@ int sequencer_continue(struct replay_opts *opts); int sequencer_rollback(struct replay_opts *opts); int sequencer_remove_state(struct replay_opts *opts); -int sequencer_make_script(int keep_empty, FILE *out, - int argc, const char **argv); +#define TODO_LIST_KEEP_EMPTY (1U << 0) +#define TODO_LIST_SHORTEN_IDS (1U << 1) +int sequencer_make_script(FILE *out, int argc, const char **argv, + unsigned flags); -int transform_todos(int shorten_ids); +int transform_todos(unsigned flags); int check_todo_list(void); int skip_unnecessary_picks(void); int rearrange_squash(void); From 0cce4a2756eb2c66544615e5ccd74671afb33256 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Tue, 5 Dec 2017 12:52:33 -0500 Subject: [PATCH 07/10] rebase -i -x: add exec commands via the rebase--helper Recent work on `git-rebase--interactive` aims to convert shell code to C. Even if this is most likely not a big performance enhancement, let's convert it too since a coming change to abbreviate command names requires it to be updated. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- builtin/rebase--helper.c | 7 ++++++- git-rebase--interactive.sh | 23 +--------------------- sequencer.c | 39 ++++++++++++++++++++++++++++++++++++++ sequencer.h | 1 + 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 1102ecb43b..4229ea0dc1 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -15,7 +15,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) unsigned flags = 0, keep_empty = 0; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, - CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH + CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, + ADD_EXEC } command = 0; struct option options[] = { OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), @@ -36,6 +37,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS), OPT_CMDMODE(0, "rearrange-squash", &command, N_("rearrange fixup/squash lines"), REARRANGE_SQUASH), + OPT_CMDMODE(0, "add-exec-commands", &command, + N_("insert exec commands in todo list"), ADD_EXEC), OPT_END() }; @@ -65,5 +68,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) return !!skip_unnecessary_picks(); if (command == REARRANGE_SQUASH && argc == 1) return !!rearrange_squash(); + if (command == ADD_EXEC && argc == 2) + return !!sequencer_add_exec_commands(argv[1]); usage_with_options(builtin_rebase_helper_usage, options); } diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh index 437815669f..e3f5a0abf3 100644 --- a/git-rebase--interactive.sh +++ b/git-rebase--interactive.sh @@ -722,27 +722,6 @@ collapse_todo_ids() { git rebase--helper --shorten-ids } -# Add commands after a pick or after a squash/fixup series -# in the todo list. -add_exec_commands () { - { - first=t - while read -r insn rest - do - case $insn in - pick) - test -n "$first" || - printf "%s" "$cmd" - ;; - esac - printf "%s %s\n" "$insn" "$rest" - first= - done - printf "%s" "$cmd" - } <"$1" >"$1.new" && - mv "$1.new" "$1" -} - # Switch to the branch in $into and notify it in the reflog checkout_onto () { GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" @@ -982,7 +961,7 @@ fi test -s "$todo" || echo noop >> "$todo" test -z "$autosquash" || git rebase--helper --rearrange-squash || exit -test -n "$cmd" && add_exec_commands "$todo" +test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd" todocount=$(git stripspace --strip-comments <"$todo" | wc -l) todocount=${todocount##* } diff --git a/sequencer.c b/sequencer.c index 8b0dd610c8..892d242f69 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2494,6 +2494,45 @@ int sequencer_make_script(FILE *out, int argc, const char **argv, return 0; } +/* + * Add commands after pick and (series of) squash/fixup commands + * in the todo list. + */ +int sequencer_add_exec_commands(const char *commands) +{ + const char *todo_file = rebase_path_todo(); + struct todo_list todo_list = TODO_LIST_INIT; + struct todo_item *item; + struct strbuf *buf = &todo_list.buf; + size_t offset = 0, commands_len = strlen(commands); + int i, first; + + if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) + return error(_("could not read '%s'."), todo_file); + + if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { + todo_list_release(&todo_list); + return error(_("unusable todo list: '%s'"), todo_file); + } + + first = 1; + /* insert before every pick except the first one */ + for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { + if (item->command == TODO_PICK && !first) { + strbuf_insert(buf, item->offset_in_buf + offset, + commands, commands_len); + offset += commands_len; + } + first = 0; + } + + /* append final */ + strbuf_add(buf, commands, commands_len); + + i = write_message(buf->buf, buf->len, todo_file, 0); + todo_list_release(&todo_list); + return i; +} int transform_todos(unsigned flags) { diff --git a/sequencer.h b/sequencer.h index 68284e9762..212426c445 100644 --- a/sequencer.h +++ b/sequencer.h @@ -50,6 +50,7 @@ int sequencer_remove_state(struct replay_opts *opts); int sequencer_make_script(FILE *out, int argc, const char **argv, unsigned flags); +int sequencer_add_exec_commands(const char *command); int transform_todos(unsigned flags); int check_todo_list(void); int skip_unnecessary_picks(void); From d8ae6c84da52a210e84b3734bb93c575638236d3 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Tue, 5 Dec 2017 12:52:34 -0500 Subject: [PATCH 08/10] rebase -i: learn to abbreviate command names `git rebase -i` already know how to interpret single-letter command names. Teach it to generate the todo list with these same abbreviated names. Based-on-patch-by: Johannes Schindelin Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- Documentation/rebase-config.txt | 20 ++++++++++++++++++++ builtin/rebase--helper.c | 3 +++ sequencer.c | 16 ++++++++++++++-- sequencer.h | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Documentation/rebase-config.txt b/Documentation/rebase-config.txt index 30ae08cb5a..42e1ba7575 100644 --- a/Documentation/rebase-config.txt +++ b/Documentation/rebase-config.txt @@ -30,3 +30,23 @@ rebase.instructionFormat:: A format string, as specified in linkgit:git-log[1], to be used for the todo list during an interactive rebase. The format will automatically have the long commit hash prepended to the format. + +rebase.abbreviateCommands:: + If set to true, `git rebase` will use abbreviated command names in the + todo list resulting in something like this: ++ +------------------------------------------- + p deadbee The oneline of the commit + p fa1afe1 The oneline of the next commit + ... +------------------------------------------- ++ +instead of: ++ +------------------------------------------- + pick deadbee The oneline of the commit + pick fa1afe1 The oneline of the next commit + ... +------------------------------------------- ++ +Defaults to false. diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c index 4229ea0dc1..7daee544b7 100644 --- a/builtin/rebase--helper.c +++ b/builtin/rebase--helper.c @@ -13,6 +13,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) { struct replay_opts opts = REPLAY_OPTS_INIT; unsigned flags = 0, keep_empty = 0; + int abbreviate_commands = 0; enum { CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, @@ -43,6 +44,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) }; git_config(git_default_config, NULL); + git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands); opts.action = REPLAY_INTERACTIVE_REBASE; opts.allow_ff = 1; @@ -52,6 +54,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix) builtin_rebase_helper_usage, PARSE_OPT_KEEP_ARGV0); flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0; + flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0; if (command == CONTINUE && argc == 1) diff --git a/sequencer.c b/sequencer.c index 892d242f69..115085d39c 100644 --- a/sequencer.c +++ b/sequencer.c @@ -795,6 +795,13 @@ static const char *command_to_string(const enum todo_command command) die("Unknown command: %d", command); } +static const char command_to_char(const enum todo_command command) +{ + if (command < TODO_COMMENT && todo_command_info[command].c) + return todo_command_info[command].c; + return comment_line_char; +} + static int is_noop(const enum todo_command command) { return TODO_NOOP <= command; @@ -2453,6 +2460,7 @@ int sequencer_make_script(FILE *out, int argc, const char **argv, struct rev_info revs; struct commit *commit; int keep_empty = flags & TODO_LIST_KEEP_EMPTY; + const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; init_revisions(&revs, NULL); revs.verbose_header = 1; @@ -2485,7 +2493,8 @@ int sequencer_make_script(FILE *out, int argc, const char **argv, strbuf_reset(&buf); if (!keep_empty && is_original_commit_empty(commit)) strbuf_addf(&buf, "%c ", comment_line_char); - strbuf_addf(&buf, "pick %s ", oid_to_hex(&commit->object.oid)); + strbuf_addf(&buf, "%s %s ", insn, + oid_to_hex(&commit->object.oid)); pretty_print_commit(&pp, commit, &buf); strbuf_addch(&buf, '\n'); fputs(buf.buf, out); @@ -2558,7 +2567,10 @@ int transform_todos(unsigned flags) } /* add command to the buffer */ - strbuf_addstr(&buf, command_to_string(item->command)); + if (flags & TODO_LIST_ABBREVIATE_CMDS) + strbuf_addch(&buf, command_to_char(item->command)); + else + strbuf_addstr(&buf, command_to_string(item->command)); /* add commit id */ if (item->commit) { diff --git a/sequencer.h b/sequencer.h index 212426c445..81f6d7d393 100644 --- a/sequencer.h +++ b/sequencer.h @@ -47,6 +47,7 @@ int sequencer_remove_state(struct replay_opts *opts); #define TODO_LIST_KEEP_EMPTY (1U << 0) #define TODO_LIST_SHORTEN_IDS (1U << 1) +#define TODO_LIST_ABBREVIATE_CMDS (1U << 2) int sequencer_make_script(FILE *out, int argc, const char **argv, unsigned flags); From 1795993488bef1b48e4224db096e9d12df075db2 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Tue, 5 Dec 2017 12:52:35 -0500 Subject: [PATCH 09/10] t3404: add test case for abbreviated commands Make sure the todo list ends up using single-letter command abbreviations when the rebase.abbreviateCommands is enabled. This configuration option should not change anything else. Signed-off-by: Liam Beguin Signed-off-by: Junio C Hamano --- t/t3404-rebase-interactive.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh index 6a82d1ed87..481a350090 100755 --- a/t/t3404-rebase-interactive.sh +++ b/t/t3404-rebase-interactive.sh @@ -1260,6 +1260,28 @@ test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' ' test B = $(git cat-file commit HEAD^ | sed -ne \$p) ' +test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' ' + rebase_setup_and_clean abbrevcmd && + test_commit "first" file1.txt "first line" first && + test_commit "second" file1.txt "another line" second && + test_commit "fixup! first" file2.txt "first line again" first_fixup && + test_commit "squash! second" file1.txt "another line here" second_squash && + cat >expected <<-EOF && + p $(git rev-list --abbrev-commit -1 first) first + f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first + x git show HEAD + p $(git rev-list --abbrev-commit -1 second) second + s $(git rev-list --abbrev-commit -1 second_squash) squash! second + x git show HEAD + EOF + git checkout abbrevcmd && + set_cat_todo_editor && + test_config rebase.abbreviateCommands true && + test_must_fail git rebase -i --exec "git show HEAD" \ + --autosquash master >actual && + test_cmp expected actual +' + test_expect_success 'static check of bad command' ' rebase_setup_and_clean bad-cmd && set_fake_editor && From ee5462d6e71aad04628ccb8f078784468bfa4139 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 27 Dec 2017 11:12:45 -0800 Subject: [PATCH 10/10] sequencer.c: drop 'const' from function return type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With -Werror=ignored-qualifiers, a function that claims to return "const char" gets this error: CC sequencer.o sequencer.c:798:19: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers] static const char command_to_char(const enum todo_command command) ^ Reported-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- sequencer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sequencer.c b/sequencer.c index 115085d39c..2a407cbe54 100644 --- a/sequencer.c +++ b/sequencer.c @@ -795,7 +795,7 @@ static const char *command_to_string(const enum todo_command command) die("Unknown command: %d", command); } -static const char command_to_char(const enum todo_command command) +static char command_to_char(const enum todo_command command) { if (command < TODO_COMMENT && todo_command_info[command].c) return todo_command_info[command].c;