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