From: Patrick Steinhardt Date: Thu, 19 Nov 2026 11:47:12 GMT Subject: [PATCH v5 11/13] builtin/rerere: use `OPT_SUBCOMMAND()` Message-ID: <20261119-pks-rerere-subcommands-v5-11-0c1092b88958@pks.im> In-Reply-To: <20261119-pks-rerere-subcommands-v5-0-0c1092b88958@pks.im> In git-rerere(1) we have our own custom logic to parse subcommands. In preceding commits we have refactored all of those subcommands so that they are ready to be used as `OPT_SUBCOMMAND()`s. Switch over to use these so that we get rid of our custom subcommand parsing. Unfortunately this means that we have a backwards-incompatible change because we no longer accept the "--[no-]rerere-autoupdate" flag for all subcommands. But this was arguably broken and misleading as we ignore this flag for any other subcommand aynway. So we cheat a bit and simply call it a bug that those flags were accepted in the first place. Signed-off-by: Patrick Steinhardt --- builtin/rerere.c | 95 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/builtin/rerere.c b/builtin/rerere.c index 3162b074ec6..8707c65560d 100644 --- a/builtin/rerere.c +++ b/builtin/rerere.c @@ -5,6 +5,7 @@ #include "rerere.h" #include "strbuf.h" #include "string-list.h" +#include "strvec.h" #include "xdiff/xdiff.h" #include "xdiff-interface.h" #include "pathspec.h" @@ -152,6 +153,31 @@ static int cmd_rerere_gc(int argc, return 0; } +static int cmd_rerere_perform(int argc, + const char **argv, + const char *prefix, + struct repository *repo) +{ + int autoupdate = -1; + struct option options[] = { + OPT_SET_INT(0, "rerere-autoupdate", &autoupdate, + N_("register clean resolutions in index"), 1), + OPT_END(), + }; + enum rerere_flags flags = 0; + + argc = parse_options(argc, argv, prefix, options, rerere_usage, 0); + if (argc) + usage(_("too many arguments")); + + if (autoupdate == 1) + flags = RERERE_AUTOUPDATE; + if (autoupdate == 0) + flags = RERERE_NOAUTOUPDATE; + + return rerere(repo, flags); +} + static int cmd_rerere_remaining(int argc, const char **argv, const char *prefix, @@ -211,41 +237,52 @@ int cmd_rerere(int argc, const char *prefix, struct repository *repo) { - int autoupdate = -1, flags = 0; - + parse_opt_subcommand_fn *fn = NULL; struct option options[] = { - OPT_SET_INT(0, "rerere-autoupdate", &autoupdate, - N_("register clean resolutions in index"), 1), + OPT_SUBCOMMAND("clear", &fn, cmd_rerere_clear), + OPT_SUBCOMMAND("diff", &fn, cmd_rerere_diff), + OPT_SUBCOMMAND("forget", &fn, cmd_rerere_forget), + OPT_SUBCOMMAND("gc", &fn, cmd_rerere_gc), + OPT_SUBCOMMAND("remaining", &fn, cmd_rerere_remaining), + OPT_SUBCOMMAND("status", &fn, cmd_rerere_status), OPT_END(), }; argc = parse_options(argc, argv, prefix, options, rerere_usage, - PARSE_OPT_KEEP_UNKNOWN_OPT); - + PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_SUBCOMMAND_OPTIONAL); repo_config(repo, git_xmerge_config, NULL); - if (autoupdate == 1) - flags = RERERE_AUTOUPDATE; - if (autoupdate == 0) - flags = RERERE_NOAUTOUPDATE; - - if (argc < 1) - return rerere(repo, flags); - - if (!strcmp(argv[0], "forget")) - return cmd_rerere_forget(argc, argv, prefix, repo); - else if (!strcmp(argv[0], "clear")) - return cmd_rerere_clear(argc, argv, prefix, repo); - else if (!strcmp(argv[0], "gc")) - return cmd_rerere_gc(argc, argv, prefix, repo); - else if (!strcmp(argv[0], "status")) - return cmd_rerere_status(argc, argv, prefix, repo); - else if (!strcmp(argv[0], "remaining")) - return cmd_rerere_remaining(argc, argv, prefix, repo); - else if (!strcmp(argv[0], "diff")) - return cmd_rerere_diff(argc, argv, prefix, repo); - else - usage_with_options(rerere_usage, options); + if (!fn) { + struct strvec args = STRVEC_INIT; + const char **args_copy; + int ret; + + /* + * The user may have either passed options for `git rerere` + * itself or they may have mistyped the subcommand name. We try + * to distinguish these two cases by checking whether argv[0] + * is a flag or not. + */ + if (argc && !starts_with(argv[0], "-")) + usage_with_options(rerere_usage, options); + + strvec_push(&args, "perform"); + strvec_pushv(&args, argv); + + /* + * We need to copy the array as it will be modified by + * `parse_options()`, and if so we leak the strings + * referenced by `args`. + */ + CALLOC_ARRAY(args_copy, args.nr + 1); + COPY_ARRAY(args_copy, args.v, args.nr); + + ret = cmd_rerere_perform(args.nr, args_copy, prefix, repo); + + strvec_clear(&args); + free(args_copy); + return ret; + } - return 0; + return fn(argc, argv, prefix, repo); } -- 2.56.1.831.g720f3f5937.dirty