mirror of
https://github.com/git/git.git
synced 2025-01-25 00:43:41 +08:00
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>
This commit is contained in:
parent
313a48eaca
commit
0cce4a2756
@ -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);
|
||||
}
|
||||
|
@ -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##* }
|
||||
|
39
sequencer.c
39
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 <commands> 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 <commands> */
|
||||
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)
|
||||
{
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user