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