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