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