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