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