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