rebase -m: fix serialization of strategy options

To store the strategy options rebase prepends " --" to each one and
writes them to a file. To load them it reads the file and passes the
contents to split_cmdline(). This roughly mimics the behavior of the
scripted rebase but has a couple of limitations, (1) options containing
whitespace are not properly preserved (this is true of the scripted
rebase as well) and (2) options containing '"' or '\' are incorrectly
parsed and may cause the parser to return an error.

Fix these limitations by quoting each option when they are stored so
that they can be parsed correctly. Now that "--preserve-merges" no
longer exist this change also stops prepending "--" to the options when
they are stored as that was an artifact of the scripted rebase.

These changes are backwards compatible so the files written by an older
version of git can still be read. They are also forwards compatible,
the file can still be parsed by recent versions of git as they treat the
"--" prefix as optional.

Reviewed-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Phillip Wood
2023-04-10 10:08:30 +01:00
committed by Junio C Hamano
parent 4a8bc9860a
commit 4960e5c7bd
5 changed files with 49 additions and 41 deletions

View File

@ -2925,7 +2925,7 @@ static void parse_strategy_opts(struct replay_opts *opts, char *raw_opts)
count = split_cmdline(strategy_opts_string, &argv);
if (count < 0)
die(_("could not split '%s': %s"), strategy_opts_string,
BUG("could not split '%s': %s", strategy_opts_string,
split_cmdline_strerror(count));
for (i = 0; i < count; i++) {
const char *arg = argv[i];
@ -3049,12 +3049,13 @@ done_rebase_i:
static void write_strategy_opts(struct replay_opts *opts)
{
int i;
struct strbuf buf = STRBUF_INIT;
for (i = 0; i < opts->xopts.nr; ++i)
strbuf_addf(&buf, " --%s", opts->xopts.v[i]);
/*
* Quote strategy options so that they can be read correctly
* by split_cmdline().
*/
quote_cmdline(&buf, opts->xopts.v);
write_file(rebase_path_strategy_opts(), "%s\n", buf.buf);
strbuf_release(&buf);
}