xdl_merge(): fix a segmentation fault when refining conflicts
[git/jnareb-git.git] / git-am.sh
blobc3bbd78eabf6f3d5eaa0f31bb0c240a26f28e0c5
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
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 resolve ends up canceling them,
92 # saying that we reverted all those changes.
94 git-merge-resolve $orig_tree -- HEAD $his_tree || {
95 if test -d "$GIT_DIR/rr-cache"
96 then
97 git-rerere
99 echo Failed to merge in the changes.
100 exit 1
104 prec=4
105 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg=
107 while case "$#" in 0) break;; esac
109 case "$1" in
110 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
111 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
112 -d|--d|--do|--dot|--dote|--dotes|--dotest)
113 case "$#" in 1) usage ;; esac; shift
114 dotest="$1"; shift;;
116 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
117 --interacti|--interactiv|--interactive)
118 interactive=t; shift ;;
120 -b|--b|--bi|--bin|--bina|--binar|--binary)
121 binary=t; shift ;;
123 -3|--3|--3w|--3wa|--3way)
124 threeway=t; shift ;;
125 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
126 sign=t; shift ;;
127 -u|--u|--ut|--utf|--utf8)
128 utf8=t; shift ;;
129 -k|--k|--ke|--kee|--keep)
130 keep=t; shift ;;
132 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
133 resolved=t; shift ;;
135 --sk|--ski|--skip)
136 skip=t; shift ;;
138 --whitespace=*)
139 ws=$1; shift ;;
141 --resolvemsg=*)
142 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
145 shift; break ;;
147 usage ;;
149 break ;;
150 esac
151 done
153 # If the dotest directory exists, but we have finished applying all the
154 # patches in them, clear it out.
155 if test -d "$dotest" &&
156 last=$(cat "$dotest/last") &&
157 next=$(cat "$dotest/next") &&
158 test $# != 0 &&
159 test "$next" -gt "$last"
160 then
161 rm -fr "$dotest"
164 if test -d "$dotest"
165 then
166 case "$#,$skip$resolved" in
167 0,*t*)
168 # Explicit resume command and we do not have file, so
169 # we are happy.
170 : ;;
172 # No file input but without resume parameters; catch
173 # user error to feed us a patch from standard input
174 # when there is already .dotest. This is somewhat
175 # unreliable -- stdin could be /dev/null for example
176 # and the caller did not intend to feed us a patch but
177 # wanted to continue unattended.
178 tty -s
181 false
183 esac ||
184 die "previous dotest directory $dotest still exists but mbox given."
185 resume=yes
186 else
187 # Make sure we are not given --skip nor --resolved
188 test ",$skip,$resolved," = ,,, ||
189 die "Resolve operation not in progress, we are not resuming."
191 # Start afresh.
192 mkdir -p "$dotest" || exit
194 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
195 rm -fr "$dotest"
196 exit 1
199 # -b, -s, -u, -k and --whitespace flags are kept for the
200 # resuming session after a patch failure.
201 # -3 and -i can and must be given when resuming.
202 echo "$binary" >"$dotest/binary"
203 echo " $ws" >"$dotest/whitespace"
204 echo "$sign" >"$dotest/sign"
205 echo "$utf8" >"$dotest/utf8"
206 echo "$keep" >"$dotest/keep"
207 echo 1 >"$dotest/next"
210 case "$resolved" in
212 files=$(git-diff-index --cached --name-only HEAD) || exit
213 if [ "$files" ]; then
214 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
215 exit 1
217 esac
219 if test "$(cat "$dotest/binary")" = t
220 then
221 binary=--allow-binary-replacement
223 if test "$(cat "$dotest/utf8")" = t
224 then
225 utf8=-u
227 if test "$(cat "$dotest/keep")" = t
228 then
229 keep=-k
231 ws=`cat "$dotest/whitespace"`
232 if test "$(cat "$dotest/sign")" = t
233 then
234 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
235 s/>.*/>/
236 s/^/Signed-off-by: /'
238 else
239 SIGNOFF=
242 last=`cat "$dotest/last"`
243 this=`cat "$dotest/next"`
244 if test "$skip" = t
245 then
246 if test -d "$GIT_DIR/rr-cache"
247 then
248 git-rerere clear
250 this=`expr "$this" + 1`
251 resume=
254 if test "$this" -gt "$last"
255 then
256 echo Nothing to do.
257 rm -fr "$dotest"
258 exit
261 while test "$this" -le "$last"
263 msgnum=`printf "%0${prec}d" $this`
264 next=`expr "$this" + 1`
265 test -f "$dotest/$msgnum" || {
266 resume=
267 go_next
268 continue
271 # If we are not resuming, parse and extract the patch information
272 # into separate files:
273 # - info records the authorship and title
274 # - msg is the rest of commit log message
275 # - patch is the patch body.
277 # When we are resuming, these files are either already prepared
278 # by the user, or the user can tell us to do so by --resolved flag.
279 case "$resume" in
281 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
282 <"$dotest/$msgnum" >"$dotest/info" ||
283 stop_here $this
284 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
286 esac
288 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
289 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
290 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
292 if test -z "$GIT_AUTHOR_EMAIL"
293 then
294 echo "Patch does not have a valid e-mail address."
295 stop_here $this
298 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
300 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
301 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
303 case "$resume" in
305 if test '' != "$SIGNOFF"
306 then
307 LAST_SIGNED_OFF_BY=`
308 sed -ne '/^Signed-off-by: /p' \
309 "$dotest/msg-clean" |
310 tail -n 1
312 ADD_SIGNOFF=`
313 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
314 test '' = "$LAST_SIGNED_OFF_BY" && echo
315 echo "$SIGNOFF"
317 else
318 ADD_SIGNOFF=
321 echo "$SUBJECT"
322 if test -s "$dotest/msg-clean"
323 then
324 echo
325 cat "$dotest/msg-clean"
327 if test '' != "$ADD_SIGNOFF"
328 then
329 echo "$ADD_SIGNOFF"
331 } >"$dotest/final-commit"
334 case "$resolved$interactive" in
336 # This is used only for interactive view option.
337 git-diff-index -p --cached HEAD >"$dotest/patch"
339 esac
340 esac
342 resume=
343 if test "$interactive" = t
344 then
345 test -t 0 ||
346 die "cannot be interactive without stdin connected to a terminal."
347 action=again
348 while test "$action" = again
350 echo "Commit Body is:"
351 echo "--------------------------"
352 cat "$dotest/final-commit"
353 echo "--------------------------"
354 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
355 read reply
356 case "$reply" in
357 [yY]*) action=yes ;;
358 [aA]*) action=yes interactive= ;;
359 [nN]*) action=skip ;;
360 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
361 action=again ;;
362 [vV]*) action=again
363 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
364 *) action=again ;;
365 esac
366 done
367 else
368 action=yes
371 if test $action = skip
372 then
373 go_next
374 continue
377 if test -x "$GIT_DIR"/hooks/applypatch-msg
378 then
379 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
380 stop_here $this
383 echo
384 echo "Applying '$SUBJECT'"
385 echo
387 case "$resolved" in
389 git-apply $binary --index $ws "$dotest/patch"
390 apply_status=$?
393 # Resolved means the user did all the hard work, and
394 # we do not have to do any patch application. Just
395 # trust what the user has in the index file and the
396 # working tree.
397 resolved=
398 changed="$(git-diff-index --cached --name-only HEAD)"
399 if test '' = "$changed"
400 then
401 echo "No changes - did you forget to use 'git add'?"
402 stop_here_user_resolve $this
404 unmerged=$(git-ls-files -u)
405 if test -n "$unmerged"
406 then
407 echo "You still have unmerged paths in your index"
408 echo "did you forget to use 'git add'?"
409 stop_here_user_resolve $this
411 apply_status=0
412 if test -d "$GIT_DIR/rr-cache"
413 then
414 git rerere
417 esac
419 if test $apply_status = 1 && test "$threeway" = t
420 then
421 if (fall_back_3way)
422 then
423 # Applying the patch to an earlier tree and merging the
424 # result may have produced the same tree as ours.
425 changed="$(git-diff-index --cached --name-only HEAD)"
426 if test '' = "$changed"
427 then
428 echo No changes -- Patch already applied.
429 go_next
430 continue
432 # clear apply_status -- we have successfully merged.
433 apply_status=0
436 if test $apply_status != 0
437 then
438 echo Patch failed at $msgnum.
439 stop_here_user_resolve $this
442 if test -x "$GIT_DIR"/hooks/pre-applypatch
443 then
444 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
447 tree=$(git-write-tree) &&
448 echo Wrote tree $tree &&
449 parent=$(git-rev-parse --verify HEAD) &&
450 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
451 echo Committed: $commit &&
452 git-update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
453 stop_here $this
455 if test -x "$GIT_DIR"/hooks/post-applypatch
456 then
457 "$GIT_DIR"/hooks/post-applypatch
460 go_next
461 done
463 rm -fr "$dotest"