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