git-commit: color status output when color.ui is set
[git/jnareb-git.git] / git-am.sh
blob4b157fe5d536fdbdbf85e0e06c419b5927a90867
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 d,dotest= (removed -- do not use)
12 i,interactive run interactively
13 b,binary (historical option -- no-op)
14 3,3way allow fall back on 3way merging if needed
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 C= pass it through git-apply
20 p= pass it through git-apply
21 resolvemsg= override error message when patch failure occurs
22 r,resolved to be used after a patch failure
23 skip skip the current patch
24 abort restore the original branch and abort the patching operation.
25 rebasing (internal use for git-rebase)"
27 . git-sh-setup
28 prefix=$(git rev-parse --show-prefix)
29 set_reflog_action am
30 require_work_tree
31 cd_to_toplevel
33 git var GIT_COMMITTER_IDENT >/dev/null ||
34 die "You need to set your committer info first"
36 stop_here () {
37 echo "$1" >"$dotest/next"
38 exit 1
41 stop_here_user_resolve () {
42 if [ -n "$resolvemsg" ]; then
43 printf '%s\n' "$resolvemsg"
44 stop_here $1
46 cmdline="git am"
47 if test '' != "$interactive"
48 then
49 cmdline="$cmdline -i"
51 if test '' != "$threeway"
52 then
53 cmdline="$cmdline -3"
55 echo "When you have resolved this problem run \"$cmdline --resolved\"."
56 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
57 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
59 stop_here $1
62 go_next () {
63 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
64 "$dotest/patch" "$dotest/info"
65 echo "$next" >"$dotest/next"
66 this=$next
69 cannot_fallback () {
70 echo "$1"
71 echo "Cannot fall back to three-way merge."
72 exit 1
75 fall_back_3way () {
76 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
78 rm -fr "$dotest"/patch-merge-*
79 mkdir "$dotest/patch-merge-tmp-dir"
81 # First see if the patch records the index info that we can use.
82 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
83 "$dotest/patch" &&
84 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
85 git write-tree >"$dotest/patch-merge-base+" ||
86 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
88 echo Using index info to reconstruct a base tree...
89 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
90 git apply --cached <"$dotest/patch"
91 then
92 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
93 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
94 else
95 cannot_fallback "Did you hand edit your patch?
96 It does not apply to blobs recorded in its index."
99 test -f "$dotest/patch-merge-index" &&
100 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
101 orig_tree=$(cat "$dotest/patch-merge-base") &&
102 rm -fr "$dotest"/patch-merge-* || exit 1
104 echo Falling back to patching base and 3-way merge...
106 # This is not so wrong. Depending on which base we picked,
107 # orig_tree may be wildly different from ours, but his_tree
108 # has the same set of wildly different changes in parts the
109 # patch did not touch, so recursive ends up canceling them,
110 # saying that we reverted all those changes.
112 eval GITHEAD_$his_tree='"$FIRSTLINE"'
113 export GITHEAD_$his_tree
114 git-merge-recursive $orig_tree -- HEAD $his_tree || {
115 git rerere
116 echo Failed to merge in the changes.
117 exit 1
119 unset GITHEAD_$his_tree
122 prec=4
123 dotest="$GIT_DIR/rebase-apply"
124 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
125 resolvemsg= resume=
126 git_apply_opt=
128 while test $# != 0
130 case "$1" in
131 -i|--interactive)
132 interactive=t ;;
133 -b|--binary)
134 : ;;
135 -3|--3way)
136 threeway=t ;;
137 -s|--signoff)
138 sign=t ;;
139 -u|--utf8)
140 utf8=t ;; # this is now default
141 --no-utf8)
142 utf8= ;;
143 -k|--keep)
144 keep=t ;;
145 -r|--resolved)
146 resolved=t ;;
147 --skip)
148 skip=t ;;
149 --abort)
150 abort=t ;;
151 --rebasing)
152 rebasing=t threeway=t keep=t ;;
153 -d|--dotest)
154 die "-d option is no longer supported. Do not use."
156 --resolvemsg)
157 shift; resolvemsg=$1 ;;
158 --whitespace)
159 git_apply_opt="$git_apply_opt $1=$2"; shift ;;
160 -C|-p)
161 git_apply_opt="$git_apply_opt $1$2"; shift ;;
163 shift; break ;;
165 usage ;;
166 esac
167 shift
168 done
170 # If the dotest directory exists, but we have finished applying all the
171 # patches in them, clear it out.
172 if test -d "$dotest" &&
173 last=$(cat "$dotest/last") &&
174 next=$(cat "$dotest/next") &&
175 test $# != 0 &&
176 test "$next" -gt "$last"
177 then
178 rm -fr "$dotest"
181 if test -d "$dotest"
182 then
183 case "$#,$skip$resolved$abort" in
184 0,*t*)
185 # Explicit resume command and we do not have file, so
186 # we are happy.
187 : ;;
189 # No file input but without resume parameters; catch
190 # user error to feed us a patch from standard input
191 # when there is already $dotest. This is somewhat
192 # unreliable -- stdin could be /dev/null for example
193 # and the caller did not intend to feed us a patch but
194 # wanted to continue unattended.
195 tty -s
198 false
200 esac ||
201 die "previous rebase directory $dotest still exists but mbox given."
202 resume=yes
204 case "$skip,$abort" in
206 git rerere clear
207 git read-tree --reset -u HEAD HEAD
208 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
209 git reset HEAD
210 git update-ref ORIG_HEAD $orig_head
213 git rerere clear
214 git read-tree --reset -u HEAD ORIG_HEAD
215 git reset ORIG_HEAD
216 rm -fr "$dotest"
217 exit ;;
218 esac
219 else
220 # Make sure we are not given --skip, --resolved, nor --abort
221 test "$skip$resolved$abort" = "" ||
222 die "Resolve operation not in progress, we are not resuming."
224 # Start afresh.
225 mkdir -p "$dotest" || exit
227 if test -n "$prefix" && test $# != 0
228 then
229 first=t
230 for arg
232 test -n "$first" && {
233 set x
234 first=
236 case "$arg" in
238 set "$@" "$arg" ;;
240 set "$@" "$prefix$arg" ;;
241 esac
242 done
243 shift
245 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
246 rm -fr "$dotest"
247 exit 1
250 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
251 # for the resuming session after a patch failure.
252 # -i can and must be given when resuming.
253 echo " $git_apply_opt" >"$dotest/apply-opt"
254 echo "$threeway" >"$dotest/threeway"
255 echo "$sign" >"$dotest/sign"
256 echo "$utf8" >"$dotest/utf8"
257 echo "$keep" >"$dotest/keep"
258 echo 1 >"$dotest/next"
259 if test -n "$rebasing"
260 then
261 : >"$dotest/rebasing"
262 else
263 : >"$dotest/applying"
264 git update-ref ORIG_HEAD HEAD
268 case "$resolved" in
270 files=$(git diff-index --cached --name-only HEAD --) || exit
271 if [ "$files" ]; then
272 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
273 exit 1
275 esac
277 if test "$(cat "$dotest/utf8")" = t
278 then
279 utf8=-u
280 else
281 utf8=-n
283 if test "$(cat "$dotest/keep")" = t
284 then
285 keep=-k
287 if test "$(cat "$dotest/threeway")" = t
288 then
289 threeway=t
291 git_apply_opt=$(cat "$dotest/apply-opt")
292 if test "$(cat "$dotest/sign")" = t
293 then
294 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
295 s/>.*/>/
296 s/^/Signed-off-by: /'
298 else
299 SIGNOFF=
302 last=`cat "$dotest/last"`
303 this=`cat "$dotest/next"`
304 if test "$skip" = t
305 then
306 this=`expr "$this" + 1`
307 resume=
310 if test "$this" -gt "$last"
311 then
312 echo Nothing to do.
313 rm -fr "$dotest"
314 exit
317 while test "$this" -le "$last"
319 msgnum=`printf "%0${prec}d" $this`
320 next=`expr "$this" + 1`
321 test -f "$dotest/$msgnum" || {
322 resume=
323 go_next
324 continue
327 # If we are not resuming, parse and extract the patch information
328 # into separate files:
329 # - info records the authorship and title
330 # - msg is the rest of commit log message
331 # - patch is the patch body.
333 # When we are resuming, these files are either already prepared
334 # by the user, or the user can tell us to do so by --resolved flag.
335 case "$resume" in
337 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
338 <"$dotest/$msgnum" >"$dotest/info" ||
339 stop_here $this
341 # skip pine's internal folder data
342 grep '^Author: Mail System Internal Data$' \
343 <"$dotest"/info >/dev/null &&
344 go_next && continue
346 test -s "$dotest/patch" || {
347 echo "Patch is empty. Was it split wrong?"
348 stop_here $this
350 if test -f "$dotest/rebasing" &&
351 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
352 -e q "$dotest/$msgnum") &&
353 test "$(git cat-file -t "$commit")" = commit
354 then
355 git cat-file commit "$commit" |
356 sed -e '1,/^$/d' >"$dotest/msg-clean"
357 else
358 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
359 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
361 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
362 git stripspace > "$dotest/msg-clean"
365 esac
367 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
368 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
369 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
371 if test -z "$GIT_AUTHOR_EMAIL"
372 then
373 echo "Patch does not have a valid e-mail address."
374 stop_here $this
377 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
379 case "$resume" in
381 if test '' != "$SIGNOFF"
382 then
383 LAST_SIGNED_OFF_BY=`
384 sed -ne '/^Signed-off-by: /p' \
385 "$dotest/msg-clean" |
386 sed -ne '$p'
388 ADD_SIGNOFF=`
389 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
390 test '' = "$LAST_SIGNED_OFF_BY" && echo
391 echo "$SIGNOFF"
393 else
394 ADD_SIGNOFF=
397 if test -s "$dotest/msg-clean"
398 then
399 cat "$dotest/msg-clean"
401 if test '' != "$ADD_SIGNOFF"
402 then
403 echo "$ADD_SIGNOFF"
405 } >"$dotest/final-commit"
408 case "$resolved$interactive" in
410 # This is used only for interactive view option.
411 git diff-index -p --cached HEAD -- >"$dotest/patch"
413 esac
414 esac
416 resume=
417 if test "$interactive" = t
418 then
419 test -t 0 ||
420 die "cannot be interactive without stdin connected to a terminal."
421 action=again
422 while test "$action" = again
424 echo "Commit Body is:"
425 echo "--------------------------"
426 cat "$dotest/final-commit"
427 echo "--------------------------"
428 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
429 read reply
430 case "$reply" in
431 [yY]*) action=yes ;;
432 [aA]*) action=yes interactive= ;;
433 [nN]*) action=skip ;;
434 [eE]*) git_editor "$dotest/final-commit"
435 action=again ;;
436 [vV]*) action=again
437 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
438 *) action=again ;;
439 esac
440 done
441 else
442 action=yes
444 FIRSTLINE=$(sed 1q "$dotest/final-commit")
446 if test $action = skip
447 then
448 go_next
449 continue
452 if test -x "$GIT_DIR"/hooks/applypatch-msg
453 then
454 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
455 stop_here $this
458 printf 'Applying: %s\n' "$FIRSTLINE"
460 case "$resolved" in
462 git apply $git_apply_opt --index "$dotest/patch"
463 apply_status=$?
466 # Resolved means the user did all the hard work, and
467 # we do not have to do any patch application. Just
468 # trust what the user has in the index file and the
469 # working tree.
470 resolved=
471 git diff-index --quiet --cached HEAD -- && {
472 echo "No changes - did you forget to use 'git add'?"
473 stop_here_user_resolve $this
475 unmerged=$(git ls-files -u)
476 if test -n "$unmerged"
477 then
478 echo "You still have unmerged paths in your index"
479 echo "did you forget to use 'git add'?"
480 stop_here_user_resolve $this
482 apply_status=0
483 git rerere
485 esac
487 if test $apply_status = 1 && test "$threeway" = t
488 then
489 if (fall_back_3way)
490 then
491 # Applying the patch to an earlier tree and merging the
492 # result may have produced the same tree as ours.
493 git diff-index --quiet --cached HEAD -- && {
494 echo No changes -- Patch already applied.
495 go_next
496 continue
498 # clear apply_status -- we have successfully merged.
499 apply_status=0
502 if test $apply_status != 0
503 then
504 echo Patch failed at $msgnum.
505 stop_here_user_resolve $this
508 if test -x "$GIT_DIR"/hooks/pre-applypatch
509 then
510 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
513 tree=$(git write-tree) &&
514 parent=$(git rev-parse --verify HEAD) &&
515 commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
516 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
517 stop_here $this
519 if test -x "$GIT_DIR"/hooks/post-applypatch
520 then
521 "$GIT_DIR"/hooks/post-applypatch
524 go_next
525 done
527 git gc --auto
529 rm -fr "$dotest"