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 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 249 this=`expr "$this" + 1` 250 resume= 251fi 252 253iftest"$this"-gt"$last" 254then 255echo Nothing to do. 256rm-fr"$dotest" 257exit 258fi 259 260whiletest"$this"-le"$last" 261do 262 msgnum=`printf "%0${prec}d"$this` 263 next=`expr "$this" + 1` 264test -f"$dotest/$msgnum"|| { 265 resume= 266 go_next 267continue 268} 269 270# If we are not resuming, parse and extract the patch information 271# into separate files: 272# - info records the authorship and title 273# - msg is the rest of commit log message 274# - patch is the patch body. 275# 276# When we are resuming, these files are either already prepared 277# by the user, or the user can tell us to do so by --resolved flag. 278case"$resume"in 279'') 280 git-mailinfo$keep $utf8"$dotest/msg""$dotest/patch" \ 281<"$dotest/$msgnum">"$dotest/info"|| 282 stop_here $this 283 git-stripspace<"$dotest/msg">"$dotest/msg-clean" 284;; 285esac 286 287 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 288 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 289 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 290 291iftest -z"$GIT_AUTHOR_EMAIL" 292then 293echo"Patch does not have a valid e-mail address." 294 stop_here $this 295fi 296 297export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 298 299 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 300case"$keep_subject"in-k) SUBJECT="[PATCH]$SUBJECT";;esac 301 302case"$resume"in 303'') 304iftest''!="$SIGNOFF" 305then 306 LAST_SIGNED_OFF_BY=` 307 sed -ne '/^Signed-off-by: /p' \ 308 "$dotest/msg-clean" | 309 tail -n 1 310 ` 311 ADD_SIGNOFF=` 312 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 313 test '' = "$LAST_SIGNED_OFF_BY" && echo 314 echo "$SIGNOFF" 315 }` 316else 317 ADD_SIGNOFF= 318fi 319{ 320echo"$SUBJECT" 321iftest -s"$dotest/msg-clean" 322then 323echo 324cat"$dotest/msg-clean" 325fi 326iftest''!="$ADD_SIGNOFF" 327then 328echo"$ADD_SIGNOFF" 329fi 330} >"$dotest/final-commit" 331;; 332*) 333case"$resolved$interactive"in 334 tt) 335# This is used only for interactive view option. 336 git-diff-index -p --cached HEAD >"$dotest/patch" 337;; 338esac 339esac 340 341 resume= 342iftest"$interactive"= t 343then 344test -t0|| 345 die "cannot be interactive without stdin connected to a terminal." 346 action=again 347whiletest"$action"= again 348do 349echo"Commit Body is:" 350echo"--------------------------" 351cat"$dotest/final-commit" 352echo"--------------------------" 353printf"Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 354read reply 355case"$reply"in 356[yY]*) action=yes;; 357[aA]*) action=yes interactive= ;; 358[nN]*) action=skip ;; 359[eE]*)"${VISUAL:-${EDITOR:-vi}}""$dotest/final-commit" 360 action=again ;; 361[vV]*) action=again 362 LESS=-S${PAGER:-less}"$dotest/patch";; 363*) action=again ;; 364esac 365done 366else 367 action=yes 368fi 369 370iftest$action= skip 371then 372 go_next 373continue 374fi 375 376iftest -x"$GIT_DIR"/hooks/applypatch-msg 377then 378"$GIT_DIR"/hooks/applypatch-msg"$dotest/final-commit"|| 379 stop_here $this 380fi 381 382echo 383echo"Applying '$SUBJECT'" 384echo 385 386case"$resolved"in 387'') 388 git-apply$binary--index$ws"$dotest/patch" 389 apply_status=$? 390;; 391 t) 392# Resolved means the user did all the hard work, and 393# we do not have to do any patch application. Just 394# trust what the user has in the index file and the 395# working tree. 396 resolved= 397 changed="$(git-diff-index --cached --name-only HEAD)" 398iftest''="$changed" 399then 400echo"No changes - did you forget update-index?" 401 stop_here_user_resolve $this 402fi 403 unmerged=$(git-ls-files -u) 404iftest -n"$unmerged" 405then 406echo"You still have unmerged paths in your index" 407echo"did you forget update-index?" 408 stop_here_user_resolve $this 409fi 410 apply_status=0 411;; 412esac 413 414iftest$apply_status=1&&test"$threeway"= t 415then 416if(fall_back_3way) 417then 418# Applying the patch to an earlier tree and merging the 419# result may have produced the same tree as ours. 420 changed="$(git-diff-index --cached --name-only HEAD)" 421iftest''="$changed" 422then 423echo No changes -- Patch already applied. 424 go_next 425continue 426fi 427# clear apply_status -- we have successfully merged. 428 apply_status=0 429fi 430fi 431iftest$apply_status!=0 432then 433echo Patch failed at$msgnum. 434 stop_here_user_resolve $this 435fi 436 437iftest -x"$GIT_DIR"/hooks/pre-applypatch 438then 439"$GIT_DIR"/hooks/pre-applypatch|| stop_here $this 440fi 441 442 tree=$(git-write-tree)&& 443echo Wrote tree $tree&& 444 parent=$(git-rev-parse --verify HEAD)&& 445 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit")&& 446echo Committed:$commit&& 447 git-update-ref -m"$rloga:$SUBJECT" HEAD $commit $parent|| 448 stop_here $this 449 450iftest -x"$GIT_DIR"/hooks/post-applypatch 451then 452"$GIT_DIR"/hooks/post-applypatch 453fi 454 455 go_next 456done 457 458rm-fr"$dotest"