completion: Add --full-diff to log options
[alt-git.git] / git-am.sh
blob58d4eb6d2dc2eaa99ae0c70a414813cd0ce43e43
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 SUBDIRECTORY_OK=Yes
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git am [options] [<mbox>|<Maildir>...]
9 git am [options] (--resolved | --skip | --abort)
11 i,interactive run interactively
12 b,binary* (historical option -- no-op)
13 3,3way allow fall back on 3way merging if needed
14 q,quiet be quiet
15 s,signoff add a Signed-off-by line to the commit message
16 u,utf8 recode into utf8 (default)
17 k,keep pass -k flag to git-mailinfo
18 whitespace= pass it through git-apply
19 directory= pass it through git-apply
20 C= pass it through git-apply
21 p= pass it through git-apply
22 reject pass it through git-apply
23 resolvemsg= override error message when patch failure occurs
24 r,resolved to be used after a patch failure
25 skip skip the current patch
26 abort restore the original branch and abort the patching operation.
27 committer-date-is-author-date lie about committer date
28 ignore-date use current timestamp for author date
29 rebasing* (internal use for git-rebase)"
31 . git-sh-setup
32 prefix=$(git rev-parse --show-prefix)
33 set_reflog_action am
34 require_work_tree
35 cd_to_toplevel
37 git var GIT_COMMITTER_IDENT >/dev/null ||
38 die "You need to set your committer info first"
40 if git rev-parse --verify -q HEAD >/dev/null
41 then
42 HAS_HEAD=yes
43 else
44 HAS_HEAD=
47 sq () {
48 git rev-parse --sq-quote "$@"
51 stop_here () {
52 echo "$1" >"$dotest/next"
53 exit 1
56 stop_here_user_resolve () {
57 if [ -n "$resolvemsg" ]; then
58 printf '%s\n' "$resolvemsg"
59 stop_here $1
61 cmdline="git am"
62 if test '' != "$interactive"
63 then
64 cmdline="$cmdline -i"
66 if test '' != "$threeway"
67 then
68 cmdline="$cmdline -3"
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\"."
72 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
74 stop_here $1
77 go_next () {
78 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
79 "$dotest/patch" "$dotest/info"
80 echo "$next" >"$dotest/next"
81 this=$next
84 cannot_fallback () {
85 echo "$1"
86 echo "Cannot fall back to three-way merge."
87 exit 1
90 fall_back_3way () {
91 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
93 rm -fr "$dotest"/patch-merge-*
94 mkdir "$dotest/patch-merge-tmp-dir"
96 # First see if the patch records the index info that we can use.
97 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
98 "$dotest/patch" &&
99 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
100 git write-tree >"$dotest/patch-merge-base+" ||
101 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
103 say Using index info to reconstruct a base tree...
104 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
105 git apply --cached <"$dotest/patch"
106 then
107 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
108 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
109 else
110 cannot_fallback "Did you hand edit your patch?
111 It does not apply to blobs recorded in its index."
114 test -f "$dotest/patch-merge-index" &&
115 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
116 orig_tree=$(cat "$dotest/patch-merge-base") &&
117 rm -fr "$dotest"/patch-merge-* || exit 1
119 say Falling back to patching base and 3-way merge...
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
124 # patch did not touch, so recursive ends up canceling them,
125 # saying that we reverted all those changes.
127 eval GITHEAD_$his_tree='"$FIRSTLINE"'
128 export GITHEAD_$his_tree
129 if test -n "$GIT_QUIET"
130 then
131 export GIT_MERGE_VERBOSITY=0
133 git-merge-recursive $orig_tree -- HEAD $his_tree || {
134 git rerere
135 echo Failed to merge in the changes.
136 exit 1
138 unset GITHEAD_$his_tree
141 prec=4
142 dotest="$GIT_DIR/rebase-apply"
143 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
144 resolvemsg= resume=
145 git_apply_opt=
146 committer_date_is_author_date=
147 ignore_date=
149 while test $# != 0
151 case "$1" in
152 -i|--interactive)
153 interactive=t ;;
154 -b|--binary)
155 : ;;
156 -3|--3way)
157 threeway=t ;;
158 -s|--signoff)
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 ;;
170 --abort)
171 abort=t ;;
172 --rebasing)
173 rebasing=t threeway=t keep=t ;;
174 -d|--dotest)
175 die "-d option is no longer supported. Do not use."
177 --resolvemsg)
178 shift; resolvemsg=$1 ;;
179 --whitespace|--directory)
180 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
181 -C|-p)
182 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
183 --reject)
184 git_apply_opt="$git_apply_opt $1" ;;
185 --committer-date-is-author-date)
186 committer_date_is_author_date=t ;;
187 --ignore-date)
188 ignore_date=t ;;
189 -q|--quiet)
190 GIT_QUIET=t ;;
192 shift; break ;;
194 usage ;;
195 esac
196 shift
197 done
199 # If the dotest directory exists, but we have finished applying all the
200 # patches in them, clear it out.
201 if test -d "$dotest" &&
202 last=$(cat "$dotest/last") &&
203 next=$(cat "$dotest/next") &&
204 test $# != 0 &&
205 test "$next" -gt "$last"
206 then
207 rm -fr "$dotest"
210 if test -d "$dotest"
211 then
212 case "$#,$skip$resolved$abort" in
213 0,*t*)
214 # Explicit resume command and we do not have file, so
215 # we are happy.
216 : ;;
218 # No file input but without resume parameters; catch
219 # user error to feed us a patch from standard input
220 # when there is already $dotest. This is somewhat
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.
224 test -t 0
227 false
229 esac ||
230 die "previous rebase directory $dotest still exists but mbox given."
231 resume=yes
233 case "$skip,$abort" in
234 t,t)
235 die "Please make up your mind. --skip or --abort?"
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
245 if test -f "$dotest/rebasing"
246 then
247 exec git rebase --abort
249 git rerere clear
250 test -f "$dotest/dirtyindex" || {
251 git read-tree --reset -u HEAD ORIG_HEAD
252 git reset ORIG_HEAD
254 rm -fr "$dotest"
255 exit ;;
256 esac
257 rm -f "$dotest/dirtyindex"
258 else
259 # Make sure we are not given --skip, --resolved, nor --abort
260 test "$skip$resolved$abort" = "" ||
261 die "Resolve operation not in progress, we are not resuming."
263 # Start afresh.
264 mkdir -p "$dotest" || exit
266 if test -n "$prefix" && test $# != 0
267 then
268 first=t
269 for arg
271 test -n "$first" && {
272 set x
273 first=
275 case "$arg" in
277 set "$@" "$arg" ;;
279 set "$@" "$prefix$arg" ;;
280 esac
281 done
282 shift
284 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
285 rm -fr "$dotest"
286 exit 1
289 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
290 # for the resuming session after a patch failure.
291 # -i can and must be given when resuming.
292 echo " $git_apply_opt" >"$dotest/apply-opt"
293 echo "$threeway" >"$dotest/threeway"
294 echo "$sign" >"$dotest/sign"
295 echo "$utf8" >"$dotest/utf8"
296 echo "$keep" >"$dotest/keep"
297 echo "$GIT_QUIET" >"$dotest/quiet"
298 echo 1 >"$dotest/next"
299 if test -n "$rebasing"
300 then
301 : >"$dotest/rebasing"
302 else
303 : >"$dotest/applying"
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
313 case "$resolved" in
315 case "$HAS_HEAD" in
317 files=$(git ls-files) ;;
319 files=$(git diff-index --cached --name-only HEAD --) ;;
320 esac || exit
321 if test "$files"
322 then
323 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
324 die "Dirty index: cannot apply patches (dirty: $files)"
326 esac
328 if test "$(cat "$dotest/utf8")" = t
329 then
330 utf8=-u
331 else
332 utf8=-n
334 if test "$(cat "$dotest/keep")" = t
335 then
336 keep=-k
338 if test "$(cat "$dotest/quiet")" = t
339 then
340 GIT_QUIET=t
342 if test "$(cat "$dotest/threeway")" = t
343 then
344 threeway=t
346 git_apply_opt=$(cat "$dotest/apply-opt")
347 if test "$(cat "$dotest/sign")" = t
348 then
349 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
350 s/>.*/>/
351 s/^/Signed-off-by: /'
353 else
354 SIGNOFF=
357 last=`cat "$dotest/last"`
358 this=`cat "$dotest/next"`
359 if test "$skip" = t
360 then
361 this=`expr "$this" + 1`
362 resume=
365 if test "$this" -gt "$last"
366 then
367 say Nothing to do.
368 rm -fr "$dotest"
369 exit
372 while test "$this" -le "$last"
374 msgnum=`printf "%0${prec}d" $this`
375 next=`expr "$this" + 1`
376 test -f "$dotest/$msgnum" || {
377 resume=
378 go_next
379 continue
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.
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.
390 case "$resume" in
392 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
393 <"$dotest/$msgnum" >"$dotest/info" ||
394 stop_here $this
396 # skip pine's internal folder data
397 grep '^Author: Mail System Internal Data$' \
398 <"$dotest"/info >/dev/null &&
399 go_next && continue
401 test -s "$dotest/patch" || {
402 echo "Patch is empty. Was it split wrong?"
403 stop_here $this
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
416 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
417 git stripspace > "$dotest/msg-clean"
420 esac
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")"
426 if test -z "$GIT_AUTHOR_EMAIL"
427 then
428 echo "Patch does not have a valid e-mail address."
429 stop_here $this
432 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
434 case "$resume" in
436 if test '' != "$SIGNOFF"
437 then
438 LAST_SIGNED_OFF_BY=`
439 sed -ne '/^Signed-off-by: /p' \
440 "$dotest/msg-clean" |
441 sed -ne '$p'
443 ADD_SIGNOFF=`
444 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
445 test '' = "$LAST_SIGNED_OFF_BY" && echo
446 echo "$SIGNOFF"
448 else
449 ADD_SIGNOFF=
452 if test -s "$dotest/msg-clean"
453 then
454 cat "$dotest/msg-clean"
456 if test '' != "$ADD_SIGNOFF"
457 then
458 echo "$ADD_SIGNOFF"
460 } >"$dotest/final-commit"
463 case "$resolved$interactive" in
465 # This is used only for interactive view option.
466 git diff-index -p --cached HEAD -- >"$dotest/patch"
468 esac
469 esac
471 resume=
472 if test "$interactive" = t
473 then
474 test -t 0 ||
475 die "cannot be interactive without stdin connected to a terminal."
476 action=again
477 while test "$action" = again
479 echo "Commit Body is:"
480 echo "--------------------------"
481 cat "$dotest/final-commit"
482 echo "--------------------------"
483 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
484 read reply
485 case "$reply" in
486 [yY]*) action=yes ;;
487 [aA]*) action=yes interactive= ;;
488 [nN]*) action=skip ;;
489 [eE]*) git_editor "$dotest/final-commit"
490 action=again ;;
491 [vV]*) action=again
492 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
493 *) action=again ;;
494 esac
495 done
496 else
497 action=yes
499 FIRSTLINE=$(sed 1q "$dotest/final-commit")
501 if test $action = skip
502 then
503 go_next
504 continue
507 if test -x "$GIT_DIR"/hooks/applypatch-msg
508 then
509 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
510 stop_here $this
513 say "Applying: $FIRSTLINE"
515 case "$resolved" in
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 '
524 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
525 apply_status=$?
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=
533 git diff-index --quiet --cached HEAD -- && {
534 echo "No changes - did you forget to use 'git add'?"
535 stop_here_user_resolve $this
537 unmerged=$(git ls-files -u)
538 if test -n "$unmerged"
539 then
540 echo "You still have unmerged paths in your index"
541 echo "did you forget to use 'git add'?"
542 stop_here_user_resolve $this
544 apply_status=0
545 git rerere
547 esac
549 if test $apply_status = 1 && test "$threeway" = t
550 then
551 if (fall_back_3way)
552 then
553 # Applying the patch to an earlier tree and merging the
554 # result may have produced the same tree as ours.
555 git diff-index --quiet --cached HEAD -- && {
556 say No changes -- Patch already applied.
557 go_next
558 continue
560 # clear apply_status -- we have successfully merged.
561 apply_status=0
564 if test $apply_status != 0
565 then
566 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
567 stop_here_user_resolve $this
570 if test -x "$GIT_DIR"/hooks/pre-applypatch
571 then
572 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
575 tree=$(git write-tree) &&
576 commit=$(
577 if test -n "$ignore_date"
578 then
579 GIT_AUTHOR_DATE=
581 parent=$(git rev-parse --verify -q HEAD) ||
582 say >&2 "applying to an empty history"
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 &&
589 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
590 ) &&
591 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
592 stop_here $this
594 if test -x "$GIT_DIR"/hooks/post-applypatch
595 then
596 "$GIT_DIR"/hooks/post-applypatch
599 go_next
600 done
602 git gc --auto
604 rm -fr "$dotest"