am, rebase: teach quiet option
[git/git.git] / git-am.sh
CommitLineData
d1c5f2a4
JH
1#!/bin/sh
2#
d64e6b04 3# Copyright (c) 2005, 2006 Junio C Hamano
d1c5f2a4 4
c1491841 5SUBDIRECTORY_OK=Yes
78443d90
PH
6OPTIONS_KEEPDASHDASH=
7OPTIONS_SPEC="\
588c038a 8git am [options] [<mbox>|<Maildir>...]
09f8d055 9git am [options] (--resolved | --skip | --abort)
78443d90 10--
66006c63 11i,interactive run interactively
98ef23b3 12b,binary* (historical option -- no-op)
78443d90 133,3way allow fall back on 3way merging if needed
0e987a12 14q,quiet be quiet
78443d90
PH
15s,signoff add a Signed-off-by line to the commit message
16u,utf8 recode into utf8 (default)
fe1fa946 17k,keep pass -k flag to git-mailinfo
78443d90 18whitespace= pass it through git-apply
b47dfe9e 19directory= pass it through git-apply
78443d90
PH
20C= pass it through git-apply
21p= pass it through git-apply
b80da424 22reject pass it through git-apply
78443d90
PH
23resolvemsg= override error message when patch failure occurs
24r,resolved to be used after a patch failure
3041c324 25skip skip the current patch
3e5057a8 26abort restore the original branch and abort the patching operation.
3f01ad66 27committer-date-is-author-date lie about committer date
a79ec62d 28ignore-date use current timestamp for author date
98ef23b3 29rebasing* (internal use for git-rebase)"
78443d90 30
cf1fe88c 31. git-sh-setup
bb034f83 32prefix=$(git rev-parse --show-prefix)
f9474132 33set_reflog_action am
7eff28a9 34require_work_tree
c1491841 35cd_to_toplevel
d1c5f2a4 36
460abee5
SB
37git var GIT_COMMITTER_IDENT >/dev/null ||
38 die "You need to set your committer info first"
d64e6b04 39
f79d4c8a
NS
40if git rev-parse --verify -q HEAD >/dev/null
41then
42 HAS_HEAD=yes
43else
44 HAS_HEAD=
45fi
46
b47dfe9e 47sq () {
47c9739e 48 git rev-parse --sq-quote "$@"
b47dfe9e
JH
49}
50
d1c5f2a4
JH
51stop_here () {
52 echo "$1" >"$dotest/next"
53 exit 1
54}
55
ced9456a 56stop_here_user_resolve () {
cc120056 57 if [ -n "$resolvemsg" ]; then
a23bfaed 58 printf '%s\n' "$resolvemsg"
cc120056
SE
59 stop_here $1
60 fi
75e1645f 61 cmdline="git am"
ced9456a
RS
62 if test '' != "$interactive"
63 then
64 cmdline="$cmdline -i"
65 fi
66 if test '' != "$threeway"
67 then
68 cmdline="$cmdline -3"
69 fi
ced9456a
RS
70 echo "When you have resolved this problem run \"$cmdline --resolved\"."
71 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
3e5057a8 72 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
ced9456a
RS
73
74 stop_here $1
75}
76
d1c5f2a4
JH
77go_next () {
78 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
79 "$dotest/patch" "$dotest/info"
80 echo "$next" >"$dotest/next"
81 this=$next
82}
83
fd7bcfb5
JH
84cannot_fallback () {
85 echo "$1"
86 echo "Cannot fall back to three-way merge."
87 exit 1
88}
89
d1c5f2a4
JH
90fall_back_3way () {
91 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
92
93 rm -fr "$dotest"/patch-merge-*
94 mkdir "$dotest/patch-merge-tmp-dir"
95
96 # First see if the patch records the index info that we can use.
7a988699
JS
97 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
98 "$dotest/patch" &&
fd7bcfb5 99 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
5be60078 100 git write-tree >"$dotest/patch-merge-base+" ||
b1440cc8 101 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
fd7bcfb5 102
0e987a12 103 say Using index info to reconstruct a base tree...
fd7bcfb5 104 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
cb3a160d 105 git apply --cached <"$dotest/patch"
d1c5f2a4 106 then
d1c5f2a4
JH
107 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
108 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
fd7bcfb5
JH
109 else
110 cannot_fallback "Did you hand edit your patch?
111It does not apply to blobs recorded in its index."
d1c5f2a4
JH
112 fi
113
114 test -f "$dotest/patch-merge-index" &&
5be60078 115 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
d1c5f2a4
JH
116 orig_tree=$(cat "$dotest/patch-merge-base") &&
117 rm -fr "$dotest"/patch-merge-* || exit 1
118
0e987a12 119 say Falling back to patching base and 3-way merge...
d1c5f2a4
JH
120
121 # This is not so wrong. Depending on which base we picked,
122 # orig_tree may be wildly different from ours, but his_tree
123 # has the same set of wildly different changes in parts the
579c9bb1 124 # patch did not touch, so recursive ends up canceling them,
d1c5f2a4
JH
125 # saying that we reverted all those changes.
126
2e6e3e82 127 eval GITHEAD_$his_tree='"$FIRSTLINE"'
579c9bb1 128 export GITHEAD_$his_tree
0e987a12
SB
129 if test -n "$GIT_QUIET"
130 then
131 export GIT_MERGE_VERBOSITY=0
132 fi
579c9bb1 133 git-merge-recursive $orig_tree -- HEAD $his_tree || {
b4372ef1 134 git rerere
d1c5f2a4
JH
135 echo Failed to merge in the changes.
136 exit 1
137 }
579c9bb1 138 unset GITHEAD_$his_tree
d1c5f2a4
JH
139}
140
141prec=4
51ef1daa 142dotest="$GIT_DIR/rebase-apply"
a2003379 143sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
3f0a8f3c 144resolvemsg= resume=
67dad687 145git_apply_opt=
3f01ad66 146committer_date_is_author_date=
a79ec62d 147ignore_date=
d1c5f2a4 148
822f7c73 149while test $# != 0
d1c5f2a4
JH
150do
151 case "$1" in
78443d90
PH
152 -i|--interactive)
153 interactive=t ;;
154 -b|--binary)
cb3a160d 155 : ;;
78443d90
PH
156 -3|--3way)
157 threeway=t ;;
dfdd7e66 158 -s|--signoff)
78443d90
PH
159 sign=t ;;
160 -u|--utf8)
161 utf8=t ;; # this is now default
162 --no-utf8)
163 utf8= ;;
164 -k|--keep)
165 keep=t ;;
166 -r|--resolved)
167 resolved=t ;;
168 --skip)
169 skip=t ;;
3e5057a8
NS
170 --abort)
171 abort=t ;;
3041c324 172 --rebasing)
cb3a160d 173 rebasing=t threeway=t keep=t ;;
78443d90 174 -d|--dotest)
e72c7406
JH
175 die "-d option is no longer supported. Do not use."
176 ;;
78443d90
PH
177 --resolvemsg)
178 shift; resolvemsg=$1 ;;
b47dfe9e
JH
179 --whitespace|--directory)
180 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
78443d90 181 -C|-p)
b47dfe9e 182 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
b80da424 183 --reject)
184 git_apply_opt="$git_apply_opt $1" ;;
3f01ad66
JH
185 --committer-date-is-author-date)
186 committer_date_is_author_date=t ;;
a79ec62d
NS
187 --ignore-date)
188 ignore_date=t ;;
0e987a12
SB
189 -q|--quiet)
190 GIT_QUIET=t ;;
d1c5f2a4 191 --)
78443d90 192 shift; break ;;
d1c5f2a4 193 *)
78443d90 194 usage ;;
d1c5f2a4 195 esac
78443d90 196 shift
d1c5f2a4
JH
197done
198
0c15cc92
JH
199# If the dotest directory exists, but we have finished applying all the
200# patches in them, clear it out.
d1c5f2a4
JH
201if test -d "$dotest" &&
202 last=$(cat "$dotest/last") &&
203 next=$(cat "$dotest/next") &&
204 test $# != 0 &&
205 test "$next" -gt "$last"
206then
207 rm -fr "$dotest"
208fi
209
210if test -d "$dotest"
211then
3e5057a8 212 case "$#,$skip$resolved$abort" in
c95b1389
JH
213 0,*t*)
214 # Explicit resume command and we do not have file, so
215 # we are happy.
216 : ;;
217 0,)
218 # No file input but without resume parameters; catch
219 # user error to feed us a patch from standard input
e72c7406 220 # when there is already $dotest. This is somewhat
c95b1389
JH
221 # unreliable -- stdin could be /dev/null for example
222 # and the caller did not intend to feed us a patch but
223 # wanted to continue unattended.
98ef23b3 224 test -t 0
c95b1389
JH
225 ;;
226 *)
227 false
228 ;;
229 esac ||
28ed6e7b 230 die "previous rebase directory $dotest still exists but mbox given."
271440e3 231 resume=yes
3e5057a8 232
95f8ebbf 233 case "$skip,$abort" in
2d56a136
JH
234 t,t)
235 die "Please make up your mind. --skip or --abort?"
236 ;;
95f8ebbf
OM
237 t,)
238 git rerere clear
239 git read-tree --reset -u HEAD HEAD
240 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
241 git reset HEAD
242 git update-ref ORIG_HEAD $orig_head
243 ;;
244 ,t)
2d56a136
JH
245 if test -f "$dotest/rebasing"
246 then
247 exec git rebase --abort
248 fi
3e5057a8 249 git rerere clear
c767184d
MG
250 test -f "$dotest/dirtyindex" || {
251 git read-tree --reset -u HEAD ORIG_HEAD
252 git reset ORIG_HEAD
253 }
3e5057a8
NS
254 rm -fr "$dotest"
255 exit ;;
256 esac
c767184d 257 rm -f "$dotest/dirtyindex"
d1c5f2a4 258else
3e5057a8
NS
259 # Make sure we are not given --skip, --resolved, nor --abort
260 test "$skip$resolved$abort" = "" ||
cc120056 261 die "Resolve operation not in progress, we are not resuming."
d1c5f2a4
JH
262
263 # Start afresh.
264 mkdir -p "$dotest" || exit
265
bb034f83
JH
266 if test -n "$prefix" && test $# != 0
267 then
268 first=t
269 for arg
270 do
271 test -n "$first" && {
272 set x
273 first=
274 }
275 case "$arg" in
276 /*)
277 set "$@" "$arg" ;;
278 *)
279 set "$@" "$prefix$arg" ;;
280 esac
281 done
282 shift
283 fi
5be60078 284 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
d1c5f2a4
JH
285 rm -fr "$dotest"
286 exit 1
287 }
288
0e987a12 289 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
a2003379 290 # for the resuming session after a patch failure.
22db240d 291 # -i can and must be given when resuming.
9b9f5a22 292 echo " $git_apply_opt" >"$dotest/apply-opt"
22db240d 293 echo "$threeway" >"$dotest/threeway"
d1c5f2a4
JH
294 echo "$sign" >"$dotest/sign"
295 echo "$utf8" >"$dotest/utf8"
296 echo "$keep" >"$dotest/keep"
0e987a12 297 echo "$GIT_QUIET" >"$dotest/quiet"
d1c5f2a4 298 echo 1 >"$dotest/next"
3041c324
JH
299 if test -n "$rebasing"
300 then
301 : >"$dotest/rebasing"
302 else
303 : >"$dotest/applying"
f79d4c8a
NS
304 if test -n "$HAS_HEAD"
305 then
306 git update-ref ORIG_HEAD HEAD
307 else
308 git update-ref -d ORIG_HEAD >/dev/null 2>&1
309 fi
3041c324 310 fi
d1c5f2a4
JH
311fi
312
0c15cc92
JH
313case "$resolved" in
314'')
f79d4c8a
NS
315 case "$HAS_HEAD" in
316 '')
317 files=$(git ls-files) ;;
318 ?*)
319 files=$(git diff-index --cached --name-only HEAD --) ;;
320 esac || exit
c767184d
MG
321 if test "$files"
322 then
f79d4c8a 323 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
c767184d
MG
324 die "Dirty index: cannot apply patches (dirty: $files)"
325 fi
0c15cc92
JH
326esac
327
d1c5f2a4
JH
328if test "$(cat "$dotest/utf8")" = t
329then
330 utf8=-u
bb1091a4
JH
331else
332 utf8=-n
d1c5f2a4
JH
333fi
334if test "$(cat "$dotest/keep")" = t
335then
336 keep=-k
337fi
0e987a12
SB
338if test "$(cat "$dotest/quiet")" = t
339then
340 GIT_QUIET=t
341fi
22db240d
JH
342if test "$(cat "$dotest/threeway")" = t
343then
344 threeway=t
345fi
9b9f5a22 346git_apply_opt=$(cat "$dotest/apply-opt")
d1c5f2a4
JH
347if test "$(cat "$dotest/sign")" = t
348then
5354a56f 349 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
d1c5f2a4
JH
350 s/>.*/>/
351 s/^/Signed-off-by: /'
352 `
353else
354 SIGNOFF=
355fi
d1c5f2a4
JH
356
357last=`cat "$dotest/last"`
358this=`cat "$dotest/next"`
359if test "$skip" = t
360then
361 this=`expr "$this" + 1`
69224716 362 resume=
d1c5f2a4
JH
363fi
364
365if test "$this" -gt "$last"
366then
0e987a12 367 say Nothing to do.
d1c5f2a4
JH
368 rm -fr "$dotest"
369 exit
370fi
371
372while test "$this" -le "$last"
373do
374 msgnum=`printf "%0${prec}d" $this`
375 next=`expr "$this" + 1`
376 test -f "$dotest/$msgnum" || {
69224716 377 resume=
d1c5f2a4
JH
378 go_next
379 continue
380 }
0c15cc92
JH
381
382 # If we are not resuming, parse and extract the patch information
383 # into separate files:
384 # - info records the authorship and title
385 # - msg is the rest of commit log message
386 # - patch is the patch body.
387 #
388 # When we are resuming, these files are either already prepared
389 # by the user, or the user can tell us to do so by --resolved flag.
271440e3
JH
390 case "$resume" in
391 '')
5be60078 392 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
271440e3
JH
393 <"$dotest/$msgnum" >"$dotest/info" ||
394 stop_here $this
ca193cf1
JS
395
396 # skip pine's internal folder data
397 grep '^Author: Mail System Internal Data$' \
398 <"$dotest"/info >/dev/null &&
399 go_next && continue
400
28ed6e7b 401 test -s "$dotest/patch" || {
6777c380 402 echo "Patch is empty. Was it split wrong?"
87ab7992
DZ
403 stop_here $this
404 }
5e835cac
JH
405 if test -f "$dotest/rebasing" &&
406 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
407 -e q "$dotest/$msgnum") &&
408 test "$(git cat-file -t "$commit")" = commit
409 then
410 git cat-file commit "$commit" |
411 sed -e '1,/^$/d' >"$dotest/msg-clean"
412 else
413 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
414 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
415
41a3e3aa 416 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
5e835cac
JH
417 git stripspace > "$dotest/msg-clean"
418 fi
271440e3
JH
419 ;;
420 esac
d1c5f2a4
JH
421
422 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
423 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
424 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
e0e3ba20 425
3b78959e 426 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba20 427 then
3b78959e 428 echo "Patch does not have a valid e-mail address."
e0e3ba20
JH
429 stop_here $this
430 fi
431
2c674191 432 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a4 433
4bfb6b62
JH
434 case "$resume" in
435 '')
436 if test '' != "$SIGNOFF"
437 then
d1c5f2a4 438 LAST_SIGNED_OFF_BY=`
4bfb6b62
JH
439 sed -ne '/^Signed-off-by: /p' \
440 "$dotest/msg-clean" |
b4ce54fc 441 sed -ne '$p'
d1c5f2a4 442 `
4bfb6b62
JH
443 ADD_SIGNOFF=`
444 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a4
JH
445 test '' = "$LAST_SIGNED_OFF_BY" && echo
446 echo "$SIGNOFF"
4bfb6b62
JH
447 }`
448 else
d1c5f2a4 449 ADD_SIGNOFF=
4bfb6b62
JH
450 fi
451 {
d1c5f2a4
JH
452 if test -s "$dotest/msg-clean"
453 then
d1c5f2a4
JH
454 cat "$dotest/msg-clean"
455 fi
456 if test '' != "$ADD_SIGNOFF"
457 then
458 echo "$ADD_SIGNOFF"
459 fi
4bfb6b62
JH
460 } >"$dotest/final-commit"
461 ;;
0c15cc92 462 *)
6d28644d 463 case "$resolved$interactive" in
0c15cc92
JH
464 tt)
465 # This is used only for interactive view option.
38762c47 466 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc92
JH
467 ;;
468 esac
4bfb6b62 469 esac
d1c5f2a4 470
4bfb6b62 471 resume=
d1c5f2a4
JH
472 if test "$interactive" = t
473 then
a1451104
JH
474 test -t 0 ||
475 die "cannot be interactive without stdin connected to a terminal."
d1c5f2a4
JH
476 action=again
477 while test "$action" = again
478 do
479 echo "Commit Body is:"
480 echo "--------------------------"
481 cat "$dotest/final-commit"
482 echo "--------------------------"
9754563c 483 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a4
JH
484 read reply
485 case "$reply" in
f89ad67f
JH
486 [yY]*) action=yes ;;
487 [aA]*) action=yes interactive= ;;
488 [nN]*) action=skip ;;
ef0c2abf 489 [eE]*) git_editor "$dotest/final-commit"
d1c5f2a4 490 action=again ;;
f89ad67f
JH
491 [vV]*) action=again
492 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
493 *) action=again ;;
d1c5f2a4
JH
494 esac
495 done
496 else
497 action=yes
498 fi
1d9b2656 499 FIRSTLINE=$(sed 1q "$dotest/final-commit")
d1c5f2a4
JH
500
501 if test $action = skip
502 then
503 go_next
504 continue
505 fi
506
507 if test -x "$GIT_DIR"/hooks/applypatch-msg
508 then
509 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
510 stop_here $this
511 fi
512
0e987a12 513 say "Applying: $FIRSTLINE"
d1c5f2a4 514
0c15cc92
JH
515 case "$resolved" in
516 '')
3ddd1703
SB
517 # When we are allowed to fall back to 3-way later, don't give
518 # false errors during the initial attempt.
519 squelch=
520 if test "$threeway" = t
521 then
522 squelch='>/dev/null 2>&1 '
523 fi
524 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
0c15cc92
JH
525 apply_status=$?
526 ;;
527 t)
528 # Resolved means the user did all the hard work, and
529 # we do not have to do any patch application. Just
530 # trust what the user has in the index file and the
531 # working tree.
532 resolved=
38762c47 533 git diff-index --quiet --cached HEAD -- && {
64646d11 534 echo "No changes - did you forget to use 'git add'?"
ced9456a 535 stop_here_user_resolve $this
06aff47b 536 }
5be60078 537 unmerged=$(git ls-files -u)
c1d1128b
JH
538 if test -n "$unmerged"
539 then
540 echo "You still have unmerged paths in your index"
64646d11 541 echo "did you forget to use 'git add'?"
ced9456a 542 stop_here_user_resolve $this
c1d1128b 543 fi
0c15cc92 544 apply_status=0
b4372ef1 545 git rerere
0c15cc92
JH
546 ;;
547 esac
548
d1c5f2a4
JH
549 if test $apply_status = 1 && test "$threeway" = t
550 then
73319032 551 if (fall_back_3way)
d1c5f2a4 552 then
73319032
JH
553 # Applying the patch to an earlier tree and merging the
554 # result may have produced the same tree as ours.
38762c47 555 git diff-index --quiet --cached HEAD -- && {
0e987a12 556 say No changes -- Patch already applied.
06aff47b
AR
557 go_next
558 continue
559 }
73319032
JH
560 # clear apply_status -- we have successfully merged.
561 apply_status=0
d1c5f2a4
JH
562 fi
563 fi
564 if test $apply_status != 0
565 then
4d2e283a 566 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
ced9456a 567 stop_here_user_resolve $this
d1c5f2a4
JH
568 fi
569
570 if test -x "$GIT_DIR"/hooks/pre-applypatch
571 then
572 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
573 fi
574
5be60078 575 tree=$(git write-tree) &&
3f01ad66 576 commit=$(
a79ec62d
NS
577 if test -n "$ignore_date"
578 then
579 GIT_AUTHOR_DATE=
580 fi
f79d4c8a 581 parent=$(git rev-parse --verify -q HEAD) ||
0e987a12 582 say >&2 "applying to an empty history"
f79d4c8a 583
3f01ad66
JH
584 if test -n "$committer_date_is_author_date"
585 then
586 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
587 export GIT_COMMITTER_DATE
588 fi &&
ea10b60c 589 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
3f01ad66 590 ) &&
2e6e3e82 591 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
d1c5f2a4
JH
592 stop_here $this
593
594 if test -x "$GIT_DIR"/hooks/post-applypatch
595 then
596 "$GIT_DIR"/hooks/post-applypatch
597 fi
598
599 go_next
600done
601
e0cd252e
MS
602git gc --auto
603
d1c5f2a4 604rm -fr "$dotest"