Code

2b5bbb772ae9e3ac716210b3e19eef64f70f0944
[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=       use <dir> and not .dotest
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     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\"."
60     stop_here $1
61 }
63 go_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 }
70 cannot_fallback () {
71         echo "$1"
72         echo "Cannot fall back to three-way merge."
73         exit 1
74 }
76 fall_back_3way () {
77     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
79     rm -fr "$dotest"/patch-merge-*
80     mkdir "$dotest/patch-merge-tmp-dir"
82     # First see if the patch records the index info that we can use.
83     git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
84         "$dotest/patch" &&
85     GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
86     git write-tree >"$dotest/patch-merge-base+" ||
87     cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
89     echo Using index info to reconstruct a base tree...
90     if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
91         git apply $binary --cached <"$dotest/patch"
92     then
93         mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
94         mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
95     else
96         cannot_fallback "Did you hand edit your patch?
97 It does not apply to blobs recorded in its index."
98     fi
100     test -f "$dotest/patch-merge-index" &&
101     his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
102     orig_tree=$(cat "$dotest/patch-merge-base") &&
103     rm -fr "$dotest"/patch-merge-* || exit 1
105     echo Falling back to patching base and 3-way merge...
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
110     # patch did not touch, so recursive ends up canceling them,
111     # saying that we reverted all those changes.
113     eval GITHEAD_$his_tree='"$SUBJECT"'
114     export GITHEAD_$his_tree
115     git-merge-recursive $orig_tree -- HEAD $his_tree || {
116             git rerere
117             echo Failed to merge in the changes.
118             exit 1
119     }
120     unset GITHEAD_$his_tree
123 reread_subject () {
124         git stripspace <"$1" | sed -e 1q
127 prec=4
128 dotest="${prefix}.dotest"
129 sign= utf8=t keep= skip= interactive= resolved= binary=
130 resolvemsg= resume=
131 git_apply_opt=
133 while test $# != 0
134 do
135         case "$1" in
136         -i|--interactive)
137                 interactive=t ;;
138         -b|--binary)
139                 binary=t ;;
140         -3|--3way)
141                 threeway=t ;;
142         -s|--signoff)
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)
155                 shift
156                 case "$1" in /*) dotest=$1;; *) dotest="$prefix$1" ;; esac ;;
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 ;;
163         --)
164                 shift; break ;;
165         *)
166                 usage ;;
167         esac
168         shift
169 done
171 # If the dotest directory exists, but we have finished applying all the
172 # patches in them, clear it out.
173 if test -d "$dotest" &&
174    last=$(cat "$dotest/last") &&
175    next=$(cat "$dotest/next") &&
176    test $# != 0 &&
177    test "$next" -gt "$last"
178 then
179    rm -fr "$dotest"
180 fi
182 if test -d "$dotest"
183 then
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."
203         resume=yes
204 else
205         # Make sure we are not given --skip nor --resolved
206         test ",$skip,$resolved," = ,,, ||
207                 die "Resolve operation not in progress, we are not resuming."
209         # Start afresh.
210         mkdir -p "$dotest" || exit
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
230         git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||  {
231                 rm -fr "$dotest"
232                 exit 1
233         }
235         # -b, -s, -u, -k and --whitespace flags are kept for the
236         # resuming session after a patch failure.
237         # -3 and -i can and must be given when resuming.
238         echo "$binary" >"$dotest/binary"
239         echo " $ws" >"$dotest/whitespace"
240         echo "$sign" >"$dotest/sign"
241         echo "$utf8" >"$dotest/utf8"
242         echo "$keep" >"$dotest/keep"
243         echo 1 >"$dotest/next"
244 fi
246 case "$resolved" in
247 '')
248         files=$(git diff-index --cached --name-only HEAD --) || exit
249         if [ "$files" ]; then
250            echo "Dirty index: cannot apply patches (dirty: $files)" >&2
251            exit 1
252         fi
253 esac
255 if test "$(cat "$dotest/binary")" = t
256 then
257         binary=--allow-binary-replacement
258 fi
259 if test "$(cat "$dotest/utf8")" = t
260 then
261         utf8=-u
262 else
263         utf8=-n
264 fi
265 if test "$(cat "$dotest/keep")" = t
266 then
267         keep=-k
268 fi
269 ws=`cat "$dotest/whitespace"`
270 if test "$(cat "$dotest/sign")" = t
271 then
272         SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
273                         s/>.*/>/
274                         s/^/Signed-off-by: /'
275                 `
276 else
277         SIGNOFF=
278 fi
280 last=`cat "$dotest/last"`
281 this=`cat "$dotest/next"`
282 if test "$skip" = t
283 then
284         git rerere clear
285         this=`expr "$this" + 1`
286         resume=
287 fi
289 if test "$this" -gt "$last"
290 then
291         echo Nothing to do.
292         rm -fr "$dotest"
293         exit
294 fi
296 while test "$this" -le "$last"
297 do
298         msgnum=`printf "%0${prec}d" $this`
299         next=`expr "$this" + 1`
300         test -f "$dotest/$msgnum" || {
301                 resume=
302                 go_next
303                 continue
304         }
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.
314         case "$resume" in
315         '')
316                 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
317                         <"$dotest/$msgnum" >"$dotest/info" ||
318                         stop_here $this
320                 # skip pine's internal folder data
321                 grep '^Author: Mail System Internal Data$' \
322                         <"$dotest"/info >/dev/null &&
323                         go_next && continue
325                 test -s $dotest/patch || {
326                         echo "Patch is empty.  Was it split wrong?"
327                         stop_here $this
328                 }
329                 git stripspace < "$dotest/msg" > "$dotest/msg-clean"
330                 ;;
331         esac
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")"
337         if test -z "$GIT_AUTHOR_EMAIL"
338         then
339                 echo "Patch does not have a valid e-mail address."
340                 stop_here $this
341         fi
343         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
345         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
346         case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
348         case "$resume" in
349         '')
350             if test '' != "$SIGNOFF"
351             then
352                 LAST_SIGNED_OFF_BY=`
353                     sed -ne '/^Signed-off-by: /p' \
354                     "$dotest/msg-clean" |
355                     tail -n 1
356                 `
357                 ADD_SIGNOFF=`
358                     test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
359                     test '' = "$LAST_SIGNED_OFF_BY" && echo
360                     echo "$SIGNOFF"
361                 }`
362             else
363                 ADD_SIGNOFF=
364             fi
365             {
366                 printf '%s\n' "$SUBJECT"
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
376             } >"$dotest/final-commit"
377             ;;
378         *)
379                 case "$resolved$interactive" in
380                 tt)
381                         # This is used only for interactive view option.
382                         git diff-index -p --cached HEAD -- >"$dotest/patch"
383                         ;;
384                 esac
385         esac
387         resume=
388         if test "$interactive" = t
389         then
390             test -t 0 ||
391             die "cannot be interactive without stdin connected to a terminal."
392             action=again
393             while test "$action" = again
394             do
395                 echo "Commit Body is:"
396                 echo "--------------------------"
397                 cat "$dotest/final-commit"
398                 echo "--------------------------"
399                 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
400                 read reply
401                 case "$reply" in
402                 [yY]*) action=yes ;;
403                 [aA]*) action=yes interactive= ;;
404                 [nN]*) action=skip ;;
405                 [eE]*) git_editor "$dotest/final-commit"
406                        SUBJECT=$(reread_subject "$dotest/final-commit")
407                        action=again ;;
408                 [vV]*) action=again
409                        LESS=-S ${PAGER:-less} "$dotest/patch" ;;
410                 *)     action=again ;;
411                 esac
412             done
413         else
414             action=yes
415         fi
417         if test $action = skip
418         then
419                 go_next
420                 continue
421         fi
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
429         printf 'Applying %s\n' "$SUBJECT"
431         case "$resolved" in
432         '')
433                 git apply $git_apply_opt $binary --index "$dotest/patch"
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=
442                 git diff-index --quiet --cached HEAD -- && {
443                         echo "No changes - did you forget to use 'git add'?"
444                         stop_here_user_resolve $this
445                 }
446                 unmerged=$(git ls-files -u)
447                 if test -n "$unmerged"
448                 then
449                         echo "You still have unmerged paths in your index"
450                         echo "did you forget to use 'git add'?"
451                         stop_here_user_resolve $this
452                 fi
453                 apply_status=0
454                 git rerere
455                 ;;
456         esac
458         if test $apply_status = 1 && test "$threeway" = t
459         then
460                 if (fall_back_3way)
461                 then
462                     # Applying the patch to an earlier tree and merging the
463                     # result may have produced the same tree as ours.
464                     git diff-index --quiet --cached HEAD -- && {
465                         echo No changes -- Patch already applied.
466                         go_next
467                         continue
468                     }
469                     # clear apply_status -- we have successfully merged.
470                     apply_status=0
471                 fi
472         fi
473         if test $apply_status != 0
474         then
475                 echo Patch failed at $msgnum.
476                 stop_here_user_resolve $this
477         fi
479         if test -x "$GIT_DIR"/hooks/pre-applypatch
480         then
481                 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
482         fi
484         tree=$(git write-tree) &&
485         parent=$(git rev-parse --verify HEAD) &&
486         commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
487         git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
488         stop_here $this
490         if test -x "$GIT_DIR"/hooks/post-applypatch
491         then
492                 "$GIT_DIR"/hooks/post-applypatch
493         fi
495         go_next
496 done
498 git gc --auto
500 rm -fr "$dotest"