Skip to content

Commit fd7d79d

Browse files
lucasoshirogitster
authored andcommitted
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>
1 parent 9a2fb14 commit fd7d79d

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

builtin/repo.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,29 @@ static get_value_fn *get_value_fn_for_key(const char *key)
8585
return found ? found->get_value : NULL;
8686
}
8787

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+
88105
static int print_fields(int argc, const char **argv,
89106
struct repository *repo,
90107
enum output_format format)
91108
{
92109
int ret = 0;
93110
struct strbuf valbuf = STRBUF_INIT;
94-
struct strbuf quotbuf = STRBUF_INIT;
95111

96112
for (int i = 0; i < argc; i++) {
97113
get_value_fn *get_value;
@@ -105,25 +121,11 @@ static int print_fields(int argc, const char **argv,
105121
}
106122

107123
strbuf_reset(&valbuf);
108-
strbuf_reset(&quotbuf);
109-
110124
get_value(repo, &valbuf);
111-
112-
switch (format) {
113-
case FORMAT_KEYVALUE:
114-
quote_c_style(valbuf.buf, &quotbuf, 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);
123126
}
124127

125128
strbuf_release(&valbuf);
126-
strbuf_release(&quotbuf);
127129
return ret;
128130
}
129131

0 commit comments

Comments
 (0)