From: kristofferhaugsbakk@fastmail.com
To: git@vger.kernel.org
Cc: Kristoffer Haugsbakk <code@khaugsbakk.name>
Subject: [PATCH 2/4] format-rev: factor option variables into a struct
Date: Thu, 13 Aug 2026 19:23:58 +0200 [thread overview]
Message-ID: <factor_opts_into_struct.b82@msgid.xyz> (raw)
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", ¬es, 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, ¬es)
+ 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(¬es, 0);
+ string_list_clear(&data.notes, 0);
release_display_notes(&format_notes_opt);
return 0;
}
--
2.54.0.22.g9e26862b904
next prev parent reply other threads:[~2026-08-13 17:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-13 17:23 [PATCH 0/4] format-rev: add --abbrev, --color, and --date kristofferhaugsbakk
2026-08-13 17:23 ` [PATCH 1/4] format-rev: use lower case for opts description kristofferhaugsbakk
2026-08-13 17:23 ` kristofferhaugsbakk [this message]
2026-08-13 18:21 ` [PATCH 2/4] format-rev: factor option variables into a struct Junio C Hamano
2026-08-13 17:23 ` [PATCH 3/4] doc: rev-list-options.adoc: factor out --date alts kristofferhaugsbakk
2026-08-13 17:24 ` [PATCH 4/4] format-rev: learn --abbrev, --color, and --date kristofferhaugsbakk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=factor_opts_into_struct.b82@msgid.xyz \
--to=kristofferhaugsbakk@fastmail.com \
--cc=code@khaugsbakk.name \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox