]> git.ipfire.org Git - thirdparty/git.git/blame - git-am.sh
Add git-am, applymbox replacement.
[thirdparty/git.git] / git-am.sh
CommitLineData
d1c5f2a4
JH
1#!/bin/sh
2#
3#
4. git-sh-setup || die "Not a git archive"
5
6files=$(git-diff-index --cached --name-only HEAD) || exit
7if [ "$files" ]; then
8 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
9 exit 1
10fi
11
12usage () {
13 echo >&2 "usage: $0 [--signoff] [--dotest=<dir>] [--utf8] [--3way] <mbox>"
14 echo >&2 " or, when resuming"
15 echo >&2 " $0 [--skip]"
16 exit 1;
17}
18
19stop_here () {
20 echo "$1" >"$dotest/next"
21 exit 1
22}
23
24go_next () {
25 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
26 "$dotest/patch" "$dotest/info"
27 echo "$next" >"$dotest/next"
28 this=$next
29}
30
31fall_back_3way () {
32 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
33
34 rm -fr "$dotest"/patch-merge-*
35 mkdir "$dotest/patch-merge-tmp-dir"
36
37 # First see if the patch records the index info that we can use.
38 if git-apply --show-index-info "$dotest/patch" \
39 >"$dotest/patch-merge-index-info" 2>/dev/null &&
40 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
41 git-update-index --index-info <"$dotest/patch-merge-index-info" &&
42 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
43 git-write-tree >"$dotest/patch-merge-base+" &&
44 # index has the base tree now.
45 (
46 cd "$dotest/patch-merge-tmp-dir" &&
47 GIT_INDEX_FILE="../patch-merge-tmp-index" \
48 GIT_OBJECT_DIRECTORY="$O_OBJECT" \
49 git-apply --index <../patch
50 )
51 then
52 echo Using index info to reconstruct a base tree...
53 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
54 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
55 else
56 # Otherwise, try nearby trees that can be used to apply the
57 # patch.
58 (
59 N=10
60
61 # Hoping the patch is against our recent commits...
62 git-rev-list --max-count=$N HEAD
63
64 # or hoping the patch is against known tags...
65 git-ls-remote --tags .
66 ) |
67 while read base junk
68 do
69 # See if we have it as a tree...
70 git-cat-file tree "$base" >/dev/null 2>&1 || continue
71
72 rm -fr "$dotest"/patch-merge-* &&
73 mkdir "$dotest/patch-merge-tmp-dir" || break
74 (
75 cd "$dotest/patch-merge-tmp-dir" &&
76 GIT_INDEX_FILE=../patch-merge-tmp-index &&
77 GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
78 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
79 git-read-tree "$base" &&
80 git-apply --index &&
81 mv ../patch-merge-tmp-index ../patch-merge-index &&
82 echo "$base" >../patch-merge-base
83 ) <"$dotest/patch" 2>/dev/null && break
84 done
85 fi
86
87 test -f "$dotest/patch-merge-index" &&
88 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
89 orig_tree=$(cat "$dotest/patch-merge-base") &&
90 rm -fr "$dotest"/patch-merge-* || exit 1
91
92 echo Falling back to patching base and 3-way merge...
93
94 # This is not so wrong. Depending on which base we picked,
95 # orig_tree may be wildly different from ours, but his_tree
96 # has the same set of wildly different changes in parts the
97 # patch did not touch, so resolve ends up cancelling them,
98 # saying that we reverted all those changes.
99
100 git-merge-resolve $orig_tree -- HEAD $his_tree || {
101 echo Failed to merge in the changes.
102 exit 1
103 }
104}
105
106prec=4
107dotest=.dotest sign= utf8= keep= skip= interactive=
108
109while case "$#" in 0) break;; esac
110do
111 case "$1" in
112 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
113 dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
114 -d|--d|--do|--dot|--dote|--dotes|--dotest)
115 case "$#" in 1) usage ;; esac; shift
116 dotest="$1"; shift;;
117
118 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
119 --interacti|--interactiv|--interactive)
120 interactive=t; shift ;;
121
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
131 --sk|--ski|--skip)
132 skip=t; shift ;;
133
134 --)
135 shift; break ;;
136 -*)
137 usage ;;
138 *)
139 break ;;
140 esac
141done
142
143if test -d "$dotest" &&
144 last=$(cat "$dotest/last") &&
145 next=$(cat "$dotest/next") &&
146 test $# != 0 &&
147 test "$next" -gt "$last"
148then
149 rm -fr "$dotest"
150fi
151
152if test -d "$dotest"
153then
154 test ",$#," = ",0," ||
155 die "previous dotest directory $dotest still exists but mbox given."
156else
157 # Make sure we are not given --skip
158 test ",$skip," = ,, ||
159 die "we are not resuming."
160
161 # Start afresh.
162 mkdir -p "$dotest" || exit
163
164 # cat does the right thing for us, including '-' to mean
165 # standard input.
166 cat "$@" |
167 git-mailsplit -d$prec "$dotest/" >"$dotest/last" || {
168 rm -fr "$dotest"
169 exit 1
170 }
171
172 echo "$sign" >"$dotest/sign"
173 echo "$utf8" >"$dotest/utf8"
174 echo "$keep" >"$dotest/keep"
175 echo "$threeway" >"$dotest/3way"
176 echo 1 >"$dotest/next"
177fi
178
179if test "$(cat "$dotest/utf8")" = t
180then
181 utf8=-u
182fi
183if test "$(cat "$dotest/keep")" = t
184then
185 keep=-k
186fi
187if test "$(cat "$dotest/sign")" = t
188then
189 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
190 s/>.*/>/
191 s/^/Signed-off-by: /'
192 `
193else
194 SIGNOFF=
195fi
196threeway=$(cat "$dotest/3way")
197
198last=`cat "$dotest/last"`
199this=`cat "$dotest/next"`
200if test "$skip" = t
201then
202 this=`expr "$this" + 1`
203fi
204
205if test "$this" -gt "$last"
206then
207 echo Nothing to do.
208 rm -fr "$dotest"
209 exit
210fi
211
212while test "$this" -le "$last"
213do
214 msgnum=`printf "%0${prec}d" $this`
215 next=`expr "$this" + 1`
216 test -f "$dotest/$msgnum" || {
217 go_next
218 continue
219 }
220 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
221 <"$dotest/$msgnum" >"$dotest/info" ||
222 stop_here $this
223 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
224
225 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
226 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
227 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
228 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
229
230 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
231 if test '' != "$SIGNOFF"
232 then
233 LAST_SIGNED_OFF_BY=`
234 sed -ne '/^Signed-off-by: /p' "$dotest/msg-clean" |
235 tail -n 1
236 `
237 ADD_SIGNOFF=$(test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
238 test '' = "$LAST_SIGNED_OFF_BY" && echo
239 echo "$SIGNOFF"
240 })
241 else
242 ADD_SIGNOFF=
243 fi
244 {
245 echo "$SUBJECT"
246 if test -s "$dotest/msg-clean"
247 then
248 echo
249 cat "$dotest/msg-clean"
250 fi
251 if test '' != "$ADD_SIGNOFF"
252 then
253 echo "$ADD_SIGNOFF"
254 fi
255 } >"$dotest/final-commit"
256
257 if test "$interactive" = t
258 then
259 action=again
260 while test "$action" = again
261 do
262 echo "Commit Body is:"
263 echo "--------------------------"
264 cat "$dotest/final-commit"
265 echo "--------------------------"
266 echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
267 read reply
268 case "$reply" in
269 y*|Y*) action=yes ;;
270 a*|A*) action=yes interactive= ;;
271 n*|N*) action=skip ;;
272 e*|E*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
273 action=again ;;
274 esac
275 done
276 else
277 action=yes
278 fi
279
280 if test $action = skip
281 then
282 go_next
283 continue
284 fi
285
286 if test -x "$GIT_DIR"/hooks/applypatch-msg
287 then
288 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
289 stop_here $this
290 fi
291
292 echo
293 echo "Applying '$SUBJECT'"
294 echo
295
296 git-apply --index "$dotest/patch"; apply_status=$?
297 if test $apply_status = 1 && test "$threeway" = t
298 then
299 (fall_back_3way) || stop_here $this
300
301 # Applying the patch to an earlier tree and merging the
302 # result may have produced the same tree as ours.
303 if test '' = "$(git-diff-index --cached --name-only -z HEAD)"
304 then
305 echo No changes -- Patch already applied.
306 go_next
307 continue
308 fi
309 fi
310 if test $apply_status != 0
311 then
312 echo Patch failed at $msgnum.
313 stop_here $this
314 fi
315
316 if test -x "$GIT_DIR"/hooks/pre-applypatch
317 then
318 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
319 fi
320
321 tree=$(git-write-tree) &&
322 echo Wrote tree $tree &&
323 parent=$(git-rev-parse --verify HEAD) &&
324 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
325 echo Committed: $commit &&
326 git-update-ref HEAD $commit $parent ||
327 stop_here $this
328
329 if test -x "$GIT_DIR"/hooks/post-applypatch
330 then
331 "$GIT_DIR"/hooks/post-applypatch
332 fi
333
334 go_next
335done
336
337rm -fr "$dotest"