short i/o: fix calls to read to use xread or read_in_full
[git.git] / git-am.sh
blobd9eb79d1aa8dccf18fa0df89abdc5d3fdfd2aa24
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 USAGE='[--signoff] [--dotest=<dir>] [--utf8 | --no-utf8] [--binary] [--3way]
6 [--interactive] [--whitespace=<option>] <mbox>...
7 or, when resuming [--skip | --resolved]'
8 . git-sh-setup
9 set_reflog_action am
11 git var GIT_COMMITTER_IDENT >/dev/null || exit
13 stop_here () {
14 echo "$1" >"$dotest/next"
15 exit 1
18 stop_here_user_resolve () {
19 if [ -n "$resolvemsg" ]; then
20 echo "$resolvemsg"
21 stop_here $1
23 cmdline=$(basename $0)
24 if test '' != "$interactive"
25 then
26 cmdline="$cmdline -i"
28 if test '' != "$threeway"
29 then
30 cmdline="$cmdline -3"
32 if test '.dotest' != "$dotest"
33 then
34 cmdline="$cmdline -d=$dotest"
36 echo "When you have resolved this problem run \"$cmdline --resolved\"."
37 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
39 stop_here $1
42 go_next () {
43 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
44 "$dotest/patch" "$dotest/info"
45 echo "$next" >"$dotest/next"
46 this=$next
49 cannot_fallback () {
50 echo "$1"
51 echo "Cannot fall back to three-way merge."
52 exit 1
55 fall_back_3way () {
56 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
58 rm -fr "$dotest"/patch-merge-*
59 mkdir "$dotest/patch-merge-tmp-dir"
61 # First see if the patch records the index info that we can use.
62 git-apply -z --index-info "$dotest/patch" \
63 >"$dotest/patch-merge-index-info" &&
64 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
65 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
66 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
67 git-write-tree >"$dotest/patch-merge-base+" ||
68 cannot_fallback "Patch does not record usable index information."
70 echo Using index info to reconstruct a base tree...
71 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
72 git-apply $binary --cached <"$dotest/patch"
73 then
74 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
75 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
76 else
77 cannot_fallback "Did you hand edit your patch?
78 It does not apply to blobs recorded in its index."
81 test -f "$dotest/patch-merge-index" &&
82 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
83 orig_tree=$(cat "$dotest/patch-merge-base") &&
84 rm -fr "$dotest"/patch-merge-* || exit 1
86 echo Falling back to patching base and 3-way merge...
88 # This is not so wrong. Depending on which base we picked,
89 # orig_tree may be wildly different from ours, but his_tree
90 # has the same set of wildly different changes in parts the
91 # patch did not touch, so recursive ends up canceling them,
92 # saying that we reverted all those changes.
94 eval GITHEAD_$his_tree='"$SUBJECT"'
95 export GITHEAD_$his_tree
96 git-merge-recursive $orig_tree -- HEAD $his_tree || {
97 if test -d "$GIT_DIR/rr-cache"
98 then
99 git-rerere
101 echo Failed to merge in the changes.
102 exit 1
104 unset GITHEAD_$his_tree
107 prec=4
108 dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary= ws= resolvemsg=
110 while case "$#" in 0) break;; esac
112 case "$1" in
113 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
114 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
115 -d|--d|--do|--dot|--dote|--dotes|--dotest)
116 case "$#" in 1) usage ;; esac; shift
117 dotest="$1"; shift;;
119 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
120 --interacti|--interactiv|--interactive)
121 interactive=t; shift ;;
123 -b|--b|--bi|--bin|--bina|--binar|--binary)
124 binary=t; shift ;;
126 -3|--3|--3w|--3wa|--3way)
127 threeway=t; shift ;;
128 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
129 sign=t; shift ;;
130 -u|--u|--ut|--utf|--utf8)
131 shift ;; # this is now default
132 --no-u|--no-ut|--no-utf|--no-utf8)
133 utf8=; shift ;;
134 -k|--k|--ke|--kee|--keep)
135 keep=t; shift ;;
137 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
138 resolved=t; shift ;;
140 --sk|--ski|--skip)
141 skip=t; shift ;;
143 --whitespace=*)
144 ws=$1; shift ;;
146 --resolvemsg=*)
147 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
150 shift; break ;;
152 usage ;;
154 break ;;
155 esac
156 done
158 # If the dotest directory exists, but we have finished applying all the
159 # patches in them, clear it out.
160 if test -d "$dotest" &&
161 last=$(cat "$dotest/last") &&
162 next=$(cat "$dotest/next") &&
163 test $# != 0 &&
164 test "$next" -gt "$last"
165 then
166 rm -fr "$dotest"
169 if test -d "$dotest"
170 then
171 case "$#,$skip$resolved" in
172 0,*t*)
173 # Explicit resume command and we do not have file, so
174 # we are happy.
175 : ;;
177 # No file input but without resume parameters; catch
178 # user error to feed us a patch from standard input
179 # when there is already .dotest. This is somewhat
180 # unreliable -- stdin could be /dev/null for example
181 # and the caller did not intend to feed us a patch but
182 # wanted to continue unattended.
183 tty -s
186 false
188 esac ||
189 die "previous dotest directory $dotest still exists but mbox given."
190 resume=yes
191 else
192 # Make sure we are not given --skip nor --resolved
193 test ",$skip,$resolved," = ,,, ||
194 die "Resolve operation not in progress, we are not resuming."
196 # Start afresh.
197 mkdir -p "$dotest" || exit
199 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
200 rm -fr "$dotest"
201 exit 1
204 # -b, -s, -u, -k and --whitespace flags are kept for the
205 # resuming session after a patch failure.
206 # -3 and -i can and must be given when resuming.
207 echo "$binary" >"$dotest/binary"
208 echo " $ws" >"$dotest/whitespace"
209 echo "$sign" >"$dotest/sign"
210 echo "$utf8" >"$dotest/utf8"
211 echo "$keep" >"$dotest/keep"
212 echo 1 >"$dotest/next"
215 case "$resolved" in
217 files=$(git-diff-index --cached --name-only HEAD) || exit
218 if [ "$files" ]; then
219 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
220 exit 1
222 esac
224 if test "$(cat "$dotest/binary")" = t
225 then
226 binary=--allow-binary-replacement
228 if test "$(cat "$dotest/utf8")" = t
229 then
230 utf8=-u
232 if test "$(cat "$dotest/keep")" = t
233 then
234 keep=-k
236 ws=`cat "$dotest/whitespace"`
237 if test "$(cat "$dotest/sign")" = t
238 then
239 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
240 s/>.*/>/
241 s/^/Signed-off-by: /'
243 else
244 SIGNOFF=
247 last=`cat "$dotest/last"`
248 this=`cat "$dotest/next"`
249 if test "$skip" = t
250 then
251 if test -d "$GIT_DIR/rr-cache"
252 then
253 git-rerere clear
255 this=`expr "$this" + 1`
256 resume=
259 if test "$this" -gt "$last"
260 then
261 echo Nothing to do.
262 rm -fr "$dotest"
263 exit
266 while test "$this" -le "$last"
268 msgnum=`printf "%0${prec}d" $this`
269 next=`expr "$this" + 1`
270 test -f "$dotest/$msgnum" || {
271 resume=
272 go_next
273 continue
276 # If we are not resuming, parse and extract the patch information
277 # into separate files:
278 # - info records the authorship and title
279 # - msg is the rest of commit log message
280 # - patch is the patch body.
282 # When we are resuming, these files are either already prepared
283 # by the user, or the user can tell us to do so by --resolved flag.
284 case "$resume" in
286 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
287 <"$dotest/$msgnum" >"$dotest/info" ||
288 stop_here $this
289 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
291 esac
293 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
294 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
295 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
297 if test -z "$GIT_AUTHOR_EMAIL"
298 then
299 echo "Patch does not have a valid e-mail address."
300 stop_here $this
303 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
305 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
306 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
308 case "$resume" in
310 if test '' != "$SIGNOFF"
311 then
312 LAST_SIGNED_OFF_BY=`
313 sed -ne '/^Signed-off-by: /p' \
314 "$dotest/msg-clean" |
315 tail -n 1
317 ADD_SIGNOFF=`
318 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
319 test '' = "$LAST_SIGNED_OFF_BY" && echo
320 echo "$SIGNOFF"
322 else
323 ADD_SIGNOFF=
326 echo "$SUBJECT"
327 if test -s "$dotest/msg-clean"
328 then
329 echo
330 cat "$dotest/msg-clean"
332 if test '' != "$ADD_SIGNOFF"
333 then
334 echo "$ADD_SIGNOFF"
336 } >"$dotest/final-commit"
339 case "$resolved$interactive" in
341 # This is used only for interactive view option.
342 git-diff-index -p --cached HEAD >"$dotest/patch"
344 esac
345 esac
347 resume=
348 if test "$interactive" = t
349 then
350 test -t 0 ||
351 die "cannot be interactive without stdin connected to a terminal."
352 action=again
353 while test "$action" = again
355 echo "Commit Body is:"
356 echo "--------------------------"
357 cat "$dotest/final-commit"
358 echo "--------------------------"
359 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
360 read reply
361 case "$reply" in
362 [yY]*) action=yes ;;
363 [aA]*) action=yes interactive= ;;
364 [nN]*) action=skip ;;
365 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
366 action=again ;;
367 [vV]*) action=again
368 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
369 *) action=again ;;
370 esac
371 done
372 else
373 action=yes
376 if test $action = skip
377 then
378 go_next
379 continue
382 if test -x "$GIT_DIR"/hooks/applypatch-msg
383 then
384 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
385 stop_here $this
388 echo
389 echo "Applying '$SUBJECT'"
390 echo
392 case "$resolved" in
394 git-apply $binary --index $ws "$dotest/patch"
395 apply_status=$?
398 # Resolved means the user did all the hard work, and
399 # we do not have to do any patch application. Just
400 # trust what the user has in the index file and the
401 # working tree.
402 resolved=
403 changed="$(git-diff-index --cached --name-only HEAD)"
404 if test '' = "$changed"
405 then
406 echo "No changes - did you forget to use 'git add'?"
407 stop_here_user_resolve $this
409 unmerged=$(git-ls-files -u)
410 if test -n "$unmerged"
411 then
412 echo "You still have unmerged paths in your index"
413 echo "did you forget to use 'git add'?"
414 stop_here_user_resolve $this
416 apply_status=0
417 if test -d "$GIT_DIR/rr-cache"
418 then
419 git rerere
422 esac
424 if test $apply_status = 1 && test "$threeway" = t
425 then
426 if (fall_back_3way)
427 then
428 # Applying the patch to an earlier tree and merging the
429 # result may have produced the same tree as ours.
430 changed="$(git-diff-index --cached --name-only HEAD)"
431 if test '' = "$changed"
432 then
433 echo No changes -- Patch already applied.
434 go_next
435 continue
437 # clear apply_status -- we have successfully merged.
438 apply_status=0
441 if test $apply_status != 0
442 then
443 echo Patch failed at $msgnum.
444 stop_here_user_resolve $this
447 if test -x "$GIT_DIR"/hooks/pre-applypatch
448 then
449 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
452 tree=$(git-write-tree) &&
453 echo Wrote tree $tree &&
454 parent=$(git-rev-parse --verify HEAD) &&
455 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
456 echo Committed: $commit &&
457 git-update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
458 stop_here $this
460 if test -x "$GIT_DIR"/hooks/post-applypatch
461 then
462 "$GIT_DIR"/hooks/post-applypatch
465 go_next
466 done
468 rm -fr "$dotest"