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

[PATCH 2/4] format-rev: factor option variables into a struct

From
Kkristofferhaugsbakk@fastmail.com <kristofferhaugsbakk@fastmail.com>
Date
Aug 13, 2026, 17:23 UTC
Message-ID
<factor_opts_into_struct.b82@msgid.xyz>
In-Reply-To
<CV_format-rev_three_more_opts.b80@msgid.xyz>
From: Kristoffer Haugsbakk <code@khaugsbakk.name>

We will in two commits add three more options to this command. Let’s prepare for that by moving option variables into a struct so that we get less local variables.

This allows us to inline `format_nul_data` into this new structure. Let’s also rename `stdin_mode_arg` to `stdin_mode`. (We couldn’t use `stdin_mode` before because of the enumeration with the same name.)

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 builtin/name-rev.c | 44 +++++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 21 deletions(-)
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 254c88199fd..7d824aa1c5d 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -772,16 +772,19 @@ int cmd_name_rev(int argc,
 	return 0;
 }
 
-struct format_nul_data {
+struct format_rev_data {
+	const char *format;
+	const char *stdin_mode;
 	bool nul_input;
 	bool nul_output;
+	struct string_list notes;
 };
 
 static int format_nul_cb(const struct option *option,
 			 const char *arg,
 			 int unset)
 {
-	struct format_nul_data *data = option->value;
+	struct format_rev_data *data = option->value;
 	data->nul_input = 1;
 	data->nul_output = 1;
 	BUG_ON_OPT_NEG(unset);
@@ -813,31 +816,30 @@ int cmd_format_rev(int argc,
 		   const char *prefix,
 		   struct repository *repo UNUSED)
 {
-	const char *format = NULL;
+	struct format_rev_data data = {
+		NULL, NULL, 0, 0, STRING_LIST_INIT_NODUP
+	};
 	enum stdin_mode stdin_mode;
-	const char *stdin_mode_arg = NULL;
-	struct format_nul_data nul_data = { 0, 0 };
 	char output_terminator;
 	strbuf_getline_fn getline_fn;
 	struct display_notes_opt format_notes_opt;
 	struct rev_info format_rev = REV_INFO_INIT;
 	struct pretty_format format_pp = { 0 };
-	struct string_list notes = STRING_LIST_INIT_NODUP;
 	struct strbuf scratch_buf = STRBUF_INIT;
 	struct command cmd;
 	struct option opts[] = {
-		OPT_STRING(0, "format", &format, N_("format"),
+		OPT_STRING(0, "format", &data.format, N_("format"),
 			   N_("pretty format to use")),
-		OPT_STRING(0, "stdin-mode", &stdin_mode_arg, N_("stdin-mode"),
+		OPT_STRING(0, "stdin-mode", &data.stdin_mode, N_("stdin-mode"),
 			   N_("how revs are processed")),
-		OPT_STRING_LIST(0, "notes", &notes, N_("notes"),
+		OPT_STRING_LIST(0, "notes", &data.notes, N_("notes"),
 				N_("display notes for pretty format")),
-		OPT_CALLBACK_F('z', "null", &nul_data, N_("z"),
+		OPT_CALLBACK_F('z', "null", &data, N_("z"),
 			       N_("use NUL for input and output termination"),
 			       PARSE_OPT_NOARG | PARSE_OPT_NONEG, format_nul_cb),
-		OPT_BOOL(0, "null-input", &nul_data.nul_input,
+		OPT_BOOL(0, "null-input", &data.nul_input,
 			 N_("use NUL for input termination")),
-		OPT_BOOL(0, "null-output", &nul_data.nul_output,
+		OPT_BOOL(0, "null-output", &data.nul_output,
 			 N_("use NUL for output termination")),
 		OPT_END(),
 	};
@@ -849,18 +851,18 @@ int cmd_format_rev(int argc,
 		usage_with_options(format_rev_usage, opts);
 	}
 
-	if (!format)
+	if (!data.format)
 		die(_("'%s' is required"), "--format");
-	if (!stdin_mode_arg)
+	if (!data.stdin_mode)
 		die(_("'%s' is required"), "--stdin-mode");
 
-	getline_fn = nul_data.nul_input ? strbuf_getline_nul : strbuf_getline_lf;
-	output_terminator = nul_data.nul_output ? '\0' : '\n';
+	getline_fn = data.nul_input ? strbuf_getline_nul : strbuf_getline_lf;
+	output_terminator = data.nul_output ? '\0' : '\n';
 
 	init_display_notes(&format_notes_opt);
-	stdin_mode = parse_stdin_mode(stdin_mode_arg);
+	stdin_mode = parse_stdin_mode(data.stdin_mode);
 
-	get_commit_format(format, &format_rev);
+	get_commit_format(data.format, &format_rev);
 	format_pp.ctx.rev = &format_rev;
 	format_pp.ctx.fmt = format_rev.commit_format;
 	format_pp.ctx.abbrev = format_rev.abbrev;
@@ -868,13 +870,13 @@ int cmd_format_rev(int argc,
 	format_pp.ctx.date_mode = format_rev.date_mode;
 	format_pp.ctx.color = GIT_COLOR_AUTO;
 
-	userformat_find_requirements(format,
+	userformat_find_requirements(data.format,
 				     &format_pp.want);
 	if (format_pp.want.notes) {
 		int ignore_show_notes = 0;
 		struct string_list_item *n;
 
-		for_each_string_list_item(n, &notes)
+		for_each_string_list_item(n, &data.notes)
 			enable_ref_display_notes(&format_notes_opt,
 						 &ignore_show_notes,
 						 n->string);
@@ -934,7 +936,7 @@ int cmd_format_rev(int argc,
 	}
 
 	strbuf_release(&scratch_buf);
-	string_list_clear(&notes, 0);
+	string_list_clear(&data.notes, 0);
 	release_display_notes(&format_notes_opt);
 	return 0;
 }
-- 
2.54.0.22.g9e26862b904
Previous: kristofferhaugsbakk@fastmail.comNext: Junio C Hamano
Message 3 of 25 in “format-rev: add --abbrev, --color, and --date”
  1. 0/4 format-rev: add --abbrev, --color, and --datekristofferhaugsbakk@fastmail.com, Aug 13, 2026
  2. 1/4 format-rev: use lower case for opts descriptionkristofferhaugsbakk@fastmail.com, Aug 13, 2026
  3. 2/4 format-rev: factor option variables into a structkristofferhaugsbakk@fastmail.com, Aug 13, 2026
  4. Junio C HamanoAug 13, 2026
  5. Kristoffer HaugsbakkAug 14, 2026
  6. 3/4 doc: rev-list-options.adoc: factor out --date altskristofferhaugsbakk@fastmail.com, Aug 13, 2026
  7. 4/4 format-rev: learn --abbrev, --color, and --datekristofferhaugsbakk@fastmail.com, Aug 13, 2026
  8. Junio C HamanoAug 15, 2026
  9. Kristoffer HaugsbakkAug 17, 2026
  10. Junio C HamanoAug 17, 2026
  11. Kristoffer HaugsbakkAug 18, 2026
  12. 0/5 format-rev: add --abbrev, --color, and --datekristofferhaugsbakk@fastmail.com, Aug 18, 2026
  13. 1/5 format-rev: use lower case for opts descriptionkristofferhaugsbakk@fastmail.com, Aug 18, 2026
  14. 2/5 format-rev: place BUG calls first in callbackkristofferhaugsbakk@fastmail.com, Aug 18, 2026
  15. 3/5 format-rev: factor option variables into a structkristofferhaugsbakk@fastmail.com, Aug 18, 2026
  16. 4/5 doc: rev-list-options.adoc: factor out --date altskristofferhaugsbakk@fastmail.com, Aug 18, 2026
  17. 5/5 format-rev: learn --abbrev, --color, and --datekristofferhaugsbakk@fastmail.com, Aug 18, 2026
  18. Junio C HamanoAug 26, 2026
  19. Ben KnobleSep 3, 2026
  20. Kristoffer HaugsbakkSep 3, 2026
  21. Junio C HamanoNov 2, 2026
  22. Kristoffer HaugsbakkNov 2, 2026
  23. Ben KnobleNov 2, 2026
  24. Kristoffer HaugsbakkNov 3, 2026
  25. D. Ben KnobleNov 7, 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.