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