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