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 <liambeguin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Liam Beguin committed Dec 5, 2017 at 12:52 UTC 0cce4a2756eb2c66544615e5ccd74671afb33256
4 files changed +47 -23
builtin/rebase--helper.c
+6 -1
@@ -15,7 +15,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
15 unsigned flags = 0, keep_empty = 0;
16 enum {
17 CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
18 - CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH
18 + CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
19 + ADD_EXEC
20 } command = 0;
21 struct option options[] = {
22 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)
37 N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS),
38 OPT_CMDMODE(0, "rearrange-squash", &command,
39 N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
40 + OPT_CMDMODE(0, "add-exec-commands", &command,
41 + N_("insert exec commands in todo list"), ADD_EXEC),
42 OPT_END()
43 };
44
@@ -65,5 +68,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
68 return !!skip_unnecessary_picks();
69 if (command == REARRANGE_SQUASH && argc == 1)
70 return !!rearrange_squash();
71 + if (command == ADD_EXEC && argc == 2)
72 + return !!sequencer_add_exec_commands(argv[1]);
73 usage_with_options(builtin_rebase_helper_usage, options);
74 }
git-rebase--interactive.sh
+1 -22
@@ -722,27 +722,6 @@ collapse_todo_ids() {
722 git rebase--helper --shorten-ids
723 }
724
725 -# Add commands after a pick or after a squash/fixup series
726 -# in the todo list.
727 -add_exec_commands () {
728 - {
729 - first=t
730 - while read -r insn rest
731 - do
732 - case $insn in
733 - pick)
734 - test -n "$first" ||
735 - printf "%s" "$cmd"
736 - ;;
737 - esac
738 - printf "%s %s\n" "$insn" "$rest"
739 - first=
740 - done
741 - printf "%s" "$cmd"
742 - } <"$1" >"$1.new" &&
743 - mv "$1.new" "$1"
744 -}
745 -
725 # Switch to the branch in $into and notify it in the reflog
726 checkout_onto () {
727 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
@@ -982,7 +961,7 @@ fi
961
962 test -s "$todo" || echo noop >> "$todo"
963 test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
985 -test -n "$cmd" && add_exec_commands "$todo"
964 +test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"
965
966 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
967 todocount=${todocount##* }
sequencer.c
+39
@@ -2494,6 +2494,45 @@ int sequencer_make_script(FILE *out, int argc, const char **argv,
2494 return 0;
2495 }
2496
2497 +/*
2498 + * Add commands after pick and (series of) squash/fixup commands
2499 + * in the todo list.
2500 + */
2501 +int sequencer_add_exec_commands(const char *commands)
2502 +{
2503 + const char *todo_file = rebase_path_todo();
2504 + struct todo_list todo_list = TODO_LIST_INIT;
2505 + struct todo_item *item;
2506 + struct strbuf *buf = &todo_list.buf;
2507 + size_t offset = 0, commands_len = strlen(commands);
2508 + int i, first;
2509 +
2510 + if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
2511 + return error(_("could not read '%s'."), todo_file);
2512 +
2513 + if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) {
2514 + todo_list_release(&todo_list);
2515 + return error(_("unusable todo list: '%s'"), todo_file);
2516 + }
2517 +
2518 + first = 1;
2519 + /* insert <commands> before every pick except the first one */
2520 + for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
2521 + if (item->command == TODO_PICK && !first) {
2522 + strbuf_insert(buf, item->offset_in_buf + offset,
2523 + commands, commands_len);
2524 + offset += commands_len;
2525 + }
2526 + first = 0;
2527 + }
2528 +
2529 + /* append final <commands> */
2530 + strbuf_add(buf, commands, commands_len);
2531 +
2532 + i = write_message(buf->buf, buf->len, todo_file, 0);
2533 + todo_list_release(&todo_list);
2534 + return i;
2535 +}
2536
2537 int transform_todos(unsigned flags)
2538 {
sequencer.h
+1
@@ -50,6 +50,7 @@ int sequencer_remove_state(struct replay_opts *opts);
50 int sequencer_make_script(FILE *out, int argc, const char **argv,
51 unsigned flags);
52
53 +int sequencer_add_exec_commands(const char *command);
54 int transform_todos(unsigned flags);
55 int check_todo_list(void);
56 int skip_unnecessary_picks(void);