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 recursive ends up canceling them, 92# saying that we reverted all those changes. 93 94eval GITHEAD_$his_tree='"$SUBJECT"' 95export GITHEAD_$his_tree 96 git-merge-recursive$orig_tree-- HEAD $his_tree|| { 97iftest -d"$GIT_DIR/rr-cache" 98then 99 git-rerere 100fi 101echo Failed to merge in the changes. 102exit1 103} 104unset GITHEAD_$his_tree 105} 106 107prec=4 108dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg= 109 110while case"$#"in0)break;;esac 111do 112case"$1"in 113-d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*) 114 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`;shift;; 115-d|--d|--do|--dot|--dote|--dotes|--dotest) 116case"$#"in1) usage ;;esac;shift 117 dotest="$1";shift;; 118 119-i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\ 120--interacti|--interactiv|--interactive) 121 interactive=t;shift;; 122 123-b|--b|--bi|--bin|--bina|--binar|--binary) 124 binary=t;shift;; 125 126-3|--3|--3w|--3wa|--3way) 127 threeway=t;shift;; 128-s|--s|--si|--sig|--sign|--signo|--signof|--signoff) 129 sign=t;shift;; 130-u|--u|--ut|--utf|--utf8) 131 utf8=t;shift;; 132-k|--k|--ke|--kee|--keep) 133 keep=t;shift;; 134 135-r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved) 136 resolved=t;shift;; 137 138--sk|--ski|--skip) 139 skip=t;shift;; 140 141--whitespace=*) 142 ws=$1;shift;; 143 144--resolvemsg=*) 145 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//");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 169case"$#,$skip$resolved"in 1700,*t*) 171# Explicit resume command and we do not have file, so 172# we are happy. 173: ;; 1740,) 175# No file input but without resume parameters; catch 176# user error to feed us a patch from standard input 177# when there is already .dotest. This is somewhat 178# unreliable -- stdin could be /dev/null for example 179# and the caller did not intend to feed us a patch but 180# wanted to continue unattended. 181 tty -s 182;; 183*) 184 false 185;; 186esac|| 187 die "previous dotest directory$doteststill exists but mbox given." 188 resume=yes 189else 190# Make sure we are not given --skip nor --resolved 191test",$skip,$resolved,"= ,,, || 192 die "Resolve operation not in progress, we are not resuming." 193 194# Start afresh. 195mkdir-p"$dotest"||exit 196 197 git-mailsplit -d"$prec"-o"$dotest"-b --"$@">"$dotest/last"|| { 198rm-fr"$dotest" 199exit1 200} 201 202# -b, -s, -u, -k and --whitespace flags are kept for the 203# resuming session after a patch failure. 204# -3 and -i can and must be given when resuming. 205echo"$binary">"$dotest/binary" 206echo"$ws">"$dotest/whitespace" 207echo"$sign">"$dotest/sign" 208echo"$utf8">"$dotest/utf8" 209echo"$keep">"$dotest/keep" 210echo1>"$dotest/next" 211fi 212 213case"$resolved"in 214'') 215 files=$(git-diff-index --cached --name-only HEAD)||exit 216if["$files"];then 217echo"Dirty index: cannot apply patches (dirty:$files)">&2 218exit1 219fi 220esac 221 222iftest"$(cat "$dotest/binary")"= t 223then 224 binary=--allow-binary-replacement 225fi 226iftest"$(cat "$dotest/utf8")"= t 227then 228 utf8=-u 229fi 230iftest"$(cat "$dotest/keep")"= t 231then 232 keep=-k 233fi 234ws=`cat "$dotest/whitespace"` 235iftest"$(cat "$dotest/sign")"= t 236then 237 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e ' 238 s/>.*/>/ 239 s/^/Signed-off-by: /' 240 ` 241else 242 SIGNOFF= 243fi 244 245last=`cat "$dotest/last"` 246this=`cat "$dotest/next"` 247iftest"$skip"= t 248then 249iftest -d"$GIT_DIR/rr-cache" 250then 251 git-rerereclear 252fi 253 this=`expr "$this" + 1` 254 resume= 255fi 256 257iftest"$this"-gt"$last" 258then 259echo Nothing to do. 260rm-fr"$dotest" 261exit 262fi 263 264whiletest"$this"-le"$last" 265do 266 msgnum=`printf "%0${prec}d"$this` 267 next=`expr "$this" + 1` 268test -f"$dotest/$msgnum"|| { 269 resume= 270 go_next 271continue 272} 273 274# If we are not resuming, parse and extract the patch information 275# into separate files: 276# - info records the authorship and title 277# - msg is the rest of commit log message 278# - patch is the patch body. 279# 280# When we are resuming, these files are either already prepared 281# by the user, or the user can tell us to do so by --resolved flag. 282case"$resume"in 283'') 284 git-mailinfo$keep $utf8"$dotest/msg""$dotest/patch" \ 285<"$dotest/$msgnum">"$dotest/info"|| 286 stop_here $this 287 git-stripspace<"$dotest/msg">"$dotest/msg-clean" 288;; 289esac 290 291 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 292 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 293 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 294 295iftest -z"$GIT_AUTHOR_EMAIL" 296then 297echo"Patch does not have a valid e-mail address." 298 stop_here $this 299fi 300 301export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 302 303 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 304case"$keep_subject"in-k) SUBJECT="[PATCH]$SUBJECT";;esac 305 306case"$resume"in 307'') 308iftest''!="$SIGNOFF" 309then 310 LAST_SIGNED_OFF_BY=` 311 sed -ne '/^Signed-off-by: /p' \ 312 "$dotest/msg-clean" | 313 tail -n 1 314 ` 315 ADD_SIGNOFF=` 316 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 317 test '' = "$LAST_SIGNED_OFF_BY" && echo 318 echo "$SIGNOFF" 319 }` 320else 321 ADD_SIGNOFF= 322fi 323{ 324echo"$SUBJECT" 325iftest -s"$dotest/msg-clean" 326then 327echo 328cat"$dotest/msg-clean" 329fi 330iftest''!="$ADD_SIGNOFF" 331then 332echo"$ADD_SIGNOFF" 333fi 334} >"$dotest/final-commit" 335;; 336*) 337case"$resolved$interactive"in 338 tt) 339# This is used only for interactive view option. 340 git-diff-index -p --cached HEAD >"$dotest/patch" 341;; 342esac 343esac 344 345 resume= 346iftest"$interactive"= t 347then 348test -t0|| 349 die "cannot be interactive without stdin connected to a terminal." 350 action=again 351whiletest"$action"= again 352do 353echo"Commit Body is:" 354echo"--------------------------" 355cat"$dotest/final-commit" 356echo"--------------------------" 357printf"Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 358read reply 359case"$reply"in 360[yY]*) action=yes;; 361[aA]*) action=yes interactive= ;; 362[nN]*) action=skip ;; 363[eE]*)"${VISUAL:-${EDITOR:-vi}}""$dotest/final-commit" 364 action=again ;; 365[vV]*) action=again 366 LESS=-S${PAGER:-less}"$dotest/patch";; 367*) action=again ;; 368esac 369done 370else 371 action=yes 372fi 373 374iftest$action= skip 375then 376 go_next 377continue 378fi 379 380iftest -x"$GIT_DIR"/hooks/applypatch-msg 381then 382"$GIT_DIR"/hooks/applypatch-msg"$dotest/final-commit"|| 383 stop_here $this 384fi 385 386echo 387echo"Applying '$SUBJECT'" 388echo 389 390case"$resolved"in 391'') 392 git-apply$binary--index$ws"$dotest/patch" 393 apply_status=$? 394;; 395 t) 396# Resolved means the user did all the hard work, and 397# we do not have to do any patch application. Just 398# trust what the user has in the index file and the 399# working tree. 400 resolved= 401 changed="$(git-diff-index --cached --name-only HEAD)" 402iftest''="$changed" 403then 404echo"No changes - did you forget to use 'git add'?" 405 stop_here_user_resolve $this 406fi 407 unmerged=$(git-ls-files -u) 408iftest -n"$unmerged" 409then 410echo"You still have unmerged paths in your index" 411echo"did you forget to use 'git add'?" 412 stop_here_user_resolve $this 413fi 414 apply_status=0 415iftest -d"$GIT_DIR/rr-cache" 416then 417 git rerere 418fi 419;; 420esac 421 422iftest$apply_status=1&&test"$threeway"= t 423then 424if(fall_back_3way) 425then 426# Applying the patch to an earlier tree and merging the 427# result may have produced the same tree as ours. 428 changed="$(git-diff-index --cached --name-only HEAD)" 429iftest''="$changed" 430then 431echo No changes -- Patch already applied. 432 go_next 433continue 434fi 435# clear apply_status -- we have successfully merged. 436 apply_status=0 437fi 438fi 439iftest$apply_status!=0 440then 441echo Patch failed at$msgnum. 442 stop_here_user_resolve $this 443fi 444 445iftest -x"$GIT_DIR"/hooks/pre-applypatch 446then 447"$GIT_DIR"/hooks/pre-applypatch|| stop_here $this 448fi 449 450 tree=$(git-write-tree)&& 451echo Wrote tree $tree&& 452 parent=$(git-rev-parse --verify HEAD)&& 453 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit")&& 454echo Committed:$commit&& 455 git-update-ref -m"$GIT_REFLOG_ACTION:$SUBJECT" HEAD $commit $parent|| 456 stop_here $this 457 458iftest -x"$GIT_DIR"/hooks/post-applypatch 459then 460"$GIT_DIR"/hooks/post-applypatch 461fi 462 463 go_next 464done 465 466rm-fr"$dotest"