]>
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="\ | |
588c038a | 8 | git am [options] [<mbox>|<Maildir>...] |
09f8d055 | 9 | git am [options] (--resolved | --skip | --abort) |
78443d90 | 10 | -- |
66006c63 | 11 | i,interactive run interactively |
98ef23b3 | 12 | b,binary* (historical option -- no-op) |
78443d90 | 13 | 3,3way allow fall back on 3way merging if needed |
0e987a12 | 14 | q,quiet be quiet |
78443d90 PH |
15 | s,signoff add a Signed-off-by line to the commit message |
16 | u,utf8 recode into utf8 (default) | |
fe1fa946 | 17 | k,keep pass -k flag to git-mailinfo |
017678b4 | 18 | c,scissors strip everything before a scissors line |
78443d90 | 19 | whitespace= pass it through git-apply |
86c91f91 GB |
20 | ignore-space-change pass it through git-apply |
21 | ignore-whitespace pass it through git-apply | |
b47dfe9e | 22 | directory= pass it through git-apply |
78443d90 PH |
23 | C= pass it through git-apply |
24 | p= pass it through git-apply | |
a5a6755a | 25 | patch-format= format the patch(es) are in |
b80da424 | 26 | reject pass it through git-apply |
78443d90 PH |
27 | resolvemsg= override error message when patch failure occurs |
28 | r,resolved to be used after a patch failure | |
3041c324 | 29 | skip skip the current patch |
3e5057a8 | 30 | abort restore the original branch and abort the patching operation. |
3f01ad66 | 31 | committer-date-is-author-date lie about committer date |
a79ec62d | 32 | ignore-date use current timestamp for author date |
cb6020bb | 33 | rerere-autoupdate update the index with reused conflict resolution if possible |
98ef23b3 | 34 | rebasing* (internal use for git-rebase)" |
78443d90 | 35 | |
cf1fe88c | 36 | . git-sh-setup |
bb034f83 | 37 | prefix=$(git rev-parse --show-prefix) |
f9474132 | 38 | set_reflog_action am |
7eff28a9 | 39 | require_work_tree |
c1491841 | 40 | cd_to_toplevel |
d1c5f2a4 | 41 | |
460abee5 SB |
42 | git var GIT_COMMITTER_IDENT >/dev/null || |
43 | die "You need to set your committer info first" | |
d64e6b04 | 44 | |
f79d4c8a NS |
45 | if git rev-parse --verify -q HEAD >/dev/null |
46 | then | |
47 | HAS_HEAD=yes | |
48 | else | |
49 | HAS_HEAD= | |
50 | fi | |
51 | ||
b47dfe9e | 52 | sq () { |
47c9739e | 53 | git rev-parse --sq-quote "$@" |
b47dfe9e JH |
54 | } |
55 | ||
d1c5f2a4 JH |
56 | stop_here () { |
57 | echo "$1" >"$dotest/next" | |
58 | exit 1 | |
59 | } | |
60 | ||
ced9456a | 61 | stop_here_user_resolve () { |
cc120056 | 62 | if [ -n "$resolvemsg" ]; then |
a23bfaed | 63 | printf '%s\n' "$resolvemsg" |
cc120056 SE |
64 | stop_here $1 |
65 | fi | |
75e1645f | 66 | cmdline="git am" |
ced9456a RS |
67 | if test '' != "$interactive" |
68 | then | |
69 | cmdline="$cmdline -i" | |
70 | fi | |
71 | if test '' != "$threeway" | |
72 | then | |
73 | cmdline="$cmdline -3" | |
74 | fi | |
ced9456a RS |
75 | echo "When you have resolved this problem run \"$cmdline --resolved\"." |
76 | echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"." | |
3e5057a8 | 77 | echo "To restore the original branch and stop patching run \"$cmdline --abort\"." |
ced9456a RS |
78 | |
79 | stop_here $1 | |
80 | } | |
81 | ||
d1c5f2a4 JH |
82 | go_next () { |
83 | rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \ | |
84 | "$dotest/patch" "$dotest/info" | |
85 | echo "$next" >"$dotest/next" | |
86 | this=$next | |
87 | } | |
88 | ||
fd7bcfb5 JH |
89 | cannot_fallback () { |
90 | echo "$1" | |
91 | echo "Cannot fall back to three-way merge." | |
92 | exit 1 | |
93 | } | |
94 | ||
d1c5f2a4 JH |
95 | fall_back_3way () { |
96 | O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd` | |
97 | ||
98 | rm -fr "$dotest"/patch-merge-* | |
99 | mkdir "$dotest/patch-merge-tmp-dir" | |
100 | ||
101 | # First see if the patch records the index info that we can use. | |
7a988699 JS |
102 | git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \ |
103 | "$dotest/patch" && | |
fd7bcfb5 | 104 | GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ |
5be60078 | 105 | git write-tree >"$dotest/patch-merge-base+" || |
b1440cc8 | 106 | cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge." |
fd7bcfb5 | 107 | |
0e987a12 | 108 | say Using index info to reconstruct a base tree... |
fd7bcfb5 | 109 | if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ |
cb3a160d | 110 | git apply --cached <"$dotest/patch" |
d1c5f2a4 | 111 | then |
d1c5f2a4 JH |
112 | mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base" |
113 | mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index" | |
fd7bcfb5 JH |
114 | else |
115 | cannot_fallback "Did you hand edit your patch? | |
116 | It does not apply to blobs recorded in its index." | |
d1c5f2a4 JH |
117 | fi |
118 | ||
119 | test -f "$dotest/patch-merge-index" && | |
5be60078 | 120 | his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) && |
d1c5f2a4 JH |
121 | orig_tree=$(cat "$dotest/patch-merge-base") && |
122 | rm -fr "$dotest"/patch-merge-* || exit 1 | |
123 | ||
0e987a12 | 124 | say Falling back to patching base and 3-way merge... |
d1c5f2a4 JH |
125 | |
126 | # This is not so wrong. Depending on which base we picked, | |
127 | # orig_tree may be wildly different from ours, but his_tree | |
128 | # has the same set of wildly different changes in parts the | |
579c9bb1 | 129 | # patch did not touch, so recursive ends up canceling them, |
d1c5f2a4 JH |
130 | # saying that we reverted all those changes. |
131 | ||
2e6e3e82 | 132 | eval GITHEAD_$his_tree='"$FIRSTLINE"' |
579c9bb1 | 133 | export GITHEAD_$his_tree |
0e987a12 SB |
134 | if test -n "$GIT_QUIET" |
135 | then | |
136 | export GIT_MERGE_VERBOSITY=0 | |
137 | fi | |
579c9bb1 | 138 | git-merge-recursive $orig_tree -- HEAD $his_tree || { |
cb6020bb | 139 | git rerere $allow_rerere_autoupdate |
d1c5f2a4 JH |
140 | echo Failed to merge in the changes. |
141 | exit 1 | |
142 | } | |
579c9bb1 | 143 | unset GITHEAD_$his_tree |
d1c5f2a4 JH |
144 | } |
145 | ||
0cd29a03 GB |
146 | clean_abort () { |
147 | test $# = 0 || echo >&2 "$@" | |
148 | rm -fr "$dotest" | |
149 | exit 1 | |
150 | } | |
151 | ||
a5a6755a GB |
152 | patch_format= |
153 | ||
154 | check_patch_format () { | |
155 | # early return if patch_format was set from the command line | |
156 | if test -n "$patch_format" | |
157 | then | |
158 | return 0 | |
159 | fi | |
15ced753 GB |
160 | |
161 | # we default to mbox format if input is from stdin and for | |
162 | # directories | |
163 | if test $# = 0 || test "x$1" = "x-" || test -d "$1" | |
164 | then | |
165 | patch_format=mbox | |
166 | return 0 | |
167 | fi | |
168 | ||
169 | # otherwise, check the first few lines of the first patch to try | |
170 | # to detect its format | |
171 | { | |
172 | read l1 | |
173 | read l2 | |
174 | read l3 | |
175 | case "$l1" in | |
176 | "From "* | "From: "*) | |
177 | patch_format=mbox | |
178 | ;; | |
179 | '# This series applies on GIT commit'*) | |
180 | patch_format=stgit-series | |
181 | ;; | |
182 | "# HG changeset patch") | |
183 | patch_format=hg | |
184 | ;; | |
185 | *) | |
186 | # if the second line is empty and the third is | |
187 | # a From, Author or Date entry, this is very | |
188 | # likely an StGIT patch | |
189 | case "$l2,$l3" in | |
190 | ,"From: "* | ,"Author: "* | ,"Date: "*) | |
191 | patch_format=stgit | |
192 | ;; | |
193 | *) | |
194 | ;; | |
195 | esac | |
196 | ;; | |
197 | esac | |
0fcb2caf JH |
198 | if test -z "$patch_format" && |
199 | test -n "$l1" && | |
200 | test -n "$l2" && | |
201 | test -n "$l3" | |
202 | then | |
203 | # This begins with three non-empty lines. Is this a | |
204 | # piece of e-mail a-la RFC2822? Grab all the headers, | |
205 | # discarding the indented remainder of folded lines, | |
206 | # and see if it looks like that they all begin with the | |
207 | # header field names... | |
e3f67d30 SB |
208 | tr -d '\015' <"$1" | |
209 | sed -n -e '/^$/q' -e '/^[ ]/d' -e p | | |
e1622bfc | 210 | sane_egrep -v '^[!-9;-~]+:' >/dev/null || |
0fcb2caf JH |
211 | patch_format=mbox |
212 | fi | |
0cd29a03 | 213 | } < "$1" || clean_abort |
a5a6755a GB |
214 | } |
215 | ||
216 | split_patches () { | |
217 | case "$patch_format" in | |
218 | mbox) | |
c2ca1d79 JH |
219 | case "$rebasing" in |
220 | '') | |
221 | keep_cr= ;; | |
222 | ?*) | |
223 | keep_cr=--keep-cr ;; | |
224 | esac | |
225 | git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" || | |
0cd29a03 | 226 | clean_abort |
a5a6755a | 227 | ;; |
c574e683 GB |
228 | stgit-series) |
229 | if test $# -ne 1 | |
230 | then | |
0cd29a03 | 231 | clean_abort "Only one StGIT patch series can be applied at once" |
c574e683 GB |
232 | fi |
233 | series_dir=`dirname "$1"` | |
234 | series_file="$1" | |
235 | shift | |
236 | { | |
237 | set x | |
238 | while read filename | |
239 | do | |
240 | set "$@" "$series_dir/$filename" | |
241 | done | |
242 | # remove the safety x | |
243 | shift | |
244 | # remove the arg coming from the first-line comment | |
245 | shift | |
0cd29a03 | 246 | } < "$series_file" || clean_abort |
c574e683 GB |
247 | # set the patch format appropriately |
248 | patch_format=stgit | |
249 | # now handle the actual StGIT patches | |
250 | split_patches "$@" | |
251 | ;; | |
252 | stgit) | |
253 | this=0 | |
254 | for stgit in "$@" | |
255 | do | |
256 | this=`expr "$this" + 1` | |
257 | msgnum=`printf "%0${prec}d" $this` | |
258 | # Perl version of StGIT parse_patch. The first nonemptyline | |
259 | # not starting with Author, From or Date is the | |
260 | # subject, and the body starts with the next nonempty | |
261 | # line not starting with Author, From or Date | |
262 | perl -ne 'BEGIN { $subject = 0 } | |
263 | if ($subject > 1) { print ; } | |
264 | elsif (/^\s+$/) { next ; } | |
265 | elsif (/^Author:/) { print s/Author/From/ ; } | |
266 | elsif (/^(From|Date)/) { print ; } | |
267 | elsif ($subject) { | |
268 | $subject = 2 ; | |
269 | print "\n" ; | |
270 | print ; | |
271 | } else { | |
272 | print "Subject: ", $_ ; | |
273 | $subject = 1; | |
274 | } | |
0cd29a03 | 275 | ' < "$stgit" > "$dotest/$msgnum" || clean_abort |
c574e683 GB |
276 | done |
277 | echo "$this" > "$dotest/last" | |
278 | this= | |
279 | msgnum= | |
280 | ;; | |
a5a6755a | 281 | *) |
46caf505 NS |
282 | if test -n "$parse_patch" ; then |
283 | clean_abort "Patch format $patch_format is not supported." | |
284 | else | |
285 | clean_abort "Patch format detection failed." | |
286 | fi | |
a5a6755a GB |
287 | ;; |
288 | esac | |
289 | } | |
290 | ||
d1c5f2a4 | 291 | prec=4 |
51ef1daa | 292 | dotest="$GIT_DIR/rebase-apply" |
a2003379 | 293 | sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= |
d25e5159 | 294 | resolvemsg= resume= scissors= no_inbody_headers= |
67dad687 | 295 | git_apply_opt= |
3f01ad66 | 296 | committer_date_is_author_date= |
a79ec62d | 297 | ignore_date= |
cb6020bb | 298 | allow_rerere_autoupdate= |
d1c5f2a4 | 299 | |
822f7c73 | 300 | while test $# != 0 |
d1c5f2a4 JH |
301 | do |
302 | case "$1" in | |
78443d90 PH |
303 | -i|--interactive) |
304 | interactive=t ;; | |
305 | -b|--binary) | |
cb3a160d | 306 | : ;; |
78443d90 PH |
307 | -3|--3way) |
308 | threeway=t ;; | |
dfdd7e66 | 309 | -s|--signoff) |
78443d90 PH |
310 | sign=t ;; |
311 | -u|--utf8) | |
312 | utf8=t ;; # this is now default | |
313 | --no-utf8) | |
314 | utf8= ;; | |
315 | -k|--keep) | |
316 | keep=t ;; | |
017678b4 JH |
317 | -c|--scissors) |
318 | scissors=t ;; | |
319 | --no-scissors) | |
320 | scissors=f ;; | |
78443d90 PH |
321 | -r|--resolved) |
322 | resolved=t ;; | |
323 | --skip) | |
324 | skip=t ;; | |
3e5057a8 NS |
325 | --abort) |
326 | abort=t ;; | |
3041c324 | 327 | --rebasing) |
d25e5159 | 328 | rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;; |
78443d90 | 329 | -d|--dotest) |
e72c7406 JH |
330 | die "-d option is no longer supported. Do not use." |
331 | ;; | |
78443d90 PH |
332 | --resolvemsg) |
333 | shift; resolvemsg=$1 ;; | |
b47dfe9e JH |
334 | --whitespace|--directory) |
335 | git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;; | |
78443d90 | 336 | -C|-p) |
b47dfe9e | 337 | git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;; |
a5a6755a GB |
338 | --patch-format) |
339 | shift ; patch_format="$1" ;; | |
86c91f91 | 340 | --reject|--ignore-whitespace|--ignore-space-change) |
b80da424 | 341 | git_apply_opt="$git_apply_opt $1" ;; |
3f01ad66 JH |
342 | --committer-date-is-author-date) |
343 | committer_date_is_author_date=t ;; | |
a79ec62d NS |
344 | --ignore-date) |
345 | ignore_date=t ;; | |
cb6020bb JH |
346 | --rerere-autoupdate|--no-rerere-autoupdate) |
347 | allow_rerere_autoupdate="$1" ;; | |
0e987a12 SB |
348 | -q|--quiet) |
349 | GIT_QUIET=t ;; | |
d1c5f2a4 | 350 | --) |
78443d90 | 351 | shift; break ;; |
d1c5f2a4 | 352 | *) |
78443d90 | 353 | usage ;; |
d1c5f2a4 | 354 | esac |
78443d90 | 355 | shift |
d1c5f2a4 JH |
356 | done |
357 | ||
0c15cc92 JH |
358 | # If the dotest directory exists, but we have finished applying all the |
359 | # patches in them, clear it out. | |
d1c5f2a4 JH |
360 | if test -d "$dotest" && |
361 | last=$(cat "$dotest/last") && | |
362 | next=$(cat "$dotest/next") && | |
363 | test $# != 0 && | |
364 | test "$next" -gt "$last" | |
365 | then | |
366 | rm -fr "$dotest" | |
367 | fi | |
368 | ||
369 | if test -d "$dotest" | |
370 | then | |
3e5057a8 | 371 | case "$#,$skip$resolved$abort" in |
c95b1389 JH |
372 | 0,*t*) |
373 | # Explicit resume command and we do not have file, so | |
374 | # we are happy. | |
375 | : ;; | |
376 | 0,) | |
377 | # No file input but without resume parameters; catch | |
378 | # user error to feed us a patch from standard input | |
e72c7406 | 379 | # when there is already $dotest. This is somewhat |
c95b1389 JH |
380 | # unreliable -- stdin could be /dev/null for example |
381 | # and the caller did not intend to feed us a patch but | |
382 | # wanted to continue unattended. | |
98ef23b3 | 383 | test -t 0 |
c95b1389 JH |
384 | ;; |
385 | *) | |
386 | false | |
387 | ;; | |
388 | esac || | |
28ed6e7b | 389 | die "previous rebase directory $dotest still exists but mbox given." |
271440e3 | 390 | resume=yes |
3e5057a8 | 391 | |
95f8ebbf | 392 | case "$skip,$abort" in |
2d56a136 JH |
393 | t,t) |
394 | die "Please make up your mind. --skip or --abort?" | |
395 | ;; | |
95f8ebbf OM |
396 | t,) |
397 | git rerere clear | |
398 | git read-tree --reset -u HEAD HEAD | |
399 | orig_head=$(cat "$GIT_DIR/ORIG_HEAD") | |
400 | git reset HEAD | |
401 | git update-ref ORIG_HEAD $orig_head | |
402 | ;; | |
403 | ,t) | |
2d56a136 JH |
404 | if test -f "$dotest/rebasing" |
405 | then | |
406 | exec git rebase --abort | |
407 | fi | |
3e5057a8 | 408 | git rerere clear |
c767184d MG |
409 | test -f "$dotest/dirtyindex" || { |
410 | git read-tree --reset -u HEAD ORIG_HEAD | |
411 | git reset ORIG_HEAD | |
412 | } | |
3e5057a8 NS |
413 | rm -fr "$dotest" |
414 | exit ;; | |
415 | esac | |
c767184d | 416 | rm -f "$dotest/dirtyindex" |
d1c5f2a4 | 417 | else |
3e5057a8 NS |
418 | # Make sure we are not given --skip, --resolved, nor --abort |
419 | test "$skip$resolved$abort" = "" || | |
cc120056 | 420 | die "Resolve operation not in progress, we are not resuming." |
d1c5f2a4 JH |
421 | |
422 | # Start afresh. | |
423 | mkdir -p "$dotest" || exit | |
424 | ||
bb034f83 JH |
425 | if test -n "$prefix" && test $# != 0 |
426 | then | |
427 | first=t | |
428 | for arg | |
429 | do | |
430 | test -n "$first" && { | |
431 | set x | |
432 | first= | |
433 | } | |
434 | case "$arg" in | |
435 | /*) | |
436 | set "$@" "$arg" ;; | |
437 | *) | |
438 | set "$@" "$prefix$arg" ;; | |
439 | esac | |
440 | done | |
441 | shift | |
442 | fi | |
a5a6755a GB |
443 | |
444 | check_patch_format "$@" | |
445 | ||
446 | split_patches "$@" | |
d1c5f2a4 | 447 | |
017678b4 JH |
448 | # -i can and must be given when resuming; everything |
449 | # else is kept | |
9b9f5a22 | 450 | echo " $git_apply_opt" >"$dotest/apply-opt" |
22db240d | 451 | echo "$threeway" >"$dotest/threeway" |
d1c5f2a4 JH |
452 | echo "$sign" >"$dotest/sign" |
453 | echo "$utf8" >"$dotest/utf8" | |
454 | echo "$keep" >"$dotest/keep" | |
017678b4 | 455 | echo "$scissors" >"$dotest/scissors" |
d25e5159 | 456 | echo "$no_inbody_headers" >"$dotest/no_inbody_headers" |
0e987a12 | 457 | echo "$GIT_QUIET" >"$dotest/quiet" |
d1c5f2a4 | 458 | echo 1 >"$dotest/next" |
3041c324 JH |
459 | if test -n "$rebasing" |
460 | then | |
461 | : >"$dotest/rebasing" | |
462 | else | |
463 | : >"$dotest/applying" | |
f79d4c8a NS |
464 | if test -n "$HAS_HEAD" |
465 | then | |
466 | git update-ref ORIG_HEAD HEAD | |
467 | else | |
468 | git update-ref -d ORIG_HEAD >/dev/null 2>&1 | |
469 | fi | |
3041c324 | 470 | fi |
d1c5f2a4 JH |
471 | fi |
472 | ||
0c15cc92 JH |
473 | case "$resolved" in |
474 | '') | |
f79d4c8a NS |
475 | case "$HAS_HEAD" in |
476 | '') | |
477 | files=$(git ls-files) ;; | |
478 | ?*) | |
479 | files=$(git diff-index --cached --name-only HEAD --) ;; | |
480 | esac || exit | |
c767184d MG |
481 | if test "$files" |
482 | then | |
f79d4c8a | 483 | test -n "$HAS_HEAD" && : >"$dotest/dirtyindex" |
c767184d MG |
484 | die "Dirty index: cannot apply patches (dirty: $files)" |
485 | fi | |
0c15cc92 JH |
486 | esac |
487 | ||
d1c5f2a4 JH |
488 | if test "$(cat "$dotest/utf8")" = t |
489 | then | |
490 | utf8=-u | |
bb1091a4 JH |
491 | else |
492 | utf8=-n | |
d1c5f2a4 JH |
493 | fi |
494 | if test "$(cat "$dotest/keep")" = t | |
495 | then | |
496 | keep=-k | |
497 | fi | |
017678b4 JH |
498 | case "$(cat "$dotest/scissors")" in |
499 | t) | |
500 | scissors=--scissors ;; | |
501 | f) | |
502 | scissors=--no-scissors ;; | |
503 | esac | |
d25e5159 LS |
504 | if test "$(cat "$dotest/no_inbody_headers")" = t |
505 | then | |
506 | no_inbody_headers=--no-inbody-headers | |
507 | else | |
508 | no_inbody_headers= | |
509 | fi | |
0e987a12 SB |
510 | if test "$(cat "$dotest/quiet")" = t |
511 | then | |
512 | GIT_QUIET=t | |
513 | fi | |
22db240d JH |
514 | if test "$(cat "$dotest/threeway")" = t |
515 | then | |
516 | threeway=t | |
517 | fi | |
9b9f5a22 | 518 | git_apply_opt=$(cat "$dotest/apply-opt") |
d1c5f2a4 JH |
519 | if test "$(cat "$dotest/sign")" = t |
520 | then | |
5354a56f | 521 | SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e ' |
d1c5f2a4 JH |
522 | s/>.*/>/ |
523 | s/^/Signed-off-by: /' | |
524 | ` | |
525 | else | |
526 | SIGNOFF= | |
527 | fi | |
d1c5f2a4 JH |
528 | |
529 | last=`cat "$dotest/last"` | |
530 | this=`cat "$dotest/next"` | |
531 | if test "$skip" = t | |
532 | then | |
533 | this=`expr "$this" + 1` | |
69224716 | 534 | resume= |
d1c5f2a4 JH |
535 | fi |
536 | ||
537 | if test "$this" -gt "$last" | |
538 | then | |
0e987a12 | 539 | say Nothing to do. |
d1c5f2a4 JH |
540 | rm -fr "$dotest" |
541 | exit | |
542 | fi | |
543 | ||
544 | while test "$this" -le "$last" | |
545 | do | |
546 | msgnum=`printf "%0${prec}d" $this` | |
547 | next=`expr "$this" + 1` | |
548 | test -f "$dotest/$msgnum" || { | |
69224716 | 549 | resume= |
d1c5f2a4 JH |
550 | go_next |
551 | continue | |
552 | } | |
0c15cc92 JH |
553 | |
554 | # If we are not resuming, parse and extract the patch information | |
555 | # into separate files: | |
556 | # - info records the authorship and title | |
557 | # - msg is the rest of commit log message | |
558 | # - patch is the patch body. | |
559 | # | |
560 | # When we are resuming, these files are either already prepared | |
561 | # by the user, or the user can tell us to do so by --resolved flag. | |
271440e3 JH |
562 | case "$resume" in |
563 | '') | |
d25e5159 | 564 | git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \ |
271440e3 JH |
565 | <"$dotest/$msgnum" >"$dotest/info" || |
566 | stop_here $this | |
ca193cf1 JS |
567 | |
568 | # skip pine's internal folder data | |
e1622bfc | 569 | sane_grep '^Author: Mail System Internal Data$' \ |
ca193cf1 JS |
570 | <"$dotest"/info >/dev/null && |
571 | go_next && continue | |
572 | ||
28ed6e7b | 573 | test -s "$dotest/patch" || { |
6777c380 | 574 | echo "Patch is empty. Was it split wrong?" |
87ab7992 DZ |
575 | stop_here $this |
576 | } | |
5e835cac JH |
577 | if test -f "$dotest/rebasing" && |
578 | commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \ | |
579 | -e q "$dotest/$msgnum") && | |
580 | test "$(git cat-file -t "$commit")" = commit | |
581 | then | |
582 | git cat-file commit "$commit" | | |
583 | sed -e '1,/^$/d' >"$dotest/msg-clean" | |
584 | else | |
c970a6fd JH |
585 | { |
586 | sed -n '/^Subject/ s/Subject: //p' "$dotest/info" | |
587 | echo | |
588 | cat "$dotest/msg" | |
589 | } | | |
590 | git stripspace > "$dotest/msg-clean" | |
5e835cac | 591 | fi |
271440e3 JH |
592 | ;; |
593 | esac | |
d1c5f2a4 JH |
594 | |
595 | GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" | |
596 | GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" | |
597 | GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" | |
e0e3ba20 | 598 | |
3b78959e | 599 | if test -z "$GIT_AUTHOR_EMAIL" |
e0e3ba20 | 600 | then |
3b78959e | 601 | echo "Patch does not have a valid e-mail address." |
e0e3ba20 JH |
602 | stop_here $this |
603 | fi | |
604 | ||
2c674191 | 605 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE |
d1c5f2a4 | 606 | |
4bfb6b62 JH |
607 | case "$resume" in |
608 | '') | |
609 | if test '' != "$SIGNOFF" | |
610 | then | |
d1c5f2a4 | 611 | LAST_SIGNED_OFF_BY=` |
4bfb6b62 JH |
612 | sed -ne '/^Signed-off-by: /p' \ |
613 | "$dotest/msg-clean" | | |
b4ce54fc | 614 | sed -ne '$p' |
d1c5f2a4 | 615 | ` |
4bfb6b62 JH |
616 | ADD_SIGNOFF=` |
617 | test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { | |
d1c5f2a4 JH |
618 | test '' = "$LAST_SIGNED_OFF_BY" && echo |
619 | echo "$SIGNOFF" | |
4bfb6b62 JH |
620 | }` |
621 | else | |
d1c5f2a4 | 622 | ADD_SIGNOFF= |
4bfb6b62 JH |
623 | fi |
624 | { | |
d1c5f2a4 JH |
625 | if test -s "$dotest/msg-clean" |
626 | then | |
d1c5f2a4 JH |
627 | cat "$dotest/msg-clean" |
628 | fi | |
629 | if test '' != "$ADD_SIGNOFF" | |
630 | then | |
631 | echo "$ADD_SIGNOFF" | |
632 | fi | |
4bfb6b62 JH |
633 | } >"$dotest/final-commit" |
634 | ;; | |
0c15cc92 | 635 | *) |
6d28644d | 636 | case "$resolved$interactive" in |
0c15cc92 JH |
637 | tt) |
638 | # This is used only for interactive view option. | |
38762c47 | 639 | git diff-index -p --cached HEAD -- >"$dotest/patch" |
0c15cc92 JH |
640 | ;; |
641 | esac | |
4bfb6b62 | 642 | esac |
d1c5f2a4 | 643 | |
4bfb6b62 | 644 | resume= |
d1c5f2a4 JH |
645 | if test "$interactive" = t |
646 | then | |
a1451104 JH |
647 | test -t 0 || |
648 | die "cannot be interactive without stdin connected to a terminal." | |
d1c5f2a4 JH |
649 | action=again |
650 | while test "$action" = again | |
651 | do | |
652 | echo "Commit Body is:" | |
653 | echo "--------------------------" | |
654 | cat "$dotest/final-commit" | |
655 | echo "--------------------------" | |
9754563c | 656 | printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " |
d1c5f2a4 JH |
657 | read reply |
658 | case "$reply" in | |
f89ad67f JH |
659 | [yY]*) action=yes ;; |
660 | [aA]*) action=yes interactive= ;; | |
661 | [nN]*) action=skip ;; | |
ef0c2abf | 662 | [eE]*) git_editor "$dotest/final-commit" |
d1c5f2a4 | 663 | action=again ;; |
f89ad67f | 664 | [vV]*) action=again |
dec543e6 JN |
665 | : ${GIT_PAGER=$(git var GIT_PAGER)} |
666 | : ${LESS=-FRSX} | |
667 | export LESS | |
668 | $GIT_PAGER "$dotest/patch" ;; | |
f89ad67f | 669 | *) action=again ;; |
d1c5f2a4 JH |
670 | esac |
671 | done | |
672 | else | |
673 | action=yes | |
674 | fi | |
1d9b2656 | 675 | FIRSTLINE=$(sed 1q "$dotest/final-commit") |
d1c5f2a4 JH |
676 | |
677 | if test $action = skip | |
678 | then | |
679 | go_next | |
680 | continue | |
681 | fi | |
682 | ||
683 | if test -x "$GIT_DIR"/hooks/applypatch-msg | |
684 | then | |
685 | "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || | |
686 | stop_here $this | |
687 | fi | |
688 | ||
0e987a12 | 689 | say "Applying: $FIRSTLINE" |
d1c5f2a4 | 690 | |
0c15cc92 JH |
691 | case "$resolved" in |
692 | '') | |
3ddd1703 SB |
693 | # When we are allowed to fall back to 3-way later, don't give |
694 | # false errors during the initial attempt. | |
695 | squelch= | |
696 | if test "$threeway" = t | |
697 | then | |
698 | squelch='>/dev/null 2>&1 ' | |
699 | fi | |
700 | eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"' | |
0c15cc92 JH |
701 | apply_status=$? |
702 | ;; | |
703 | t) | |
704 | # Resolved means the user did all the hard work, and | |
705 | # we do not have to do any patch application. Just | |
706 | # trust what the user has in the index file and the | |
707 | # working tree. | |
708 | resolved= | |
38762c47 | 709 | git diff-index --quiet --cached HEAD -- && { |
64646d11 | 710 | echo "No changes - did you forget to use 'git add'?" |
ced9456a | 711 | stop_here_user_resolve $this |
06aff47b | 712 | } |
5be60078 | 713 | unmerged=$(git ls-files -u) |
c1d1128b JH |
714 | if test -n "$unmerged" |
715 | then | |
716 | echo "You still have unmerged paths in your index" | |
64646d11 | 717 | echo "did you forget to use 'git add'?" |
ced9456a | 718 | stop_here_user_resolve $this |
c1d1128b | 719 | fi |
0c15cc92 | 720 | apply_status=0 |
b4372ef1 | 721 | git rerere |
0c15cc92 JH |
722 | ;; |
723 | esac | |
724 | ||
d1c5f2a4 JH |
725 | if test $apply_status = 1 && test "$threeway" = t |
726 | then | |
73319032 | 727 | if (fall_back_3way) |
d1c5f2a4 | 728 | then |
73319032 JH |
729 | # Applying the patch to an earlier tree and merging the |
730 | # result may have produced the same tree as ours. | |
38762c47 | 731 | git diff-index --quiet --cached HEAD -- && { |
0e987a12 | 732 | say No changes -- Patch already applied. |
06aff47b AR |
733 | go_next |
734 | continue | |
735 | } | |
73319032 JH |
736 | # clear apply_status -- we have successfully merged. |
737 | apply_status=0 | |
d1c5f2a4 JH |
738 | fi |
739 | fi | |
740 | if test $apply_status != 0 | |
741 | then | |
4d2e283a | 742 | printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE" |
ced9456a | 743 | stop_here_user_resolve $this |
d1c5f2a4 JH |
744 | fi |
745 | ||
746 | if test -x "$GIT_DIR"/hooks/pre-applypatch | |
747 | then | |
748 | "$GIT_DIR"/hooks/pre-applypatch || stop_here $this | |
749 | fi | |
750 | ||
5be60078 | 751 | tree=$(git write-tree) && |
3f01ad66 | 752 | commit=$( |
a79ec62d NS |
753 | if test -n "$ignore_date" |
754 | then | |
755 | GIT_AUTHOR_DATE= | |
756 | fi | |
f79d4c8a | 757 | parent=$(git rev-parse --verify -q HEAD) || |
0e987a12 | 758 | say >&2 "applying to an empty history" |
f79d4c8a | 759 | |
3f01ad66 JH |
760 | if test -n "$committer_date_is_author_date" |
761 | then | |
762 | GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" | |
763 | export GIT_COMMITTER_DATE | |
764 | fi && | |
ea10b60c | 765 | git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit" |
3f01ad66 | 766 | ) && |
2e6e3e82 | 767 | git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent || |
d1c5f2a4 JH |
768 | stop_here $this |
769 | ||
770 | if test -x "$GIT_DIR"/hooks/post-applypatch | |
771 | then | |
772 | "$GIT_DIR"/hooks/post-applypatch | |
773 | fi | |
774 | ||
775 | go_next | |
776 | done | |
777 | ||
e0cd252e MS |
778 | git gc --auto |
779 | ||
d1c5f2a4 | 780 | rm -fr "$dotest" |