]>
Commit | Line | Data |
---|---|---|
d1c5f2a4 JH |
1 | #!/bin/sh |
2 | # | |
d64e6b04 | 3 | # Copyright (c) 2005, 2006 Junio C Hamano |
d1c5f2a4 | 4 | |
c1491841 | 5 | SUBDIRECTORY_OK=Yes |
78443d90 PH |
6 | OPTIONS_KEEPDASHDASH= |
7 | OPTIONS_SPEC="\ | |
8 | git-am [options] <mbox>|<Maildir>... | |
9 | git-am [options] --resolved | |
10 | git-am [options] --skip | |
11 | -- | |
e72c7406 | 12 | d,dotest= (removed -- do not use) |
66006c63 | 13 | i,interactive run interactively |
78443d90 PH |
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) | |
fe1fa946 | 18 | k,keep pass -k flag to git-mailinfo |
78443d90 PH |
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 | |
3041c324 JH |
24 | skip skip the current patch |
25 | rebasing (internal use for git-rebase)" | |
78443d90 | 26 | |
cf1fe88c | 27 | . git-sh-setup |
bb034f83 | 28 | prefix=$(git rev-parse --show-prefix) |
f9474132 | 29 | set_reflog_action am |
7eff28a9 | 30 | require_work_tree |
c1491841 | 31 | cd_to_toplevel |
d1c5f2a4 | 32 | |
d64e6b04 JH |
33 | git var GIT_COMMITTER_IDENT >/dev/null || exit |
34 | ||
d1c5f2a4 JH |
35 | stop_here () { |
36 | echo "$1" >"$dotest/next" | |
37 | exit 1 | |
38 | } | |
39 | ||
ced9456a | 40 | stop_here_user_resolve () { |
cc120056 | 41 | if [ -n "$resolvemsg" ]; then |
a23bfaed | 42 | printf '%s\n' "$resolvemsg" |
cc120056 SE |
43 | stop_here $1 |
44 | fi | |
ced9456a RS |
45 | cmdline=$(basename $0) |
46 | if test '' != "$interactive" | |
47 | then | |
48 | cmdline="$cmdline -i" | |
49 | fi | |
50 | if test '' != "$threeway" | |
51 | then | |
52 | cmdline="$cmdline -3" | |
53 | fi | |
ced9456a RS |
54 | echo "When you have resolved this problem run \"$cmdline --resolved\"." |
55 | echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"." | |
56 | ||
57 | stop_here $1 | |
58 | } | |
59 | ||
d1c5f2a4 JH |
60 | go_next () { |
61 | rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \ | |
62 | "$dotest/patch" "$dotest/info" | |
63 | echo "$next" >"$dotest/next" | |
64 | this=$next | |
65 | } | |
66 | ||
fd7bcfb5 JH |
67 | cannot_fallback () { |
68 | echo "$1" | |
69 | echo "Cannot fall back to three-way merge." | |
70 | exit 1 | |
71 | } | |
72 | ||
d1c5f2a4 JH |
73 | fall_back_3way () { |
74 | O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd` | |
75 | ||
76 | rm -fr "$dotest"/patch-merge-* | |
77 | mkdir "$dotest/patch-merge-tmp-dir" | |
78 | ||
79 | # First see if the patch records the index info that we can use. | |
7a988699 JS |
80 | git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \ |
81 | "$dotest/patch" && | |
fd7bcfb5 | 82 | GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ |
5be60078 | 83 | git write-tree >"$dotest/patch-merge-base+" || |
b1440cc8 | 84 | cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge." |
fd7bcfb5 JH |
85 | |
86 | echo Using index info to reconstruct a base tree... | |
87 | if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ | |
5be60078 | 88 | git apply $binary --cached <"$dotest/patch" |
d1c5f2a4 | 89 | then |
d1c5f2a4 JH |
90 | mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base" |
91 | mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index" | |
fd7bcfb5 JH |
92 | else |
93 | cannot_fallback "Did you hand edit your patch? | |
94 | It does not apply to blobs recorded in its index." | |
d1c5f2a4 JH |
95 | fi |
96 | ||
97 | test -f "$dotest/patch-merge-index" && | |
5be60078 | 98 | his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) && |
d1c5f2a4 JH |
99 | orig_tree=$(cat "$dotest/patch-merge-base") && |
100 | rm -fr "$dotest"/patch-merge-* || exit 1 | |
101 | ||
102 | echo Falling back to patching base and 3-way merge... | |
103 | ||
104 | # This is not so wrong. Depending on which base we picked, | |
105 | # orig_tree may be wildly different from ours, but his_tree | |
106 | # has the same set of wildly different changes in parts the | |
579c9bb1 | 107 | # patch did not touch, so recursive ends up canceling them, |
d1c5f2a4 JH |
108 | # saying that we reverted all those changes. |
109 | ||
579c9bb1 SP |
110 | eval GITHEAD_$his_tree='"$SUBJECT"' |
111 | export GITHEAD_$his_tree | |
112 | git-merge-recursive $orig_tree -- HEAD $his_tree || { | |
b4372ef1 | 113 | git rerere |
d1c5f2a4 JH |
114 | echo Failed to merge in the changes. |
115 | exit 1 | |
116 | } | |
579c9bb1 | 117 | unset GITHEAD_$his_tree |
d1c5f2a4 JH |
118 | } |
119 | ||
f23272f3 JH |
120 | reread_subject () { |
121 | git stripspace <"$1" | sed -e 1q | |
122 | } | |
123 | ||
d1c5f2a4 | 124 | prec=4 |
e72c7406 | 125 | dotest=".dotest" |
3041c324 | 126 | sign= utf8=t keep= skip= interactive= resolved= binary= rebasing= |
3f0a8f3c | 127 | resolvemsg= resume= |
67dad687 | 128 | git_apply_opt= |
d1c5f2a4 | 129 | |
822f7c73 | 130 | while test $# != 0 |
d1c5f2a4 JH |
131 | do |
132 | case "$1" in | |
78443d90 PH |
133 | -i|--interactive) |
134 | interactive=t ;; | |
135 | -b|--binary) | |
136 | binary=t ;; | |
137 | -3|--3way) | |
138 | threeway=t ;; | |
dfdd7e66 | 139 | -s|--signoff) |
78443d90 PH |
140 | sign=t ;; |
141 | -u|--utf8) | |
142 | utf8=t ;; # this is now default | |
143 | --no-utf8) | |
144 | utf8= ;; | |
145 | -k|--keep) | |
146 | keep=t ;; | |
147 | -r|--resolved) | |
148 | resolved=t ;; | |
149 | --skip) | |
150 | skip=t ;; | |
3041c324 JH |
151 | --rebasing) |
152 | rebasing=t threeway=t keep=t binary=t ;; | |
78443d90 | 153 | -d|--dotest) |
e72c7406 JH |
154 | die "-d option is no longer supported. Do not use." |
155 | ;; | |
78443d90 PH |
156 | --resolvemsg) |
157 | shift; resolvemsg=$1 ;; | |
158 | --whitespace) | |
159 | git_apply_opt="$git_apply_opt $1=$2"; shift ;; | |
160 | -C|-p) | |
161 | git_apply_opt="$git_apply_opt $1$2"; shift ;; | |
d1c5f2a4 | 162 | --) |
78443d90 | 163 | shift; break ;; |
d1c5f2a4 | 164 | *) |
78443d90 | 165 | usage ;; |
d1c5f2a4 | 166 | esac |
78443d90 | 167 | shift |
d1c5f2a4 JH |
168 | done |
169 | ||
0c15cc92 JH |
170 | # If the dotest directory exists, but we have finished applying all the |
171 | # patches in them, clear it out. | |
d1c5f2a4 JH |
172 | if test -d "$dotest" && |
173 | last=$(cat "$dotest/last") && | |
174 | next=$(cat "$dotest/next") && | |
175 | test $# != 0 && | |
176 | test "$next" -gt "$last" | |
177 | then | |
178 | rm -fr "$dotest" | |
179 | fi | |
180 | ||
181 | if test -d "$dotest" | |
182 | then | |
c95b1389 JH |
183 | case "$#,$skip$resolved" in |
184 | 0,*t*) | |
185 | # Explicit resume command and we do not have file, so | |
186 | # we are happy. | |
187 | : ;; | |
188 | 0,) | |
189 | # No file input but without resume parameters; catch | |
190 | # user error to feed us a patch from standard input | |
e72c7406 | 191 | # when there is already $dotest. This is somewhat |
c95b1389 JH |
192 | # unreliable -- stdin could be /dev/null for example |
193 | # and the caller did not intend to feed us a patch but | |
194 | # wanted to continue unattended. | |
195 | tty -s | |
196 | ;; | |
197 | *) | |
198 | false | |
199 | ;; | |
200 | esac || | |
201 | die "previous dotest directory $dotest still exists but mbox given." | |
271440e3 | 202 | resume=yes |
d1c5f2a4 | 203 | else |
0c15cc92 JH |
204 | # Make sure we are not given --skip nor --resolved |
205 | test ",$skip,$resolved," = ,,, || | |
cc120056 | 206 | die "Resolve operation not in progress, we are not resuming." |
d1c5f2a4 JH |
207 | |
208 | # Start afresh. | |
209 | mkdir -p "$dotest" || exit | |
210 | ||
bb034f83 JH |
211 | if test -n "$prefix" && test $# != 0 |
212 | then | |
213 | first=t | |
214 | for arg | |
215 | do | |
216 | test -n "$first" && { | |
217 | set x | |
218 | first= | |
219 | } | |
220 | case "$arg" in | |
221 | /*) | |
222 | set "$@" "$arg" ;; | |
223 | *) | |
224 | set "$@" "$prefix$arg" ;; | |
225 | esac | |
226 | done | |
227 | shift | |
228 | fi | |
5be60078 | 229 | git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || { |
d1c5f2a4 JH |
230 | rm -fr "$dotest" |
231 | exit 1 | |
232 | } | |
233 | ||
8c31cb82 JH |
234 | # -b, -s, -u, -k and --whitespace flags are kept for the |
235 | # resuming session after a patch failure. | |
0c15cc92 | 236 | # -3 and -i can and must be given when resuming. |
087b6742 | 237 | echo "$binary" >"$dotest/binary" |
8c31cb82 | 238 | echo " $ws" >"$dotest/whitespace" |
d1c5f2a4 JH |
239 | echo "$sign" >"$dotest/sign" |
240 | echo "$utf8" >"$dotest/utf8" | |
241 | echo "$keep" >"$dotest/keep" | |
d1c5f2a4 | 242 | echo 1 >"$dotest/next" |
3041c324 JH |
243 | if test -n "$rebasing" |
244 | then | |
245 | : >"$dotest/rebasing" | |
246 | else | |
247 | : >"$dotest/applying" | |
248 | fi | |
d1c5f2a4 JH |
249 | fi |
250 | ||
0c15cc92 JH |
251 | case "$resolved" in |
252 | '') | |
38762c47 | 253 | files=$(git diff-index --cached --name-only HEAD --) || exit |
0c15cc92 JH |
254 | if [ "$files" ]; then |
255 | echo "Dirty index: cannot apply patches (dirty: $files)" >&2 | |
256 | exit 1 | |
257 | fi | |
258 | esac | |
259 | ||
087b6742 JH |
260 | if test "$(cat "$dotest/binary")" = t |
261 | then | |
262 | binary=--allow-binary-replacement | |
263 | fi | |
d1c5f2a4 JH |
264 | if test "$(cat "$dotest/utf8")" = t |
265 | then | |
266 | utf8=-u | |
bb1091a4 JH |
267 | else |
268 | utf8=-n | |
d1c5f2a4 JH |
269 | fi |
270 | if test "$(cat "$dotest/keep")" = t | |
271 | then | |
272 | keep=-k | |
273 | fi | |
8c31cb82 | 274 | ws=`cat "$dotest/whitespace"` |
d1c5f2a4 JH |
275 | if test "$(cat "$dotest/sign")" = t |
276 | then | |
277 | SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e ' | |
278 | s/>.*/>/ | |
279 | s/^/Signed-off-by: /' | |
280 | ` | |
281 | else | |
282 | SIGNOFF= | |
283 | fi | |
d1c5f2a4 JH |
284 | |
285 | last=`cat "$dotest/last"` | |
286 | this=`cat "$dotest/next"` | |
287 | if test "$skip" = t | |
288 | then | |
b4372ef1 | 289 | git rerere clear |
d1c5f2a4 | 290 | this=`expr "$this" + 1` |
69224716 | 291 | resume= |
d1c5f2a4 JH |
292 | fi |
293 | ||
294 | if test "$this" -gt "$last" | |
295 | then | |
296 | echo Nothing to do. | |
297 | rm -fr "$dotest" | |
298 | exit | |
299 | fi | |
300 | ||
301 | while test "$this" -le "$last" | |
302 | do | |
303 | msgnum=`printf "%0${prec}d" $this` | |
304 | next=`expr "$this" + 1` | |
305 | test -f "$dotest/$msgnum" || { | |
69224716 | 306 | resume= |
d1c5f2a4 JH |
307 | go_next |
308 | continue | |
309 | } | |
0c15cc92 JH |
310 | |
311 | # If we are not resuming, parse and extract the patch information | |
312 | # into separate files: | |
313 | # - info records the authorship and title | |
314 | # - msg is the rest of commit log message | |
315 | # - patch is the patch body. | |
316 | # | |
317 | # When we are resuming, these files are either already prepared | |
318 | # by the user, or the user can tell us to do so by --resolved flag. | |
271440e3 JH |
319 | case "$resume" in |
320 | '') | |
5be60078 | 321 | git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \ |
271440e3 JH |
322 | <"$dotest/$msgnum" >"$dotest/info" || |
323 | stop_here $this | |
ca193cf1 JS |
324 | |
325 | # skip pine's internal folder data | |
326 | grep '^Author: Mail System Internal Data$' \ | |
327 | <"$dotest"/info >/dev/null && | |
328 | go_next && continue | |
329 | ||
87ab7992 | 330 | test -s $dotest/patch || { |
6777c380 | 331 | echo "Patch is empty. Was it split wrong?" |
87ab7992 DZ |
332 | stop_here $this |
333 | } | |
5be60078 | 334 | git stripspace < "$dotest/msg" > "$dotest/msg-clean" |
271440e3 JH |
335 | ;; |
336 | esac | |
d1c5f2a4 JH |
337 | |
338 | GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" | |
339 | GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" | |
340 | GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" | |
e0e3ba20 | 341 | |
3b78959e | 342 | if test -z "$GIT_AUTHOR_EMAIL" |
e0e3ba20 | 343 | then |
3b78959e | 344 | echo "Patch does not have a valid e-mail address." |
e0e3ba20 JH |
345 | stop_here $this |
346 | fi | |
347 | ||
2c674191 | 348 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE |
d1c5f2a4 | 349 | |
4bfb6b62 | 350 | SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" |
d1c5f2a4 | 351 | case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac |
4bfb6b62 JH |
352 | |
353 | case "$resume" in | |
354 | '') | |
355 | if test '' != "$SIGNOFF" | |
356 | then | |
d1c5f2a4 | 357 | LAST_SIGNED_OFF_BY=` |
4bfb6b62 JH |
358 | sed -ne '/^Signed-off-by: /p' \ |
359 | "$dotest/msg-clean" | | |
360 | tail -n 1 | |
d1c5f2a4 | 361 | ` |
4bfb6b62 JH |
362 | ADD_SIGNOFF=` |
363 | test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { | |
d1c5f2a4 JH |
364 | test '' = "$LAST_SIGNED_OFF_BY" && echo |
365 | echo "$SIGNOFF" | |
4bfb6b62 JH |
366 | }` |
367 | else | |
d1c5f2a4 | 368 | ADD_SIGNOFF= |
4bfb6b62 JH |
369 | fi |
370 | { | |
4b7cc26a | 371 | printf '%s\n' "$SUBJECT" |
d1c5f2a4 JH |
372 | if test -s "$dotest/msg-clean" |
373 | then | |
374 | echo | |
375 | cat "$dotest/msg-clean" | |
376 | fi | |
377 | if test '' != "$ADD_SIGNOFF" | |
378 | then | |
379 | echo "$ADD_SIGNOFF" | |
380 | fi | |
4bfb6b62 JH |
381 | } >"$dotest/final-commit" |
382 | ;; | |
0c15cc92 | 383 | *) |
6d28644d | 384 | case "$resolved$interactive" in |
0c15cc92 JH |
385 | tt) |
386 | # This is used only for interactive view option. | |
38762c47 | 387 | git diff-index -p --cached HEAD -- >"$dotest/patch" |
0c15cc92 JH |
388 | ;; |
389 | esac | |
4bfb6b62 | 390 | esac |
d1c5f2a4 | 391 | |
4bfb6b62 | 392 | resume= |
d1c5f2a4 JH |
393 | if test "$interactive" = t |
394 | then | |
a1451104 JH |
395 | test -t 0 || |
396 | die "cannot be interactive without stdin connected to a terminal." | |
d1c5f2a4 JH |
397 | action=again |
398 | while test "$action" = again | |
399 | do | |
400 | echo "Commit Body is:" | |
401 | echo "--------------------------" | |
402 | cat "$dotest/final-commit" | |
403 | echo "--------------------------" | |
9754563c | 404 | printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " |
d1c5f2a4 JH |
405 | read reply |
406 | case "$reply" in | |
f89ad67f JH |
407 | [yY]*) action=yes ;; |
408 | [aA]*) action=yes interactive= ;; | |
409 | [nN]*) action=skip ;; | |
ef0c2abf | 410 | [eE]*) git_editor "$dotest/final-commit" |
f23272f3 | 411 | SUBJECT=$(reread_subject "$dotest/final-commit") |
d1c5f2a4 | 412 | action=again ;; |
f89ad67f JH |
413 | [vV]*) action=again |
414 | LESS=-S ${PAGER:-less} "$dotest/patch" ;; | |
415 | *) action=again ;; | |
d1c5f2a4 JH |
416 | esac |
417 | done | |
418 | else | |
419 | action=yes | |
420 | fi | |
421 | ||
422 | if test $action = skip | |
423 | then | |
424 | go_next | |
425 | continue | |
426 | fi | |
427 | ||
428 | if test -x "$GIT_DIR"/hooks/applypatch-msg | |
429 | then | |
430 | "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || | |
431 | stop_here $this | |
432 | fi | |
433 | ||
4b7cc26a | 434 | printf 'Applying %s\n' "$SUBJECT" |
d1c5f2a4 | 435 | |
0c15cc92 JH |
436 | case "$resolved" in |
437 | '') | |
5be60078 | 438 | git apply $git_apply_opt $binary --index "$dotest/patch" |
0c15cc92 JH |
439 | apply_status=$? |
440 | ;; | |
441 | t) | |
442 | # Resolved means the user did all the hard work, and | |
443 | # we do not have to do any patch application. Just | |
444 | # trust what the user has in the index file and the | |
445 | # working tree. | |
446 | resolved= | |
38762c47 | 447 | git diff-index --quiet --cached HEAD -- && { |
64646d11 | 448 | echo "No changes - did you forget to use 'git add'?" |
ced9456a | 449 | stop_here_user_resolve $this |
06aff47b | 450 | } |
5be60078 | 451 | unmerged=$(git ls-files -u) |
c1d1128b JH |
452 | if test -n "$unmerged" |
453 | then | |
454 | echo "You still have unmerged paths in your index" | |
64646d11 | 455 | echo "did you forget to use 'git add'?" |
ced9456a | 456 | stop_here_user_resolve $this |
c1d1128b | 457 | fi |
0c15cc92 | 458 | apply_status=0 |
b4372ef1 | 459 | git rerere |
0c15cc92 JH |
460 | ;; |
461 | esac | |
462 | ||
d1c5f2a4 JH |
463 | if test $apply_status = 1 && test "$threeway" = t |
464 | then | |
73319032 | 465 | if (fall_back_3way) |
d1c5f2a4 | 466 | then |
73319032 JH |
467 | # Applying the patch to an earlier tree and merging the |
468 | # result may have produced the same tree as ours. | |
38762c47 | 469 | git diff-index --quiet --cached HEAD -- && { |
06aff47b AR |
470 | echo No changes -- Patch already applied. |
471 | go_next | |
472 | continue | |
473 | } | |
73319032 JH |
474 | # clear apply_status -- we have successfully merged. |
475 | apply_status=0 | |
d1c5f2a4 JH |
476 | fi |
477 | fi | |
478 | if test $apply_status != 0 | |
479 | then | |
480 | echo Patch failed at $msgnum. | |
ced9456a | 481 | stop_here_user_resolve $this |
d1c5f2a4 JH |
482 | fi |
483 | ||
484 | if test -x "$GIT_DIR"/hooks/pre-applypatch | |
485 | then | |
486 | "$GIT_DIR"/hooks/pre-applypatch || stop_here $this | |
487 | fi | |
488 | ||
5be60078 | 489 | tree=$(git write-tree) && |
5be60078 JH |
490 | parent=$(git rev-parse --verify HEAD) && |
491 | commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") && | |
5be60078 | 492 | git update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent || |
d1c5f2a4 JH |
493 | stop_here $this |
494 | ||
495 | if test -x "$GIT_DIR"/hooks/post-applypatch | |
496 | then | |
497 | "$GIT_DIR"/hooks/post-applypatch | |
498 | fi | |
499 | ||
500 | go_next | |
501 | done | |
502 | ||
e0cd252e MS |
503 | git gc --auto |
504 | ||
d1c5f2a4 | 505 | rm -fr "$dotest" |