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