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