format-patch: threading test reactivation
[git/dkf.git] / git-am.sh
blob8bcb206022ae5d4ae82d2a37d8f79454c043ad78
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 SUBDIRECTORY_OK=Yes
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git am [options] [<mbox>|<Maildir>...]
9 git am [options] (--resolved | --skip | --abort)
11 i,interactive run interactively
12 b,binary* (historical option -- no-op)
13 3,3way allow fall back on 3way merging if needed
14 s,signoff add a Signed-off-by line to the commit message
15 u,utf8 recode into utf8 (default)
16 k,keep pass -k flag to git-mailinfo
17 whitespace= pass it through git-apply
18 directory= pass it through git-apply
19 C= pass it through git-apply
20 p= pass it through git-apply
21 reject pass it through git-apply
22 resolvemsg= override error message when patch failure occurs
23 r,resolved to be used after a patch failure
24 skip skip the current patch
25 abort restore the original branch and abort the patching operation.
26 committer-date-is-author-date lie about committer date
27 ignore-date use current timestamp for author date
28 rebasing* (internal use for git-rebase)"
30 . git-sh-setup
31 prefix=$(git rev-parse --show-prefix)
32 set_reflog_action am
33 require_work_tree
34 cd_to_toplevel
36 git var GIT_COMMITTER_IDENT >/dev/null ||
37 die "You need to set your committer info first"
39 sq () {
40 for sqarg
42 printf "%s" "$sqarg" |
43 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
44 done
47 stop_here () {
48 echo "$1" >"$dotest/next"
49 exit 1
52 stop_here_user_resolve () {
53 if [ -n "$resolvemsg" ]; then
54 printf '%s\n' "$resolvemsg"
55 stop_here $1
57 cmdline="git am"
58 if test '' != "$interactive"
59 then
60 cmdline="$cmdline -i"
62 if test '' != "$threeway"
63 then
64 cmdline="$cmdline -3"
66 echo "When you have resolved this problem run \"$cmdline --resolved\"."
67 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
68 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
70 stop_here $1
73 go_next () {
74 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
75 "$dotest/patch" "$dotest/info"
76 echo "$next" >"$dotest/next"
77 this=$next
80 cannot_fallback () {
81 echo "$1"
82 echo "Cannot fall back to three-way merge."
83 exit 1
86 fall_back_3way () {
87 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
89 rm -fr "$dotest"/patch-merge-*
90 mkdir "$dotest/patch-merge-tmp-dir"
92 # First see if the patch records the index info that we can use.
93 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
94 "$dotest/patch" &&
95 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
96 git write-tree >"$dotest/patch-merge-base+" ||
97 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
99 echo Using index info to reconstruct a base tree...
100 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
101 git apply --cached <"$dotest/patch"
102 then
103 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
104 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
105 else
106 cannot_fallback "Did you hand edit your patch?
107 It does not apply to blobs recorded in its index."
110 test -f "$dotest/patch-merge-index" &&
111 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
112 orig_tree=$(cat "$dotest/patch-merge-base") &&
113 rm -fr "$dotest"/patch-merge-* || exit 1
115 echo Falling back to patching base and 3-way merge...
117 # This is not so wrong. Depending on which base we picked,
118 # orig_tree may be wildly different from ours, but his_tree
119 # has the same set of wildly different changes in parts the
120 # patch did not touch, so recursive ends up canceling them,
121 # saying that we reverted all those changes.
123 eval GITHEAD_$his_tree='"$FIRSTLINE"'
124 export GITHEAD_$his_tree
125 git-merge-recursive $orig_tree -- HEAD $his_tree || {
126 git rerere
127 echo Failed to merge in the changes.
128 exit 1
130 unset GITHEAD_$his_tree
133 prec=4
134 dotest="$GIT_DIR/rebase-apply"
135 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
136 resolvemsg= resume=
137 git_apply_opt=
138 committer_date_is_author_date=
139 ignore_date=
141 while test $# != 0
143 case "$1" in
144 -i|--interactive)
145 interactive=t ;;
146 -b|--binary)
147 : ;;
148 -3|--3way)
149 threeway=t ;;
150 -s|--signoff)
151 sign=t ;;
152 -u|--utf8)
153 utf8=t ;; # this is now default
154 --no-utf8)
155 utf8= ;;
156 -k|--keep)
157 keep=t ;;
158 -r|--resolved)
159 resolved=t ;;
160 --skip)
161 skip=t ;;
162 --abort)
163 abort=t ;;
164 --rebasing)
165 rebasing=t threeway=t keep=t ;;
166 -d|--dotest)
167 die "-d option is no longer supported. Do not use."
169 --resolvemsg)
170 shift; resolvemsg=$1 ;;
171 --whitespace|--directory)
172 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
173 -C|-p)
174 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
175 --reject)
176 git_apply_opt="$git_apply_opt $1" ;;
177 --committer-date-is-author-date)
178 committer_date_is_author_date=t ;;
179 --ignore-date)
180 ignore_date=t ;;
182 shift; break ;;
184 usage ;;
185 esac
186 shift
187 done
189 # If the dotest directory exists, but we have finished applying all the
190 # patches in them, clear it out.
191 if test -d "$dotest" &&
192 last=$(cat "$dotest/last") &&
193 next=$(cat "$dotest/next") &&
194 test $# != 0 &&
195 test "$next" -gt "$last"
196 then
197 rm -fr "$dotest"
200 if test -d "$dotest"
201 then
202 case "$#,$skip$resolved$abort" in
203 0,*t*)
204 # Explicit resume command and we do not have file, so
205 # we are happy.
206 : ;;
208 # No file input but without resume parameters; catch
209 # user error to feed us a patch from standard input
210 # when there is already $dotest. This is somewhat
211 # unreliable -- stdin could be /dev/null for example
212 # and the caller did not intend to feed us a patch but
213 # wanted to continue unattended.
214 test -t 0
217 false
219 esac ||
220 die "previous rebase directory $dotest still exists but mbox given."
221 resume=yes
223 case "$skip,$abort" in
225 git rerere clear
226 git read-tree --reset -u HEAD HEAD
227 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
228 git reset HEAD
229 git update-ref ORIG_HEAD $orig_head
232 git rerere clear
233 git read-tree --reset -u HEAD ORIG_HEAD
234 git reset ORIG_HEAD
235 rm -fr "$dotest"
236 exit ;;
237 esac
238 else
239 # Make sure we are not given --skip, --resolved, nor --abort
240 test "$skip$resolved$abort" = "" ||
241 die "Resolve operation not in progress, we are not resuming."
243 # Start afresh.
244 mkdir -p "$dotest" || exit
246 if test -n "$prefix" && test $# != 0
247 then
248 first=t
249 for arg
251 test -n "$first" && {
252 set x
253 first=
255 case "$arg" in
257 set "$@" "$arg" ;;
259 set "$@" "$prefix$arg" ;;
260 esac
261 done
262 shift
264 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
265 rm -fr "$dotest"
266 exit 1
269 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
270 # for the resuming session after a patch failure.
271 # -i can and must be given when resuming.
272 echo " $git_apply_opt" >"$dotest/apply-opt"
273 echo "$threeway" >"$dotest/threeway"
274 echo "$sign" >"$dotest/sign"
275 echo "$utf8" >"$dotest/utf8"
276 echo "$keep" >"$dotest/keep"
277 echo 1 >"$dotest/next"
278 if test -n "$rebasing"
279 then
280 : >"$dotest/rebasing"
281 else
282 : >"$dotest/applying"
283 git update-ref ORIG_HEAD HEAD
287 case "$resolved" in
289 files=$(git diff-index --cached --name-only HEAD --) || exit
290 test "$files" && die "Dirty index: cannot apply patches (dirty: $files)"
291 esac
293 if test "$(cat "$dotest/utf8")" = t
294 then
295 utf8=-u
296 else
297 utf8=-n
299 if test "$(cat "$dotest/keep")" = t
300 then
301 keep=-k
303 if test "$(cat "$dotest/threeway")" = t
304 then
305 threeway=t
307 git_apply_opt=$(cat "$dotest/apply-opt")
308 if test "$(cat "$dotest/sign")" = t
309 then
310 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
311 s/>.*/>/
312 s/^/Signed-off-by: /'
314 else
315 SIGNOFF=
318 last=`cat "$dotest/last"`
319 this=`cat "$dotest/next"`
320 if test "$skip" = t
321 then
322 this=`expr "$this" + 1`
323 resume=
326 if test "$this" -gt "$last"
327 then
328 echo Nothing to do.
329 rm -fr "$dotest"
330 exit
333 while test "$this" -le "$last"
335 msgnum=`printf "%0${prec}d" $this`
336 next=`expr "$this" + 1`
337 test -f "$dotest/$msgnum" || {
338 resume=
339 go_next
340 continue
343 # If we are not resuming, parse and extract the patch information
344 # into separate files:
345 # - info records the authorship and title
346 # - msg is the rest of commit log message
347 # - patch is the patch body.
349 # When we are resuming, these files are either already prepared
350 # by the user, or the user can tell us to do so by --resolved flag.
351 case "$resume" in
353 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
354 <"$dotest/$msgnum" >"$dotest/info" ||
355 stop_here $this
357 # skip pine's internal folder data
358 grep '^Author: Mail System Internal Data$' \
359 <"$dotest"/info >/dev/null &&
360 go_next && continue
362 test -s "$dotest/patch" || {
363 echo "Patch is empty. Was it split wrong?"
364 stop_here $this
366 if test -f "$dotest/rebasing" &&
367 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
368 -e q "$dotest/$msgnum") &&
369 test "$(git cat-file -t "$commit")" = commit
370 then
371 git cat-file commit "$commit" |
372 sed -e '1,/^$/d' >"$dotest/msg-clean"
373 else
374 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
375 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
377 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
378 git stripspace > "$dotest/msg-clean"
381 esac
383 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
384 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
385 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
387 if test -z "$GIT_AUTHOR_EMAIL"
388 then
389 echo "Patch does not have a valid e-mail address."
390 stop_here $this
393 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
395 case "$resume" in
397 if test '' != "$SIGNOFF"
398 then
399 LAST_SIGNED_OFF_BY=`
400 sed -ne '/^Signed-off-by: /p' \
401 "$dotest/msg-clean" |
402 sed -ne '$p'
404 ADD_SIGNOFF=`
405 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
406 test '' = "$LAST_SIGNED_OFF_BY" && echo
407 echo "$SIGNOFF"
409 else
410 ADD_SIGNOFF=
413 if test -s "$dotest/msg-clean"
414 then
415 cat "$dotest/msg-clean"
417 if test '' != "$ADD_SIGNOFF"
418 then
419 echo "$ADD_SIGNOFF"
421 } >"$dotest/final-commit"
424 case "$resolved$interactive" in
426 # This is used only for interactive view option.
427 git diff-index -p --cached HEAD -- >"$dotest/patch"
429 esac
430 esac
432 resume=
433 if test "$interactive" = t
434 then
435 test -t 0 ||
436 die "cannot be interactive without stdin connected to a terminal."
437 action=again
438 while test "$action" = again
440 echo "Commit Body is:"
441 echo "--------------------------"
442 cat "$dotest/final-commit"
443 echo "--------------------------"
444 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
445 read reply
446 case "$reply" in
447 [yY]*) action=yes ;;
448 [aA]*) action=yes interactive= ;;
449 [nN]*) action=skip ;;
450 [eE]*) git_editor "$dotest/final-commit"
451 action=again ;;
452 [vV]*) action=again
453 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
454 *) action=again ;;
455 esac
456 done
457 else
458 action=yes
460 FIRSTLINE=$(sed 1q "$dotest/final-commit")
462 if test $action = skip
463 then
464 go_next
465 continue
468 if test -x "$GIT_DIR"/hooks/applypatch-msg
469 then
470 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
471 stop_here $this
474 printf 'Applying: %s\n' "$FIRSTLINE"
476 case "$resolved" in
478 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
479 apply_status=$?
482 # Resolved means the user did all the hard work, and
483 # we do not have to do any patch application. Just
484 # trust what the user has in the index file and the
485 # working tree.
486 resolved=
487 git diff-index --quiet --cached HEAD -- && {
488 echo "No changes - did you forget to use 'git add'?"
489 stop_here_user_resolve $this
491 unmerged=$(git ls-files -u)
492 if test -n "$unmerged"
493 then
494 echo "You still have unmerged paths in your index"
495 echo "did you forget to use 'git add'?"
496 stop_here_user_resolve $this
498 apply_status=0
499 git rerere
501 esac
503 if test $apply_status = 1 && test "$threeway" = t
504 then
505 if (fall_back_3way)
506 then
507 # Applying the patch to an earlier tree and merging the
508 # result may have produced the same tree as ours.
509 git diff-index --quiet --cached HEAD -- && {
510 echo No changes -- Patch already applied.
511 go_next
512 continue
514 # clear apply_status -- we have successfully merged.
515 apply_status=0
518 if test $apply_status != 0
519 then
520 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
521 stop_here_user_resolve $this
524 if test -x "$GIT_DIR"/hooks/pre-applypatch
525 then
526 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
529 tree=$(git write-tree) &&
530 parent=$(git rev-parse --verify HEAD) &&
531 commit=$(
532 if test -n "$ignore_date"
533 then
534 GIT_AUTHOR_DATE=
536 if test -n "$committer_date_is_author_date"
537 then
538 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
539 export GIT_COMMITTER_DATE
540 fi &&
541 git commit-tree $tree -p $parent <"$dotest/final-commit"
542 ) &&
543 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
544 stop_here $this
546 if test -x "$GIT_DIR"/hooks/post-applypatch
547 then
548 "$GIT_DIR"/hooks/post-applypatch
551 go_next
552 done
554 git gc --auto
556 rm -fr "$dotest"