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 LC_ALL=C 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= 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 ;; 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"$GIT_QUIET">"$dotest/quiet" 452echo1>"$dotest/next" 453iftest -n"$rebasing" 454then 455: >"$dotest/rebasing" 456else 457: >"$dotest/applying" 458iftest -n"$HAS_HEAD" 459then 460 git update-ref ORIG_HEAD HEAD 461else 462 git update-ref -d ORIG_HEAD >/dev/null 2>&1 463fi 464fi 465fi 466 467case"$resolved"in 468'') 469case"$HAS_HEAD"in 470'') 471 files=$(git ls-files);; 472 ?*) 473 files=$(git diff-index --cached --name-only HEAD --);; 474esac||exit 475iftest"$files" 476then 477test -n"$HAS_HEAD"&& : >"$dotest/dirtyindex" 478 die "Dirty index: cannot apply patches (dirty:$files)" 479fi 480esac 481 482iftest"$(cat "$dotest/utf8")"= t 483then 484 utf8=-u 485else 486 utf8=-n 487fi 488iftest"$(cat "$dotest/keep")"= t 489then 490 keep=-k 491fi 492case"$(cat "$dotest/scissors")"in 493t) 494 scissors=--scissors;; 495f) 496 scissors=--no-scissors;; 497esac 498iftest"$(cat "$dotest/quiet")"= t 499then 500 GIT_QUIET=t 501fi 502iftest"$(cat "$dotest/threeway")"= t 503then 504 threeway=t 505fi 506git_apply_opt=$(cat "$dotest/apply-opt") 507iftest"$(cat "$dotest/sign")"= t 508then 509 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e ' 510 s/>.*/>/ 511 s/^/Signed-off-by: /' 512 ` 513else 514 SIGNOFF= 515fi 516 517last=`cat "$dotest/last"` 518this=`cat "$dotest/next"` 519iftest"$skip"= t 520then 521 this=`expr "$this" + 1` 522 resume= 523fi 524 525iftest"$this"-gt"$last" 526then 527 say Nothing to do. 528rm-fr"$dotest" 529exit 530fi 531 532whiletest"$this"-le"$last" 533do 534 msgnum=`printf "%0${prec}d"$this` 535 next=`expr "$this" + 1` 536test -f"$dotest/$msgnum"|| { 537 resume= 538 go_next 539continue 540} 541 542# If we are not resuming, parse and extract the patch information 543# into separate files: 544# - info records the authorship and title 545# - msg is the rest of commit log message 546# - patch is the patch body. 547# 548# When we are resuming, these files are either already prepared 549# by the user, or the user can tell us to do so by --resolved flag. 550case"$resume"in 551'') 552 git mailinfo $keep $scissors $utf8"$dotest/msg""$dotest/patch" \ 553<"$dotest/$msgnum">"$dotest/info"|| 554 stop_here $this 555 556# skip pine's internal folder data 557grep'^Author: Mail System Internal Data$' \ 558<"$dotest"/info >/dev/null && 559 go_next &&continue 560 561test -s"$dotest/patch"|| { 562echo"Patch is empty. Was it split wrong?" 563 stop_here $this 564} 565iftest -f"$dotest/rebasing"&& 566 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \ 567 -e q "$dotest/$msgnum") && 568 test "$(git cat-file -t "$commit")" = commit 569 then 570 git cat-file commit "$commit" | 571 sed -e '1,/^$/d' >"$dotest/msg-clean" 572 else 573 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 574 case "$keep_subject" in -k) SUBJECT="[PATCH]$SUBJECT" ;; esac 575 576 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") | 577 git stripspace > "$dotest/msg-clean" 578 fi 579 ;; 580 esac 581 582 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 583 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 584 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 585 586 if test -z "$GIT_AUTHOR_EMAIL" 587 then 588 echo "Patch does not have a valid e-mail address." 589 stop_here$this 590 fi 591 592 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 593 594 case "$resume" in 595 '') 596 if test '' != "$SIGNOFF" 597 then 598 LAST_SIGNED_OFF_BY=` 599 sed -ne '/^Signed-off-by: /p' \ 600 "$dotest/msg-clean" | 601 sed -ne '$p' 602 ` 603 ADD_SIGNOFF=` 604 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 605 test '' = "$LAST_SIGNED_OFF_BY" && echo 606 echo "$SIGNOFF" 607 }` 608 else 609 ADD_SIGNOFF= 610 fi 611 { 612 if test -s "$dotest/msg-clean" 613 then 614 cat "$dotest/msg-clean" 615 fi 616 if test '' != "$ADD_SIGNOFF" 617 then 618 echo "$ADD_SIGNOFF" 619 fi 620 } >"$dotest/final-commit" 621 ;; 622 *) 623 case "$resolved$interactive" in 624 tt) 625 # This is used only for interactive view option. 626 git diff-index -p --cached HEAD -- >"$dotest/patch" 627 ;; 628 esac 629 esac 630 631 resume= 632 if test "$interactive" = t 633 then 634 test -t 0 || 635 die "cannot be interactive without stdin connected to a terminal." 636 action=again 637 while test "$action" = again 638 do 639 echo "Commit Body is:" 640 echo "--------------------------" 641 cat "$dotest/final-commit" 642 echo "--------------------------" 643 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 644 read reply 645 case "$reply" in 646 [yY]*) action=yes ;; 647 [aA]*) action=yes interactive= ;; 648 [nN]*) action=skip ;; 649 [eE]*) git_editor "$dotest/final-commit" 650 action=again ;; 651 [vV]*) action=again 652 LESS=-S${PAGER:-less}"$dotest/patch" ;; 653 *) action=again ;; 654 esac 655 done 656 else 657 action=yes 658 fi 659 FIRSTLINE=$(sed 1q "$dotest/final-commit") 660 661 if test$action= skip 662 then 663 go_next 664 continue 665 fi 666 667 if test -x "$GIT_DIR"/hooks/applypatch-msg 668 then 669 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || 670 stop_here$this 671 fi 672 673 say "Applying:$FIRSTLINE" 674 675 case "$resolved" in 676 '') 677 # When we are allowed to fall back to 3-way later, don't give 678# false errors during the initial attempt. 679 squelch= 680iftest"$threeway"= t 681then 682 squelch='>/dev/null 2>&1 ' 683fi 684eval"git apply$squelch$git_apply_opt"' --index "$dotest/patch"' 685 apply_status=$? 686;; 687 t) 688# Resolved means the user did all the hard work, and 689# we do not have to do any patch application. Just 690# trust what the user has in the index file and the 691# working tree. 692 resolved= 693 git diff-index --quiet --cached HEAD --&& { 694echo"No changes - did you forget to use 'git add'?" 695 stop_here_user_resolve $this 696} 697 unmerged=$(git ls-files -u) 698iftest -n"$unmerged" 699then 700echo"You still have unmerged paths in your index" 701echo"did you forget to use 'git add'?" 702 stop_here_user_resolve $this 703fi 704 apply_status=0 705 git rerere 706;; 707esac 708 709iftest$apply_status=1&&test"$threeway"= t 710then 711if(fall_back_3way) 712then 713# Applying the patch to an earlier tree and merging the 714# result may have produced the same tree as ours. 715 git diff-index --quiet --cached HEAD --&& { 716 say No changes -- Patch already applied. 717 go_next 718continue 719} 720# clear apply_status -- we have successfully merged. 721 apply_status=0 722fi 723fi 724iftest$apply_status!=0 725then 726printf'Patch failed at %s %s\n'"$msgnum""$FIRSTLINE" 727 stop_here_user_resolve $this 728fi 729 730iftest -x"$GIT_DIR"/hooks/pre-applypatch 731then 732"$GIT_DIR"/hooks/pre-applypatch|| stop_here $this 733fi 734 735 tree=$(git write-tree)&& 736 commit=$( 737iftest -n"$ignore_date" 738then 739 GIT_AUTHOR_DATE= 740fi 741 parent=$(git rev-parse --verify -q HEAD)|| 742 say >&2"applying to an empty history" 743 744iftest -n"$committer_date_is_author_date" 745then 746 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" 747export GIT_COMMITTER_DATE 748fi&& 749 git commit-tree$tree ${parent:+-p} $parent<"$dotest/final-commit" 750) && 751 git update-ref -m"$GIT_REFLOG_ACTION:$FIRSTLINE" HEAD $commit $parent|| 752 stop_here $this 753 754iftest -x"$GIT_DIR"/hooks/post-applypatch 755then 756"$GIT_DIR"/hooks/post-applypatch 757fi 758 759 go_next 760done 761 762git gc --auto 763 764rm-fr"$dotest"