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