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