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