repo: factor out field printing to dedicated function
Move the field printing in git-repo-info to a new function called `print_field`, allowing it to be called by functions other than `print_fields`. Also change its use of quote_c_style() helper to output directly to the standard output stream, instead of taking a result in a strbuf and then printing it outselves. Signed-off-by: Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Lucas Seiki Oshiro committed
Nov 18, 2025 at 17:37 UTC
fd7d79d068dd14a4d7a4a93f7bfd31cf24020aec
1 file changed
+18
-16
builtin/repo.c
+18
-16
@@ -85,13 +85,29 @@ static get_value_fn *get_value_fn_for_key(const char *key)
85
return found ? found->get_value : NULL;
86
}
87
88
+static void print_field(enum output_format format, const char *key,
89
+ const char *value)
90
+{
91
+ switch (format) {
92
+ case FORMAT_KEYVALUE:
93
+ printf("%s=", key);
94
+ quote_c_style(value, NULL, stdout, 0);
95
+ putchar('\n');
96
+ break;
97
+ case FORMAT_NUL_TERMINATED:
98
+ printf("%s\n%s%c", key, value, '\0');
99
+ break;
100
+ default:
101
+ BUG("not a valid output format: %d", format);
102
+ }
103
+}
104
+
105
static int print_fields(int argc, const char **argv,
106
struct repository *repo,
107
enum output_format format)
108
{
109
int ret = 0;
110
struct strbuf valbuf = STRBUF_INIT;
94
- struct strbuf quotbuf = STRBUF_INIT;
111
112
for (int i = 0; i < argc; i++) {
113
get_value_fn *get_value;
@@ -105,25 +121,11 @@ static int print_fields(int argc, const char **argv,
121
}
122
123
strbuf_reset(&valbuf);
108
- strbuf_reset("buf);
109
-
124
get_value(repo, &valbuf);
111
-
112
- switch (format) {
113
- case FORMAT_KEYVALUE:
114
- quote_c_style(valbuf.buf, "buf, NULL, 0);
115
- printf("%s=%s\n", key, quotbuf.buf);
116
- break;
117
- case FORMAT_NUL_TERMINATED:
118
- printf("%s\n%s%c", key, valbuf.buf, '\0');
119
- break;
120
- default:
121
- BUG("not a valid output format: %d", format);
122
- }
125
+ print_field(format, key, valbuf.buf);
126
}
127
128
strbuf_release(&valbuf);
126
- strbuf_release("buf);
129
return ret;
130
}
131