Skip to content

Commit 9d4fcfe

Browse files
vdyegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56d26ad commit 9d4fcfe

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

builtin/branch.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ static const char *head;
4545
static struct object_id head_oid;
4646
static int recurse_submodules = 0;
4747
static int submodule_propagate_branches = 0;
48-
static int omit_empty = 0;
4948

5049
static int branch_use_color = -1;
5150
static char branch_colors[][COLOR_MAXLEN] = {
@@ -480,7 +479,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
480479
string_list_append(output, out.buf);
481480
} else {
482481
fwrite(out.buf, 1, out.len, stdout);
483-
if (out.len || !omit_empty)
482+
if (out.len || !format->array_opts.omit_empty)
484483
putchar('\n');
485484
}
486485
}
@@ -737,7 +736,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
737736
OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
738737
OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
739738
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,
741740
N_("do not output a newline after empty formatted refs")),
742741
OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
743742
OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),

builtin/for-each-ref.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ static char const * const for_each_ref_usage[] = {
1919

2020
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
2121
{
22-
int i;
22+
int i, total;
2323
struct ref_sorting *sorting;
2424
struct string_list sorting_options = STRING_LIST_INIT_DUP;
25-
int maxcount = 0, icase = 0, omit_empty = 0;
25+
int icase = 0;
2626
struct ref_array array;
2727
struct ref_filter filter = REF_FILTER_INIT;
2828
struct ref_format format = REF_FORMAT_INIT;
@@ -40,11 +40,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
4040
N_("quote placeholders suitably for python"), QUOTE_PYTHON),
4141
OPT_BIT(0 , "tcl", &format.quote_style,
4242
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,
4444
N_("do not output a newline after empty formatted refs")),
4545

4646
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")),
4848
OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
4949
OPT__COLOR(&format.use_color, N_("respect format colors")),
5050
OPT_REF_FILTER_EXCLUDE(&filter),
@@ -71,8 +71,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
7171
string_list_append(&sorting_options, "refname");
7272

7373
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);
7676
usage_with_options(for_each_ref_usage, opts);
7777
}
7878
if (HAS_MULTI_BITS(format.quote_style)) {
@@ -109,15 +109,16 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
109109

110110
ref_array_sort(sorting, &array);
111111

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++) {
115116
strbuf_reset(&err);
116117
strbuf_reset(&output);
117118
if (format_ref_array_item(array.items[i], &format, &output, &err))
118119
die("%s", err.buf);
119120
fwrite(output.buf, 1, output.len, stdout);
120-
if (output.len || !omit_empty)
121+
if (output.len || !format.array_opts.omit_empty)
121122
putchar('\n');
122123
}
123124

builtin/tag.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ static const char * const git_tag_usage[] = {
4444
static unsigned int colopts;
4545
static int force_sign_annotate;
4646
static int config_sign_tag = -1; /* unspecified */
47-
static int omit_empty = 0;
4847

4948
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
5049
struct ref_format *format)
@@ -83,7 +82,7 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
8382
if (format_ref_array_item(array.items[i], format, &output, &err))
8483
die("%s", err.buf);
8584
fwrite(output.buf, 1, output.len, stdout);
86-
if (output.len || !omit_empty)
85+
if (output.len || !format->array_opts.omit_empty)
8786
putchar('\n');
8887
}
8988

@@ -481,7 +480,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
481480
OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
482481
OPT_MERGED(&filter, N_("print only tags that are merged")),
483482
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,
485484
N_("do not output a newline after empty formatted refs")),
486485
OPT_REF_SORT(&sorting_options),
487486
{

ref-filter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ struct ref_format {
9292

9393
/* List of bases for ahead-behind counts. */
9494
struct string_list bases;
95+
96+
struct {
97+
int max_count;
98+
int omit_empty;
99+
} array_opts;
95100
};
96101

97102
#define REF_FILTER_INIT { \

0 commit comments

Comments
 (0)