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

[PATCH v3 04/16] builtin/multi-pack-index.c: split sub-commands

From
Taylor Blau <me@ttaylorr.com>
Date
Mar 11, 2021, 17:04 UTC
Message-ID
<d084f90466813597feb27975654e57de36eb62a4.1615482270.git.me@ttaylorr.com>
In-Reply-To
<cover.1615482270.git.me@ttaylorr.com>

Handle sub-commands of the 'git multi-pack-index' builtin (e.g., "write", "repack", etc.) separately from one another. This allows sub-commands with unique options, without forcing cmd_multi_pack_index() to reject invalid combinations itself.

This comes at the cost of some duplication and boilerplate. Luckily, the duplication is reduced to a minimum, since common options are shared among sub-commands due to a suggestion by Ævar. (Sub-commands do have to retain the common options, too, since this builtin accepts common options on either side of the sub-command).

Roughly speaking, cmd_multi_pack_index() parses options (including common ones), and stops at the first non-option, which is the sub-command. It then dispatches to the appropriate sub-command, which parses the remaining options (also including common options).

Unknown options are kept by the sub-commands in order to detect their presence (and complain that too many arguments were given).

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 builtin/multi-pack-index.c | 131 ++++++++++++++++++++++++++++++-------
 1 file changed, 106 insertions(+), 25 deletions(-)
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index eea498e026..23e51dfeb4 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -17,6 +17,22 @@
 #define BUILTIN_MIDX_REPACK_USAGE \
 	N_("git multi-pack-index [<options>] repack [--batch-size=<size>]")
 
+static char const * const builtin_multi_pack_index_write_usage[] = {
+	BUILTIN_MIDX_WRITE_USAGE,
+	NULL
+};
+static char const * const builtin_multi_pack_index_verify_usage[] = {
+	BUILTIN_MIDX_VERIFY_USAGE,
+	NULL
+};
+static char const * const builtin_multi_pack_index_expire_usage[] = {
+	BUILTIN_MIDX_EXPIRE_USAGE,
+	NULL
+};
+static char const * const builtin_multi_pack_index_repack_usage[] = {
+	BUILTIN_MIDX_REPACK_USAGE,
+	NULL
+};
 static char const * const builtin_multi_pack_index_usage[] = {
 	BUILTIN_MIDX_WRITE_USAGE,
 	BUILTIN_MIDX_VERIFY_USAGE,
@@ -31,25 +47,99 @@ static struct opts_multi_pack_index {
 	unsigned flags;
 } opts;
 
-int cmd_multi_pack_index(int argc, const char **argv,
-			 const char *prefix)
+static struct option common_opts[] = {
+	OPT_FILENAME(0, "object-dir", &opts.object_dir,
+	  N_("object directory containing set of packfile and pack-index pairs")),
+	OPT_BIT(0, "progress", &opts.flags, N_("force progress reporting"), MIDX_PROGRESS),
+	OPT_END(),
+};
+
+static struct option *add_common_options(struct option *prev)
 {
-	static struct option builtin_multi_pack_index_options[] = {
-		OPT_FILENAME(0, "object-dir", &opts.object_dir,
-		  N_("object directory containing set of packfile and pack-index pairs")),
-		OPT_BIT(0, "progress", &opts.flags, N_("force progress reporting"), MIDX_PROGRESS),
+	struct option *with_common = parse_options_concat(common_opts, prev);
+	free(prev);
+	return with_common;
+}
+
+static int cmd_multi_pack_index_write(int argc, const char **argv)
+{
+	struct option *options = common_opts;
+
+	argc = parse_options(argc, argv, NULL,
+			     options, builtin_multi_pack_index_write_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+	if (argc)
+		usage_with_options(builtin_multi_pack_index_write_usage,
+				   options);
+
+	return write_midx_file(opts.object_dir, opts.flags);
+}
+
+static int cmd_multi_pack_index_verify(int argc, const char **argv)
+{
+	struct option *options = common_opts;
+
+	argc = parse_options(argc, argv, NULL,
+			     options, builtin_multi_pack_index_verify_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+	if (argc)
+		usage_with_options(builtin_multi_pack_index_verify_usage,
+				   options);
+
+	return verify_midx_file(the_repository, opts.object_dir, opts.flags);
+}
+
+static int cmd_multi_pack_index_expire(int argc, const char **argv)
+{
+	struct option *options = common_opts;
+
+	argc = parse_options(argc, argv, NULL,
+			     options, builtin_multi_pack_index_expire_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+	if (argc)
+		usage_with_options(builtin_multi_pack_index_expire_usage,
+				   options);
+
+	return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
+}
+
+static int cmd_multi_pack_index_repack(int argc, const char **argv)
+{
+	struct option *options;
+	static struct option builtin_multi_pack_index_repack_options[] = {
 		OPT_MAGNITUDE(0, "batch-size", &opts.batch_size,
 		  N_("during repack, collect pack-files of smaller size into a batch that is larger than this size")),
 		OPT_END(),
 	};
 
+	options = parse_options_dup(builtin_multi_pack_index_repack_options);
+	options = add_common_options(options);
+
+	argc = parse_options(argc, argv, NULL,
+			     options,
+			     builtin_multi_pack_index_repack_usage,
+			     PARSE_OPT_KEEP_UNKNOWN);
+	if (argc)
+		usage_with_options(builtin_multi_pack_index_repack_usage,
+				   options);
+
+	return midx_repack(the_repository, opts.object_dir,
+			   (size_t)opts.batch_size, opts.flags);
+}
+
+int cmd_multi_pack_index(int argc, const char **argv,
+			 const char *prefix)
+{
+	struct option *builtin_multi_pack_index_options = common_opts;
+
 	git_config(git_default_config, NULL);
 
 	if (isatty(2))
 		opts.flags |= MIDX_PROGRESS;
 	argc = parse_options(argc, argv, prefix,
 			     builtin_multi_pack_index_options,
-			     builtin_multi_pack_index_usage, 0);
+			     builtin_multi_pack_index_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
 
 	if (!opts.object_dir)
 		opts.object_dir = get_object_directory();
@@ -58,25 +148,16 @@ int cmd_multi_pack_index(int argc, const char **argv,
 		usage_with_options(builtin_multi_pack_index_usage,
 				   builtin_multi_pack_index_options);
 
-	if (argc > 1) {
-		die(_("too many arguments"));
-		return 1;
-	}
-
 	trace2_cmd_mode(argv[0]);
 
 	if (!strcmp(argv[0], "repack"))
-		return midx_repack(the_repository, opts.object_dir,
-			(size_t)opts.batch_size, opts.flags);
-	if (opts.batch_size)
-		die(_("--batch-size option is only for 'repack' subcommand"));
-
-	if (!strcmp(argv[0], "write"))
-		return write_midx_file(opts.object_dir, opts.flags);
-	if (!strcmp(argv[0], "verify"))
-		return verify_midx_file(the_repository, opts.object_dir, opts.flags);
-	if (!strcmp(argv[0], "expire"))
-		return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
-
-	die(_("unrecognized subcommand: %s"), argv[0]);
+		return cmd_multi_pack_index_repack(argc, argv);
+	else if (!strcmp(argv[0], "write"))
+		return cmd_multi_pack_index_write(argc, argv);
+	else if (!strcmp(argv[0], "verify"))
+		return cmd_multi_pack_index_verify(argc, argv);
+	else if (!strcmp(argv[0], "expire"))
+		return cmd_multi_pack_index_expire(argc, argv);
+	else
+		die(_("unrecognized subcommand: %s"), argv[0]);
 }
-- 
2.30.0.667.g81c0cbc6fd
Previous: Taylor BlauNext: Jeff King
Message 107 of 171 in “midx: implement a multi-pack reverse index”
  1. 0/9 midx: implement a multi-pack reverse indexTaylor Blau, Feb 10, 2021
  2. 1/9 t/helper/test-read-midx.c: add '--show-objects'Taylor Blau, Feb 10, 2021
  3. Derrick StoleeFeb 11, 2021
  4. Taylor BlauFeb 11, 2021
  5. 2/9 midx: allow marking a pack as preferredTaylor Blau, Feb 10, 2021
  6. SZEDER GáborFeb 11, 2021
  7. Taylor BlauFeb 15, 2021
  8. Ævar Arnfjörð BjarmasonFeb 15, 2021
  9. 0/5 commit-graph: parse_options() cleanupÆvar Arnfjörð Bjarmason, Feb 15, 2021
  10. 1/5 commit-graph: define common usage with a macroÆvar Arnfjörð Bjarmason, Feb 15, 2021
  11. Derrick StoleeFeb 16, 2021
  12. 2/5 commit-graph: remove redundant handling of -hÆvar Arnfjörð Bjarmason, Feb 15, 2021
  13. Derrick StoleeFeb 16, 2021
  14. 4/5 commit-graph: refactor dispatch loop for styleÆvar Arnfjörð Bjarmason, Feb 15, 2021
  15. Taylor BlauFeb 15, 2021
  16. Derrick StoleeFeb 16, 2021
  17. Ævar Arnfjörð BjarmasonFeb 16, 2021
  18. Derrick StoleeFeb 16, 2021
  19. 3/5 commit-graph: use parse_options_concat()Ævar Arnfjörð Bjarmason, Feb 15, 2021
  20. Taylor BlauFeb 15, 2021
  21. Taylor BlauFeb 15, 2021
  22. Ævar Arnfjörð BjarmasonFeb 15, 2021
  23. SZEDER GáborSep 17, 2021
  24. Jeff KingSep 17, 2021
  25. Taylor BlauSep 18, 2021
  26. Ævar Arnfjörð BjarmasonSep 18, 2021
  27. Taylor BlauSep 18, 2021
  28. Taylor BlauSep 18, 2021
  29. Ævar Arnfjörð BjarmasonSep 18, 2021
  30. 5/5 commit-graph: show usage on "commit-graph [write|verify] garbage"Ævar Arnfjörð Bjarmason, Feb 15, 2021
  31. Taylor BlauFeb 15, 2021
  32. Derrick StoleeFeb 16, 2021
  33. 0/4 midx: split out sub-commandsTaylor Blau, Feb 15, 2021
  34. 1/4 builtin/multi-pack-index.c: inline 'flags' with optionsTaylor Blau, Feb 15, 2021
  35. 2/4 builtin/multi-pack-index.c: don't handle 'progress' separatelyTaylor Blau, Feb 15, 2021
  36. Ævar Arnfjörð BjarmasonFeb 15, 2021
  37. Taylor BlauFeb 15, 2021
  38. Derrick StoleeFeb 16, 2021
  39. 3/4 builtin/multi-pack-index.c: define common usage with a macroTaylor Blau, Feb 15, 2021
  40. 4/4 builtin/multi-pack-index.c: split sub-commandsTaylor Blau, Feb 15, 2021
  41. Ævar Arnfjörð BjarmasonFeb 15, 2021
  42. Taylor BlauFeb 15, 2021
  43. Ævar Arnfjörð BjarmasonFeb 15, 2021
  44. Taylor BlauFeb 15, 2021
  45. Derrick StoleeFeb 16, 2021
  46. Taylor BlauFeb 16, 2021
  47. 3/9 midx: don't free midx_name earlyTaylor Blau, Feb 10, 2021
  48. 4/9 midx: keep track of the checksumTaylor Blau, Feb 10, 2021
  49. Derrick StoleeFeb 11, 2021
  50. Taylor BlauFeb 11, 2021
  51. 5/9 midx: make some functions non-staticTaylor Blau, Feb 10, 2021
  52. 6/9 Documentation/technical: describe multi-pack reverse indexesTaylor Blau, Feb 10, 2021
  53. Derrick StoleeFeb 11, 2021
  54. Taylor BlauFeb 11, 2021
  55. 7/9 pack-revindex: read multi-pack reverse indexesTaylor Blau, Feb 10, 2021
  56. Derrick StoleeFeb 11, 2021
  57. Taylor BlauFeb 11, 2021
  58. Junio C HamanoFeb 11, 2021
  59. Taylor BlauFeb 11, 2021
  60. 8/9 pack-write.c: extract 'write_rev_file_order'Taylor Blau, Feb 10, 2021
  61. 9/9 pack-revindex: write multi-pack reverse indexesTaylor Blau, Feb 10, 2021
  62. Derrick StoleeFeb 11, 2021
  63. Taylor BlauFeb 11, 2021
  64. Junio C HamanoFeb 11, 2021
  65. Derrick StoleeFeb 11, 2021
  66. Junio C HamanoFeb 11, 2021
  67. 00/15 midx: implement a multi-pack reverse indexTaylor Blau, Feb 24, 2021
  68. 01/15 builtin/multi-pack-index.c: inline 'flags' with optionsTaylor Blau, Feb 24, 2021
  69. 03/15 builtin/multi-pack-index.c: define common usage with a macroTaylor Blau, Feb 24, 2021
  70. 02/15 builtin/multi-pack-index.c: don't handle 'progress' separatelyTaylor Blau, Feb 24, 2021
  71. 04/15 builtin/multi-pack-index.c: split sub-commandsTaylor Blau, Feb 24, 2021
  72. Jonathan TanMar 2, 2021
  73. Taylor BlauMar 2, 2021
  74. Jonathan TanMar 4, 2021
  75. Taylor BlauMar 4, 2021
  76. 06/15 builtin/multi-pack-index.c: display usage on unrecognized commandTaylor Blau, Feb 24, 2021
  77. 05/15 builtin/multi-pack-index.c: don't enter bogus cmd_modeTaylor Blau, Feb 24, 2021
  78. 07/15 t/helper/test-read-midx.c: add '--show-objects'Taylor Blau, Feb 24, 2021
  79. 08/15 midx: allow marking a pack as preferredTaylor Blau, Feb 24, 2021
  80. Jonathan TanMar 2, 2021
  81. Taylor BlauMar 2, 2021
  82. Jonathan TanMar 4, 2021
  83. Taylor BlauMar 4, 2021
  84. 15/15 pack-revindex: write multi-pack reverse indexesTaylor Blau, Feb 24, 2021
  85. Jonathan TanMar 2, 2021
  86. Taylor BlauMar 3, 2021
  87. Jonathan TanMar 4, 2021
  88. Taylor BlauMar 4, 2021
  89. 09/15 midx: don't free midx_name earlyTaylor Blau, Feb 24, 2021
  90. 11/15 midx: make some functions non-staticTaylor Blau, Feb 24, 2021
  91. 10/15 midx: keep track of the checksumTaylor Blau, Feb 24, 2021
  92. 12/15 Documentation/technical: describe multi-pack reverse indexesTaylor Blau, Feb 24, 2021
  93. Jonathan TanMar 2, 2021
  94. Taylor BlauMar 2, 2021
  95. Taylor BlauMar 2, 2021
  96. Jonathan TanMar 4, 2021
  97. 13/15 pack-revindex: read multi-pack reverse indexesTaylor Blau, Feb 24, 2021
  98. Jonathan TanMar 2, 2021
  99. Taylor BlauMar 3, 2021
  100. 14/15 pack-write.c: extract 'write_rev_file_order'Taylor Blau, Feb 24, 2021
  101. 00/16 midx: implement a multi-pack reverse indexTaylor Blau, Mar 11, 2021
  102. 02/16 builtin/multi-pack-index.c: don't handle 'progress' separatelyTaylor Blau, Mar 11, 2021
  103. Jeff KingMar 29, 2021
  104. 01/16 builtin/multi-pack-index.c: inline 'flags' with optionsTaylor Blau, Mar 11, 2021
  105. Jeff KingMar 29, 2021
  106. 03/16 builtin/multi-pack-index.c: define common usage with a macroTaylor Blau, Mar 11, 2021
  107. 04/16 builtin/multi-pack-index.c: split sub-commandsTaylor Blau, Mar 11, 2021
  108. Jeff KingMar 29, 2021
  109. Taylor BlauMar 29, 2021
  110. Jeff KingMar 30, 2021
  111. 05/16 builtin/multi-pack-index.c: don't enter bogus cmd_modeTaylor Blau, Mar 11, 2021
  112. 09/16 midx: don't free midx_name earlyTaylor Blau, Mar 11, 2021
  113. 07/16 t/helper/test-read-midx.c: add '--show-objects'Taylor Blau, Mar 11, 2021
  114. 06/16 builtin/multi-pack-index.c: display usage on unrecognized commandTaylor Blau, Mar 11, 2021
  115. Jeff KingMar 29, 2021
  116. Taylor BlauMar 29, 2021
  117. 08/16 midx: allow marking a pack as preferredTaylor Blau, Mar 11, 2021
  118. Jeff KingMar 29, 2021
  119. Taylor BlauMar 29, 2021
  120. Jeff KingMar 30, 2021
  121. 10/16 midx: keep track of the checksumTaylor Blau, Mar 11, 2021
  122. 11/16 midx: make some functions non-staticTaylor Blau, Mar 11, 2021
  123. 12/16 Documentation/technical: describe multi-pack reverse indexesTaylor Blau, Mar 11, 2021
  124. Jeff KingMar 29, 2021
  125. Taylor BlauMar 29, 2021
  126. 13/16 pack-revindex: read multi-pack reverse indexesTaylor Blau, Mar 11, 2021
  127. Jeff KingMar 29, 2021
  128. Taylor BlauMar 29, 2021
  129. 14/16 pack-write.c: extract 'write_rev_file_order'Taylor Blau, Mar 11, 2021
  130. 15/16 pack-revindex: write multi-pack reverse indexesTaylor Blau, Mar 11, 2021
  131. Jeff KingMar 29, 2021
  132. Taylor BlauMar 29, 2021
  133. 16/16 midx.c: improve cache locality in midx_pack_order_cmp()Taylor Blau, Mar 11, 2021
  134. Jeff KingMar 29, 2021
  135. Taylor BlauMar 29, 2021
  136. Jeff KingMar 30, 2021
  137. Derrick StoleeMar 12, 2021
  138. Jeff KingMar 29, 2021
  139. Junio C HamanoMar 29, 2021
  140. Taylor BlauMar 29, 2021
  141. Jeff KingMar 30, 2021
  142. Taylor BlauMar 30, 2021
  143. 00/16 midx: implement a multi-pack reverse indexTaylor Blau, Mar 30, 2021
  144. 01/16 builtin/multi-pack-index.c: inline 'flags' with optionsTaylor Blau, Mar 30, 2021
  145. 02/16 builtin/multi-pack-index.c: don't handle 'progress' separatelyTaylor Blau, Mar 30, 2021
  146. 04/16 builtin/multi-pack-index.c: split sub-commandsTaylor Blau, Mar 30, 2021
  147. 03/16 builtin/multi-pack-index.c: define common usage with a macroTaylor Blau, Mar 30, 2021
  148. 06/16 builtin/multi-pack-index.c: display usage on unrecognized commandTaylor Blau, Mar 30, 2021
  149. 05/16 builtin/multi-pack-index.c: don't enter bogus cmd_modeTaylor Blau, Mar 30, 2021
  150. 07/16 t/helper/test-read-midx.c: add '--show-objects'Taylor Blau, Mar 30, 2021
  151. 08/16 midx: allow marking a pack as preferredTaylor Blau, Mar 30, 2021
  152. Taylor BlauApr 1, 2021
  153. 10/16 midx: keep track of the checksumTaylor Blau, Mar 30, 2021
  154. 09/16 midx: don't free midx_name earlyTaylor Blau, Mar 30, 2021
  155. 11/16 midx: make some functions non-staticTaylor Blau, Mar 30, 2021
  156. 14/16 pack-write.c: extract 'write_rev_file_order'Taylor Blau, Mar 30, 2021
  157. pack-write: skip *.rev work when not writing *.revÆvar Arnfjörð Bjarmason, Sep 8, 2021
  158. Carlo ArenasSep 8, 2021
  159. Taylor BlauSep 8, 2021
  160. Junio C HamanoSep 8, 2021
  161. Taylor BlauSep 8, 2021
  162. Taylor BlauSep 8, 2021
  163. Ævar Arnfjörð BjarmasonSep 8, 2021
  164. Taylor BlauSep 8, 2021
  165. 12/16 Documentation/technical: describe multi-pack reverse indexesTaylor Blau, Mar 30, 2021
  166. 13/16 pack-revindex: read multi-pack reverse indexesTaylor Blau, Mar 30, 2021
  167. 16/16 midx.c: improve cache locality in midx_pack_order_cmp()Taylor Blau, Mar 30, 2021
  168. 15/16 pack-revindex: write multi-pack reverse indexesTaylor Blau, Mar 30, 2021
  169. Jeff KingMar 30, 2021
  170. Taylor BlauMar 30, 2021
  171. Jeff KingMar 30, 2021

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.