git-submodule summary: fix that some "wc" flavors produce leading spaces
[git/dscho.git] / git-am.sh
bloba2c6fea47af1e4616bc549f26f3fe1b364841abe
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
10 git-am [options] --skip
12 d,dotest= use <dir> and not .dotest
13 i,interactive run interactively
14 b,binary pass --allo-binary-replacement to git-apply
15 3,3way allow fall back on 3way merging if needed
16 s,signoff add a Signed-off-by line to the commit message
17 u,utf8 recode into utf8 (default)
18 k,keep pass -k flag to git-mailinfo
19 whitespace= pass it through git-apply
20 C= pass it through git-apply
21 p= 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"
26 . git-sh-setup
27 set_reflog_action am
28 require_work_tree
29 cd_to_toplevel
31 git var GIT_COMMITTER_IDENT >/dev/null || exit
33 stop_here () {
34 echo "$1" >"$dotest/next"
35 exit 1
38 stop_here_user_resolve () {
39 if [ -n "$resolvemsg" ]; then
40 printf '%s\n' "$resolvemsg"
41 stop_here $1
43 cmdline=$(basename $0)
44 if test '' != "$interactive"
45 then
46 cmdline="$cmdline -i"
48 if test '' != "$threeway"
49 then
50 cmdline="$cmdline -3"
52 if test '.dotest' != "$dotest"
53 then
54 cmdline="$cmdline -d=$dotest"
56 echo "When you have resolved this problem run \"$cmdline --resolved\"."
57 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
59 stop_here $1
62 go_next () {
63 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
64 "$dotest/patch" "$dotest/info"
65 echo "$next" >"$dotest/next"
66 this=$next
69 cannot_fallback () {
70 echo "$1"
71 echo "Cannot fall back to three-way merge."
72 exit 1
75 fall_back_3way () {
76 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
78 rm -fr "$dotest"/patch-merge-*
79 mkdir "$dotest/patch-merge-tmp-dir"
81 # First see if the patch records the index info that we can use.
82 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
83 "$dotest/patch" &&
84 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
85 git write-tree >"$dotest/patch-merge-base+" ||
86 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
88 echo Using index info to reconstruct a base tree...
89 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
90 git apply $binary --cached <"$dotest/patch"
91 then
92 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
93 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
94 else
95 cannot_fallback "Did you hand edit your patch?
96 It does not apply to blobs recorded in its index."
99 test -f "$dotest/patch-merge-index" &&
100 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
101 orig_tree=$(cat "$dotest/patch-merge-base") &&
102 rm -fr "$dotest"/patch-merge-* || exit 1
104 echo Falling back to patching base and 3-way merge...
106 # This is not so wrong. Depending on which base we picked,
107 # orig_tree may be wildly different from ours, but his_tree
108 # has the same set of wildly different changes in parts the
109 # patch did not touch, so recursive ends up canceling them,
110 # saying that we reverted all those changes.
112 eval GITHEAD_$his_tree='"$SUBJECT"'
113 export GITHEAD_$his_tree
114 git-merge-recursive $orig_tree -- HEAD $his_tree || {
115 git rerere
116 echo Failed to merge in the changes.
117 exit 1
119 unset GITHEAD_$his_tree
122 reread_subject () {
123 git stripspace <"$1" | sed -e 1q
126 prec=4
127 dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary=
128 resolvemsg= resume=
129 git_apply_opt=
131 while test $# != 0
133 case "$1" in
134 -i|--interactive)
135 interactive=t ;;
136 -b|--binary)
137 binary=t ;;
138 -3|--3way)
139 threeway=t ;;
140 -s|--signoff)
141 sign=t ;;
142 -u|--utf8)
143 utf8=t ;; # this is now default
144 --no-utf8)
145 utf8= ;;
146 -k|--keep)
147 keep=t ;;
148 -r|--resolved)
149 resolved=t ;;
150 --skip)
151 skip=t ;;
152 -d|--dotest)
153 shift; dotest=$1;;
154 --resolvemsg)
155 shift; resolvemsg=$1 ;;
156 --whitespace)
157 git_apply_opt="$git_apply_opt $1=$2"; shift ;;
158 -C|-p)
159 git_apply_opt="$git_apply_opt $1$2"; shift ;;
161 shift; break ;;
163 usage ;;
164 esac
165 shift
166 done
168 # If the dotest directory exists, but we have finished applying all the
169 # patches in them, clear it out.
170 if test -d "$dotest" &&
171 last=$(cat "$dotest/last") &&
172 next=$(cat "$dotest/next") &&
173 test $# != 0 &&
174 test "$next" -gt "$last"
175 then
176 rm -fr "$dotest"
179 if test -d "$dotest"
180 then
181 case "$#,$skip$resolved" in
182 0,*t*)
183 # Explicit resume command and we do not have file, so
184 # we are happy.
185 : ;;
187 # No file input but without resume parameters; catch
188 # user error to feed us a patch from standard input
189 # when there is already .dotest. This is somewhat
190 # unreliable -- stdin could be /dev/null for example
191 # and the caller did not intend to feed us a patch but
192 # wanted to continue unattended.
193 tty -s
196 false
198 esac ||
199 die "previous dotest directory $dotest still exists but mbox given."
200 resume=yes
201 else
202 # Make sure we are not given --skip nor --resolved
203 test ",$skip,$resolved," = ,,, ||
204 die "Resolve operation not in progress, we are not resuming."
206 # Start afresh.
207 mkdir -p "$dotest" || exit
209 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
210 rm -fr "$dotest"
211 exit 1
214 # -b, -s, -u, -k and --whitespace flags are kept for the
215 # resuming session after a patch failure.
216 # -3 and -i can and must be given when resuming.
217 echo "$binary" >"$dotest/binary"
218 echo " $ws" >"$dotest/whitespace"
219 echo "$sign" >"$dotest/sign"
220 echo "$utf8" >"$dotest/utf8"
221 echo "$keep" >"$dotest/keep"
222 echo 1 >"$dotest/next"
225 case "$resolved" in
227 files=$(git diff-index --cached --name-only HEAD --) || exit
228 if [ "$files" ]; then
229 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
230 exit 1
232 esac
234 if test "$(cat "$dotest/binary")" = t
235 then
236 binary=--allow-binary-replacement
238 if test "$(cat "$dotest/utf8")" = t
239 then
240 utf8=-u
241 else
242 utf8=-n
244 if test "$(cat "$dotest/keep")" = t
245 then
246 keep=-k
248 ws=`cat "$dotest/whitespace"`
249 if test "$(cat "$dotest/sign")" = t
250 then
251 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
252 s/>.*/>/
253 s/^/Signed-off-by: /'
255 else
256 SIGNOFF=
259 last=`cat "$dotest/last"`
260 this=`cat "$dotest/next"`
261 if test "$skip" = t
262 then
263 git rerere clear
264 this=`expr "$this" + 1`
265 resume=
268 if test "$this" -gt "$last"
269 then
270 echo Nothing to do.
271 rm -fr "$dotest"
272 exit
275 while test "$this" -le "$last"
277 msgnum=`printf "%0${prec}d" $this`
278 next=`expr "$this" + 1`
279 test -f "$dotest/$msgnum" || {
280 resume=
281 go_next
282 continue
285 # If we are not resuming, parse and extract the patch information
286 # into separate files:
287 # - info records the authorship and title
288 # - msg is the rest of commit log message
289 # - patch is the patch body.
291 # When we are resuming, these files are either already prepared
292 # by the user, or the user can tell us to do so by --resolved flag.
293 case "$resume" in
295 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
296 <"$dotest/$msgnum" >"$dotest/info" ||
297 stop_here $this
299 # skip pine's internal folder data
300 grep '^Author: Mail System Internal Data$' \
301 <"$dotest"/info >/dev/null &&
302 go_next && continue
304 test -s $dotest/patch || {
305 echo "Patch is empty. Was it split wrong?"
306 stop_here $this
308 git stripspace < "$dotest/msg" > "$dotest/msg-clean"
310 esac
312 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
313 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
314 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
316 if test -z "$GIT_AUTHOR_EMAIL"
317 then
318 echo "Patch does not have a valid e-mail address."
319 stop_here $this
322 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
324 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
325 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
327 case "$resume" in
329 if test '' != "$SIGNOFF"
330 then
331 LAST_SIGNED_OFF_BY=`
332 sed -ne '/^Signed-off-by: /p' \
333 "$dotest/msg-clean" |
334 tail -n 1
336 ADD_SIGNOFF=`
337 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
338 test '' = "$LAST_SIGNED_OFF_BY" && echo
339 echo "$SIGNOFF"
341 else
342 ADD_SIGNOFF=
345 printf '%s\n' "$SUBJECT"
346 if test -s "$dotest/msg-clean"
347 then
348 echo
349 cat "$dotest/msg-clean"
351 if test '' != "$ADD_SIGNOFF"
352 then
353 echo "$ADD_SIGNOFF"
355 } >"$dotest/final-commit"
358 case "$resolved$interactive" in
360 # This is used only for interactive view option.
361 git diff-index -p --cached HEAD -- >"$dotest/patch"
363 esac
364 esac
366 resume=
367 if test "$interactive" = t
368 then
369 test -t 0 ||
370 die "cannot be interactive without stdin connected to a terminal."
371 action=again
372 while test "$action" = again
374 echo "Commit Body is:"
375 echo "--------------------------"
376 cat "$dotest/final-commit"
377 echo "--------------------------"
378 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
379 read reply
380 case "$reply" in
381 [yY]*) action=yes ;;
382 [aA]*) action=yes interactive= ;;
383 [nN]*) action=skip ;;
384 [eE]*) git_editor "$dotest/final-commit"
385 SUBJECT=$(reread_subject "$dotest/final-commit")
386 action=again ;;
387 [vV]*) action=again
388 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
389 *) action=again ;;
390 esac
391 done
392 else
393 action=yes
396 if test $action = skip
397 then
398 go_next
399 continue
402 if test -x "$GIT_DIR"/hooks/applypatch-msg
403 then
404 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
405 stop_here $this
408 printf 'Applying %s\n' "$SUBJECT"
410 case "$resolved" in
412 git apply $git_apply_opt $binary --index "$dotest/patch"
413 apply_status=$?
416 # Resolved means the user did all the hard work, and
417 # we do not have to do any patch application. Just
418 # trust what the user has in the index file and the
419 # working tree.
420 resolved=
421 git diff-index --quiet --cached HEAD -- && {
422 echo "No changes - did you forget to use 'git add'?"
423 stop_here_user_resolve $this
425 unmerged=$(git ls-files -u)
426 if test -n "$unmerged"
427 then
428 echo "You still have unmerged paths in your index"
429 echo "did you forget to use 'git add'?"
430 stop_here_user_resolve $this
432 apply_status=0
433 git rerere
435 esac
437 if test $apply_status = 1 && test "$threeway" = t
438 then
439 if (fall_back_3way)
440 then
441 # Applying the patch to an earlier tree and merging the
442 # result may have produced the same tree as ours.
443 git diff-index --quiet --cached HEAD -- && {
444 echo No changes -- Patch already applied.
445 go_next
446 continue
448 # clear apply_status -- we have successfully merged.
449 apply_status=0
452 if test $apply_status != 0
453 then
454 echo Patch failed at $msgnum.
455 stop_here_user_resolve $this
458 if test -x "$GIT_DIR"/hooks/pre-applypatch
459 then
460 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
463 tree=$(git write-tree) &&
464 parent=$(git rev-parse --verify HEAD) &&
465 commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
466 git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
467 stop_here $this
469 if test -x "$GIT_DIR"/hooks/post-applypatch
470 then
471 "$GIT_DIR"/hooks/post-applypatch
474 go_next
475 done
477 git gc --auto
479 rm -fr "$dotest"