Skip to content

Commit 3e4f52e

Browse files
committed
advice: extract vadvise() from advise()
extract a version of advise() that uses an explict 'va_list' parameter. Call it from advise() and advise_if_enabled() for a functionally equivalent version. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Heba Waly <[email protected]>
1 parent 080d12b commit 3e4f52e

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

advice.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,20 @@ static const char *advice_config_keys[] = {
128128
[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
129129
};
130130

131-
void advise(const char *advice, ...)
131+
static const char turn_off_instructions[] =
132+
N_("\n"
133+
"Disable this message with \"git config %s false\"");
134+
135+
static void vadvise(const char *advice, va_list params,
136+
int display_instructions, char *key)
132137
{
133138
struct strbuf buf = STRBUF_INIT;
134-
va_list params;
135139
const char *cp, *np;
136140

137-
va_start(params, advice);
138141
strbuf_vaddf(&buf, advice, params);
139-
va_end(params);
142+
143+
if(display_instructions)
144+
strbuf_addf(&buf, turn_off_instructions, key);
140145

141146
for (cp = buf.buf; *cp; cp = np) {
142147
np = strchrnul(cp, '\n');
@@ -165,37 +170,25 @@ int advice_push_update_rejected_enabled(void)
165170

166171
}
167172

168-
static const char turn_off_instructions[] =
169-
N_("\n"
170-
"Disable this message with \"git config %s false\"");
173+
void advise(const char *advice, ...)
174+
{
175+
va_list params;
176+
va_start(params, advice);
177+
vadvise(advice, params, 0, "");
178+
va_end(params);
179+
}
171180

172181
void advise_if_enabled(enum advice_type type, const char *advice, ...)
173182
{
174-
struct strbuf buf = STRBUF_INIT;
175-
char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
176183
va_list params;
177-
const char *cp, *np;
178-
184+
char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
185+
179186
if(!advice_enabled(type))
180187
return;
181188

182189
va_start(params, advice);
183-
strbuf_vaddf(&buf, advice, params);
190+
vadvise(advice, params, 1, key);
184191
va_end(params);
185-
186-
strbuf_addf(&buf, turn_off_instructions, key);
187-
188-
for (cp = buf.buf; *cp; cp = np) {
189-
np = strchrnul(cp, '\n');
190-
fprintf(stderr, _("%shint: %.*s%s\n"),
191-
advise_get_color(ADVICE_COLOR_HINT),
192-
(int)(np - cp), cp,
193-
advise_get_color(ADVICE_COLOR_RESET));
194-
if (*np)
195-
np++;
196-
}
197-
strbuf_release(&buf);
198-
199192
}
200193

201194
int git_default_advice_config(const char *var, const char *value)

0 commit comments

Comments
 (0)