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