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