rebase: cleanup "--exec" option handling

When handling "--exec" rebase collects the commands into a struct string_list, then prepends "exec " to each command creating a multi line string and finally splits that string back into a list of commands. This is an artifact of the scripted rebase and the need to support "rebase --preserve-merges". Now that "--preserve-merges" no-longer exists we can cleanup the way the argument is handled. There is no need to add the "exec " prefix to the commands as that is added by todo_list_to_strbuf(). Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jan 12, 2023 at 16:50 UTC e57d2c59377943fed09ec30012634f60788bdfca
2 files changed +13 -36
builtin/rebase.c
+11 -34
@@ -113,7 +113,7 @@ struct rebase_options {
113 int autostash;
114 int committer_date_is_author_date;
115 int ignore_date;
116 - char *cmd;
116 + struct string_list exec;
117 int allow_empty_message;
118 int rebase_merges, rebase_cousins;
119 char *strategy, *strategy_opts;
@@ -131,6 +131,7 @@ struct rebase_options {
131 .default_backend = "merge", \
132 .flags = REBASE_NO_QUIET, \
133 .git_am_opts = STRVEC_INIT, \
134 + .exec = STRING_LIST_INIT_NODUP, \
135 .git_format_patch_opt = STRBUF_INIT, \
136 .fork_point = -1, \
137 }
@@ -243,17 +244,6 @@ static int init_basic_state(struct replay_opts *opts, const char *head_name,
244 return write_basic_state(opts, head_name, onto, orig_head);
245 }
246
246 -static void split_exec_commands(const char *cmd, struct string_list *commands)
247 -{
248 - if (cmd && *cmd) {
249 - string_list_split(commands, cmd, '\n', -1);
250 -
251 - /* rebase.c adds a new line to cmd after every command,
252 - * so here the last command is always empty */
253 - string_list_remove_empty_items(commands, 0);
254 - }
255 -}
256 -
247 static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
248 {
249 int ret;
@@ -261,7 +251,6 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
251 struct strvec make_script_args = STRVEC_INIT;
252 struct todo_list todo_list = TODO_LIST_INIT;
253 struct replay_opts replay = get_replay_opts(opts);
264 - struct string_list commands = STRING_LIST_INIT_DUP;
254
255 if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head->object.oid,
256 &revisions, &shortrevisions))
@@ -297,14 +286,12 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
286 &todo_list))
287 BUG("unusable todo list");
288
300 - split_exec_commands(opts->cmd, &commands);
289 ret = complete_action(the_repository, &replay, flags,
290 shortrevisions, opts->onto_name, opts->onto,
303 - &opts->orig_head->object.oid, &commands,
291 + &opts->orig_head->object.oid, &opts->exec,
292 opts->autosquash, opts->update_refs, &todo_list);
293 }
294
307 - string_list_clear(&commands, 0);
295 free(revisions);
296 free(shortrevisions);
297 todo_list_release(&todo_list);
@@ -1032,7 +1019,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1019 struct object_id branch_base;
1020 int ignore_whitespace = 0;
1021 const char *gpg_sign = NULL;
1035 - struct string_list exec = STRING_LIST_INIT_NODUP;
1022 const char *rebase_merges = NULL;
1023 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
1024 struct object_id squash_onto;
@@ -1127,7 +1113,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1113 N_("GPG-sign commits"),
1114 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1115 OPT_AUTOSTASH(&options.autostash),
1130 - OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1116 + OPT_STRING_LIST('x', "exec", &options.exec, N_("exec"),
1117 N_("add exec lines after each commit of the "
1118 "editable list")),
1119 OPT_BOOL_F(0, "allow-empty-message",
@@ -1250,7 +1236,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1236 if (trace2_is_enabled()) {
1237 if (is_merge(&options))
1238 trace2_cmd_mode("interactive");
1253 - else if (exec.nr)
1239 + else if (options.exec.nr)
1240 trace2_cmd_mode("interactive-exec");
1241 else
1242 trace2_cmd_mode(action_names[options.action]);
@@ -1378,7 +1364,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1364
1365 if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) ||
1366 (options.action != ACTION_NONE) ||
1381 - (exec.nr > 0) ||
1367 + (options.exec.nr > 0) ||
1368 options.autosquash) {
1369 allow_preemptive_ff = 0;
1370 }
@@ -1402,8 +1388,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1388 }
1389 }
1390
1405 - for (i = 0; i < exec.nr; i++)
1406 - if (check_exec_cmd(exec.items[i].string))
1391 + for (i = 0; i < options.exec.nr; i++)
1392 + if (check_exec_cmd(options.exec.items[i].string))
1393 exit(1);
1394
1395 if (!(options.flags & REBASE_NO_QUIET))
@@ -1422,17 +1408,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1408 if (gpg_sign)
1409 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1410
1425 - if (exec.nr) {
1426 - int i;
1427 -
1411 + if (options.exec.nr)
1412 imply_merge(&options, "--exec");
1413
1430 - strbuf_reset(&buf);
1431 - for (i = 0; i < exec.nr; i++)
1432 - strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1433 - options.cmd = xstrdup(buf.buf);
1434 - }
1435 -
1414 if (rebase_merges) {
1415 if (!*rebase_merges)
1416 ; /* default mode; do nothing */
@@ -1543,7 +1521,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1521 if (options.empty == EMPTY_UNSPECIFIED) {
1522 if (options.flags & REBASE_INTERACTIVE_EXPLICIT)
1523 options.empty = EMPTY_ASK;
1546 - else if (exec.nr > 0)
1524 + else if (options.exec.nr > 0)
1525 options.empty = EMPTY_KEEP;
1526 else
1527 options.empty = EMPTY_DROP;
@@ -1831,11 +1809,10 @@ cleanup:
1809 free(options.head_name);
1810 strvec_clear(&options.git_am_opts);
1811 free(options.gpg_sign_opt);
1834 - free(options.cmd);
1812 + string_list_clear(&options.exec, 0);
1813 free(options.strategy);
1814 strbuf_release(&options.git_format_patch_opt);
1815 free(squash_onto_name);
1838 - string_list_clear(&exec, 0);
1816 string_list_clear(&strategy_options, 0);
1817 return !!ret;
1818 }
sequencer.c
+2 -2
@@ -5745,8 +5745,8 @@ static void todo_list_add_exec_commands(struct todo_list *todo_list,
5745
5746 base_items[i].command = TODO_EXEC;
5747 base_items[i].offset_in_buf = base_offset;
5748 - base_items[i].arg_offset = base_offset + strlen("exec ");
5749 - base_items[i].arg_len = command_len - strlen("exec ");
5748 + base_items[i].arg_offset = base_offset;
5749 + base_items[i].arg_len = command_len;
5750
5751 base_offset += command_len + 1;
5752 }