git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 11/12] builtin/rerere: use `OPT_SUBCOMMAND()`

From
Karthik Nayak <karthik.188@gmail.com>
Date
Nov 6, 2026, 11:44 UTC
Message-ID
<CAOLa=ZQrOyA2k+5EDmh+VJD=gC=68m3D17uKvNUTmaxH2FuNZg@mail.gmail.com>
In-Reply-To
<20261029-pks-rerere-subcommands-v2-11-07d5ba18e882@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 11 quoted lines
> 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.
Show 46 quoted lines
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  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.

Show 26 quoted lines
>  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.
Show 7 quoted lines
>  	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.

Show 22 quoted lines
> +		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?
Show 11 quoted lines
> +		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.

Show 12 quoted lines
> +		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
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 39 of 89 in “builtin/rerere: convert to use subcommands”
  1. 00/12 builtin/rerere: convert to use subcommandsPatrick Steinhardt, Oct 27, 2026
  2. 01/12 rerere: rename `repo_rerere()`Patrick Steinhardt, Oct 27, 2026
  3. Junio C HamanoOct 27, 2026
  4. Patrick SteinhardtOct 28, 2026
  5. Junio C HamanoOct 28, 2026
  6. 02/12 rerere: convert flags into an enumPatrick Steinhardt, Oct 27, 2026
  7. Mark C. Chu-CarrollOct 27, 2026
  8. Junio C HamanoOct 27, 2026
  9. Patrick SteinhardtOct 28, 2026
  10. Mark C. Chu-CarrollOct 28, 2026
  11. Junio C HamanoOct 28, 2026
  12. 03/12 builtin/rerere: drop `the_repository`Patrick Steinhardt, Oct 27, 2026
  13. 04/12 builtin/rerere: stop passing flags to subcommands that ignore themPatrick Steinhardt, Oct 27, 2026
  14. 05/12 builtin/rerere: turn "forget" into a proper subcommandPatrick Steinhardt, Oct 27, 2026
  15. 06/12 builtin/rerere: turn "clear" into a proper subcommandPatrick Steinhardt, Oct 27, 2026
  16. 07/12 builtin/rerere: turn "status" into a proper subcommandPatrick Steinhardt, Oct 27, 2026
  17. 08/12 builtin/rerere: turn "remaining" into a proper subcommandPatrick Steinhardt, Oct 27, 2026
  18. 09/12 builtin/rerere: turn "diff" into a proper subcommandPatrick Steinhardt, Oct 27, 2026
  19. 10/12 builtin/rerere: turn "gc" into a proper subcommandPatrick Steinhardt, Oct 27, 2026
  20. 11/12 builtin/rerere: use `OPT_SUBCOMMAND()`Patrick Steinhardt, Oct 27, 2026
  21. 12/12 builtin/rerere: introduce proper usage strings for subcommandsPatrick Steinhardt, Oct 27, 2026
  22. Junio C HamanoOct 27, 2026
  23. 00/12 builtin/rerere: convert to use subcommandsPatrick Steinhardt, Oct 29, 2026
  24. 01/12 rerere: rename `repo_rerere()`Patrick Steinhardt, Oct 29, 2026
  25. 02/12 rerere: convert flags into an enumPatrick Steinhardt, Oct 29, 2026
  26. Karthik NayakNov 6, 2026
  27. Patrick SteinhardtNov 6, 2026
  28. Karthik NayakNov 6, 2026
  29. 03/12 builtin/rerere: drop `the_repository`Patrick Steinhardt, Oct 29, 2026
  30. 04/12 builtin/rerere: stop passing flags to subcommands that ignore themPatrick Steinhardt, Oct 29, 2026
  31. 05/12 builtin/rerere: turn "forget" into a proper subcommandPatrick Steinhardt, Oct 29, 2026
  32. 06/12 builtin/rerere: turn "clear" into a proper subcommandPatrick Steinhardt, Oct 29, 2026
  33. 07/12 builtin/rerere: turn "status" into a proper subcommandPatrick Steinhardt, Oct 29, 2026
  34. 08/12 builtin/rerere: turn "remaining" into a proper subcommandPatrick Steinhardt, Oct 29, 2026
  35. 09/12 builtin/rerere: turn "diff" into a proper subcommandPatrick Steinhardt, Oct 29, 2026
  36. 10/12 builtin/rerere: turn "gc" into a proper subcommandPatrick Steinhardt, Oct 29, 2026
  37. Karthik NayakNov 6, 2026
  38. 11/12 builtin/rerere: use `OPT_SUBCOMMAND()`Patrick Steinhardt, Oct 29, 2026
  39. Karthik NayakNov 6, 2026
  40. Patrick SteinhardtNov 6, 2026
  41. 12/12 builtin/rerere: introduce proper usage strings for subcommandsPatrick Steinhardt, Oct 29, 2026
  42. Karthik NayakNov 6, 2026
  43. 00/12 builtin/rerere: convert to use subcommandsPatrick Steinhardt, Nov 6, 2026
  44. 01/12 rerere: rename `repo_rerere()`Patrick Steinhardt, Nov 6, 2026
  45. 02/12 rerere: convert flags into an enumPatrick Steinhardt, Nov 6, 2026
  46. Junio C HamanoNov 6, 2026
  47. Patrick SteinhardtNov 9, 2026
  48. Junio C HamanoNov 9, 2026
  49. Junio C HamanoNov 9, 2026
  50. Patrick SteinhardtNov 11, 2026
  51. 03/12 builtin/rerere: drop `the_repository`Patrick Steinhardt, Nov 6, 2026
  52. 04/12 builtin/rerere: stop passing flags to subcommands that ignore themPatrick Steinhardt, Nov 6, 2026
  53. 05/12 builtin/rerere: turn "forget" into a proper subcommandPatrick Steinhardt, Nov 6, 2026
  54. 06/12 builtin/rerere: turn "clear" into a proper subcommandPatrick Steinhardt, Nov 6, 2026
  55. 07/12 builtin/rerere: turn "status" into a proper subcommandPatrick Steinhardt, Nov 6, 2026
  56. 08/12 builtin/rerere: turn "remaining" into a proper subcommandPatrick Steinhardt, Nov 6, 2026
  57. 09/12 builtin/rerere: turn "diff" into a proper subcommandPatrick Steinhardt, Nov 6, 2026
  58. 10/12 builtin/rerere: turn "gc" into a proper subcommandPatrick Steinhardt, Nov 6, 2026
  59. 11/12 builtin/rerere: use `OPT_SUBCOMMAND()`Patrick Steinhardt, Nov 6, 2026
  60. 12/12 builtin/rerere: introduce proper usage strings for subcommandsPatrick Steinhardt, Nov 6, 2026
  61. 00/12 builtin/rerere: convert to use subcommandsPatrick Steinhardt, Nov 11, 2026
  62. 01/12 rerere: rename `repo_rerere()`Patrick Steinhardt, Nov 11, 2026
  63. 02/12 rerere: convert flags into an enumPatrick Steinhardt, Nov 11, 2026
  64. 03/12 builtin/rerere: drop `the_repository`Patrick Steinhardt, Nov 11, 2026
  65. 04/12 builtin/rerere: stop passing flags to subcommands that ignore themPatrick Steinhardt, Nov 11, 2026
  66. 05/12 builtin/rerere: turn "forget" into a proper subcommandPatrick Steinhardt, Nov 11, 2026
  67. 06/12 builtin/rerere: turn "clear" into a proper subcommandPatrick Steinhardt, Nov 11, 2026
  68. 07/12 builtin/rerere: turn "status" into a proper subcommandPatrick Steinhardt, Nov 11, 2026
  69. 08/12 builtin/rerere: turn "remaining" into a proper subcommandPatrick Steinhardt, Nov 11, 2026
  70. 09/12 builtin/rerere: turn "diff" into a proper subcommandPatrick Steinhardt, Nov 11, 2026
  71. Junio C HamanoNov 17, 2026
  72. 10/12 builtin/rerere: turn "gc" into a proper subcommandPatrick Steinhardt, Nov 11, 2026
  73. Junio C HamanoNov 17, 2026
  74. 11/12 builtin/rerere: use `OPT_SUBCOMMAND()`Patrick Steinhardt, Nov 11, 2026
  75. Junio C HamanoNov 17, 2026
  76. 12/12 builtin/rerere: introduce proper usage strings for subcommandsPatrick Steinhardt, Nov 11, 2026
  77. Junio C HamanoNov 17, 2026
  78. Karthik NayakNov 12, 2026
  79. Patrick SteinhardtNov 13, 2026
  80. Junio C HamanoNov 13, 2026
  81. Tuomas AholaNov 13, 2026
  82. Phillip WoodNov 13, 2026
  83. Junio C HamanoNov 13, 2026
  84. Tuomas AholaNov 14, 2026
  85. Patrick SteinhardtNov 16, 2026
  86. Junio C HamanoNov 14, 2026
  87. Patrick SteinhardtNov 16, 2026
  88. Junio C HamanoNov 16, 2026
  89. Junio C HamanoNov 17, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.