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