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