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