Skip to content

Commit 0e987a1

Browse files
bebarinogitster
authored andcommitted
am, rebase: teach quiet option
git-am and git-rebase are talkative scripts. Teach them to be quiet when told, allowing them to speak only when they fail or experience errors. The quiet option is maintained when git-am or git-rebase fails to apply a patch. This means subsequent --resolved, --continue, --skip, --abort invocations will be quiet if the original invocation was quiet. Drop a handful of >&2 redirection; the rest of the program sends all the info messages to stdout, not to stderr. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e6a30e commit 0e987a1

File tree

6 files changed

+97
-24
lines changed

6 files changed

+97
-24
lines changed

Documentation/git-am.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SYNOPSIS
1313
[--3way] [--interactive] [--committer-date-is-author-date]
1414
[--ignore-date]
1515
[--whitespace=<option>] [-C<n>] [-p<n>] [--directory=<dir>]
16-
[--reject]
16+
[--reject] [-q | --quiet]
1717
[<mbox> | <Maildir>...]
1818
'git am' (--skip | --resolved | --abort)
1919

@@ -39,6 +39,10 @@ OPTIONS
3939
--keep::
4040
Pass `-k` flag to 'git-mailinfo' (see linkgit:git-mailinfo[1]).
4141

42+
-q::
43+
--quiet::
44+
Be quiet. Only print error messages.
45+
4246
-u::
4347
--utf8::
4448
Pass `-u` flag to 'git-mailinfo' (see linkgit:git-mailinfo[1]).

Documentation/git-rebase.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ OPTIONS
236236
is used instead ('git-merge-recursive' when merging a single
237237
head, 'git-merge-octopus' otherwise). This implies --merge.
238238

239+
-q::
240+
--quiet::
241+
Be quiet. Implies --no-stat.
242+
239243
-v::
240244
--verbose::
241245
Be verbose. Implies --stat.

git-am.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ git am [options] (--resolved | --skip | --abort)
1111
i,interactive run interactively
1212
b,binary* (historical option -- no-op)
1313
3,3way allow fall back on 3way merging if needed
14+
q,quiet be quiet
1415
s,signoff add a Signed-off-by line to the commit message
1516
u,utf8 recode into utf8 (default)
1617
k,keep pass -k flag to git-mailinfo
@@ -99,7 +100,7 @@ fall_back_3way () {
99100
git write-tree >"$dotest/patch-merge-base+" ||
100101
cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
101102

102-
echo Using index info to reconstruct a base tree...
103+
say Using index info to reconstruct a base tree...
103104
if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
104105
git apply --cached <"$dotest/patch"
105106
then
@@ -115,7 +116,7 @@ It does not apply to blobs recorded in its index."
115116
orig_tree=$(cat "$dotest/patch-merge-base") &&
116117
rm -fr "$dotest"/patch-merge-* || exit 1
117118

118-
echo Falling back to patching base and 3-way merge...
119+
say Falling back to patching base and 3-way merge...
119120

120121
# This is not so wrong. Depending on which base we picked,
121122
# orig_tree may be wildly different from ours, but his_tree
@@ -125,6 +126,10 @@ It does not apply to blobs recorded in its index."
125126

126127
eval GITHEAD_$his_tree='"$FIRSTLINE"'
127128
export GITHEAD_$his_tree
129+
if test -n "$GIT_QUIET"
130+
then
131+
export GIT_MERGE_VERBOSITY=0
132+
fi
128133
git-merge-recursive $orig_tree -- HEAD $his_tree || {
129134
git rerere
130135
echo Failed to merge in the changes.
@@ -181,6 +186,8 @@ do
181186
committer_date_is_author_date=t ;;
182187
--ignore-date)
183188
ignore_date=t ;;
189+
-q|--quiet)
190+
GIT_QUIET=t ;;
184191
--)
185192
shift; break ;;
186193
*)
@@ -279,14 +286,15 @@ else
279286
exit 1
280287
}
281288

282-
# -s, -u, -k, --whitespace, -3, -C and -p flags are kept
289+
# -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
283290
# for the resuming session after a patch failure.
284291
# -i can and must be given when resuming.
285292
echo " $git_apply_opt" >"$dotest/apply-opt"
286293
echo "$threeway" >"$dotest/threeway"
287294
echo "$sign" >"$dotest/sign"
288295
echo "$utf8" >"$dotest/utf8"
289296
echo "$keep" >"$dotest/keep"
297+
echo "$GIT_QUIET" >"$dotest/quiet"
290298
echo 1 >"$dotest/next"
291299
if test -n "$rebasing"
292300
then
@@ -327,6 +335,10 @@ if test "$(cat "$dotest/keep")" = t
327335
then
328336
keep=-k
329337
fi
338+
if test "$(cat "$dotest/quiet")" = t
339+
then
340+
GIT_QUIET=t
341+
fi
330342
if test "$(cat "$dotest/threeway")" = t
331343
then
332344
threeway=t
@@ -352,7 +364,7 @@ fi
352364

353365
if test "$this" -gt "$last"
354366
then
355-
echo Nothing to do.
367+
say Nothing to do.
356368
rm -fr "$dotest"
357369
exit
358370
fi
@@ -498,7 +510,7 @@ do
498510
stop_here $this
499511
fi
500512

501-
printf 'Applying: %s\n' "$FIRSTLINE"
513+
say "Applying: $FIRSTLINE"
502514

503515
case "$resolved" in
504516
'')
@@ -541,7 +553,7 @@ do
541553
# Applying the patch to an earlier tree and merging the
542554
# result may have produced the same tree as ours.
543555
git diff-index --quiet --cached HEAD -- && {
544-
echo No changes -- Patch already applied.
556+
say No changes -- Patch already applied.
545557
go_next
546558
continue
547559
}
@@ -567,7 +579,7 @@ do
567579
GIT_AUTHOR_DATE=
568580
fi
569581
parent=$(git rev-parse --verify -q HEAD) ||
570-
echo >&2 "applying to an empty history"
582+
say >&2 "applying to an empty history"
571583
572584
if test -n "$committer_date_is_author_date"
573585
then

git-rebase.sh

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) 2005 Junio C Hamano.
44
#
55

6-
USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--onto <newbase>] [<upstream>|--root] [<branch>]'
6+
USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--onto <newbase>] [<upstream>|--root] [<branch>] [--quiet | -q]'
77
LONG_USAGE='git-rebase replaces <branch> with a new branch of the
88
same name. When the --onto option is provided the new branch starts
99
out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
@@ -72,11 +72,20 @@ continue_merge () {
7272
echo "directly, but instead do one of the following: "
7373
die "$RESOLVEMSG"
7474
fi
75-
printf "Committed: %0${prec}d " $msgnum
75+
if test -z "$GIT_QUIET"
76+
then
77+
printf "Committed: %0${prec}d " $msgnum
78+
fi
7679
else
77-
printf "Already applied: %0${prec}d " $msgnum
80+
if test -z "$GIT_QUIET"
81+
then
82+
printf "Already applied: %0${prec}d " $msgnum
83+
fi
84+
fi
85+
if test -z "$GIT_QUIET"
86+
then
87+
git rev-list --pretty=oneline -1 "$cmt" | sed -e 's/^[^ ]* //'
7888
fi
79-
git rev-list --pretty=oneline -1 "$cmt" | sed -e 's/^[^ ]* //'
8089

8190
prev_head=`git rev-parse HEAD^0`
8291
# save the resulting commit so we can read-tree on it later
@@ -97,6 +106,10 @@ call_merge () {
97106
eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
98107
eval GITHEAD_$hd='$(cat "$dotest/onto_name")'
99108
export GITHEAD_$cmt GITHEAD_$hd
109+
if test -n "$GIT_QUIET"
110+
then
111+
export GIT_MERGE_VERBOSITY=1
112+
fi
100113
git-merge-$strategy "$cmt^" -- "$hd" "$cmt"
101114
rv=$?
102115
case "$rv" in
@@ -138,7 +151,7 @@ move_to_original_branch () {
138151
finish_rb_merge () {
139152
move_to_original_branch
140153
rm -r "$dotest"
141-
echo "All done."
154+
say All done.
142155
}
143156

144157
is_interactive () {
@@ -209,6 +222,7 @@ do
209222
end=$(cat "$dotest/end")
210223
msgnum=$(cat "$dotest/msgnum")
211224
onto=$(cat "$dotest/onto")
225+
GIT_QUIET=$(cat "$dotest/quiet")
212226
continue_merge
213227
while test "$msgnum" -le "$end"
214228
do
@@ -221,6 +235,7 @@ do
221235
head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
222236
onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
223237
orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
238+
GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet)
224239
git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
225240
move_to_original_branch
226241
exit
@@ -238,6 +253,7 @@ do
238253
msgnum=$(cat "$dotest/msgnum")
239254
msgnum=$(($msgnum + 1))
240255
onto=$(cat "$dotest/onto")
256+
GIT_QUIET=$(cat "$dotest/quiet")
241257
while test "$msgnum" -le "$end"
242258
do
243259
call_merge "$msgnum"
@@ -249,6 +265,7 @@ do
249265
head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
250266
onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
251267
orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
268+
GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet)
252269
git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
253270
move_to_original_branch
254271
exit
@@ -260,9 +277,11 @@ do
260277
git rerere clear
261278
if test -d "$dotest"
262279
then
280+
GIT_QUIET=$(cat "$dotest/quiet")
263281
move_to_original_branch
264282
else
265283
dotest="$GIT_DIR"/rebase-apply
284+
GIT_QUIET=$(cat "$dotest/quiet")
266285
move_to_original_branch
267286
fi
268287
git reset --hard $(cat "$dotest/orig-head")
@@ -300,6 +319,13 @@ do
300319
-v|--verbose)
301320
verbose=t
302321
diffstat=t
322+
GIT_QUIET=
323+
;;
324+
-q|--quiet)
325+
GIT_QUIET=t
326+
git_am_opt="$git_am_opt -q"
327+
verbose=
328+
diffstat=
303329
;;
304330
--whitespace=*)
305331
git_am_opt="$git_am_opt $1"
@@ -445,15 +471,15 @@ then
445471
then
446472
# Lazily switch to the target branch if needed...
447473
test -z "$switch_to" || git checkout "$switch_to"
448-
echo >&2 "Current branch $branch_name is up to date."
474+
say "Current branch $branch_name is up to date."
449475
exit 0
450476
else
451-
echo "Current branch $branch_name is up to date, rebase forced."
477+
say "Current branch $branch_name is up to date, rebase forced."
452478
fi
453479
fi
454480

455481
# Detach HEAD and reset the tree
456-
echo "First, rewinding head to replay your work on top of it..."
482+
say "First, rewinding head to replay your work on top of it..."
457483
git checkout -q "$onto^0" || die "could not detach HEAD"
458484
git update-ref ORIG_HEAD $branch
459485

@@ -471,7 +497,7 @@ fi
471497
# we just fast forwarded.
472498
if test "$mb" = "$branch"
473499
then
474-
echo >&2 "Fast-forwarded $branch_name to $onto_name."
500+
say "Fast-forwarded $branch_name to $onto_name."
475501
move_to_original_branch
476502
exit 0
477503
fi
@@ -493,7 +519,8 @@ then
493519
test 0 != $ret -a -d "$GIT_DIR"/rebase-apply &&
494520
echo $head_name > "$GIT_DIR"/rebase-apply/head-name &&
495521
echo $onto > "$GIT_DIR"/rebase-apply/onto &&
496-
echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head
522+
echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head &&
523+
echo "$GIT_QUIET" > "$GIT_DIR"/rebase-apply/quiet
497524
exit $ret
498525
fi
499526

@@ -507,6 +534,7 @@ prev_head=$orig_head
507534
echo "$prev_head" > "$dotest/prev_head"
508535
echo "$orig_head" > "$dotest/orig-head"
509536
echo "$head_name" > "$dotest/head-name"
537+
echo "$GIT_QUIET" > "$dotest/quiet"
510538

511539
msgnum=0
512540
for cmt in `git rev-list --reverse --no-merges "$revisions"`

t/t3400-rebase.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ test_expect_success 'rebase against master' '
5454
git rebase master'
5555

5656
test_expect_success 'rebase against master twice' '
57-
git rebase master 2>err &&
58-
grep "Current branch my-topic-branch is up to date" err
57+
git rebase master >out &&
58+
grep "Current branch my-topic-branch is up to date" out
5959
'
6060

6161
test_expect_success 'rebase against master twice with --force' '
@@ -65,14 +65,14 @@ test_expect_success 'rebase against master twice with --force' '
6565

6666
test_expect_success 'rebase against master twice from another branch' '
6767
git checkout my-topic-branch^ &&
68-
git rebase master my-topic-branch 2>err &&
69-
grep "Current branch my-topic-branch is up to date" err
68+
git rebase master my-topic-branch >out &&
69+
grep "Current branch my-topic-branch is up to date" out
7070
'
7171

7272
test_expect_success 'rebase fast-forward to master' '
7373
git checkout my-topic-branch^ &&
74-
git rebase my-topic-branch 2>err &&
75-
grep "Fast-forwarded HEAD to my-topic-branch" err
74+
git rebase my-topic-branch >out &&
75+
grep "Fast-forwarded HEAD to my-topic-branch" out
7676
'
7777

7878
test_expect_success \
@@ -126,4 +126,11 @@ test_expect_success 'Show verbose error when HEAD could not be detached' '
126126
grep "Untracked working tree file .B. would be overwritten" output.err
127127
'
128128

129+
test_expect_success 'rebase -q is quiet' '
130+
rm B &&
131+
git checkout -b quiet topic &&
132+
git rebase -q master > output.out 2>&1 &&
133+
test ! -s output.out
134+
'
135+
129136
test_done

t/t4150-am.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ test_expect_success 'am -3 falls back to 3-way merge' '
180180
test -z "$(git diff lorem)"
181181
'
182182

183+
test_expect_success 'am -3 -q is quiet' '
184+
git reset master2 --hard &&
185+
sed -n -e "3,\$p" msg >file &&
186+
head -n 9 msg >>file &&
187+
git add file &&
188+
test_tick &&
189+
git commit -m "copied stuff" &&
190+
git am -3 -q lorem-move.patch > output.out 2>&1 &&
191+
! test -s output.out
192+
'
193+
183194
test_expect_success 'am pauses on conflict' '
184195
git checkout lorem2^^ &&
185196
test_must_fail git am lorem-move.patch &&
@@ -313,4 +324,11 @@ test_expect_success 'am newline in subject' '
313324
grep "^Applying: second \\\n foo$" output.out
314325
'
315326

327+
test_expect_success 'am -q is quiet' '
328+
git checkout first &&
329+
test_tick &&
330+
git am -q < patch1 > output.out 2>&1 &&
331+
! test -s output.out
332+
'
333+
316334
test_done

0 commit comments

Comments
 (0)