Skip to content

Commit 5d28cf7

Browse files
pyokagangitster
authored andcommitted
builtin-am: implement -q/--quiet
Since 0e987a1 (am, rebase: teach quiet option, 2009-06-16), git-am supported the --quiet option, and when told to be quiet, would only speak on failure. Re-implement this by introducing the say() function, which works like fprintf_ln(), but would only write to the stream when state->quiet is false. Signed-off-by: Paul Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d18550 commit 5d28cf7

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

builtin/am.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ struct am_state {
8080

8181
/* number of digits in patch filename */
8282
int prec;
83+
84+
/* various operating modes and command line options */
85+
int quiet;
8386
};
8487

8588
/**
@@ -116,6 +119,22 @@ static inline const char *am_path(const struct am_state *state, const char *path
116119
return mkpath("%s/%s", state->dir, path);
117120
}
118121

122+
/**
123+
* If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
124+
* at the end.
125+
*/
126+
static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
127+
{
128+
va_list ap;
129+
130+
va_start(ap, fmt);
131+
if (!state->quiet) {
132+
vfprintf(fp, fmt, ap);
133+
putc('\n', fp);
134+
}
135+
va_end(ap);
136+
}
137+
119138
/**
120139
* Returns 1 if there is an am session in progress, 0 otherwise.
121140
*/
@@ -330,6 +349,9 @@ static void am_load(struct am_state *state)
330349

331350
read_commit_msg(state);
332351

352+
read_state_file(&sb, state, "quiet", 1);
353+
state->quiet = !strcmp(sb.buf, "t");
354+
333355
strbuf_release(&sb);
334356
}
335357

@@ -508,6 +530,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
508530
die(_("Failed to split patches."));
509531
}
510532

533+
write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
534+
511535
if (!get_sha1("HEAD", curr_head)) {
512536
write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
513537
update_ref("am", "ORIG_HEAD", curr_head, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
@@ -756,7 +780,7 @@ static void do_commit(const struct am_state *state)
756780
commit_list_insert(lookup_commit(parent), &parents);
757781
} else {
758782
ptr = NULL;
759-
fprintf_ln(stderr, _("applying to an empty history"));
783+
say(state, stderr, _("applying to an empty history"));
760784
}
761785

762786
author = fmt_ident(state->author_name, state->author_email,
@@ -833,7 +857,7 @@ static void am_run(struct am_state *state, int resume)
833857
write_commit_msg(state);
834858
}
835859

836-
printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
860+
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
837861

838862
if (run_apply(state) < 0) {
839863
int advice_amworkdir = 1;
@@ -869,7 +893,7 @@ static void am_resolve(struct am_state *state)
869893
{
870894
validate_resume_state(state);
871895

872-
printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
896+
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
873897

874898
if (!index_has_changes(NULL)) {
875899
printf_ln(_("No changes - did you forget to use 'git add'?\n"
@@ -1105,6 +1129,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
11051129
};
11061130

11071131
struct option options[] = {
1132+
OPT__QUIET(&state.quiet, N_("be quiet")),
11081133
OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
11091134
N_("format the patch(es) are in"),
11101135
parse_opt_patchformat),

0 commit comments

Comments
 (0)