Skip to content

Commit 4a68e36

Browse files
peffgitster
authored andcommitted
ref-filter: abstract ref format into its own struct
The ref-filter module provides routines for formatting a ref for output. The fundamental interface for the format is a "const char *" containing the format, and any additional options need to be passed to each invocation of show_ref_array_item. Instead, let's make a ref_format struct that holds the format, along with any associated format options. That will make some enhancements easier in the future: 1. new formatting options can be added without disrupting existing callers 2. some state can be carried in the struct rather than as global variables For now this just has the text format itself along with the quote_style option, but we'll add more fields in future patches. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51331aa commit 4a68e36

File tree

6 files changed

+70
-52
lines changed

6 files changed

+70
-52
lines changed

builtin/branch.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
383383
return strbuf_detach(&fmt, NULL);
384384
}
385385

386-
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
386+
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
387387
{
388388
int i;
389389
struct ref_array array;
@@ -407,16 +407,16 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
407407
if (filter->verbose)
408408
maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
409409

410-
if (!format)
411-
format = to_free = build_format(filter, maxwidth, remote_prefix);
410+
if (!format->format)
411+
format->format = to_free = build_format(filter, maxwidth, remote_prefix);
412412

413413
if (verify_ref_format(format))
414414
die(_("unable to parse format string"));
415415

416416
ref_array_sort(sorting, &array);
417417

418418
for (i = 0; i < array.nr; i++) {
419-
format_ref_array_item(array.items[i], format, 0, &out);
419+
format_ref_array_item(array.items[i], format, &out);
420420
if (column_active(colopts)) {
421421
assert(!filter->verbose && "--column and --verbose are incompatible");
422422
/* format to a string_list to let print_columns() do its job */
@@ -551,7 +551,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
551551
struct ref_filter filter;
552552
int icase = 0;
553553
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
554-
const char *format = NULL;
554+
struct ref_format format = REF_FORMAT_INIT;
555555

556556
struct option options[] = {
557557
OPT_GROUP(N_("Generic options")),
@@ -595,7 +595,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
595595
N_("print only branches of the object"), 0, parse_opt_object_name
596596
},
597597
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
598-
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
598+
OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
599599
OPT_END(),
600600
};
601601

@@ -669,7 +669,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
669669
if (!sorting)
670670
sorting = ref_default_sorting();
671671
sorting->ignore_case = icase;
672-
print_ref_list(&filter, sorting, format);
672+
print_ref_list(&filter, sorting, &format);
673673
print_columns(&output, colopts, NULL);
674674
string_list_clear(&output, 0);
675675
return 0;

builtin/for-each-ref.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ static char const * const for_each_ref_usage[] = {
1717
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1818
{
1919
int i;
20-
const char *format = "%(objectname) %(objecttype)\t%(refname)";
2120
struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
22-
int maxcount = 0, quote_style = 0, icase = 0;
21+
int maxcount = 0, icase = 0;
2322
struct ref_array array;
2423
struct ref_filter filter;
24+
struct ref_format format = REF_FORMAT_INIT;
2525

2626
struct option opts[] = {
27-
OPT_BIT('s', "shell", &quote_style,
27+
OPT_BIT('s', "shell", &format.quote_style,
2828
N_("quote placeholders suitably for shells"), QUOTE_SHELL),
29-
OPT_BIT('p', "perl", &quote_style,
29+
OPT_BIT('p', "perl", &format.quote_style,
3030
N_("quote placeholders suitably for perl"), QUOTE_PERL),
31-
OPT_BIT(0 , "python", &quote_style,
31+
OPT_BIT(0 , "python", &format.quote_style,
3232
N_("quote placeholders suitably for python"), QUOTE_PYTHON),
33-
OPT_BIT(0 , "tcl", &quote_style,
33+
OPT_BIT(0 , "tcl", &format.quote_style,
3434
N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
3535

3636
OPT_GROUP(""),
3737
OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
38-
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
38+
OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
3939
OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
4040
N_("field name to sort on"), &parse_opt_ref_sorting),
4141
OPT_CALLBACK(0, "points-at", &filter.points_at,
@@ -52,16 +52,18 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
5252
memset(&array, 0, sizeof(array));
5353
memset(&filter, 0, sizeof(filter));
5454

55+
format.format = "%(objectname) %(objecttype)\t%(refname)";
56+
5557
parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);
5658
if (maxcount < 0) {
5759
error("invalid --count argument: `%d'", maxcount);
5860
usage_with_options(for_each_ref_usage, opts);
5961
}
60-
if (HAS_MULTI_BITS(quote_style)) {
62+
if (HAS_MULTI_BITS(format.quote_style)) {
6163
error("more than one quoting style?");
6264
usage_with_options(for_each_ref_usage, opts);
6365
}
64-
if (verify_ref_format(format))
66+
if (verify_ref_format(&format))
6567
usage_with_options(for_each_ref_usage, opts);
6668

6769
if (!sorting)
@@ -80,7 +82,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
8082
if (!maxcount || array.nr < maxcount)
8183
maxcount = array.nr;
8284
for (i = 0; i < maxcount; i++)
83-
show_ref_array_item(array.items[i], format, quote_style);
85+
show_ref_array_item(array.items[i], &format);
8486
ref_array_clear(&array);
8587
return 0;
8688
}

builtin/tag.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static const char * const git_tag_usage[] = {
3232
static unsigned int colopts;
3333
static int force_sign_annotate;
3434

35-
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
35+
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
36+
struct ref_format *format)
3637
{
3738
struct ref_array array;
3839
char *to_free = NULL;
@@ -43,14 +44,14 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
4344
if (filter->lines == -1)
4445
filter->lines = 0;
4546

46-
if (!format) {
47+
if (!format->format) {
4748
if (filter->lines) {
4849
to_free = xstrfmt("%s %%(contents:lines=%d)",
4950
"%(align:15)%(refname:lstrip=2)%(end)",
5051
filter->lines);
51-
format = to_free;
52+
format->format = to_free;
5253
} else
53-
format = "%(refname:lstrip=2)";
54+
format->format = "%(refname:lstrip=2)";
5455
}
5556

5657
if (verify_ref_format(format))
@@ -60,7 +61,7 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
6061
ref_array_sort(sorting, &array);
6162

6263
for (i = 0; i < array.nr; i++)
63-
show_ref_array_item(array.items[i], format, 0);
64+
show_ref_array_item(array.items[i], format);
6465
ref_array_clear(&array);
6566
free(to_free);
6667

@@ -106,17 +107,17 @@ static int verify_tag(const char *name, const char *ref,
106107
const struct object_id *oid, const void *cb_data)
107108
{
108109
int flags;
109-
const char *fmt_pretty = cb_data;
110+
const struct ref_format *format = cb_data;
110111
flags = GPG_VERIFY_VERBOSE;
111112

112-
if (fmt_pretty)
113+
if (format->format)
113114
flags = GPG_VERIFY_OMIT_STATUS;
114115

115116
if (gpg_verify_tag(oid->hash, name, flags))
116117
return -1;
117118

118-
if (fmt_pretty)
119-
pretty_print_ref(name, oid->hash, fmt_pretty);
119+
if (format->format)
120+
pretty_print_ref(name, oid->hash, format);
120121

121122
return 0;
122123
}
@@ -393,7 +394,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
393394
struct strbuf err = STRBUF_INIT;
394395
struct ref_filter filter;
395396
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
396-
const char *format = NULL;
397+
struct ref_format format = REF_FORMAT_INIT;
397398
int icase = 0;
398399
struct option options[] = {
399400
OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
@@ -432,7 +433,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
432433
N_("print only tags of the object"), PARSE_OPT_LASTARG_DEFAULT,
433434
parse_opt_object_name, (intptr_t) "HEAD"
434435
},
435-
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
436+
OPT_STRING( 0 , "format", &format.format, N_("format"),
437+
N_("format to use for the output")),
436438
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
437439
OPT_END()
438440
};
@@ -484,7 +486,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
484486
run_column_filter(colopts, &copts);
485487
}
486488
filter.name_patterns = argv;
487-
ret = list_tags(&filter, sorting, format);
489+
ret = list_tags(&filter, sorting, &format);
488490
if (column_active(colopts))
489491
stop_column_filter();
490492
return ret;
@@ -502,9 +504,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
502504
if (cmdmode == 'd')
503505
return for_each_tag_name(argv, delete_tag, NULL);
504506
if (cmdmode == 'v') {
505-
if (format && verify_ref_format(format))
507+
if (format.format && verify_ref_format(&format))
506508
usage_with_options(git_tag_usage, options);
507-
return for_each_tag_name(argv, verify_tag, format);
509+
return for_each_tag_name(argv, verify_tag, &format);
508510
}
509511

510512
if (msg.given || msgfile) {

builtin/verify-tag.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
3232
{
3333
int i = 1, verbose = 0, had_error = 0;
3434
unsigned flags = 0;
35-
char *fmt_pretty = NULL;
35+
struct ref_format format = REF_FORMAT_INIT;
3636
const struct option verify_tag_options[] = {
3737
OPT__VERBOSE(&verbose, N_("print tag contents")),
3838
OPT_BIT(0, "raw", &flags, N_("print raw gpg status output"), GPG_VERIFY_RAW),
39-
OPT_STRING( 0 , "format", &fmt_pretty, N_("format"), N_("format to use for the output")),
39+
OPT_STRING(0, "format", &format.format, N_("format"), N_("format to use for the output")),
4040
OPT_END()
4141
};
4242

@@ -50,8 +50,8 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
5050
if (verbose)
5151
flags |= GPG_VERIFY_VERBOSE;
5252

53-
if (fmt_pretty) {
54-
if (verify_ref_format(fmt_pretty))
53+
if (format.format) {
54+
if (verify_ref_format(&format))
5555
usage_with_options(verify_tag_usage,
5656
verify_tag_options);
5757
flags |= GPG_VERIFY_OMIT_STATUS;
@@ -70,8 +70,8 @@ int cmd_verify_tag(int argc, const char **argv, const char *prefix)
7070
continue;
7171
}
7272

73-
if (fmt_pretty)
74-
pretty_print_ref(name, sha1, fmt_pretty);
73+
if (format.format)
74+
pretty_print_ref(name, sha1, &format);
7575
}
7676
return had_error;
7777
}

ref-filter.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,12 @@ static const char *find_next(const char *cp)
657657
* Make sure the format string is well formed, and parse out
658658
* the used atoms.
659659
*/
660-
int verify_ref_format(const char *format)
660+
int verify_ref_format(struct ref_format *format)
661661
{
662662
const char *cp, *sp;
663663

664664
need_color_reset_at_eol = 0;
665-
for (cp = format; *cp && (sp = find_next(cp)); ) {
665+
for (cp = format->format; *cp && (sp = find_next(cp)); ) {
666666
const char *color, *ep = strchr(sp, ')');
667667
int at;
668668

@@ -2060,16 +2060,17 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
20602060
}
20612061
}
20622062

2063-
void format_ref_array_item(struct ref_array_item *info, const char *format,
2064-
int quote_style, struct strbuf *final_buf)
2063+
void format_ref_array_item(struct ref_array_item *info,
2064+
const struct ref_format *format,
2065+
struct strbuf *final_buf)
20652066
{
20662067
const char *cp, *sp, *ep;
20672068
struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
20682069

2069-
state.quote_style = quote_style;
2070+
state.quote_style = format->quote_style;
20702071
push_stack_element(&state.stack);
20712072

2072-
for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
2073+
for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
20732074
struct atom_value *atomv;
20742075

20752076
ep = strchr(sp, ')');
@@ -2093,23 +2094,24 @@ void format_ref_array_item(struct ref_array_item *info, const char *format,
20932094
pop_stack_element(&state.stack);
20942095
}
20952096

2096-
void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
2097+
void show_ref_array_item(struct ref_array_item *info,
2098+
const struct ref_format *format)
20972099
{
20982100
struct strbuf final_buf = STRBUF_INIT;
20992101

2100-
format_ref_array_item(info, format, quote_style, &final_buf);
2102+
format_ref_array_item(info, format, &final_buf);
21012103
fwrite(final_buf.buf, 1, final_buf.len, stdout);
21022104
strbuf_release(&final_buf);
21032105
putchar('\n');
21042106
}
21052107

21062108
void pretty_print_ref(const char *name, const unsigned char *sha1,
2107-
const char *format)
2109+
const struct ref_format *format)
21082110
{
21092111
struct ref_array_item *ref_item;
21102112
ref_item = new_ref_array_item(name, sha1, 0);
21112113
ref_item->kind = ref_kind_from_refname(name);
2112-
show_ref_array_item(ref_item, format, 0);
2114+
show_ref_array_item(ref_item, format);
21132115
free_array_item(ref_item);
21142116
}
21152117

ref-filter.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ struct ref_filter {
7272
verbose;
7373
};
7474

75+
struct ref_format {
76+
/*
77+
* Set these to define the format; make sure you call
78+
* verify_ref_format() afterwards to finalize.
79+
*/
80+
const char *format;
81+
int quote_style;
82+
};
83+
84+
#define REF_FORMAT_INIT { NULL, 0 }
85+
7586
/* Macros for checking --merged and --no-merged options */
7687
#define _OPT_MERGED_NO_MERGED(option, filter, h) \
7788
{ OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
@@ -93,14 +104,15 @@ void ref_array_clear(struct ref_array *array);
93104
/* Parse format string and sort specifiers */
94105
int parse_ref_filter_atom(const char *atom, const char *ep);
95106
/* Used to verify if the given format is correct and to parse out the used atoms */
96-
int verify_ref_format(const char *format);
107+
int verify_ref_format(struct ref_format *format);
97108
/* Sort the given ref_array as per the ref_sorting provided */
98109
void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
99110
/* Based on the given format and quote_style, fill the strbuf */
100-
void format_ref_array_item(struct ref_array_item *info, const char *format,
101-
int quote_style, struct strbuf *final_buf);
111+
void format_ref_array_item(struct ref_array_item *info,
112+
const struct ref_format *format,
113+
struct strbuf *final_buf);
102114
/* Print the ref using the given format and quote_style */
103-
void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style);
115+
void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
104116
/* Callback function for parsing the sort option */
105117
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
106118
/* Default sort option based on refname */
@@ -117,6 +129,6 @@ void setup_ref_filter_porcelain_msg(void);
117129
* name must be a fully qualified refname.
118130
*/
119131
void pretty_print_ref(const char *name, const unsigned char *sha1,
120-
const char *format);
132+
const struct ref_format *format);
121133

122134
#endif /* REF_FILTER_H */

0 commit comments

Comments
 (0)