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