git-am: use apply --cached
[git.git] / git-am.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005, 2006 Junio C Hamano
4
5 USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
6   [--interactive] [--whitespace=<option>] <mbox>...
7   or, when resuming [--skip | --resolved]'
8 . git-sh-setup
9
10 git var GIT_COMMITTER_IDENT >/dev/null || exit
11
12 stop_here () {
13     echo "$1" >"$dotest/next"
14     exit 1
15 }
16
17 stop_here_user_resolve () {
18     cmdline=$(basename $0)
19     if test '' != "$interactive"
20     then
21         cmdline="$cmdline -i"
22     fi
23     if test '' != "$threeway"
24     then
25         cmdline="$cmdline -3"
26     fi
27     if test '.dotest' != "$dotest"
28     then
29         cmdline="$cmdline -d=$dotest"
30     fi
31     echo "When you have resolved this problem run \"$cmdline --resolved\"."
32     echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
33
34     stop_here $1
35 }
36
37 go_next () {
38         rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
39                 "$dotest/patch" "$dotest/info"
40         echo "$next" >"$dotest/next"
41         this=$next
42 }
43
44 fall_back_3way () {
45     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
46
47     rm -fr "$dotest"/patch-merge-*
48     mkdir "$dotest/patch-merge-tmp-dir"
49
50     # First see if the patch records the index info that we can use.
51     if git-apply -z --index-info "$dotest/patch" \
52         >"$dotest/patch-merge-index-info" 2>/dev/null &&
53         GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
54         git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
55         GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
56         git-write-tree >"$dotest/patch-merge-base+" &&
57         # index has the base tree now.
58         GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
59         git-apply $binary --cached <"$dotest/patch"
60     then
61         echo Using index info to reconstruct a base tree...
62         mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
63         mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
64     fi
65
66     test -f "$dotest/patch-merge-index" &&
67     his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
68     orig_tree=$(cat "$dotest/patch-merge-base") &&
69     rm -fr "$dotest"/patch-merge-* || exit 1
70
71     echo Falling back to patching base and 3-way merge...
72
73     # This is not so wrong.  Depending on which base we picked,
74     # orig_tree may be wildly different from ours, but his_tree
75     # has the same set of wildly different changes in parts the
76     # patch did not touch, so resolve ends up cancelling them,
77     # saying that we reverted all those changes.
78
79     git-merge-resolve $orig_tree -- HEAD $his_tree || {
80             if test -d "$GIT_DIR/rr-cache"
81             then
82                 git-rerere
83             fi
84             echo Failed to merge in the changes.
85             exit 1
86     }
87 }
88
89 prec=4
90 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws=
91
92 while case "$#" in 0) break;; esac
93 do
94         case "$1" in
95         -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
96         dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
97         -d|--d|--do|--dot|--dote|--dotes|--dotest)
98         case "$#" in 1) usage ;; esac; shift
99         dotest="$1"; shift;;
100
101         -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
102         --interacti|--interactiv|--interactive)
103         interactive=t; shift ;;
104
105         -b|--b|--bi|--bin|--bina|--binar|--binary)
106         binary=t; shift ;;
107
108         -3|--3|--3w|--3wa|--3way)
109         threeway=t; shift ;;
110         -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
111         sign=t; shift ;;
112         -u|--u|--ut|--utf|--utf8)
113         utf8=t; shift ;;
114         -k|--k|--ke|--kee|--keep)
115         keep=t; shift ;;
116
117         -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
118         resolved=t; shift ;;
119
120         --sk|--ski|--skip)
121         skip=t; shift ;;
122
123         --whitespace=*)
124         ws=$1; shift ;;
125
126         --)
127         shift; break ;;
128         -*)
129         usage ;;
130         *)
131         break ;;
132         esac
133 done
134
135 # If the dotest directory exists, but we have finished applying all the
136 # patches in them, clear it out.
137 if test -d "$dotest" &&
138    last=$(cat "$dotest/last") &&
139    next=$(cat "$dotest/next") &&
140    test $# != 0 &&
141    test "$next" -gt "$last"
142 then
143    rm -fr "$dotest"
144 fi
145
146 if test -d "$dotest"
147 then
148         test ",$#," = ",0," ||
149         die "previous dotest directory $dotest still exists but mbox given."
150         resume=yes
151 else
152         # Make sure we are not given --skip nor --resolved
153         test ",$skip,$resolved," = ,,, ||
154                 die "we are not resuming."
155
156         # Start afresh.
157         mkdir -p "$dotest" || exit
158
159         git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||  {
160                 rm -fr "$dotest"
161                 exit 1
162         }
163
164         # -b, -s, -u, -k and --whitespace flags are kept for the
165         # resuming session after a patch failure.
166         # -3 and -i can and must be given when resuming.
167         echo "$binary" >"$dotest/binary"
168         echo " $ws" >"$dotest/whitespace"
169         echo "$sign" >"$dotest/sign"
170         echo "$utf8" >"$dotest/utf8"
171         echo "$keep" >"$dotest/keep"
172         echo 1 >"$dotest/next"
173 fi
174
175 case "$resolved" in
176 '')
177         files=$(git-diff-index --cached --name-only HEAD) || exit
178         if [ "$files" ]; then
179            echo "Dirty index: cannot apply patches (dirty: $files)" >&2
180            exit 1
181         fi
182 esac
183
184 if test "$(cat "$dotest/binary")" = t
185 then
186         binary=--allow-binary-replacement
187 fi
188 if test "$(cat "$dotest/utf8")" = t
189 then
190         utf8=-u
191 fi
192 if test "$(cat "$dotest/keep")" = t
193 then
194         keep=-k
195 fi
196 ws=`cat "$dotest/whitespace"`
197 if test "$(cat "$dotest/sign")" = t
198 then
199         SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
200                         s/>.*/>/
201                         s/^/Signed-off-by: /'
202                 `
203 else
204         SIGNOFF=
205 fi
206
207 last=`cat "$dotest/last"`
208 this=`cat "$dotest/next"`
209 if test "$skip" = t
210 then
211         this=`expr "$this" + 1`
212         resume=
213 fi
214
215 if test "$this" -gt "$last"
216 then
217         echo Nothing to do.
218         rm -fr "$dotest"
219         exit
220 fi
221
222 while test "$this" -le "$last"
223 do
224         msgnum=`printf "%0${prec}d" $this`
225         next=`expr "$this" + 1`
226         test -f "$dotest/$msgnum" || {
227                 resume=
228                 go_next
229                 continue
230         }
231
232         # If we are not resuming, parse and extract the patch information
233         # into separate files:
234         #  - info records the authorship and title
235         #  - msg is the rest of commit log message
236         #  - patch is the patch body.
237         #
238         # When we are resuming, these files are either already prepared
239         # by the user, or the user can tell us to do so by --resolved flag.
240         case "$resume" in
241         '')
242                 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
243                         <"$dotest/$msgnum" >"$dotest/info" ||
244                         stop_here $this
245                 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
246                 ;;
247         esac
248
249         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
250         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
251         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
252
253         if test -z "$GIT_AUTHOR_EMAIL"
254         then
255                 echo "Patch does not have a valid e-mail address."
256                 stop_here $this
257         fi
258
259         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
260
261         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
262         case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
263
264         case "$resume" in
265         '')
266             if test '' != "$SIGNOFF"
267             then
268                 LAST_SIGNED_OFF_BY=`
269                     sed -ne '/^Signed-off-by: /p' \
270                     "$dotest/msg-clean" |
271                     tail -n 1
272                 `
273                 ADD_SIGNOFF=`
274                     test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
275                     test '' = "$LAST_SIGNED_OFF_BY" && echo
276                     echo "$SIGNOFF"
277                 }`
278             else
279                 ADD_SIGNOFF=
280             fi
281             {
282                 echo "$SUBJECT"
283                 if test -s "$dotest/msg-clean"
284                 then
285                         echo
286                         cat "$dotest/msg-clean"
287                 fi
288                 if test '' != "$ADD_SIGNOFF"
289                 then
290                         echo "$ADD_SIGNOFF"
291                 fi
292             } >"$dotest/final-commit"
293             ;;
294         *)
295                 case "$resolved$interactive" in
296                 tt)
297                         # This is used only for interactive view option.
298                         git-diff-index -p --cached HEAD >"$dotest/patch"
299                         ;;
300                 esac
301         esac
302
303         resume=
304         if test "$interactive" = t
305         then
306             test -t 0 ||
307             die "cannot be interactive without stdin connected to a terminal."
308             action=again
309             while test "$action" = again
310             do
311                 echo "Commit Body is:"
312                 echo "--------------------------"
313                 cat "$dotest/final-commit"
314                 echo "--------------------------"
315                 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
316                 read reply
317                 case "$reply" in
318                 [yY]*) action=yes ;;
319                 [aA]*) action=yes interactive= ;;
320                 [nN]*) action=skip ;;
321                 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
322                        action=again ;;
323                 [vV]*) action=again
324                        LESS=-S ${PAGER:-less} "$dotest/patch" ;;
325                 *)     action=again ;;
326                 esac
327             done
328         else
329             action=yes
330         fi
331
332         if test $action = skip
333         then
334                 go_next
335                 continue
336         fi
337
338         if test -x "$GIT_DIR"/hooks/applypatch-msg
339         then
340                 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
341                 stop_here $this
342         fi
343
344         echo
345         echo "Applying '$SUBJECT'"
346         echo
347
348         case "$resolved" in
349         '')
350                 git-apply $binary --index $ws "$dotest/patch"
351                 apply_status=$?
352                 ;;
353         t)
354                 # Resolved means the user did all the hard work, and
355                 # we do not have to do any patch application.  Just
356                 # trust what the user has in the index file and the
357                 # working tree.
358                 resolved=
359                 changed="$(git-diff-index --cached --name-only HEAD)"
360                 if test '' = "$changed"
361                 then
362                         echo "No changes - did you forget update-index?"
363                         stop_here_user_resolve $this
364                 fi
365                 unmerged=$(git-ls-files -u)
366                 if test -n "$unmerged"
367                 then
368                         echo "You still have unmerged paths in your index"
369                         echo "did you forget update-index?"
370                         stop_here_user_resolve $this
371                 fi
372                 apply_status=0
373                 ;;
374         esac
375
376         if test $apply_status = 1 && test "$threeway" = t
377         then
378                 if (fall_back_3way)
379                 then
380                     # Applying the patch to an earlier tree and merging the
381                     # result may have produced the same tree as ours.
382                     changed="$(git-diff-index --cached --name-only HEAD)"
383                     if test '' = "$changed"
384                     then
385                             echo No changes -- Patch already applied.
386                             go_next
387                             continue
388                     fi
389                     # clear apply_status -- we have successfully merged.
390                     apply_status=0
391                 fi
392         fi
393         if test $apply_status != 0
394         then
395                 echo Patch failed at $msgnum.
396                 stop_here_user_resolve $this
397         fi
398
399         if test -x "$GIT_DIR"/hooks/pre-applypatch
400         then
401                 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
402         fi
403
404         tree=$(git-write-tree) &&
405         echo Wrote tree $tree &&
406         parent=$(git-rev-parse --verify HEAD) &&
407         commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
408         echo Committed: $commit &&
409         git-update-ref HEAD $commit $parent ||
410         stop_here $this
411
412         if test -x "$GIT_DIR"/hooks/post-applypatch
413         then
414                 "$GIT_DIR"/hooks/post-applypatch
415         fi
416
417         go_next
418 done
419
420 rm -fr "$dotest"