ref-filter: get rid of show_ref_array_item
Inlining the exported function `show_ref_array_item()`, which is not providing the right level of abstraction, simplifies the API and can unlock improvements at the former call sites. Helped-by: René Scharfe <l.s.r@web.de> Signed-off-by: ZheNing Hu <adlternative@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
ZheNing Hu committed
Apr 19, 2021 at 11:28 UTC
22f69a85edf29dba2278b55f14419e4ea48148d2
4 files changed
+34
-21
builtin/for-each-ref.c
+12
-2
@@ -80,8 +80,18 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
80
81
if (!maxcount || array.nr < maxcount)
82
maxcount = array.nr;
83
- for (i = 0; i < maxcount; i++)
84
- show_ref_array_item(array.items[i], &format);
83
+ for (i = 0; i < maxcount; i++) {
84
+ struct strbuf output = STRBUF_INIT;
85
+ struct strbuf err = STRBUF_INIT;
86
+
87
+ if (format_ref_array_item(array.items[i], &format, &output, &err))
88
+ die("%s", err.buf);
89
+ fwrite(output.buf, 1, output.len, stdout);
90
+ putchar('\n');
91
+
92
+ strbuf_release(&err);
93
+ strbuf_release(&output);
94
+ }
95
ref_array_clear(&array);
96
return 0;
97
}
builtin/tag.c
+12
-2
@@ -63,8 +63,18 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
63
filter_refs(&array, filter, FILTER_REFS_TAGS);
64
ref_array_sort(sorting, &array);
65
66
- for (i = 0; i < array.nr; i++)
67
- show_ref_array_item(array.items[i], format);
66
+ for (i = 0; i < array.nr; i++) {
67
+ struct strbuf output = STRBUF_INIT;
68
+ struct strbuf err = STRBUF_INIT;
69
+
70
+ if (format_ref_array_item(array.items[i], format, &output, &err))
71
+ die("%s", err.buf);
72
+ fwrite(output.buf, 1, output.len, stdout);
73
+ putchar('\n');
74
+
75
+ strbuf_release(&err);
76
+ strbuf_release(&output);
77
+ }
78
ref_array_clear(&array);
79
free(to_free);
80
ref-filter.c
+10
-15
@@ -2435,27 +2435,22 @@ int format_ref_array_item(struct ref_array_item *info,
2435
return 0;
2436
}
2437
2438
-void show_ref_array_item(struct ref_array_item *info,
2439
- const struct ref_format *format)
2440
-{
2441
- struct strbuf final_buf = STRBUF_INIT;
2442
- struct strbuf error_buf = STRBUF_INIT;
2443
-
2444
- if (format_ref_array_item(info, format, &final_buf, &error_buf))
2445
- die("%s", error_buf.buf);
2446
- fwrite(final_buf.buf, 1, final_buf.len, stdout);
2447
- strbuf_release(&error_buf);
2448
- strbuf_release(&final_buf);
2449
- putchar('\n');
2450
-}
2451
-
2438
void pretty_print_ref(const char *name, const struct object_id *oid,
2439
const struct ref_format *format)
2440
{
2441
struct ref_array_item *ref_item;
2442
+ struct strbuf output = STRBUF_INIT;
2443
+ struct strbuf err = STRBUF_INIT;
2444
+
2445
ref_item = new_ref_array_item(name, oid);
2446
ref_item->kind = ref_kind_from_refname(name);
2458
- show_ref_array_item(ref_item, format);
2447
+ if (format_ref_array_item(ref_item, format, &output, &err))
2448
+ die("%s", err.buf);
2449
+ fwrite(output.buf, 1, output.len, stdout);
2450
+ putchar('\n');
2451
+
2452
+ strbuf_release(&err);
2453
+ strbuf_release(&output);
2454
free_array_item(ref_item);
2455
}
2456
ref-filter.h
-2
@@ -119,8 +119,6 @@ int format_ref_array_item(struct ref_array_item *info,
119
const struct ref_format *format,
120
struct strbuf *final_buf,
121
struct strbuf *error_buf);
122
-/* Print the ref using the given format and quote_style */
123
-void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
122
/* Parse a single sort specifier and add it to the list */
123
void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *atom);
124
/* Callback function for parsing the sort option */