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