Keep Porcelainish from failing by broken ident after making changes.
[git.git] / git-am.sh
blob85ecada657d21aba4b54034dbaf869f8387d7d02
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way] <mbox>
6 or, when resuming [--skip | --resolved]'
7 . git-sh-setup
9 git var GIT_COMMITTER_IDENT >/dev/null || exit
11 stop_here () {
12 echo "$1" >"$dotest/next"
13 exit 1
16 go_next () {
17 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
18 "$dotest/patch" "$dotest/info"
19 echo "$next" >"$dotest/next"
20 this=$next
23 fall_back_3way () {
24 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
26 rm -fr "$dotest"/patch-merge-*
27 mkdir "$dotest/patch-merge-tmp-dir"
29 # First see if the patch records the index info that we can use.
30 if git-apply -z --index-info "$dotest/patch" \
31 >"$dotest/patch-merge-index-info" 2>/dev/null &&
32 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
33 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
34 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
35 git-write-tree >"$dotest/patch-merge-base+" &&
36 # index has the base tree now.
38 cd "$dotest/patch-merge-tmp-dir" &&
39 GIT_INDEX_FILE="../patch-merge-tmp-index" \
40 GIT_OBJECT_DIRECTORY="$O_OBJECT" \
41 git-apply $binary --index <../patch
43 then
44 echo Using index info to reconstruct a base tree...
45 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
46 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
47 else
48 # Otherwise, try nearby trees that can be used to apply the
49 # patch.
51 N=10
53 # Hoping the patch is against our recent commits...
54 git-rev-list --max-count=$N HEAD
56 # or hoping the patch is against known tags...
57 git-ls-remote --tags .
58 ) |
59 while read base junk
61 # See if we have it as a tree...
62 git-cat-file tree "$base" >/dev/null 2>&1 || continue
64 rm -fr "$dotest"/patch-merge-* &&
65 mkdir "$dotest/patch-merge-tmp-dir" || break
67 cd "$dotest/patch-merge-tmp-dir" &&
68 GIT_INDEX_FILE=../patch-merge-tmp-index &&
69 GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
70 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
71 git-read-tree "$base" &&
72 git-apply $binary --index &&
73 mv ../patch-merge-tmp-index ../patch-merge-index &&
74 echo "$base" >../patch-merge-base
75 ) <"$dotest/patch" 2>/dev/null && break
76 done
79 test -f "$dotest/patch-merge-index" &&
80 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
81 orig_tree=$(cat "$dotest/patch-merge-base") &&
82 rm -fr "$dotest"/patch-merge-* || exit 1
84 echo Falling back to patching base and 3-way merge...
86 # This is not so wrong. Depending on which base we picked,
87 # orig_tree may be wildly different from ours, but his_tree
88 # has the same set of wildly different changes in parts the
89 # patch did not touch, so resolve ends up cancelling them,
90 # saying that we reverted all those changes.
92 git-merge-resolve $orig_tree -- HEAD $his_tree || {
93 if test -d "$GIT_DIR/rr-cache"
94 then
95 git-rerere
97 echo Failed to merge in the changes.
98 exit 1
102 prec=4
103 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary=
105 while case "$#" in 0) break;; esac
107 case "$1" in
108 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
109 dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
110 -d|--d|--do|--dot|--dote|--dotes|--dotest)
111 case "$#" in 1) usage ;; esac; shift
112 dotest="$1"; shift;;
114 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
115 --interacti|--interactiv|--interactive)
116 interactive=t; shift ;;
118 -b|--b|--bi|--bin|--bina|--binar|--binary)
119 binary=t; shift ;;
121 -3|--3|--3w|--3wa|--3way)
122 threeway=t; shift ;;
123 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
124 sign=t; shift ;;
125 -u|--u|--ut|--utf|--utf8)
126 utf8=t; shift ;;
127 -k|--k|--ke|--kee|--keep)
128 keep=t; shift ;;
130 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
131 resolved=t; shift ;;
133 --sk|--ski|--skip)
134 skip=t; shift ;;
137 shift; break ;;
139 usage ;;
141 break ;;
142 esac
143 done
145 # If the dotest directory exists, but we have finished applying all the
146 # patches in them, clear it out.
147 if test -d "$dotest" &&
148 last=$(cat "$dotest/last") &&
149 next=$(cat "$dotest/next") &&
150 test $# != 0 &&
151 test "$next" -gt "$last"
152 then
153 rm -fr "$dotest"
156 if test -d "$dotest"
157 then
158 test ",$#," = ",0," ||
159 die "previous dotest directory $dotest still exists but mbox given."
160 resume=yes
161 else
162 # Make sure we are not given --skip nor --resolved
163 test ",$skip,$resolved," = ,,, ||
164 die "we are not resuming."
166 # Start afresh.
167 mkdir -p "$dotest" || exit
169 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
170 rm -fr "$dotest"
171 exit 1
174 # -b, -s, -u and -k flags are kept for the resuming session after
175 # a patch failure.
176 # -3 and -i can and must be given when resuming.
177 echo "$binary" >"$dotest/binary"
178 echo "$sign" >"$dotest/sign"
179 echo "$utf8" >"$dotest/utf8"
180 echo "$keep" >"$dotest/keep"
181 echo 1 >"$dotest/next"
184 case "$resolved" in
186 files=$(git-diff-index --cached --name-only HEAD) || exit
187 if [ "$files" ]; then
188 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
189 exit 1
191 esac
193 if test "$(cat "$dotest/binary")" = t
194 then
195 binary=--allow-binary-replacement
197 if test "$(cat "$dotest/utf8")" = t
198 then
199 utf8=-u
201 if test "$(cat "$dotest/keep")" = t
202 then
203 keep=-k
205 if test "$(cat "$dotest/sign")" = t
206 then
207 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
208 s/>.*/>/
209 s/^/Signed-off-by: /'
211 else
212 SIGNOFF=
215 last=`cat "$dotest/last"`
216 this=`cat "$dotest/next"`
217 if test "$skip" = t
218 then
219 this=`expr "$this" + 1`
220 resume=
223 if test "$this" -gt "$last"
224 then
225 echo Nothing to do.
226 rm -fr "$dotest"
227 exit
230 while test "$this" -le "$last"
232 msgnum=`printf "%0${prec}d" $this`
233 next=`expr "$this" + 1`
234 test -f "$dotest/$msgnum" || {
235 resume=
236 go_next
237 continue
240 # If we are not resuming, parse and extract the patch information
241 # into separate files:
242 # - info records the authorship and title
243 # - msg is the rest of commit log message
244 # - patch is the patch body.
246 # When we are resuming, these files are either already prepared
247 # by the user, or the user can tell us to do so by --resolved flag.
248 case "$resume" in
250 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
251 <"$dotest/$msgnum" >"$dotest/info" ||
252 stop_here $this
253 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
255 esac
257 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
258 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
259 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
261 if test -z "$GIT_AUTHOR_EMAIL"
262 then
263 echo "Patch does not have a valid e-mail address."
264 stop_here $this
267 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
269 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
270 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
272 case "$resume" in
274 if test '' != "$SIGNOFF"
275 then
276 LAST_SIGNED_OFF_BY=`
277 sed -ne '/^Signed-off-by: /p' \
278 "$dotest/msg-clean" |
279 tail -n 1
281 ADD_SIGNOFF=`
282 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
283 test '' = "$LAST_SIGNED_OFF_BY" && echo
284 echo "$SIGNOFF"
286 else
287 ADD_SIGNOFF=
290 echo "$SUBJECT"
291 if test -s "$dotest/msg-clean"
292 then
293 echo
294 cat "$dotest/msg-clean"
296 if test '' != "$ADD_SIGNOFF"
297 then
298 echo "$ADD_SIGNOFF"
300 } >"$dotest/final-commit"
303 case "$resolved,$interactive" in
305 # This is used only for interactive view option.
306 git-diff-index -p --cached HEAD >"$dotest/patch"
308 esac
309 esac
311 resume=
312 if test "$interactive" = t
313 then
314 test -t 0 ||
315 die "cannot be interactive without stdin connected to a terminal."
316 action=again
317 while test "$action" = again
319 echo "Commit Body is:"
320 echo "--------------------------"
321 cat "$dotest/final-commit"
322 echo "--------------------------"
323 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
324 read reply
325 case "$reply" in
326 [yY]*) action=yes ;;
327 [aA]*) action=yes interactive= ;;
328 [nN]*) action=skip ;;
329 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
330 action=again ;;
331 [vV]*) action=again
332 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
333 *) action=again ;;
334 esac
335 done
336 else
337 action=yes
340 if test $action = skip
341 then
342 go_next
343 continue
346 if test -x "$GIT_DIR"/hooks/applypatch-msg
347 then
348 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
349 stop_here $this
352 echo
353 echo "Applying '$SUBJECT'"
354 echo
356 case "$resolved" in
358 git-apply $binary --index "$dotest/patch"
359 apply_status=$?
362 # Resolved means the user did all the hard work, and
363 # we do not have to do any patch application. Just
364 # trust what the user has in the index file and the
365 # working tree.
366 resolved=
367 apply_status=0
369 esac
371 if test $apply_status = 1 && test "$threeway" = t
372 then
373 if (fall_back_3way)
374 then
375 # Applying the patch to an earlier tree and merging the
376 # result may have produced the same tree as ours.
377 changed="$(git-diff-index --cached --name-only -z HEAD)"
378 if test '' = "$changed"
379 then
380 echo No changes -- Patch already applied.
381 go_next
382 continue
384 # clear apply_status -- we have successfully merged.
385 apply_status=0
388 if test $apply_status != 0
389 then
390 echo Patch failed at $msgnum.
391 stop_here $this
394 if test -x "$GIT_DIR"/hooks/pre-applypatch
395 then
396 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
399 tree=$(git-write-tree) &&
400 echo Wrote tree $tree &&
401 parent=$(git-rev-parse --verify HEAD) &&
402 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
403 echo Committed: $commit &&
404 git-update-ref HEAD $commit $parent ||
405 stop_here $this
407 if test -x "$GIT_DIR"/hooks/post-applypatch
408 then
409 "$GIT_DIR"/hooks/post-applypatch
412 go_next
413 done
415 rm -fr "$dotest"