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