ref-filter.h: add max_count and omit_empty to ref_format

Add an internal 'array_opts' struct to 'struct ref_format' containing formatting options that pertain to the formatting of an entire ref array: 'max_count' and 'omit_empty'. These values are specified by the '--count' and '--omit-empty' options, respectively, to 'for-each-ref'/'tag'/'branch'. Storing these values in the 'ref_format' will simplify the consolidation of ref array formatting logic across builtins in later patches. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Nov 14, 2023 at 19:53 UTC 9d4fcfe1ff5b901f47f8226d078d22370bb955be
4 files changed +20 -16
builtin/branch.c
+2 -3
@@ -45,7 +45,6 @@ static const char *head;
45 static struct object_id head_oid;
46 static int recurse_submodules = 0;
47 static int submodule_propagate_branches = 0;
48 -static int omit_empty = 0;
48
49 static int branch_use_color = -1;
50 static char branch_colors[][COLOR_MAXLEN] = {
@@ -480,7 +479,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
479 string_list_append(output, out.buf);
480 } else {
481 fwrite(out.buf, 1, out.len, stdout);
483 - if (out.len || !omit_empty)
482 + if (out.len || !format->array_opts.omit_empty)
483 putchar('\n');
484 }
485 }
@@ -737,7 +736,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
736 OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
737 OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
738 OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
740 - OPT_BOOL(0, "omit-empty", &omit_empty,
739 + OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty,
740 N_("do not output a newline after empty formatted refs")),
741 OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
742 OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
builtin/for-each-ref.c
+11 -10
@@ -19,10 +19,10 @@ static char const * const for_each_ref_usage[] = {
19
20 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
21 {
22 - int i;
22 + int i, total;
23 struct ref_sorting *sorting;
24 struct string_list sorting_options = STRING_LIST_INIT_DUP;
25 - int maxcount = 0, icase = 0, omit_empty = 0;
25 + int icase = 0;
26 struct ref_array array;
27 struct ref_filter filter = REF_FILTER_INIT;
28 struct ref_format format = REF_FORMAT_INIT;
@@ -40,11 +40,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
40 N_("quote placeholders suitably for python"), QUOTE_PYTHON),
41 OPT_BIT(0 , "tcl", &format.quote_style,
42 N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
43 - OPT_BOOL(0, "omit-empty", &omit_empty,
43 + OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty,
44 N_("do not output a newline after empty formatted refs")),
45
46 OPT_GROUP(""),
47 - OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
47 + OPT_INTEGER( 0 , "count", &format.array_opts.max_count, N_("show only <n> matched refs")),
48 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
49 OPT__COLOR(&format.use_color, N_("respect format colors")),
50 OPT_REF_FILTER_EXCLUDE(&filter),
@@ -71,8 +71,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
71 string_list_append(&sorting_options, "refname");
72
73 parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
74 - if (maxcount < 0) {
75 - error("invalid --count argument: `%d'", maxcount);
74 + if (format.array_opts.max_count < 0) {
75 + error("invalid --count argument: `%d'", format.array_opts.max_count);
76 usage_with_options(for_each_ref_usage, opts);
77 }
78 if (HAS_MULTI_BITS(format.quote_style)) {
@@ -109,15 +109,16 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
109
110 ref_array_sort(sorting, &array);
111
112 - if (!maxcount || array.nr < maxcount)
113 - maxcount = array.nr;
114 - for (i = 0; i < maxcount; i++) {
112 + total = format.array_opts.max_count;
113 + if (!total || array.nr < total)
114 + total = array.nr;
115 + for (i = 0; i < total; i++) {
116 strbuf_reset(&err);
117 strbuf_reset(&output);
118 if (format_ref_array_item(array.items[i], &format, &output, &err))
119 die("%s", err.buf);
120 fwrite(output.buf, 1, output.len, stdout);
120 - if (output.len || !omit_empty)
121 + if (output.len || !format.array_opts.omit_empty)
122 putchar('\n');
123 }
124
builtin/tag.c
+2 -3
@@ -44,7 +44,6 @@ static const char * const git_tag_usage[] = {
44 static unsigned int colopts;
45 static int force_sign_annotate;
46 static int config_sign_tag = -1; /* unspecified */
47 -static int omit_empty = 0;
47
48 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
49 struct ref_format *format)
@@ -83,7 +82,7 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
82 if (format_ref_array_item(array.items[i], format, &output, &err))
83 die("%s", err.buf);
84 fwrite(output.buf, 1, output.len, stdout);
86 - if (output.len || !omit_empty)
85 + if (output.len || !format->array_opts.omit_empty)
86 putchar('\n');
87 }
88
@@ -481,7 +480,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
480 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
481 OPT_MERGED(&filter, N_("print only tags that are merged")),
482 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
484 - OPT_BOOL(0, "omit-empty", &omit_empty,
483 + OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty,
484 N_("do not output a newline after empty formatted refs")),
485 OPT_REF_SORT(&sorting_options),
486 {
ref-filter.h
+5
@@ -92,6 +92,11 @@ struct ref_format {
92
93 /* List of bases for ahead-behind counts. */
94 struct string_list bases;
95 +
96 + struct {
97 + int max_count;
98 + int omit_empty;
99 + } array_opts;
100 };
101
102 #define REF_FILTER_INIT { \