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