git-am: use apply --cached
[git/jnareb-git.git] / git-am.sh
blobf50dff22229b7940aea3d8a90a1836e62894a997
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
6 [--interactive] [--whitespace=<option>] <mbox>...
7 or, when resuming [--skip | --resolved]'
8 . git-sh-setup
10 git var GIT_COMMITTER_IDENT >/dev/null || exit
12 stop_here () {
13 echo "$1" >"$dotest/next"
14 exit 1
17 stop_here_user_resolve () {
18 cmdline=$(basename $0)
19 if test '' != "$interactive"
20 then
21 cmdline="$cmdline -i"
23 if test '' != "$threeway"
24 then
25 cmdline="$cmdline -3"
27 if test '.dotest' != "$dotest"
28 then
29 cmdline="$cmdline -d=$dotest"
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\"."
34 stop_here $1
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
44 fall_back_3way () {
45 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
47 rm -fr "$dotest"/patch-merge-*
48 mkdir "$dotest/patch-merge-tmp-dir"
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"
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
71 echo Falling back to patching base and 3-way merge...
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.
79 git-merge-resolve $orig_tree -- HEAD $his_tree || {
80 if test -d "$GIT_DIR/rr-cache"
81 then
82 git-rerere
84 echo Failed to merge in the changes.
85 exit 1
89 prec=4
90 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws=
92 while case "$#" in 0) break;; esac
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;;
101 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
102 --interacti|--interactiv|--interactive)
103 interactive=t; shift ;;
105 -b|--b|--bi|--bin|--bina|--binar|--binary)
106 binary=t; shift ;;
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 ;;
117 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
118 resolved=t; shift ;;
120 --sk|--ski|--skip)
121 skip=t; shift ;;
123 --whitespace=*)
124 ws=$1; shift ;;
127 shift; break ;;
129 usage ;;
131 break ;;
132 esac
133 done
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"
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."
156 # Start afresh.
157 mkdir -p "$dotest" || exit
159 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
160 rm -fr "$dotest"
161 exit 1
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"
175 case "$resolved" in
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
182 esac
184 if test "$(cat "$dotest/binary")" = t
185 then
186 binary=--allow-binary-replacement
188 if test "$(cat "$dotest/utf8")" = t
189 then
190 utf8=-u
192 if test "$(cat "$dotest/keep")" = t
193 then
194 keep=-k
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: /'
203 else
204 SIGNOFF=
207 last=`cat "$dotest/last"`
208 this=`cat "$dotest/next"`
209 if test "$skip" = t
210 then
211 this=`expr "$this" + 1`
212 resume=
215 if test "$this" -gt "$last"
216 then
217 echo Nothing to do.
218 rm -fr "$dotest"
219 exit
222 while test "$this" -le "$last"
224 msgnum=`printf "%0${prec}d" $this`
225 next=`expr "$this" + 1`
226 test -f "$dotest/$msgnum" || {
227 resume=
228 go_next
229 continue
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.
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
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"
247 esac
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")"
253 if test -z "$GIT_AUTHOR_EMAIL"
254 then
255 echo "Patch does not have a valid e-mail address."
256 stop_here $this
259 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
261 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
262 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
264 case "$resume" in
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
273 ADD_SIGNOFF=`
274 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
275 test '' = "$LAST_SIGNED_OFF_BY" && echo
276 echo "$SIGNOFF"
278 else
279 ADD_SIGNOFF=
282 echo "$SUBJECT"
283 if test -s "$dotest/msg-clean"
284 then
285 echo
286 cat "$dotest/msg-clean"
288 if test '' != "$ADD_SIGNOFF"
289 then
290 echo "$ADD_SIGNOFF"
292 } >"$dotest/final-commit"
295 case "$resolved$interactive" in
297 # This is used only for interactive view option.
298 git-diff-index -p --cached HEAD >"$dotest/patch"
300 esac
301 esac
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
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
332 if test $action = skip
333 then
334 go_next
335 continue
338 if test -x "$GIT_DIR"/hooks/applypatch-msg
339 then
340 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
341 stop_here $this
344 echo
345 echo "Applying '$SUBJECT'"
346 echo
348 case "$resolved" in
350 git-apply $binary --index $ws "$dotest/patch"
351 apply_status=$?
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
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
372 apply_status=0
374 esac
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
389 # clear apply_status -- we have successfully merged.
390 apply_status=0
393 if test $apply_status != 0
394 then
395 echo Patch failed at $msgnum.
396 stop_here_user_resolve $this
399 if test -x "$GIT_DIR"/hooks/pre-applypatch
400 then
401 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
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
412 if test -x "$GIT_DIR"/hooks/post-applypatch
413 then
414 "$GIT_DIR"/hooks/post-applypatch
417 go_next
418 done
420 rm -fr "$dotest"