remote.c: do not trigger remote.<name>.<var> codepath for two-level names
[git/dscho.git] / git-am.sh
blob6d1848b6cce89e4953a3ca6e1b2e6e1611277a4a
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 SUBDIRECTORY_OK=Yes
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git am [options] [<mbox>|<Maildir>...]
9 git am [options] (--resolved | --skip | --abort)
11 i,interactive run interactively
12 b,binary* (historical option -- no-op)
13 3,3way allow fall back on 3way merging if needed
14 s,signoff add a Signed-off-by line to the commit message
15 u,utf8 recode into utf8 (default)
16 k,keep pass -k flag to git-mailinfo
17 whitespace= pass it through git-apply
18 directory= pass it through git-apply
19 C= pass it through git-apply
20 p= pass it through git-apply
21 reject pass it through git-apply
22 resolvemsg= override error message when patch failure occurs
23 r,resolved to be used after a patch failure
24 skip skip the current patch
25 abort restore the original branch and abort the patching operation.
26 committer-date-is-author-date lie about committer date
27 ignore-date use current timestamp for author date
28 rebasing* (internal use for git-rebase)"
30 . git-sh-setup
31 prefix=$(git rev-parse --show-prefix)
32 set_reflog_action am
33 require_work_tree
34 cd_to_toplevel
36 git var GIT_COMMITTER_IDENT >/dev/null ||
37 die "You need to set your committer info first"
39 if git rev-parse --verify -q HEAD >/dev/null
40 then
41 HAS_HEAD=yes
42 else
43 HAS_HEAD=
46 sq () {
47 for sqarg
49 printf "%s" "$sqarg" |
50 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
51 done
54 stop_here () {
55 echo "$1" >"$dotest/next"
56 exit 1
59 stop_here_user_resolve () {
60 if [ -n "$resolvemsg" ]; then
61 printf '%s\n' "$resolvemsg"
62 stop_here $1
64 cmdline="git am"
65 if test '' != "$interactive"
66 then
67 cmdline="$cmdline -i"
69 if test '' != "$threeway"
70 then
71 cmdline="$cmdline -3"
73 echo "When you have resolved this problem run \"$cmdline --resolved\"."
74 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
75 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
77 stop_here $1
80 go_next () {
81 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
82 "$dotest/patch" "$dotest/info"
83 echo "$next" >"$dotest/next"
84 this=$next
87 cannot_fallback () {
88 echo "$1"
89 echo "Cannot fall back to three-way merge."
90 exit 1
93 fall_back_3way () {
94 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
96 rm -fr "$dotest"/patch-merge-*
97 mkdir "$dotest/patch-merge-tmp-dir"
99 # First see if the patch records the index info that we can use.
100 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
101 "$dotest/patch" &&
102 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
103 git write-tree >"$dotest/patch-merge-base+" ||
104 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
106 echo Using index info to reconstruct a base tree...
107 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
108 git apply --cached <"$dotest/patch"
109 then
110 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
111 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
112 else
113 cannot_fallback "Did you hand edit your patch?
114 It does not apply to blobs recorded in its index."
117 test -f "$dotest/patch-merge-index" &&
118 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
119 orig_tree=$(cat "$dotest/patch-merge-base") &&
120 rm -fr "$dotest"/patch-merge-* || exit 1
122 echo Falling back to patching base and 3-way merge...
124 # This is not so wrong. Depending on which base we picked,
125 # orig_tree may be wildly different from ours, but his_tree
126 # has the same set of wildly different changes in parts the
127 # patch did not touch, so recursive ends up canceling them,
128 # saying that we reverted all those changes.
130 eval GITHEAD_$his_tree='"$FIRSTLINE"'
131 export GITHEAD_$his_tree
132 git-merge-recursive $orig_tree -- HEAD $his_tree || {
133 git rerere
134 echo Failed to merge in the changes.
135 exit 1
137 unset GITHEAD_$his_tree
140 prec=4
141 dotest="$GIT_DIR/rebase-apply"
142 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
143 resolvemsg= resume=
144 git_apply_opt=
145 committer_date_is_author_date=
146 ignore_date=
148 while test $# != 0
150 case "$1" in
151 -i|--interactive)
152 interactive=t ;;
153 -b|--binary)
154 : ;;
155 -3|--3way)
156 threeway=t ;;
157 -s|--signoff)
158 sign=t ;;
159 -u|--utf8)
160 utf8=t ;; # this is now default
161 --no-utf8)
162 utf8= ;;
163 -k|--keep)
164 keep=t ;;
165 -r|--resolved)
166 resolved=t ;;
167 --skip)
168 skip=t ;;
169 --abort)
170 abort=t ;;
171 --rebasing)
172 rebasing=t threeway=t keep=t ;;
173 -d|--dotest)
174 die "-d option is no longer supported. Do not use."
176 --resolvemsg)
177 shift; resolvemsg=$1 ;;
178 --whitespace|--directory)
179 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
180 -C|-p)
181 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
182 --reject)
183 git_apply_opt="$git_apply_opt $1" ;;
184 --committer-date-is-author-date)
185 committer_date_is_author_date=t ;;
186 --ignore-date)
187 ignore_date=t ;;
189 shift; break ;;
191 usage ;;
192 esac
193 shift
194 done
196 # If the dotest directory exists, but we have finished applying all the
197 # patches in them, clear it out.
198 if test -d "$dotest" &&
199 last=$(cat "$dotest/last") &&
200 next=$(cat "$dotest/next") &&
201 test $# != 0 &&
202 test "$next" -gt "$last"
203 then
204 rm -fr "$dotest"
207 if test -d "$dotest"
208 then
209 case "$#,$skip$resolved$abort" in
210 0,*t*)
211 # Explicit resume command and we do not have file, so
212 # we are happy.
213 : ;;
215 # No file input but without resume parameters; catch
216 # user error to feed us a patch from standard input
217 # when there is already $dotest. This is somewhat
218 # unreliable -- stdin could be /dev/null for example
219 # and the caller did not intend to feed us a patch but
220 # wanted to continue unattended.
221 test -t 0
224 false
226 esac ||
227 die "previous rebase directory $dotest still exists but mbox given."
228 resume=yes
230 case "$skip,$abort" in
231 t,t)
232 die "Please make up your mind. --skip or --abort?"
235 git rerere clear
236 git read-tree --reset -u HEAD HEAD
237 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
238 git reset HEAD
239 git update-ref ORIG_HEAD $orig_head
242 if test -f "$dotest/rebasing"
243 then
244 exec git rebase --abort
246 git rerere clear
247 test -f "$dotest/dirtyindex" || {
248 git read-tree --reset -u HEAD ORIG_HEAD
249 git reset ORIG_HEAD
251 rm -fr "$dotest"
252 exit ;;
253 esac
254 rm -f "$dotest/dirtyindex"
255 else
256 # Make sure we are not given --skip, --resolved, nor --abort
257 test "$skip$resolved$abort" = "" ||
258 die "Resolve operation not in progress, we are not resuming."
260 # Start afresh.
261 mkdir -p "$dotest" || exit
263 if test -n "$prefix" && test $# != 0
264 then
265 first=t
266 for arg
268 test -n "$first" && {
269 set x
270 first=
272 case "$arg" in
274 set "$@" "$arg" ;;
276 set "$@" "$prefix$arg" ;;
277 esac
278 done
279 shift
281 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
282 rm -fr "$dotest"
283 exit 1
286 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
287 # for the resuming session after a patch failure.
288 # -i can and must be given when resuming.
289 echo " $git_apply_opt" >"$dotest/apply-opt"
290 echo "$threeway" >"$dotest/threeway"
291 echo "$sign" >"$dotest/sign"
292 echo "$utf8" >"$dotest/utf8"
293 echo "$keep" >"$dotest/keep"
294 echo 1 >"$dotest/next"
295 if test -n "$rebasing"
296 then
297 : >"$dotest/rebasing"
298 else
299 : >"$dotest/applying"
300 if test -n "$HAS_HEAD"
301 then
302 git update-ref ORIG_HEAD HEAD
303 else
304 git update-ref -d ORIG_HEAD >/dev/null 2>&1
309 case "$resolved" in
311 case "$HAS_HEAD" in
313 files=$(git ls-files) ;;
315 files=$(git diff-index --cached --name-only HEAD --) ;;
316 esac || exit
317 if test "$files"
318 then
319 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
320 die "Dirty index: cannot apply patches (dirty: $files)"
322 esac
324 if test "$(cat "$dotest/utf8")" = t
325 then
326 utf8=-u
327 else
328 utf8=-n
330 if test "$(cat "$dotest/keep")" = t
331 then
332 keep=-k
334 if test "$(cat "$dotest/threeway")" = t
335 then
336 threeway=t
338 git_apply_opt=$(cat "$dotest/apply-opt")
339 if test "$(cat "$dotest/sign")" = t
340 then
341 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
342 s/>.*/>/
343 s/^/Signed-off-by: /'
345 else
346 SIGNOFF=
349 last=`cat "$dotest/last"`
350 this=`cat "$dotest/next"`
351 if test "$skip" = t
352 then
353 this=`expr "$this" + 1`
354 resume=
357 if test "$this" -gt "$last"
358 then
359 echo Nothing to do.
360 rm -fr "$dotest"
361 exit
364 while test "$this" -le "$last"
366 msgnum=`printf "%0${prec}d" $this`
367 next=`expr "$this" + 1`
368 test -f "$dotest/$msgnum" || {
369 resume=
370 go_next
371 continue
374 # If we are not resuming, parse and extract the patch information
375 # into separate files:
376 # - info records the authorship and title
377 # - msg is the rest of commit log message
378 # - patch is the patch body.
380 # When we are resuming, these files are either already prepared
381 # by the user, or the user can tell us to do so by --resolved flag.
382 case "$resume" in
384 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
385 <"$dotest/$msgnum" >"$dotest/info" ||
386 stop_here $this
388 # skip pine's internal folder data
389 grep '^Author: Mail System Internal Data$' \
390 <"$dotest"/info >/dev/null &&
391 go_next && continue
393 test -s "$dotest/patch" || {
394 echo "Patch is empty. Was it split wrong?"
395 stop_here $this
397 if test -f "$dotest/rebasing" &&
398 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
399 -e q "$dotest/$msgnum") &&
400 test "$(git cat-file -t "$commit")" = commit
401 then
402 git cat-file commit "$commit" |
403 sed -e '1,/^$/d' >"$dotest/msg-clean"
404 else
405 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
406 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
408 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
409 git stripspace > "$dotest/msg-clean"
412 esac
414 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
415 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
416 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
418 if test -z "$GIT_AUTHOR_EMAIL"
419 then
420 echo "Patch does not have a valid e-mail address."
421 stop_here $this
424 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
426 case "$resume" in
428 if test '' != "$SIGNOFF"
429 then
430 LAST_SIGNED_OFF_BY=`
431 sed -ne '/^Signed-off-by: /p' \
432 "$dotest/msg-clean" |
433 sed -ne '$p'
435 ADD_SIGNOFF=`
436 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
437 test '' = "$LAST_SIGNED_OFF_BY" && echo
438 echo "$SIGNOFF"
440 else
441 ADD_SIGNOFF=
444 if test -s "$dotest/msg-clean"
445 then
446 cat "$dotest/msg-clean"
448 if test '' != "$ADD_SIGNOFF"
449 then
450 echo "$ADD_SIGNOFF"
452 } >"$dotest/final-commit"
455 case "$resolved$interactive" in
457 # This is used only for interactive view option.
458 git diff-index -p --cached HEAD -- >"$dotest/patch"
460 esac
461 esac
463 resume=
464 if test "$interactive" = t
465 then
466 test -t 0 ||
467 die "cannot be interactive without stdin connected to a terminal."
468 action=again
469 while test "$action" = again
471 echo "Commit Body is:"
472 echo "--------------------------"
473 cat "$dotest/final-commit"
474 echo "--------------------------"
475 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
476 read reply
477 case "$reply" in
478 [yY]*) action=yes ;;
479 [aA]*) action=yes interactive= ;;
480 [nN]*) action=skip ;;
481 [eE]*) git_editor "$dotest/final-commit"
482 action=again ;;
483 [vV]*) action=again
484 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
485 *) action=again ;;
486 esac
487 done
488 else
489 action=yes
491 FIRSTLINE=$(sed 1q "$dotest/final-commit")
493 if test $action = skip
494 then
495 go_next
496 continue
499 if test -x "$GIT_DIR"/hooks/applypatch-msg
500 then
501 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
502 stop_here $this
505 printf 'Applying: %s\n' "$FIRSTLINE"
507 case "$resolved" in
509 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
510 apply_status=$?
513 # Resolved means the user did all the hard work, and
514 # we do not have to do any patch application. Just
515 # trust what the user has in the index file and the
516 # working tree.
517 resolved=
518 git diff-index --quiet --cached HEAD -- && {
519 echo "No changes - did you forget to use 'git add'?"
520 stop_here_user_resolve $this
522 unmerged=$(git ls-files -u)
523 if test -n "$unmerged"
524 then
525 echo "You still have unmerged paths in your index"
526 echo "did you forget to use 'git add'?"
527 stop_here_user_resolve $this
529 apply_status=0
530 git rerere
532 esac
534 if test $apply_status = 1 && test "$threeway" = t
535 then
536 if (fall_back_3way)
537 then
538 # Applying the patch to an earlier tree and merging the
539 # result may have produced the same tree as ours.
540 git diff-index --quiet --cached HEAD -- && {
541 echo No changes -- Patch already applied.
542 go_next
543 continue
545 # clear apply_status -- we have successfully merged.
546 apply_status=0
549 if test $apply_status != 0
550 then
551 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
552 stop_here_user_resolve $this
555 if test -x "$GIT_DIR"/hooks/pre-applypatch
556 then
557 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
560 tree=$(git write-tree) &&
561 commit=$(
562 if test -n "$ignore_date"
563 then
564 GIT_AUTHOR_DATE=
566 parent=$(git rev-parse --verify -q HEAD) ||
567 echo >&2 "applying to an empty history"
569 if test -n "$committer_date_is_author_date"
570 then
571 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
572 export GIT_COMMITTER_DATE
573 fi &&
574 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
575 ) &&
576 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
577 stop_here $this
579 if test -x "$GIT_DIR"/hooks/post-applypatch
580 then
581 "$GIT_DIR"/hooks/post-applypatch
584 go_next
585 done
587 git gc --auto
589 rm -fr "$dotest"