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