Skip to content

Commit 0cce4a2

Browse files
liambeguingitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 313a48e commit 0cce4a2

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

builtin/rebase--helper.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1515
unsigned flags = 0, keep_empty = 0;
1616
enum {
1717
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
1920
} command = 0;
2021
struct option options[] = {
2122
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)
3637
N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS),
3738
OPT_CMDMODE(0, "rearrange-squash", &command,
3839
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),
3942
OPT_END()
4043
};
4144

@@ -65,5 +68,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
6568
return !!skip_unnecessary_picks();
6669
if (command == REARRANGE_SQUASH && argc == 1)
6770
return !!rearrange_squash();
71+
if (command == ADD_EXEC && argc == 2)
72+
return !!sequencer_add_exec_commands(argv[1]);
6873
usage_with_options(builtin_rebase_helper_usage, options);
6974
}

git-rebase--interactive.sh

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -722,27 +722,6 @@ collapse_todo_ids() {
722722
git rebase--helper --shorten-ids
723723
}
724724

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-
746725
# Switch to the branch in $into and notify it in the reflog
747726
checkout_onto () {
748727
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
@@ -982,7 +961,7 @@ fi
982961

983962
test -s "$todo" || echo noop >> "$todo"
984963
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"
986965

987966
todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
988967
todocount=${todocount##* }

sequencer.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,45 @@ int sequencer_make_script(FILE *out, int argc, const char **argv,
24942494
return 0;
24952495
}
24962496

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+
}
24972536

24982537
int transform_todos(unsigned flags)
24992538
{

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ int sequencer_remove_state(struct replay_opts *opts);
5050
int sequencer_make_script(FILE *out, int argc, const char **argv,
5151
unsigned flags);
5252

53+
int sequencer_add_exec_commands(const char *command);
5354
int transform_todos(unsigned flags);
5455
int check_todo_list(void);
5556
int skip_unnecessary_picks(void);

0 commit comments

Comments
 (0)