Code

am: remove support for -d .dotest
[git.git] / git-am.sh
1 #!/bin/sh
2 #
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
10 git-am [options] --skip
11 --
12 d,dotest=       (removed -- do not use)
13 i,interactive   run interactively
14 b,binary        pass --allo-binary-replacement to git-apply
15 3,3way          allow fall back on 3way merging if needed
16 s,signoff       add a Signed-off-by line to the commit message
17 u,utf8          recode into utf8 (default)
18 k,keep          pass -k flag to git-mailinfo
19 whitespace=     pass it through git-apply
20 C=              pass it through git-apply
21 p=              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"
26 . git-sh-setup
27 prefix=$(git rev-parse --show-prefix)
28 set_reflog_action am
29 require_work_tree
30 cd_to_toplevel
32 git var GIT_COMMITTER_IDENT >/dev/null || exit
34 stop_here () {
35     echo "$1" >"$dotest/next"
36     exit 1
37 }
39 stop_here_user_resolve () {
40     if [ -n "$resolvemsg" ]; then
41             printf '%s\n' "$resolvemsg"
42             stop_here $1
43     fi
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     echo "When you have resolved this problem run \"$cmdline --resolved\"."
54     echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
56     stop_here $1
57 }
59 go_next () {
60         rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
61                 "$dotest/patch" "$dotest/info"
62         echo "$next" >"$dotest/next"
63         this=$next
64 }
66 cannot_fallback () {
67         echo "$1"
68         echo "Cannot fall back to three-way merge."
69         exit 1
70 }
72 fall_back_3way () {
73     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
75     rm -fr "$dotest"/patch-merge-*
76     mkdir "$dotest/patch-merge-tmp-dir"
78     # First see if the patch records the index info that we can use.
79     git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
80         "$dotest/patch" &&
81     GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
82     git write-tree >"$dotest/patch-merge-base+" ||
83     cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
85     echo Using index info to reconstruct a base tree...
86     if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
87         git apply $binary --cached <"$dotest/patch"
88     then
89         mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
90         mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
91     else
92         cannot_fallback "Did you hand edit your patch?
93 It does not apply to blobs recorded in its index."
94     fi
96     test -f "$dotest/patch-merge-index" &&
97     his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
98     orig_tree=$(cat "$dotest/patch-merge-base") &&
99     rm -fr "$dotest"/patch-merge-* || exit 1
101     echo Falling back to patching base and 3-way merge...
103     # This is not so wrong.  Depending on which base we picked,
104     # orig_tree may be wildly different from ours, but his_tree
105     # has the same set of wildly different changes in parts the
106     # patch did not touch, so recursive ends up canceling them,
107     # saying that we reverted all those changes.
109     eval GITHEAD_$his_tree='"$SUBJECT"'
110     export GITHEAD_$his_tree
111     git-merge-recursive $orig_tree -- HEAD $his_tree || {
112             git rerere
113             echo Failed to merge in the changes.
114             exit 1
115     }
116     unset GITHEAD_$his_tree
119 reread_subject () {
120         git stripspace <"$1" | sed -e 1q
123 prec=4
124 dotest=".dotest"
125 sign= utf8=t keep= skip= interactive= resolved= binary=
126 resolvemsg= resume=
127 git_apply_opt=
129 while test $# != 0
130 do
131         case "$1" in
132         -i|--interactive)
133                 interactive=t ;;
134         -b|--binary)
135                 binary=t ;;
136         -3|--3way)
137                 threeway=t ;;
138         -s|--signoff)
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 ;;
150         -d|--dotest)
151                 die "-d option is no longer supported.  Do not use."
152                 ;;
153         --resolvemsg)
154                 shift; resolvemsg=$1 ;;
155         --whitespace)
156                 git_apply_opt="$git_apply_opt $1=$2"; shift ;;
157         -C|-p)
158                 git_apply_opt="$git_apply_opt $1$2"; shift ;;
159         --)
160                 shift; break ;;
161         *)
162                 usage ;;
163         esac
164         shift
165 done
167 # If the dotest directory exists, but we have finished applying all the
168 # patches in them, clear it out.
169 if test -d "$dotest" &&
170    last=$(cat "$dotest/last") &&
171    next=$(cat "$dotest/next") &&
172    test $# != 0 &&
173    test "$next" -gt "$last"
174 then
175    rm -fr "$dotest"
176 fi
178 if test -d "$dotest"
179 then
180         case "$#,$skip$resolved" in
181         0,*t*)
182                 # Explicit resume command and we do not have file, so
183                 # we are happy.
184                 : ;;
185         0,)
186                 # No file input but without resume parameters; catch
187                 # user error to feed us a patch from standard input
188                 # when there is already $dotest.  This is somewhat
189                 # unreliable -- stdin could be /dev/null for example
190                 # and the caller did not intend to feed us a patch but
191                 # wanted to continue unattended.
192                 tty -s
193                 ;;
194         *)
195                 false
196                 ;;
197         esac ||
198         die "previous dotest directory $dotest still exists but mbox given."
199         resume=yes
200 else
201         # Make sure we are not given --skip nor --resolved
202         test ",$skip,$resolved," = ,,, ||
203                 die "Resolve operation not in progress, we are not resuming."
205         # Start afresh.
206         mkdir -p "$dotest" || exit
208         if test -n "$prefix" && test $# != 0
209         then
210                 first=t
211                 for arg
212                 do
213                         test -n "$first" && {
214                                 set x
215                                 first=
216                         }
217                         case "$arg" in
218                         /*)
219                                 set "$@" "$arg" ;;
220                         *)
221                                 set "$@" "$prefix$arg" ;;
222                         esac
223                 done
224                 shift
225         fi
226         git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||  {
227                 rm -fr "$dotest"
228                 exit 1
229         }
231         # -b, -s, -u, -k and --whitespace flags are kept for the
232         # resuming session after a patch failure.
233         # -3 and -i can and must be given when resuming.
234         echo "$binary" >"$dotest/binary"
235         echo " $ws" >"$dotest/whitespace"
236         echo "$sign" >"$dotest/sign"
237         echo "$utf8" >"$dotest/utf8"
238         echo "$keep" >"$dotest/keep"
239         echo 1 >"$dotest/next"
240 fi
242 case "$resolved" in
243 '')
244         files=$(git diff-index --cached --name-only HEAD --) || exit
245         if [ "$files" ]; then
246            echo "Dirty index: cannot apply patches (dirty: $files)" >&2
247            exit 1
248         fi
249 esac
251 if test "$(cat "$dotest/binary")" = t
252 then
253         binary=--allow-binary-replacement
254 fi
255 if test "$(cat "$dotest/utf8")" = t
256 then
257         utf8=-u
258 else
259         utf8=-n
260 fi
261 if test "$(cat "$dotest/keep")" = t
262 then
263         keep=-k
264 fi
265 ws=`cat "$dotest/whitespace"`
266 if test "$(cat "$dotest/sign")" = t
267 then
268         SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
269                         s/>.*/>/
270                         s/^/Signed-off-by: /'
271                 `
272 else
273         SIGNOFF=
274 fi
276 last=`cat "$dotest/last"`
277 this=`cat "$dotest/next"`
278 if test "$skip" = t
279 then
280         git rerere clear
281         this=`expr "$this" + 1`
282         resume=
283 fi
285 if test "$this" -gt "$last"
286 then
287         echo Nothing to do.
288         rm -fr "$dotest"
289         exit
290 fi
292 while test "$this" -le "$last"
293 do
294         msgnum=`printf "%0${prec}d" $this`
295         next=`expr "$this" + 1`
296         test -f "$dotest/$msgnum" || {
297                 resume=
298                 go_next
299                 continue
300         }
302         # If we are not resuming, parse and extract the patch information
303         # into separate files:
304         #  - info records the authorship and title
305         #  - msg is the rest of commit log message
306         #  - patch is the patch body.
307         #
308         # When we are resuming, these files are either already prepared
309         # by the user, or the user can tell us to do so by --resolved flag.
310         case "$resume" in
311         '')
312                 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
313                         <"$dotest/$msgnum" >"$dotest/info" ||
314                         stop_here $this
316                 # skip pine's internal folder data
317                 grep '^Author: Mail System Internal Data$' \
318                         <"$dotest"/info >/dev/null &&
319                         go_next && continue
321                 test -s $dotest/patch || {
322                         echo "Patch is empty.  Was it split wrong?"
323                         stop_here $this
324                 }
325                 git stripspace < "$dotest/msg" > "$dotest/msg-clean"
326                 ;;
327         esac
329         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
330         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
331         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
333         if test -z "$GIT_AUTHOR_EMAIL"
334         then
335                 echo "Patch does not have a valid e-mail address."
336                 stop_here $this
337         fi
339         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
341         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
342         case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
344         case "$resume" in
345         '')
346             if test '' != "$SIGNOFF"
347             then
348                 LAST_SIGNED_OFF_BY=`
349                     sed -ne '/^Signed-off-by: /p' \
350                     "$dotest/msg-clean" |
351                     tail -n 1
352                 `
353                 ADD_SIGNOFF=`
354                     test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
355                     test '' = "$LAST_SIGNED_OFF_BY" && echo
356                     echo "$SIGNOFF"
357                 }`
358             else
359                 ADD_SIGNOFF=
360             fi
361             {
362                 printf '%s\n' "$SUBJECT"
363                 if test -s "$dotest/msg-clean"
364                 then
365                         echo
366                         cat "$dotest/msg-clean"
367                 fi
368                 if test '' != "$ADD_SIGNOFF"
369                 then
370                         echo "$ADD_SIGNOFF"
371                 fi
372             } >"$dotest/final-commit"
373             ;;
374         *)
375                 case "$resolved$interactive" in
376                 tt)
377                         # This is used only for interactive view option.
378                         git diff-index -p --cached HEAD -- >"$dotest/patch"
379                         ;;
380                 esac
381         esac
383         resume=
384         if test "$interactive" = t
385         then
386             test -t 0 ||
387             die "cannot be interactive without stdin connected to a terminal."
388             action=again
389             while test "$action" = again
390             do
391                 echo "Commit Body is:"
392                 echo "--------------------------"
393                 cat "$dotest/final-commit"
394                 echo "--------------------------"
395                 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
396                 read reply
397                 case "$reply" in
398                 [yY]*) action=yes ;;
399                 [aA]*) action=yes interactive= ;;
400                 [nN]*) action=skip ;;
401                 [eE]*) git_editor "$dotest/final-commit"
402                        SUBJECT=$(reread_subject "$dotest/final-commit")
403                        action=again ;;
404                 [vV]*) action=again
405                        LESS=-S ${PAGER:-less} "$dotest/patch" ;;
406                 *)     action=again ;;
407                 esac
408             done
409         else
410             action=yes
411         fi
413         if test $action = skip
414         then
415                 go_next
416                 continue
417         fi
419         if test -x "$GIT_DIR"/hooks/applypatch-msg
420         then
421                 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
422                 stop_here $this
423         fi
425         printf 'Applying %s\n' "$SUBJECT"
427         case "$resolved" in
428         '')
429                 git apply $git_apply_opt $binary --index "$dotest/patch"
430                 apply_status=$?
431                 ;;
432         t)
433                 # Resolved means the user did all the hard work, and
434                 # we do not have to do any patch application.  Just
435                 # trust what the user has in the index file and the
436                 # working tree.
437                 resolved=
438                 git diff-index --quiet --cached HEAD -- && {
439                         echo "No changes - did you forget to use 'git add'?"
440                         stop_here_user_resolve $this
441                 }
442                 unmerged=$(git ls-files -u)
443                 if test -n "$unmerged"
444                 then
445                         echo "You still have unmerged paths in your index"
446                         echo "did you forget to use 'git add'?"
447                         stop_here_user_resolve $this
448                 fi
449                 apply_status=0
450                 git rerere
451                 ;;
452         esac
454         if test $apply_status = 1 && test "$threeway" = t
455         then
456                 if (fall_back_3way)
457                 then
458                     # Applying the patch to an earlier tree and merging the
459                     # result may have produced the same tree as ours.
460                     git diff-index --quiet --cached HEAD -- && {
461                         echo No changes -- Patch already applied.
462                         go_next
463                         continue
464                     }
465                     # clear apply_status -- we have successfully merged.
466                     apply_status=0
467                 fi
468         fi
469         if test $apply_status != 0
470         then
471                 echo Patch failed at $msgnum.
472                 stop_here_user_resolve $this
473         fi
475         if test -x "$GIT_DIR"/hooks/pre-applypatch
476         then
477                 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
478         fi
480         tree=$(git write-tree) &&
481         parent=$(git rev-parse --verify HEAD) &&
482         commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
483         git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
484         stop_here $this
486         if test -x "$GIT_DIR"/hooks/post-applypatch
487         then
488                 "$GIT_DIR"/hooks/post-applypatch
489         fi
491         go_next
492 done
494 git gc --auto
496 rm -fr "$dotest"