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