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