4 . git-sh-setup || die "Not a git archive"
6 files=$(git-diff-index --cached --name-only HEAD) || exit
8 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
13 echo >&2 "usage: $0 [--signoff] [--dotest=<dir>] [--utf8] [--3way] <mbox>"
14 echo >&2 " or, when resuming"
15 echo >&2 " $0 [--skip]"
20 echo "$1" >"$dotest/next"
25 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
26 "$dotest/patch" "$dotest/info"
27 echo "$next" >"$dotest/next"
32 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
34 rm -fr "$dotest"/patch-merge-*
35 mkdir "$dotest/patch-merge-tmp-dir"
37 # First see if the patch records the index info that we can use.
38 if git-apply -z --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 -z --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.
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
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"
56 # Otherwise, try nearby trees that can be used to apply the
61 # Hoping the patch is against our recent commits...
62 git-rev-list --max-count=$N HEAD
64 # or hoping the patch is against known tags...
65 git-ls-remote --tags .
69 # See if we have it as a tree...
70 git-cat-file tree "$base" >/dev/null 2>&1 || continue
72 rm -fr "$dotest"/patch-merge-* &&
73 mkdir "$dotest/patch-merge-tmp-dir" || break
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" &&
81 mv ../patch-merge-tmp-index ../patch-merge-index &&
82 echo "$base" >../patch-merge-base
83 ) <"$dotest/patch" 2>/dev/null && break
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
92 echo Falling back to patching base and 3-way merge...
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.
100 git-merge-resolve $orig_tree -- HEAD $his_tree || {
101 echo Failed to merge in the changes.
107 dotest=.dotest sign= utf8= keep= skip= interactive=
109 while case "$#" in 0) break;; esac
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
118 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
119 --interacti|--interactiv|--interactive)
120 interactive=t; shift ;;
122 -3|--3|--3w|--3wa|--3way)
124 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
126 -u|--u|--ut|--utf|--utf8)
128 -k|--k|--ke|--kee|--keep)
143 if test -d "$dotest" &&
144 last=$(cat "$dotest/last") &&
145 next=$(cat "$dotest/next") &&
147 test "$next" -gt "$last"
154 test ",$#," = ",0," ||
155 die "previous dotest directory $dotest still exists but mbox given."
158 # Make sure we are not given --skip
159 test ",$skip," = ,, ||
160 die "we are not resuming."
163 mkdir -p "$dotest" || exit
165 # cat does the right thing for us, including '-' to mean
168 git-mailsplit -d$prec "$dotest/" >"$dotest/last" || {
173 echo "$sign" >"$dotest/sign"
174 echo "$utf8" >"$dotest/utf8"
175 echo "$keep" >"$dotest/keep"
176 echo 1 >"$dotest/next"
179 if test "$(cat "$dotest/utf8")" = t
183 if test "$(cat "$dotest/keep")" = t
187 if test "$(cat "$dotest/sign")" = t
189 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
191 s/^/Signed-off-by: /'
197 last=`cat "$dotest/last"`
198 this=`cat "$dotest/next"`
201 this=`expr "$this" + 1`
204 if test "$this" -gt "$last"
211 while test "$this" -le "$last"
213 msgnum=`printf "%0${prec}d" $this`
214 next=`expr "$this" + 1`
215 test -f "$dotest/$msgnum" || {
221 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
222 <"$dotest/$msgnum" >"$dotest/info" ||
224 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
229 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
230 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
231 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
232 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
233 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
235 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
236 if test '' != "$SIGNOFF"
239 sed -ne '/^Signed-off-by: /p' "$dotest/msg-clean" |
242 ADD_SIGNOFF=$(test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
243 test '' = "$LAST_SIGNED_OFF_BY" && echo
251 if test -s "$dotest/msg-clean"
254 cat "$dotest/msg-clean"
256 if test '' != "$ADD_SIGNOFF"
260 } >"$dotest/final-commit"
262 if test "$interactive" = t
265 die "cannot be interactive without stdin connected to a terminal."
267 while test "$action" = again
269 echo "Commit Body is:"
270 echo "--------------------------"
271 cat "$dotest/final-commit"
272 echo "--------------------------"
273 echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
277 a*|A*) action=yes interactive= ;;
278 n*|N*) action=skip ;;
279 e*|E*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
287 if test $action = skip
293 if test -x "$GIT_DIR"/hooks/applypatch-msg
295 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
300 echo "Applying '$SUBJECT'"
303 git-apply --index "$dotest/patch"; apply_status=$?
304 if test $apply_status = 1 && test "$threeway" = t
308 # Applying the patch to an earlier tree and merging the
309 # result may have produced the same tree as ours.
310 changed="$(git-diff-index --cached --name-only -z HEAD)"
311 if test '' = "$changed"
313 echo No changes -- Patch already applied.
317 # clear apply_status -- we have successfully merged.
321 if test $apply_status != 0
323 echo Patch failed at $msgnum.
327 if test -x "$GIT_DIR"/hooks/pre-applypatch
329 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
332 tree=$(git-write-tree) &&
333 echo Wrote tree $tree &&
334 parent=$(git-rev-parse --verify HEAD) &&
335 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
336 echo Committed: $commit &&
337 git-update-ref HEAD $commit $parent ||
340 if test -x "$GIT_DIR"/hooks/post-applypatch
342 "$GIT_DIR"/hooks/post-applypatch