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

[PATCH 5/7] builtin/commit-graph: simplify how we wire up common options

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 28, 2026, 11:57 UTC
Message-ID
<20261028-pks-commit-graph-cleanups-v1-5-c549344c363f@pks.im>
In-Reply-To
<20261028-pks-commit-graph-cleanups-v1-0-c549344c363f@pks.im>

All of the git-commit-graph(1) subcommands accept the "--object-dir=" option. To avoid duplication we thus have a helper function that adds this common option to our array of command-specific options. This works alright, but it feels rather awkward as this forces us to allocate and free a secondary array of options.

Refactor the code to instead have common options as a preprocessor macro that we can add. This avoids the extra allocation and is overall a bit less verbose.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/commit-graph.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 7140433a39a..165dfcb827f 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -52,17 +52,10 @@ static struct opts_commit_graph {
 	int enable_changed_paths;
 } opts;
 
-static struct option common_opts[] = {
-	OPT_STRING(0, "object-dir", &opts.obj_dir,
-		   N_("dir"),
-		   N_("the object directory to store the graph")),
-	OPT_END()
-};
-
-static struct option *add_common_options(struct option *to)
-{
-	return parse_options_concat(common_opts, to);
-}
+#define COMMIT_GRAPH_COMMON_OPTIONS(opts) \
+	OPT_STRING(0, "object-dir", &(opts)->obj_dir, \
+		   N_("dir"), \
+		   N_("the object directory to store the graph"))
 
 static int cmd_commit_graph_verify(int argc,
 				   const char **argv,
@@ -79,15 +72,14 @@ static int cmd_commit_graph_verify(int argc,
 	int flags = 0;
 	int incomplete_chain = 0;
 	int ret;
-
-	static struct option builtin_commit_graph_verify_options[] = {
+	static struct option options[] = {
 		OPT_BOOL(0, "shallow", &opts.shallow,
 			 N_("if the commit-graph is split, only verify the tip file")),
 		OPT_BOOL(0, "progress", &opts.progress,
 			 N_("force progress reporting")),
+		COMMIT_GRAPH_COMMON_OPTIONS(&opts),
 		OPT_END(),
 	};
-	struct option *options = add_common_options(builtin_commit_graph_verify_options);
 
 	trace2_cmd_mode("verify");
 
@@ -123,7 +115,6 @@ static int cmd_commit_graph_verify(int argc,
 
 	FREE_AND_NULL(graph_name);
 	FREE_AND_NULL(chain_name);
-	FREE_AND_NULL(options);
 
 	if (opened == OPENED_NONE)
 		return 0;
@@ -238,8 +229,7 @@ static int cmd_commit_graph_write(int argc,
 	int result = 0;
 	enum commit_graph_write_flags flags = 0;
 	struct progress *progress = NULL;
-
-	static struct option builtin_commit_graph_write_options[] = {
+	static struct option options[] = {
 		OPT_BOOL(0, "reachable", &opts.reachable,
 			N_("start walk at all refs")),
 		OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
@@ -265,9 +255,9 @@ static int cmd_commit_graph_write(int argc,
 			0, write_option_max_new_filters),
 		OPT_BOOL(0, "progress", &opts.progress,
 			 N_("force progress reporting")),
+		COMMIT_GRAPH_COMMON_OPTIONS(&opts),
 		OPT_END(),
 	};
-	struct option *options = add_common_options(builtin_commit_graph_write_options);
 
 	opts.progress = isatty(2);
 	opts.enable_changed_paths = -1;
@@ -352,7 +342,6 @@ static int cmd_commit_graph_write(int argc,
 		result = 1;
 
 cleanup:
-	FREE_AND_NULL(options);
 	string_list_clear(&pack_indexes, 0);
 	strbuf_release(&buf);
 	oidset_clear(&commits);
-- 
2.56.0.341.g393f52fd96.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 6 of 12 in “builtin/commit-graph: some small cleanups”
  1. 0/7 builtin/commit-graph: some small cleanupsPatrick Steinhardt, Oct 28, 2026
  2. 1/7 builtin/commit-graph: rename subcommand functionsPatrick Steinhardt, Oct 28, 2026
  3. 2/7 builtin/commit-graph: drop declaration for `read_replace_refs`Patrick Steinhardt, Oct 28, 2026
  4. 3/7 builtin/commit-graph: get rid of `the_repository`Patrick Steinhardt, Oct 28, 2026
  5. 4/7 builtin/commit-graph: drop global "--object-dir=" switchPatrick Steinhardt, Oct 28, 2026
  6. 5/7 builtin/commit-graph: simplify how we wire up common optionsPatrick Steinhardt, Oct 28, 2026
  7. 6/7 builtin/commit-graph: de-globalize write options handlingPatrick Steinhardt, Oct 28, 2026
  8. 7/7 builtin/commit-graph: de-globalize options handlingPatrick Steinhardt, Oct 28, 2026
  9. Mark C. Chu-CarrollOct 28, 2026
  10. Junio C HamanoOct 28, 2026
  11. Junio C HamanoOct 28, 2026
  12. Karthik NayakOct 29, 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.