From: Karthik Nayak Date: Fri, 06 Nov 2026 11:44:38 GMT Subject: Re: [PATCH v2 11/12] builtin/rerere: use `OPT_SUBCOMMAND()` Message-ID: In-Reply-To: <20261029-pks-rerere-subcommands-v2-11-07d5ba18e882@pks.im> Patrick Steinhardt writes: > 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. > Okay. > Signed-off-by: Patrick Steinhardt > --- > builtin/rerere.c | 84 +++++++++++++++++++++++++++++++++++++------------------- > 1 file changed, 56 insertions(+), 28 deletions(-) > > diff --git a/builtin/rerere.c b/builtin/rerere.c > index 3162b074ec6..97c791e420b 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); > +} > + So I guess this would be the default case if no subcommand is used, meaning the subcommands should be optional. > static int cmd_rerere_remaining(int argc, > const char **argv, > const char *prefix, > @@ -211,41 +237,43 @@ 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); So this matches what I thought. > repo_config(repo, git_xmerge_config, NULL); > > - if (autoupdate == 1) > - flags = RERERE_AUTOUPDATE; > - if (autoupdate == 0) > - flags = RERERE_NOAUTOUPDATE; > + if (!fn) { Okay if there's no function we call the default case. I wonder if we could've done the inverse to reduce code block. if (fn) reutrn fn(...). Doesn't matter much anyway. > + struct strvec args = STRVEC_INIT; > + const char **args_copy; > + int ret; > > - 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); > + strvec_push(&args, "perform"); Worthwhile adding a comment that this is a dummy placeholder subcommand? > + strvec_pushv(&args, argv); > > - return 0; > + /* > + * 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); > + COPY_ARRAY(args_copy, args.v, args.nr); > + Since we need to add the dummy 'perform' and don't want to clear the actual argv, we copy it over. makes sense. > + ret = cmd_rerere_perform(args.nr, args_copy, prefix, repo); > + > + strvec_clear(&args); > + free(args_copy); > + return ret; > + } > + > + return fn(argc, argv, prefix, repo); > } > > -- > 2.56.0.341.g393f52fd96.dirty