1#!/bin/sh 2# 3# 4 5USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way] <mbox> 6 or, when resuming [--skip | --resolved]' 7. git-sh-setup 8 9stop_here () { 10echo"$1">"$dotest/next" 11exit1 12} 13 14go_next () { 15rm-f"$dotest/$msgnum""$dotest/msg""$dotest/msg-clean" \ 16"$dotest/patch""$dotest/info" 17echo"$next">"$dotest/next" 18 this=$next 19} 20 21fall_back_3way () { 22 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd` 23 24rm-fr"$dotest"/patch-merge-* 25mkdir"$dotest/patch-merge-tmp-dir" 26 27# First see if the patch records the index info that we can use. 28if 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( 36cd"$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) 41then 42echo Using index info to reconstruct a base tree... 43mv"$dotest/patch-merge-base+""$dotest/patch-merge-base" 44mv"$dotest/patch-merge-tmp-index""$dotest/patch-merge-index" 45else 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) | 57whileread base junk 58do 59# See if we have it as a tree... 60 git-cat-file tree "$base">/dev/null 2>&1||continue 61 62rm-fr"$dotest"/patch-merge-* && 63mkdir"$dotest/patch-merge-tmp-dir"||break 64( 65cd"$dotest/patch-merge-tmp-dir"&& 66 GIT_INDEX_FILE=../patch-merge-tmp-index&& 67 GIT_OBJECT_DIRECTORY="$O_OBJECT"&& 68export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY && 69 git-read-tree"$base"&& 70 git-apply$binary--index&& 71mv ../patch-merge-tmp-index ../patch-merge-index&& 72echo"$base">../patch-merge-base 73) <"$dotest/patch"2>/dev/null &&break 74done 75fi 76 77test -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")&& 80rm-fr"$dotest"/patch-merge-* ||exit1 81 82echo 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|| { 91echo Failed to merge in the changes. 92exit1 93} 94} 95 96prec=4 97dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= 98 99while case"$#"in0)break;;esac 100do 101case"$1"in 102-d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*) 103 dotest=`expr "$1" : '-[^=]*=\(.*\)'`;shift;; 104-d|--d|--do|--dot|--dote|--dotes|--dotest) 105case"$#"in1) 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--) 131shift;break;; 132-*) 133 usage ;; 134*) 135break;; 136esac 137done 138 139# If the dotest directory exists, but we have finished applying all the 140# patches in them, clear it out. 141iftest -d"$dotest"&& 142 last=$(cat "$dotest/last")&& 143 next=$(cat "$dotest/next")&& 144test$#!=0&& 145test"$next"-gt"$last" 146then 147rm-fr"$dotest" 148fi 149 150iftest -d"$dotest" 151then 152test",$#,"=",0,"|| 153 die "previous dotest directory$doteststill exists but mbox given." 154 resume=yes 155else 156# Make sure we are not given --skip nor --resolved 157test",$skip,$resolved,"= ,,, || 158 die "we are not resuming." 159 160# Start afresh. 161mkdir-p"$dotest"||exit 162 163 git-mailsplit -d"$prec"-o"$dotest"-b --"$@">"$dotest/last"|| { 164rm-fr"$dotest" 165exit1 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. 171echo"$binary">"$dotest/binary" 172echo"$sign">"$dotest/sign" 173echo"$utf8">"$dotest/utf8" 174echo"$keep">"$dotest/keep" 175echo1>"$dotest/next" 176fi 177 178case"$resolved"in 179'') 180 files=$(git-diff-index --cached --name-only HEAD)||exit 181if["$files"];then 182echo"Dirty index: cannot apply patches (dirty:$files)">&2 183exit1 184fi 185esac 186 187iftest"$(cat "$dotest/binary")"= t 188then 189 binary=--allow-binary-replacement 190fi 191iftest"$(cat "$dotest/utf8")"= t 192then 193 utf8=-u 194fi 195iftest"$(cat "$dotest/keep")"= t 196then 197 keep=-k 198fi 199iftest"$(cat "$dotest/sign")"= t 200then 201 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e ' 202 s/>.*/>/ 203 s/^/Signed-off-by: /' 204 ` 205else 206 SIGNOFF= 207fi 208 209last=`cat "$dotest/last"` 210this=`cat "$dotest/next"` 211iftest"$skip"= t 212then 213 this=`expr "$this" + 1` 214fi 215 216iftest"$this"-gt"$last" 217then 218echo Nothing to do. 219rm-fr"$dotest" 220exit 221fi 222 223whiletest"$this"-le"$last" 224do 225 msgnum=`printf "%0${prec}d"$this` 226 next=`expr "$this" + 1` 227test -f"$dotest/$msgnum"|| { 228 go_next 229continue 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. 240case"$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;; 247esac 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 253iftest -z"$GIT_AUTHOR_EMAIL" 254then 255echo"Patch does not have a valid e-mail address." 256 stop_here $this 257fi 258 259export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 260 261 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 262case"$keep_subject"in-k) SUBJECT="[PATCH]$SUBJECT";;esac 263 264case"$resume"in 265'') 266iftest''!="$SIGNOFF" 267then 268 LAST_SIGNED_OFF_BY=` 269 sed -ne '/^Signed-off-by: /p' \ 270 "$dotest/msg-clean" | 271 tail -n 1 272 ` 273 ADD_SIGNOFF=` 274 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 275 test '' = "$LAST_SIGNED_OFF_BY" && echo 276 echo "$SIGNOFF" 277 }` 278else 279 ADD_SIGNOFF= 280fi 281{ 282echo"$SUBJECT" 283iftest -s"$dotest/msg-clean" 284then 285echo 286cat"$dotest/msg-clean" 287fi 288iftest''!="$ADD_SIGNOFF" 289then 290echo"$ADD_SIGNOFF" 291fi 292} >"$dotest/final-commit" 293;; 294*) 295case"$resolved,$interactive"in 296 tt) 297# This is used only for interactive view option. 298 git-diff-index -p --cached HEAD >"$dotest/patch" 299;; 300esac 301esac 302 303 resume= 304iftest"$interactive"= t 305then 306test -t0|| 307 die "cannot be interactive without stdin connected to a terminal." 308 action=again 309whiletest"$action"= again 310do 311echo"Commit Body is:" 312echo"--------------------------" 313cat"$dotest/final-commit" 314echo"--------------------------" 315printf"Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 316read reply 317case"$reply"in 318[yY]*) action=yes;; 319[aA]*) action=yes interactive= ;; 320[nN]*) action=skip ;; 321[eE]*)"${VISUAL:-${EDITOR:-vi}}""$dotest/final-commit" 322 action=again ;; 323[vV]*) action=again 324 LESS=-S${PAGER:-less}"$dotest/patch";; 325*) action=again ;; 326esac 327done 328else 329 action=yes 330fi 331 332iftest$action= skip 333then 334 go_next 335continue 336fi 337 338iftest -x"$GIT_DIR"/hooks/applypatch-msg 339then 340"$GIT_DIR"/hooks/applypatch-msg"$dotest/final-commit"|| 341 stop_here $this 342fi 343 344echo 345echo"Applying '$SUBJECT'" 346echo 347 348case"$resolved"in 349'') 350 git-apply$binary--index"$dotest/patch" 351 apply_status=$? 352;; 353 t) 354# Resolved means the user did all the hard work, and 355# we do not have to do any patch application. Just 356# trust what the user has in the index file and the 357# working tree. 358 resolved= 359 apply_status=0 360;; 361esac 362 363iftest$apply_status=1&&test"$threeway"= t 364then 365if(fall_back_3way) 366then 367# Applying the patch to an earlier tree and merging the 368# result may have produced the same tree as ours. 369 changed="$(git-diff-index --cached --name-only -z HEAD)" 370iftest''="$changed" 371then 372echo No changes -- Patch already applied. 373 go_next 374continue 375fi 376# clear apply_status -- we have successfully merged. 377 apply_status=0 378fi 379fi 380iftest$apply_status!=0 381then 382echo Patch failed at$msgnum. 383 stop_here $this 384fi 385 386iftest -x"$GIT_DIR"/hooks/pre-applypatch 387then 388"$GIT_DIR"/hooks/pre-applypatch|| stop_here $this 389fi 390 391 tree=$(git-write-tree)&& 392echo Wrote tree $tree&& 393 parent=$(git-rev-parse --verify HEAD)&& 394 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit")&& 395echo Committed:$commit&& 396 git-update-ref HEAD $commit $parent|| 397 stop_here $this 398 399iftest -x"$GIT_DIR"/hooks/post-applypatch 400then 401"$GIT_DIR"/hooks/post-applypatch 402fi 403 404 go_next 405done 406 407rm-fr"$dotest"