format-rev: factor option variables into a struct

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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
seen
Kristoffer Haugsbakk 2026-08-18 11:57:32 +02:00 committed by Junio C Hamano
parent 0ac2a41b18
commit 71051871fa
1 changed files with 23 additions and 21 deletions

View File

@ -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;
BUG_ON_OPT_NEG(unset);
BUG_ON_OPT_ARG(arg);
data->nul_input = 1;
@ -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 = {
.notes = 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;
}