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