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-- 11i,interactive run interactively 12b,binary* (historical option -- no-op) 133,3way allow fall back on 3way merging if needed 14q,quiet be quiet 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 19directory= pass it through git-apply 20C= pass it through git-apply 21p= pass it through git-apply 22patch-format= format the patch(es) are in 23reject pass it through git-apply 24resolvemsg= override error message when patch failure occurs 25r,resolved to be used after a patch failure 26skip skip the current patch 27abort restore the original branch and abort the patching operation. 28committer-date-is-author-date lie about committer date 29ignore-date use current timestamp for author date 30rebasing* (internal use for git-rebase)" 31 32. git-sh-setup 33prefix=$(git rev-parse --show-prefix) 34set_reflog_action am 35require_work_tree 36cd_to_toplevel 37 38git var GIT_COMMITTER_IDENT >/dev/null || 39 die "You need to set your committer info first" 40 41if git rev-parse --verify -q HEAD >/dev/null 42then 43 HAS_HEAD=yes 44else 45 HAS_HEAD= 46fi 47 48sq() { 49 git rev-parse --sq-quote"$@" 50} 51 52stop_here () { 53echo"$1">"$dotest/next" 54exit1 55} 56 57stop_here_user_resolve () { 58if[-n"$resolvemsg"];then 59printf'%s\n'"$resolvemsg" 60 stop_here $1 61fi 62 cmdline="git am" 63iftest''!="$interactive" 64then 65 cmdline="$cmdline-i" 66fi 67iftest''!="$threeway" 68then 69 cmdline="$cmdline-3" 70fi 71echo"When you have resolved this problem run\"$cmdline--resolved\"." 72echo"If you would prefer to skip this patch, instead run\"$cmdline--skip\"." 73echo"To restore the original branch and stop patching run\"$cmdline--abort\"." 74 75 stop_here $1 76} 77 78go_next () { 79rm-f"$dotest/$msgnum""$dotest/msg""$dotest/msg-clean" \ 80"$dotest/patch""$dotest/info" 81echo"$next">"$dotest/next" 82 this=$next 83} 84 85cannot_fallback () { 86echo"$1" 87echo"Cannot fall back to three-way merge." 88exit1 89} 90 91fall_back_3way () { 92 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd` 93 94rm-fr"$dotest"/patch-merge-* 95mkdir"$dotest/patch-merge-tmp-dir" 96 97# First see if the patch records the index info that we can use. 98 git apply --build-fake-ancestor"$dotest/patch-merge-tmp-index" \ 99"$dotest/patch"&& 100 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ 101 git write-tree>"$dotest/patch-merge-base+"|| 102 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge." 103 104 say Using index info to reconstruct a base tree... 105if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ 106 git apply --cached<"$dotest/patch" 107then 108mv"$dotest/patch-merge-base+""$dotest/patch-merge-base" 109mv"$dotest/patch-merge-tmp-index""$dotest/patch-merge-index" 110else 111 cannot_fallback "Did you hand edit your patch? 112It does not apply to blobs recorded in its index." 113fi 114 115test -f"$dotest/patch-merge-index"&& 116 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree)&& 117 orig_tree=$(cat "$dotest/patch-merge-base")&& 118rm-fr"$dotest"/patch-merge-* ||exit1 119 120 say Falling back to patching base and 3-way merge... 121 122# This is not so wrong. Depending on which base we picked, 123# orig_tree may be wildly different from ours, but his_tree 124# has the same set of wildly different changes in parts the 125# patch did not touch, so recursive ends up canceling them, 126# saying that we reverted all those changes. 127 128eval GITHEAD_$his_tree='"$FIRSTLINE"' 129export GITHEAD_$his_tree 130iftest -n"$GIT_QUIET" 131then 132export GIT_MERGE_VERBOSITY=0 133fi 134 git-merge-recursive$orig_tree-- HEAD $his_tree|| { 135 git rerere 136echo Failed to merge in the changes. 137exit1 138} 139unset GITHEAD_$his_tree 140} 141 142clean_abort () { 143test$#=0||echo>&2"$@" 144rm-fr"$dotest" 145exit1 146} 147 148patch_format= 149 150check_patch_format () { 151# early return if patch_format was set from the command line 152iftest -n"$patch_format" 153then 154return0 155fi 156 157# we default to mbox format if input is from stdin and for 158# directories 159iftest$#=0||test"x$1"="x-"||test -d"$1" 160then 161 patch_format=mbox 162return0 163fi 164 165# otherwise, check the first few lines of the first patch to try 166# to detect its format 167{ 168read l1 169read l2 170read l3 171case"$l1"in 172"From "* |"From: "*) 173 patch_format=mbox 174;; 175'# This series applies on GIT commit'*) 176 patch_format=stgit-series 177;; 178"# HG changeset patch") 179 patch_format=hg 180;; 181*) 182# if the second line is empty and the third is 183# a From, Author or Date entry, this is very 184# likely an StGIT patch 185case"$l2,$l3"in 186,"From: "* | ,"Author: "* | ,"Date: "*) 187 patch_format=stgit 188;; 189*) 190;; 191esac 192;; 193esac 194iftest -z"$patch_format"&& 195test -n"$l1"&& 196test -n"$l2"&& 197test -n"$l3" 198then 199# This begins with three non-empty lines. Is this a 200# piece of e-mail a-la RFC2822? Grab all the headers, 201# discarding the indented remainder of folded lines, 202# and see if it looks like that they all begin with the 203# header field names... 204sed-n -e'/^$/q'-e'/^[ ]/d'-e p "$1"| 205egrep-v'^[A-Za-z]+(-[A-Za-z]+)*:'>/dev/null || 206 patch_format=mbox 207fi 208} <"$1"|| clean_abort 209} 210 211split_patches () { 212case"$patch_format"in 213 mbox) 214 git mailsplit -d"$prec"-o"$dotest"-b --"$@">"$dotest/last"|| 215 clean_abort 216;; 217 stgit-series) 218iftest$#-ne1 219then 220 clean_abort "Only one StGIT patch series can be applied at once" 221fi 222 series_dir=`dirname "$1"` 223 series_file="$1" 224shift 225{ 226set x 227whileread filename 228do 229set"$@""$series_dir/$filename" 230done 231# remove the safety x 232shift 233# remove the arg coming from the first-line comment 234shift 235} <"$series_file"|| clean_abort 236# set the patch format appropriately 237 patch_format=stgit 238# now handle the actual StGIT patches 239 split_patches "$@" 240;; 241 stgit) 242 this=0 243for stgit in"$@" 244do 245 this=`expr "$this" + 1` 246 msgnum=`printf "%0${prec}d"$this` 247# Perl version of StGIT parse_patch. The first nonemptyline 248# not starting with Author, From or Date is the 249# subject, and the body starts with the next nonempty 250# line not starting with Author, From or Date 251 perl -ne'BEGIN {$subject= 0 } 252 if ($subject> 1) { print ; } 253 elsif (/^\s+$/) { next ; } 254 elsif (/^Author:/) { print s/Author/From/ ; } 255 elsif (/^(From|Date)/) { print ; } 256 elsif ($subject) { 257$subject= 2 ; 258 print "\n" ; 259 print ; 260 } else { 261 print "Subject: ",$_; 262$subject= 1; 263 } 264 '<"$stgit">"$dotest/$msgnum"|| clean_abort 265done 266echo"$this">"$dotest/last" 267 this= 268 msgnum= 269;; 270*) 271iftest -n"$parse_patch";then 272 clean_abort "Patch format$patch_formatis not supported." 273else 274 clean_abort "Patch format detection failed." 275fi 276;; 277esac 278} 279 280prec=4 281dotest="$GIT_DIR/rebase-apply" 282sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= 283resolvemsg= resume= 284git_apply_opt= 285committer_date_is_author_date= 286ignore_date= 287 288whiletest$#!=0 289do 290case"$1"in 291-i|--interactive) 292 interactive=t ;; 293-b|--binary) 294: ;; 295-3|--3way) 296 threeway=t ;; 297-s|--signoff) 298 sign=t ;; 299-u|--utf8) 300 utf8=t ;;# this is now default 301--no-utf8) 302 utf8= ;; 303-k|--keep) 304 keep=t ;; 305-r|--resolved) 306 resolved=t ;; 307--skip) 308 skip=t ;; 309--abort) 310 abort=t ;; 311--rebasing) 312 rebasing=t threeway=t keep=t ;; 313-d|--dotest) 314 die "-d option is no longer supported. Do not use." 315;; 316--resolvemsg) 317shift; resolvemsg=$1;; 318--whitespace|--directory) 319 git_apply_opt="$git_apply_opt$(sq "$1=$2")";shift;; 320-C|-p) 321 git_apply_opt="$git_apply_opt$(sq "$1$2")";shift;; 322--patch-format) 323shift; patch_format="$1";; 324--reject) 325 git_apply_opt="$git_apply_opt$1";; 326--committer-date-is-author-date) 327 committer_date_is_author_date=t ;; 328--ignore-date) 329 ignore_date=t ;; 330-q|--quiet) 331 GIT_QUIET=t ;; 332--) 333shift;break;; 334*) 335 usage ;; 336esac 337shift 338done 339 340# If the dotest directory exists, but we have finished applying all the 341# patches in them, clear it out. 342iftest -d"$dotest"&& 343 last=$(cat "$dotest/last")&& 344 next=$(cat "$dotest/next")&& 345test$#!=0&& 346test"$next"-gt"$last" 347then 348rm-fr"$dotest" 349fi 350 351iftest -d"$dotest" 352then 353case"$#,$skip$resolved$abort"in 3540,*t*) 355# Explicit resume command and we do not have file, so 356# we are happy. 357: ;; 3580,) 359# No file input but without resume parameters; catch 360# user error to feed us a patch from standard input 361# when there is already $dotest. This is somewhat 362# unreliable -- stdin could be /dev/null for example 363# and the caller did not intend to feed us a patch but 364# wanted to continue unattended. 365test -t0 366;; 367*) 368 false 369;; 370esac|| 371 die "previous rebase directory$doteststill exists but mbox given." 372 resume=yes 373 374case"$skip,$abort"in 375 t,t) 376 die "Please make up your mind. --skip or --abort?" 377;; 378 t,) 379 git rerere clear 380 git read-tree --reset -u HEAD HEAD 381 orig_head=$(cat "$GIT_DIR/ORIG_HEAD") 382 git reset HEAD 383 git update-ref ORIG_HEAD $orig_head 384;; 385,t) 386iftest -f"$dotest/rebasing" 387then 388exec git rebase --abort 389fi 390 git rerere clear 391test -f"$dotest/dirtyindex"|| { 392 git read-tree --reset -u HEAD ORIG_HEAD 393 git reset ORIG_HEAD 394} 395rm-fr"$dotest" 396exit;; 397esac 398rm-f"$dotest/dirtyindex" 399else 400# Make sure we are not given --skip, --resolved, nor --abort 401test"$skip$resolved$abort"=""|| 402 die "Resolve operation not in progress, we are not resuming." 403 404# Start afresh. 405mkdir-p"$dotest"||exit 406 407iftest -n"$prefix"&&test$#!=0 408then 409 first=t 410for arg 411do 412test -n"$first"&& { 413set x 414 first= 415} 416case"$arg"in 417/*) 418set"$@""$arg";; 419*) 420set"$@""$prefix$arg";; 421esac 422done 423shift 424fi 425 426 check_patch_format "$@" 427 428 split_patches "$@" 429 430# -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept 431# for the resuming session after a patch failure. 432# -i can and must be given when resuming. 433echo"$git_apply_opt">"$dotest/apply-opt" 434echo"$threeway">"$dotest/threeway" 435echo"$sign">"$dotest/sign" 436echo"$utf8">"$dotest/utf8" 437echo"$keep">"$dotest/keep" 438echo"$GIT_QUIET">"$dotest/quiet" 439echo1>"$dotest/next" 440iftest -n"$rebasing" 441then 442: >"$dotest/rebasing" 443else 444: >"$dotest/applying" 445iftest -n"$HAS_HEAD" 446then 447 git update-ref ORIG_HEAD HEAD 448else 449 git update-ref -d ORIG_HEAD >/dev/null 2>&1 450fi 451fi 452fi 453 454case"$resolved"in 455'') 456case"$HAS_HEAD"in 457'') 458 files=$(git ls-files);; 459 ?*) 460 files=$(git diff-index --cached --name-only HEAD --);; 461esac||exit 462iftest"$files" 463then 464test -n"$HAS_HEAD"&& : >"$dotest/dirtyindex" 465 die "Dirty index: cannot apply patches (dirty:$files)" 466fi 467esac 468 469iftest"$(cat "$dotest/utf8")"= t 470then 471 utf8=-u 472else 473 utf8=-n 474fi 475iftest"$(cat "$dotest/keep")"= t 476then 477 keep=-k 478fi 479iftest"$(cat "$dotest/quiet")"= t 480then 481 GIT_QUIET=t 482fi 483iftest"$(cat "$dotest/threeway")"= t 484then 485 threeway=t 486fi 487git_apply_opt=$(cat "$dotest/apply-opt") 488iftest"$(cat "$dotest/sign")"= t 489then 490 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e ' 491 s/>.*/>/ 492 s/^/Signed-off-by: /' 493 ` 494else 495 SIGNOFF= 496fi 497 498last=`cat "$dotest/last"` 499this=`cat "$dotest/next"` 500iftest"$skip"= t 501then 502 this=`expr "$this" + 1` 503 resume= 504fi 505 506iftest"$this"-gt"$last" 507then 508 say Nothing to do. 509rm-fr"$dotest" 510exit 511fi 512 513whiletest"$this"-le"$last" 514do 515 msgnum=`printf "%0${prec}d"$this` 516 next=`expr "$this" + 1` 517test -f"$dotest/$msgnum"|| { 518 resume= 519 go_next 520continue 521} 522 523# If we are not resuming, parse and extract the patch information 524# into separate files: 525# - info records the authorship and title 526# - msg is the rest of commit log message 527# - patch is the patch body. 528# 529# When we are resuming, these files are either already prepared 530# by the user, or the user can tell us to do so by --resolved flag. 531case"$resume"in 532'') 533 git mailinfo $keep $utf8"$dotest/msg""$dotest/patch" \ 534<"$dotest/$msgnum">"$dotest/info"|| 535 stop_here $this 536 537# skip pine's internal folder data 538grep'^Author: Mail System Internal Data$' \ 539<"$dotest"/info >/dev/null && 540 go_next &&continue 541 542test -s"$dotest/patch"|| { 543echo"Patch is empty. Was it split wrong?" 544 stop_here $this 545} 546iftest -f"$dotest/rebasing"&& 547 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \ 548 -e q "$dotest/$msgnum") && 549 test "$(git cat-file -t "$commit")" = commit 550 then 551 git cat-file commit "$commit" | 552 sed -e '1,/^$/d' >"$dotest/msg-clean" 553 else 554 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 555 case "$keep_subject" in -k) SUBJECT="[PATCH]$SUBJECT" ;; esac 556 557 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") | 558 git stripspace > "$dotest/msg-clean" 559 fi 560 ;; 561 esac 562 563 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 564 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 565 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 566 567 if test -z "$GIT_AUTHOR_EMAIL" 568 then 569 echo "Patch does not have a valid e-mail address." 570 stop_here$this 571 fi 572 573 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 574 575 case "$resume" in 576 '') 577 if test '' != "$SIGNOFF" 578 then 579 LAST_SIGNED_OFF_BY=` 580 sed -ne '/^Signed-off-by: /p' \ 581 "$dotest/msg-clean" | 582 sed -ne '$p' 583 ` 584 ADD_SIGNOFF=` 585 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 586 test '' = "$LAST_SIGNED_OFF_BY" && echo 587 echo "$SIGNOFF" 588 }` 589 else 590 ADD_SIGNOFF= 591 fi 592 { 593 if test -s "$dotest/msg-clean" 594 then 595 cat "$dotest/msg-clean" 596 fi 597 if test '' != "$ADD_SIGNOFF" 598 then 599 echo "$ADD_SIGNOFF" 600 fi 601 } >"$dotest/final-commit" 602 ;; 603 *) 604 case "$resolved$interactive" in 605 tt) 606 # This is used only for interactive view option. 607 git diff-index -p --cached HEAD -- >"$dotest/patch" 608 ;; 609 esac 610 esac 611 612 resume= 613 if test "$interactive" = t 614 then 615 test -t 0 || 616 die "cannot be interactive without stdin connected to a terminal." 617 action=again 618 while test "$action" = again 619 do 620 echo "Commit Body is:" 621 echo "--------------------------" 622 cat "$dotest/final-commit" 623 echo "--------------------------" 624 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 625 read reply 626 case "$reply" in 627 [yY]*) action=yes ;; 628 [aA]*) action=yes interactive= ;; 629 [nN]*) action=skip ;; 630 [eE]*) git_editor "$dotest/final-commit" 631 action=again ;; 632 [vV]*) action=again 633 LESS=-S${PAGER:-less}"$dotest/patch" ;; 634 *) action=again ;; 635 esac 636 done 637 else 638 action=yes 639 fi 640 FIRSTLINE=$(sed 1q "$dotest/final-commit") 641 642 if test$action= skip 643 then 644 go_next 645 continue 646 fi 647 648 if test -x "$GIT_DIR"/hooks/applypatch-msg 649 then 650 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || 651 stop_here$this 652 fi 653 654 say "Applying:$FIRSTLINE" 655 656 case "$resolved" in 657 '') 658 # When we are allowed to fall back to 3-way later, don't give 659# false errors during the initial attempt. 660 squelch= 661iftest"$threeway"= t 662then 663 squelch='>/dev/null 2>&1 ' 664fi 665eval"git apply$squelch$git_apply_opt"' --index "$dotest/patch"' 666 apply_status=$? 667;; 668 t) 669# Resolved means the user did all the hard work, and 670# we do not have to do any patch application. Just 671# trust what the user has in the index file and the 672# working tree. 673 resolved= 674 git diff-index --quiet --cached HEAD --&& { 675echo"No changes - did you forget to use 'git add'?" 676 stop_here_user_resolve $this 677} 678 unmerged=$(git ls-files -u) 679iftest -n"$unmerged" 680then 681echo"You still have unmerged paths in your index" 682echo"did you forget to use 'git add'?" 683 stop_here_user_resolve $this 684fi 685 apply_status=0 686 git rerere 687;; 688esac 689 690iftest$apply_status=1&&test"$threeway"= t 691then 692if(fall_back_3way) 693then 694# Applying the patch to an earlier tree and merging the 695# result may have produced the same tree as ours. 696 git diff-index --quiet --cached HEAD --&& { 697 say No changes -- Patch already applied. 698 go_next 699continue 700} 701# clear apply_status -- we have successfully merged. 702 apply_status=0 703fi 704fi 705iftest$apply_status!=0 706then 707printf'Patch failed at %s %s\n'"$msgnum""$FIRSTLINE" 708 stop_here_user_resolve $this 709fi 710 711iftest -x"$GIT_DIR"/hooks/pre-applypatch 712then 713"$GIT_DIR"/hooks/pre-applypatch|| stop_here $this 714fi 715 716 tree=$(git write-tree)&& 717 commit=$( 718iftest -n"$ignore_date" 719then 720 GIT_AUTHOR_DATE= 721fi 722 parent=$(git rev-parse --verify -q HEAD)|| 723 say >&2"applying to an empty history" 724 725iftest -n"$committer_date_is_author_date" 726then 727 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" 728export GIT_COMMITTER_DATE 729fi&& 730 git commit-tree$tree ${parent:+-p} $parent<"$dotest/final-commit" 731) && 732 git update-ref -m"$GIT_REFLOG_ACTION:$FIRSTLINE" HEAD $commit $parent|| 733 stop_here $this 734 735iftest -x"$GIT_DIR"/hooks/post-applypatch 736then 737"$GIT_DIR"/hooks/post-applypatch 738fi 739 740 go_next 741done 742 743git gc --auto 744 745rm-fr"$dotest"