]> git.ipfire.org Git - thirdparty/git.git/blame - git-am.sh
am: read from the right mailbox when started from a subdirectory
[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="\
8git-am [options] <mbox>|<Maildir>...
9git-am [options] --resolved
10git-am [options] --skip
11--
12d,dotest= use <dir> and not .dotest
66006c63 13i,interactive run interactively
78443d90
PH
14b,binary pass --allo-binary-replacement to git-apply
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
24skip skip the current patch"
25
cf1fe88c 26. git-sh-setup
bb034f83 27prefix=$(git rev-parse --show-prefix)
f9474132 28set_reflog_action am
7eff28a9 29require_work_tree
c1491841 30cd_to_toplevel
d1c5f2a4 31
d64e6b04
JH
32git var GIT_COMMITTER_IDENT >/dev/null || exit
33
d1c5f2a4
JH
34stop_here () {
35 echo "$1" >"$dotest/next"
36 exit 1
37}
38
ced9456a 39stop_here_user_resolve () {
cc120056 40 if [ -n "$resolvemsg" ]; then
a23bfaed 41 printf '%s\n' "$resolvemsg"
cc120056
SE
42 stop_here $1
43 fi
ced9456a
RS
44 cmdline=$(basename $0)
45 if test '' != "$interactive"
46 then
47 cmdline="$cmdline -i"
48 fi
49 if test '' != "$threeway"
50 then
51 cmdline="$cmdline -3"
52 fi
53 if test '.dotest' != "$dotest"
54 then
55 cmdline="$cmdline -d=$dotest"
56 fi
57 echo "When you have resolved this problem run \"$cmdline --resolved\"."
58 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
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
579c9bb1
SP
113 eval GITHEAD_$his_tree='"$SUBJECT"'
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
f23272f3
JH
123reread_subject () {
124 git stripspace <"$1" | sed -e 1q
125}
126
d1c5f2a4 127prec=4
bb034f83
JH
128dotest="${prefix}.dotest"
129sign= utf8=t keep= skip= interactive= resolved= binary=
3f0a8f3c 130resolvemsg= resume=
67dad687 131git_apply_opt=
d1c5f2a4 132
822f7c73 133while test $# != 0
d1c5f2a4
JH
134do
135 case "$1" in
78443d90
PH
136 -i|--interactive)
137 interactive=t ;;
138 -b|--binary)
139 binary=t ;;
140 -3|--3way)
141 threeway=t ;;
dfdd7e66 142 -s|--signoff)
78443d90
PH
143 sign=t ;;
144 -u|--utf8)
145 utf8=t ;; # this is now default
146 --no-utf8)
147 utf8= ;;
148 -k|--keep)
149 keep=t ;;
150 -r|--resolved)
151 resolved=t ;;
152 --skip)
153 skip=t ;;
154 -d|--dotest)
bb034f83
JH
155 shift
156 case "$1" in /*) dotest=$1;; *) dotest="$prefix$1" ;; esac ;;
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
c95b1389
JH
184 case "$#,$skip$resolved" in
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
192 # when there is already .dotest. This is somewhat
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 ||
202 die "previous dotest directory $dotest still exists but mbox given."
271440e3 203 resume=yes
d1c5f2a4 204else
0c15cc92
JH
205 # Make sure we are not given --skip nor --resolved
206 test ",$skip,$resolved," = ,,, ||
cc120056 207 die "Resolve operation not in progress, we are not resuming."
d1c5f2a4
JH
208
209 # Start afresh.
210 mkdir -p "$dotest" || exit
211
bb034f83
JH
212 if test -n "$prefix" && test $# != 0
213 then
214 first=t
215 for arg
216 do
217 test -n "$first" && {
218 set x
219 first=
220 }
221 case "$arg" in
222 /*)
223 set "$@" "$arg" ;;
224 *)
225 set "$@" "$prefix$arg" ;;
226 esac
227 done
228 shift
229 fi
5be60078 230 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
d1c5f2a4
JH
231 rm -fr "$dotest"
232 exit 1
233 }
234
8c31cb82
JH
235 # -b, -s, -u, -k and --whitespace flags are kept for the
236 # resuming session after a patch failure.
0c15cc92 237 # -3 and -i can and must be given when resuming.
087b6742 238 echo "$binary" >"$dotest/binary"
8c31cb82 239 echo " $ws" >"$dotest/whitespace"
d1c5f2a4
JH
240 echo "$sign" >"$dotest/sign"
241 echo "$utf8" >"$dotest/utf8"
242 echo "$keep" >"$dotest/keep"
d1c5f2a4
JH
243 echo 1 >"$dotest/next"
244fi
245
0c15cc92
JH
246case "$resolved" in
247'')
38762c47 248 files=$(git diff-index --cached --name-only HEAD --) || exit
0c15cc92
JH
249 if [ "$files" ]; then
250 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
251 exit 1
252 fi
253esac
254
087b6742
JH
255if test "$(cat "$dotest/binary")" = t
256then
257 binary=--allow-binary-replacement
258fi
d1c5f2a4
JH
259if test "$(cat "$dotest/utf8")" = t
260then
261 utf8=-u
bb1091a4
JH
262else
263 utf8=-n
d1c5f2a4
JH
264fi
265if test "$(cat "$dotest/keep")" = t
266then
267 keep=-k
268fi
8c31cb82 269ws=`cat "$dotest/whitespace"`
d1c5f2a4
JH
270if test "$(cat "$dotest/sign")" = t
271then
272 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
273 s/>.*/>/
274 s/^/Signed-off-by: /'
275 `
276else
277 SIGNOFF=
278fi
d1c5f2a4
JH
279
280last=`cat "$dotest/last"`
281this=`cat "$dotest/next"`
282if test "$skip" = t
283then
b4372ef1 284 git rerere clear
d1c5f2a4 285 this=`expr "$this" + 1`
69224716 286 resume=
d1c5f2a4
JH
287fi
288
289if test "$this" -gt "$last"
290then
291 echo Nothing to do.
292 rm -fr "$dotest"
293 exit
294fi
295
296while test "$this" -le "$last"
297do
298 msgnum=`printf "%0${prec}d" $this`
299 next=`expr "$this" + 1`
300 test -f "$dotest/$msgnum" || {
69224716 301 resume=
d1c5f2a4
JH
302 go_next
303 continue
304 }
0c15cc92
JH
305
306 # If we are not resuming, parse and extract the patch information
307 # into separate files:
308 # - info records the authorship and title
309 # - msg is the rest of commit log message
310 # - patch is the patch body.
311 #
312 # When we are resuming, these files are either already prepared
313 # by the user, or the user can tell us to do so by --resolved flag.
271440e3
JH
314 case "$resume" in
315 '')
5be60078 316 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
271440e3
JH
317 <"$dotest/$msgnum" >"$dotest/info" ||
318 stop_here $this
ca193cf1
JS
319
320 # skip pine's internal folder data
321 grep '^Author: Mail System Internal Data$' \
322 <"$dotest"/info >/dev/null &&
323 go_next && continue
324
87ab7992 325 test -s $dotest/patch || {
6777c380 326 echo "Patch is empty. Was it split wrong?"
87ab7992
DZ
327 stop_here $this
328 }
5be60078 329 git stripspace < "$dotest/msg" > "$dotest/msg-clean"
271440e3
JH
330 ;;
331 esac
d1c5f2a4
JH
332
333 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
334 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
335 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
e0e3ba20 336
3b78959e 337 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba20 338 then
3b78959e 339 echo "Patch does not have a valid e-mail address."
e0e3ba20
JH
340 stop_here $this
341 fi
342
2c674191 343 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a4 344
4bfb6b62 345 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
d1c5f2a4 346 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
4bfb6b62
JH
347
348 case "$resume" in
349 '')
350 if test '' != "$SIGNOFF"
351 then
d1c5f2a4 352 LAST_SIGNED_OFF_BY=`
4bfb6b62
JH
353 sed -ne '/^Signed-off-by: /p' \
354 "$dotest/msg-clean" |
355 tail -n 1
d1c5f2a4 356 `
4bfb6b62
JH
357 ADD_SIGNOFF=`
358 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a4
JH
359 test '' = "$LAST_SIGNED_OFF_BY" && echo
360 echo "$SIGNOFF"
4bfb6b62
JH
361 }`
362 else
d1c5f2a4 363 ADD_SIGNOFF=
4bfb6b62
JH
364 fi
365 {
4b7cc26a 366 printf '%s\n' "$SUBJECT"
d1c5f2a4
JH
367 if test -s "$dotest/msg-clean"
368 then
369 echo
370 cat "$dotest/msg-clean"
371 fi
372 if test '' != "$ADD_SIGNOFF"
373 then
374 echo "$ADD_SIGNOFF"
375 fi
4bfb6b62
JH
376 } >"$dotest/final-commit"
377 ;;
0c15cc92 378 *)
6d28644d 379 case "$resolved$interactive" in
0c15cc92
JH
380 tt)
381 # This is used only for interactive view option.
38762c47 382 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc92
JH
383 ;;
384 esac
4bfb6b62 385 esac
d1c5f2a4 386
4bfb6b62 387 resume=
d1c5f2a4
JH
388 if test "$interactive" = t
389 then
a1451104
JH
390 test -t 0 ||
391 die "cannot be interactive without stdin connected to a terminal."
d1c5f2a4
JH
392 action=again
393 while test "$action" = again
394 do
395 echo "Commit Body is:"
396 echo "--------------------------"
397 cat "$dotest/final-commit"
398 echo "--------------------------"
9754563c 399 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a4
JH
400 read reply
401 case "$reply" in
f89ad67f
JH
402 [yY]*) action=yes ;;
403 [aA]*) action=yes interactive= ;;
404 [nN]*) action=skip ;;
ef0c2abf 405 [eE]*) git_editor "$dotest/final-commit"
f23272f3 406 SUBJECT=$(reread_subject "$dotest/final-commit")
d1c5f2a4 407 action=again ;;
f89ad67f
JH
408 [vV]*) action=again
409 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
410 *) action=again ;;
d1c5f2a4
JH
411 esac
412 done
413 else
414 action=yes
415 fi
416
417 if test $action = skip
418 then
419 go_next
420 continue
421 fi
422
423 if test -x "$GIT_DIR"/hooks/applypatch-msg
424 then
425 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
426 stop_here $this
427 fi
428
4b7cc26a 429 printf 'Applying %s\n' "$SUBJECT"
d1c5f2a4 430
0c15cc92
JH
431 case "$resolved" in
432 '')
5be60078 433 git apply $git_apply_opt $binary --index "$dotest/patch"
0c15cc92
JH
434 apply_status=$?
435 ;;
436 t)
437 # Resolved means the user did all the hard work, and
438 # we do not have to do any patch application. Just
439 # trust what the user has in the index file and the
440 # working tree.
441 resolved=
38762c47 442 git diff-index --quiet --cached HEAD -- && {
64646d11 443 echo "No changes - did you forget to use 'git add'?"
ced9456a 444 stop_here_user_resolve $this
06aff47b 445 }
5be60078 446 unmerged=$(git ls-files -u)
c1d1128b
JH
447 if test -n "$unmerged"
448 then
449 echo "You still have unmerged paths in your index"
64646d11 450 echo "did you forget to use 'git add'?"
ced9456a 451 stop_here_user_resolve $this
c1d1128b 452 fi
0c15cc92 453 apply_status=0
b4372ef1 454 git rerere
0c15cc92
JH
455 ;;
456 esac
457
d1c5f2a4
JH
458 if test $apply_status = 1 && test "$threeway" = t
459 then
73319032 460 if (fall_back_3way)
d1c5f2a4 461 then
73319032
JH
462 # Applying the patch to an earlier tree and merging the
463 # result may have produced the same tree as ours.
38762c47 464 git diff-index --quiet --cached HEAD -- && {
06aff47b
AR
465 echo No changes -- Patch already applied.
466 go_next
467 continue
468 }
73319032
JH
469 # clear apply_status -- we have successfully merged.
470 apply_status=0
d1c5f2a4
JH
471 fi
472 fi
473 if test $apply_status != 0
474 then
475 echo Patch failed at $msgnum.
ced9456a 476 stop_here_user_resolve $this
d1c5f2a4
JH
477 fi
478
479 if test -x "$GIT_DIR"/hooks/pre-applypatch
480 then
481 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
482 fi
483
5be60078 484 tree=$(git write-tree) &&
5be60078
JH
485 parent=$(git rev-parse --verify HEAD) &&
486 commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
5be60078 487 git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
d1c5f2a4
JH
488 stop_here $this
489
490 if test -x "$GIT_DIR"/hooks/post-applypatch
491 then
492 "$GIT_DIR"/hooks/post-applypatch
493 fi
494
495 go_next
496done
497
e0cd252e
MS
498git gc --auto
499
d1c5f2a4 500rm -fr "$dotest"