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 <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Aug 4, 2015 at 21:51 UTC 5d28cf78196acc454b4430b1efe3b5f9e55ae2e6
1 file changed +28 -3
builtin/am.c
+28 -3
@@ -80,6 +80,9 @@ struct am_state {
80
81 /* number of digits in patch filename */
82 int prec;
83 +
84 + /* various operating modes and command line options */
85 + int quiet;
86 };
87
88 /**
@@ -116,6 +119,22 @@ static inline const char *am_path(const struct am_state *state, const char *path
119 return mkpath("%s/%s", state->dir, path);
120 }
121
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 +
138 /**
139 * Returns 1 if there is an am session in progress, 0 otherwise.
140 */
@@ -330,6 +349,9 @@ static void am_load(struct am_state *state)
349
350 read_commit_msg(state);
351
352 + read_state_file(&sb, state, "quiet", 1);
353 + state->quiet = !strcmp(sb.buf, "t");
354 +
355 strbuf_release(&sb);
356 }
357
@@ -508,6 +530,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
530 die(_("Failed to split patches."));
531 }
532
533 + write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
534 +
535 if (!get_sha1("HEAD", curr_head)) {
536 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
537 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)
780 commit_list_insert(lookup_commit(parent), &parents);
781 } else {
782 ptr = NULL;
759 - fprintf_ln(stderr, _("applying to an empty history"));
783 + say(state, stderr, _("applying to an empty history"));
784 }
785
786 author = fmt_ident(state->author_name, state->author_email,
@@ -833,7 +857,7 @@ static void am_run(struct am_state *state, int resume)
857 write_commit_msg(state);
858 }
859
836 - printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
860 + say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
861
862 if (run_apply(state) < 0) {
863 int advice_amworkdir = 1;
@@ -869,7 +893,7 @@ static void am_resolve(struct am_state *state)
893 {
894 validate_resume_state(state);
895
872 - printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
896 + say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
897
898 if (!index_has_changes(NULL)) {
899 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)
1129 };
1130
1131 struct option options[] = {
1132 + OPT__QUIET(&state.quiet, N_("be quiet")),
1133 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1134 N_("format the patch(es) are in"),
1135 parse_opt_patchformat),