1#!/bin/sh 2# 3# Copyright (c) 2005, 2006 Junio C Hamano 4 5SUBDIRECTORY_OK=Yes 6OPTIONS_KEEPDASHDASH= 7OPTIONS_STUCKLONG=t 8OPTIONS_SPEC="\ 9git am [options] [(<mbox>|<Maildir>)...] 10git am [options] (--continue | --skip | --abort) 11-- 12i,interactive run interactively 13b,binary* (historical option -- no-op) 143,3way allow fall back on 3way merging if needed 15q,quiet be quiet 16s,signoff add a Signed-off-by line to the commit message 17u,utf8 recode into utf8 (default) 18k,keep pass -k flag to git-mailinfo 19keep-non-patch pass -b flag to git-mailinfo 20m,message-id pass -m flag to git-mailinfo 21keep-cr pass --keep-cr flag to git-mailsplit for mbox format 22no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr 23c,scissors strip everything before a scissors line 24whitespace= pass it through git-apply 25ignore-space-change pass it through git-apply 26ignore-whitespace pass it through git-apply 27directory= pass it through git-apply 28exclude= pass it through git-apply 29include= pass it through git-apply 30C= pass it through git-apply 31p= pass it through git-apply 32patch-format= format the patch(es) are in 33reject pass it through git-apply 34resolvemsg= override error message when patch failure occurs 35continue continue applying patches after resolving a conflict 36r,resolved synonyms for --continue 37skip skip the current patch 38abort restore the original branch and abort the patching operation. 39committer-date-is-author-date lie about committer date 40ignore-date use current timestamp for author date 41rerere-autoupdate update the index with reused conflict resolution if possible 42S,gpg-sign? GPG-sign commits 43rebasing* (internal use for git-rebase)" 44 45. git-sh-setup 46. git-sh-i18n 47prefix=$(git rev-parse --show-prefix) 48set_reflog_action am 49require_work_tree 50cd_to_toplevel 51 52git var GIT_COMMITTER_IDENT >/dev/null || 53 die "$(gettext "You need to set your committer info first")" 54 55if git rev-parse --verify -q HEAD >/dev/null 56then 57 HAS_HEAD=yes 58else 59 HAS_HEAD= 60fi 61 62cmdline="git am" 63iftest''!="$interactive" 64then 65 cmdline="$cmdline-i" 66fi 67iftest''!="$threeway" 68then 69 cmdline="$cmdline-3" 70fi 71 72empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904 73 74sq() { 75 git rev-parse --sq-quote"$@" 76} 77 78stop_here () { 79echo"$1">"$dotest/next" 80 git rev-parse --verify -q HEAD >"$dotest/abort-safety" 81exit1 82} 83 84safe_to_abort () { 85iftest -f"$dotest/dirtyindex" 86then 87return1 88fi 89 90if!test -f"$dotest/abort-safety" 91then 92return0 93fi 94 95 abort_safety=$(cat "$dotest/abort-safety") 96iftest"z$(git rev-parse --verify -q HEAD)"="z$abort_safety" 97then 98return0 99fi 100 gettextln "You seem to have moved HEAD since the last 'am' failure. 101Not rewinding to ORIG_HEAD">&2 102return1 103} 104 105stop_here_user_resolve () { 106if[-n"$resolvemsg"];then 107printf'%s\n'"$resolvemsg" 108 stop_here $1 109fi 110 eval_gettextln "When you have resolved this problem, run\"\$cmdline--continue\". 111If you prefer to skip this patch, run\"\$cmdline--skip\"instead. 112To restore the original branch and stop patching, run\"\$cmdline--abort\"." 113 114 stop_here $1 115} 116 117go_next () { 118rm-f"$dotest/$msgnum""$dotest/msg""$dotest/msg-clean" \ 119"$dotest/patch""$dotest/info" 120echo"$next">"$dotest/next" 121 this=$next 122} 123 124cannot_fallback () { 125echo"$1" 126 gettextln "Cannot fall back to three-way merge." 127exit1 128} 129 130fall_back_3way () { 131 O_OBJECT=$(cd "$GIT_OBJECT_DIRECTORY" && pwd) 132 133rm-fr"$dotest"/patch-merge-* 134mkdir"$dotest/patch-merge-tmp-dir" 135 136# First see if the patch records the index info that we can use. 137 cmd="git apply$git_apply_opt--build-fake-ancestor"&& 138 cmd="$cmd"'"$dotest/patch-merge-tmp-index" "$dotest/patch"'&& 139eval"$cmd"&& 140 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ 141 git write-tree>"$dotest/patch-merge-base+"|| 142 cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")" 143 144 say "$(gettext "Using index info to reconstruct a base tree...")" 145 146 cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"' 147 148iftest -z"$GIT_QUIET" 149then 150eval"$cmdgit diff-index --cached --diff-filter=AM --name-status HEAD" 151fi 152 153 cmd="$cmdgit apply --cached$git_apply_opt"' <"$dotest/patch"' 154ifeval"$cmd" 155then 156mv"$dotest/patch-merge-base+""$dotest/patch-merge-base" 157mv"$dotest/patch-merge-tmp-index""$dotest/patch-merge-index" 158else 159 cannot_fallback "$(gettext "Did you hand edit your patch? 160It does not apply to blobs recorded in its index.")" 161fi 162 163test -f"$dotest/patch-merge-index"&& 164 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree)&& 165 orig_tree=$(cat "$dotest/patch-merge-base")&& 166rm-fr"$dotest"/patch-merge-* ||exit1 167 168 say "$(gettext "Falling back to patching base and 3-way merge...")" 169 170# This is not so wrong. Depending on which base we picked, 171# orig_tree may be wildly different from ours, but his_tree 172# has the same set of wildly different changes in parts the 173# patch did not touch, so recursive ends up canceling them, 174# saying that we reverted all those changes. 175 176eval GITHEAD_$his_tree='"$FIRSTLINE"' 177export GITHEAD_$his_tree 178iftest -n"$GIT_QUIET" 179then 180 GIT_MERGE_VERBOSITY=0&&export GIT_MERGE_VERBOSITY 181fi 182 our_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) 183 git-merge-recursive$orig_tree--$our_tree $his_tree|| { 184 git rerere $allow_rerere_autoupdate 185 die "$(gettext "Failed to merge in the changes.")" 186} 187unset GITHEAD_$his_tree 188} 189 190clean_abort () { 191test$#=0||echo>&2"$@" 192rm-fr"$dotest" 193exit1 194} 195 196patch_format= 197 198check_patch_format () { 199# early return if patch_format was set from the command line 200iftest -n"$patch_format" 201then 202return0 203fi 204 205# we default to mbox format if input is from stdin and for 206# directories 207iftest$#=0||test"x$1"="x-"||test -d"$1" 208then 209 patch_format=mbox 210return0 211fi 212 213# otherwise, check the first few non-blank lines of the first 214# patch to try to detect its format 215{ 216# Start from first line containing non-whitespace 217 l1= 218whiletest -z"$l1" 219do 220read l1 ||break 221done 222read l2 223read l3 224case"$l1"in 225"From "* |"From: "*) 226 patch_format=mbox 227;; 228'# This series applies on GIT commit'*) 229 patch_format=stgit-series 230;; 231"# HG changeset patch") 232 patch_format=hg 233;; 234*) 235# if the second line is empty and the third is 236# a From, Author or Date entry, this is very 237# likely an StGIT patch 238case"$l2,$l3"in 239,"From: "* | ,"Author: "* | ,"Date: "*) 240 patch_format=stgit 241;; 242*) 243;; 244esac 245;; 246esac 247iftest -z"$patch_format"&& 248test -n"$l1"&& 249test -n"$l2"&& 250test -n"$l3" 251then 252# This begins with three non-empty lines. Is this a 253# piece of e-mail a-la RFC2822? Grab all the headers, 254# discarding the indented remainder of folded lines, 255# and see if it looks like that they all begin with the 256# header field names... 257tr-d'\015'<"$1"| 258sed-n -e'/^$/q'-e'/^[ ]/d'-e p | 259 sane_egrep -v'^[!-9;-~]+:'>/dev/null || 260 patch_format=mbox 261fi 262} <"$1"|| clean_abort 263} 264 265split_patches () { 266case"$patch_format"in 267 mbox) 268iftest t ="$keepcr" 269then 270 keep_cr=--keep-cr 271else 272 keep_cr= 273fi 274 git mailsplit -d"$prec"-o"$dotest"-b$keep_cr--"$@">"$dotest/last"|| 275 clean_abort 276;; 277 stgit-series) 278iftest$#-ne1 279then 280 clean_abort "$(gettext "Only one StGIT patch series can be applied at once")" 281fi 282 series_dir=$(dirname "$1") 283 series_file="$1" 284shift 285{ 286set x 287whileread filename 288do 289set"$@""$series_dir/$filename" 290done 291# remove the safety x 292shift 293# remove the arg coming from the first-line comment 294shift 295} <"$series_file"|| clean_abort 296# set the patch format appropriately 297 patch_format=stgit 298# now handle the actual StGIT patches 299 split_patches "$@" 300;; 301 stgit) 302 this=0 303for stgit in"$@" 304do 305 this=$(expr "$this" + 1) 306 msgnum=$(printf "%0${prec}d" $this) 307# Perl version of StGIT parse_patch. The first nonemptyline 308# not starting with Author, From or Date is the 309# subject, and the body starts with the next nonempty 310# line not starting with Author, From or Date 311 @@PERL@@ -ne'BEGIN {$subject= 0 } 312 if ($subject> 1) { print ; } 313 elsif (/^\s+$/) { next ; } 314 elsif (/^Author:/) { s/Author/From/ ; print ;} 315 elsif (/^(From|Date)/) { print ; } 316 elsif ($subject) { 317$subject= 2 ; 318 print "\n" ; 319 print ; 320 } else { 321 print "Subject: ",$_; 322$subject= 1; 323 } 324 '<"$stgit">"$dotest/$msgnum"|| clean_abort 325done 326echo"$this">"$dotest/last" 327 this= 328 msgnum= 329;; 330 hg) 331 this=0 332for hg in"$@" 333do 334 this=$(( $this + 1 )) 335 msgnum=$(printf "%0${prec}d" $this) 336# hg stores changeset metadata in #-commented lines preceding 337# the commit message and diff(s). The only metadata we care about 338# are the User and Date (Node ID and Parent are hashes which are 339# only relevant to the hg repository and thus not useful to us) 340# Since we cannot guarantee that the commit message is in 341# git-friendly format, we put no Subject: line and just consume 342# all of the message as the body 343 LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)'-ne'BEGIN {$subject= 0 } 344 if ($subject) { print ; } 345 elsif (/^\# User /) { s/\# User/From:/ ; print ; } 346 elsif (/^\# Date /) { 347 my ($hashsign,$str,$time,$tz) = split ; 348$tz= sprintf "%+05d", (0-$tz)/36; 349 print "Date: " . 350 strftime("%a, %d %b %Y %H:%M:%S ", 351 localtime($time)) 352 . "$tz\n"; 353 } elsif (/^\# /) { next ; } 354 else { 355 print "\n",$_; 356$subject= 1; 357 } 358 '<"$hg">"$dotest/$msgnum"|| clean_abort 359done 360echo"$this">"$dotest/last" 361 this= 362 msgnum= 363;; 364*) 365iftest -n"$patch_format" 366then 367 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")" 368else 369 clean_abort "$(gettext "Patch format detection failed.")" 370fi 371;; 372esac 373} 374 375prec=4 376dotest="$GIT_DIR/rebase-apply" 377sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort= 378messageid= resolvemsg= resume= scissors= no_inbody_headers= 379git_apply_opt= 380committer_date_is_author_date= 381ignore_date= 382allow_rerere_autoupdate= 383gpg_sign_opt= 384threeway= 385 386iftest"$(git config --bool --get am.messageid)"= true 387then 388 messageid=t 389fi 390 391iftest"$(git config --bool --get am.keepcr)"= true 392then 393 keepcr=t 394fi 395 396whiletest$#!=0 397do 398case"$1"in 399-i|--interactive) 400 interactive=t ;; 401-b|--binary) 402 gettextln >&2"The -b/--binary option has been a no-op for long time, and 403it will be removed. Please do not use it anymore." 404;; 405-3|--3way) 406 threeway=t ;; 407-s|--signoff) 408 sign=t ;; 409-u|--utf8) 410 utf8=t ;;# this is now default 411--no-utf8) 412 utf8= ;; 413-m|--message-id) 414 messageid=t ;; 415--no-message-id) 416 messageid=f ;; 417-k|--keep) 418 keep=t ;; 419--keep-non-patch) 420 keep=b ;; 421-c|--scissors) 422 scissors=t ;; 423--no-scissors) 424 scissors=f ;; 425-r|--resolved|--continue) 426 resolved=t ;; 427--skip) 428 skip=t ;; 429--abort) 430 abort=t ;; 431--rebasing) 432 rebasing=t threeway=t ;; 433--resolvemsg=*) 434 resolvemsg="${1#--resolvemsg=}";; 435--whitespace=*|--directory=*|--exclude=*|--include=*) 436 git_apply_opt="$git_apply_opt$(sq "$1")";; 437-C*|-p*) 438 git_apply_opt="$git_apply_opt$(sq "$1")";; 439--patch-format=*) 440 patch_format="${1#--patch-format=}";; 441--reject|--ignore-whitespace|--ignore-space-change) 442 git_apply_opt="$git_apply_opt$1";; 443--committer-date-is-author-date) 444 committer_date_is_author_date=t ;; 445--ignore-date) 446 ignore_date=t ;; 447--rerere-autoupdate|--no-rerere-autoupdate) 448 allow_rerere_autoupdate="$1";; 449-q|--quiet) 450 GIT_QUIET=t ;; 451--keep-cr) 452 keepcr=t ;; 453--no-keep-cr) 454 keepcr=f ;; 455--gpg-sign) 456 gpg_sign_opt=-S;; 457--gpg-sign=*) 458 gpg_sign_opt="-S${1#--gpg-sign=}";; 459--) 460shift;break;; 461*) 462 usage ;; 463esac 464shift 465done 466 467# If the dotest directory exists, but we have finished applying all the 468# patches in them, clear it out. 469iftest -d"$dotest"&& 470test -f"$dotest/last"&& 471test -f"$dotest/next"&& 472 last=$(cat "$dotest/last")&& 473 next=$(cat "$dotest/next")&& 474test$#!=0&& 475test"$next"-gt"$last" 476then 477rm-fr"$dotest" 478fi 479 480iftest -d"$dotest"&&test -f"$dotest/last"&&test -f"$dotest/next" 481then 482case"$#,$skip$resolved$abort"in 4830,*t*) 484# Explicit resume command and we do not have file, so 485# we are happy. 486: ;; 4870,) 488# No file input but without resume parameters; catch 489# user error to feed us a patch from standard input 490# when there is already $dotest. This is somewhat 491# unreliable -- stdin could be /dev/null for example 492# and the caller did not intend to feed us a patch but 493# wanted to continue unattended. 494test -t0 495;; 496*) 497 false 498;; 499esac|| 500 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")" 501 resume=yes 502 503case"$skip,$abort"in 504 t,t) 505 die "$(gettext "Please make up your mind. --skip or --abort?")" 506;; 507 t,) 508 git rerere clear 509 head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree)&& 510 git read-tree --reset -u$head_tree $head_tree&& 511 index_tree=$(git write-tree)&& 512 git read-tree -m -u$index_tree $head_tree 513 git read-tree -m$head_tree 514;; 515,t) 516iftest -f"$dotest/rebasing" 517then 518exec git rebase --abort 519fi 520 git rerere clear 521if safe_to_abort 522then 523 head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree)&& 524 git read-tree --reset -u$head_tree $head_tree&& 525 index_tree=$(git write-tree)&& 526 orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree)&& 527 git read-tree -m -u$index_tree $orig_head 528if git rev-parse --verify -q ORIG_HEAD >/dev/null 2>&1 529then 530 git reset ORIG_HEAD 531else 532 git read-tree$empty_tree 533 curr_branch=$(git symbolic-ref HEAD 2>/dev/null)&& 534 git update-ref -d$curr_branch 535fi 536fi 537rm-fr"$dotest" 538exit;; 539esac 540rm-f"$dotest/dirtyindex" 541else 542# Possible stray $dotest directory in the independent-run 543# case; in the --rebasing case, it is upto the caller 544# (git-rebase--am) to take care of stray directories. 545iftest -d"$dotest"&&test -z"$rebasing" 546then 547case"$skip,$resolved,$abort"in 548,,t) 549rm-fr"$dotest" 550exit0 551;; 552*) 553 die "$(eval_gettext "Stray \$dotest directory found. 554Use \"git am --abort\" to remove it.")" 555;; 556esac 557fi 558 559# Make sure we are not given --skip, --continue, or --abort 560test"$skip$resolved$abort"=""|| 561 die "$(gettext "Resolve operation not in progress, we are not resuming.")" 562 563# Start afresh. 564mkdir-p"$dotest"||exit 565 566iftest -n"$prefix"&&test$#!=0 567then 568 first=t 569for arg 570do 571test -n"$first"&& { 572set x 573 first= 574} 575if is_absolute_path "$arg" 576then 577set"$@""$arg" 578else 579set"$@""$prefix$arg" 580fi 581done 582shift 583fi 584 585 check_patch_format "$@" 586 587 split_patches "$@" 588 589# -i can and must be given when resuming; everything 590# else is kept 591echo"$git_apply_opt">"$dotest/apply-opt" 592echo"$threeway">"$dotest/threeway" 593echo"$sign">"$dotest/sign" 594echo"$utf8">"$dotest/utf8" 595echo"$keep">"$dotest/keep" 596echo"$messageid">"$dotest/messageid" 597echo"$scissors">"$dotest/scissors" 598echo"$no_inbody_headers">"$dotest/no_inbody_headers" 599echo"$GIT_QUIET">"$dotest/quiet" 600echo1>"$dotest/next" 601iftest -n"$rebasing" 602then 603: >"$dotest/rebasing" 604else 605: >"$dotest/applying" 606iftest -n"$HAS_HEAD" 607then 608 git update-ref ORIG_HEAD HEAD 609else 610 git update-ref -d ORIG_HEAD >/dev/null 2>&1 611fi 612fi 613fi 614 615git update-index -q --refresh 616 617case"$resolved"in 618'') 619case"$HAS_HEAD"in 620'') 621 files=$(git ls-files);; 622 ?*) 623 files=$(git diff-index --cached --name-only HEAD --);; 624esac||exit 625iftest"$files" 626then 627test -n"$HAS_HEAD"&& : >"$dotest/dirtyindex" 628 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")" 629 fi 630esac 631 632# Now, decide what command line options we will give to the git 633# commands we invoke, based on the result of parsing command line 634# options and previous invocation state stored in$dotest/ files. 635 636if test "$(cat "$dotest/utf8")" = t 637then 638 utf8=-u 639else 640 utf8=-n 641fi 642keep=$(cat "$dotest/keep") 643case "$keep" in 644t) 645 keep=-k ;; 646b) 647 keep=-b ;; 648*) 649 keep= ;; 650esac 651case "$(cat "$dotest/messageid")" in 652t) 653 messageid=-m ;; 654f) 655 messageid= ;; 656esac 657case "$(cat "$dotest/scissors")" in 658t) 659 scissors=--scissors ;; 660f) 661 scissors=--no-scissors ;; 662esac 663if test "$(cat "$dotest/no_inbody_headers")" = t 664then 665 no_inbody_headers=--no-inbody-headers 666else 667 no_inbody_headers= 668fi 669if test "$(cat "$dotest/quiet")" = t 670then 671 GIT_QUIET=t 672fi 673if test "$(cat "$dotest/threeway")" = t 674then 675 threeway=t 676fi 677git_apply_opt=$(cat "$dotest/apply-opt") 678if test "$(cat "$dotest/sign")" = t 679then 680 SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e ' 681 s/>.*/>/ 682 s/^/Signed-off-by: /' 683 ) 684else 685 SIGNOFF= 686fi 687 688last=$(cat "$dotest/last") 689this=$(cat "$dotest/next") 690if test "$skip" = t 691then 692 this=$(expr "$this" + 1) 693 resume= 694fi 695 696while test "$this" -le "$last" 697do 698 msgnum=$(printf "%0${prec}d" $this) 699 next=$(expr "$this" + 1) 700 test -f "$dotest/$msgnum" || { 701 resume= 702 go_next 703 continue 704 } 705 706 # If we are not resuming, parse and extract the patch information 707 # into separate files: 708 # - info records the authorship and title 709 # - msg is the rest of commit log message 710 # - patch is the patch body. 711 # 712 # When we are resuming, these files are either already prepared 713 # by the user, or the user can tell us to do so by --continue flag. 714 case "$resume" in 715 '') 716 if test -f "$dotest/rebasing" 717 then 718 commit=$(sed -e 's/^From \([0-9a-f]*\).*/\1/' \ 719 -e q "$dotest/$msgnum") && 720 test "$(git cat-file -t "$commit")" = commit || 721 stop_here$this 722 git cat-file commit "$commit" | 723 sed -e '1,/^$/d' >"$dotest/msg-clean" 724 echo "$commit" >"$dotest/original-commit" 725 get_author_ident_from_commit "$commit" >"$dotest/author-script" 726 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch" 727 else 728 git mailinfo$keep$no_inbody_headers$messageid$scissors$utf8"$dotest/msg" "$dotest/patch" \ 729 <"$dotest/$msgnum" >"$dotest/info" || 730 stop_here$this 731 732 # skip pine's internal folder data 733 sane_grep '^Author: Mail System Internal Data$' \ 734 <"$dotest"/info >/dev/null && 735 go_next && continue 736 737 test -s "$dotest/patch" || { 738 eval_gettextln "Patch is empty. Was it split wrong? 739If you would prefer to skip this patch, instead run \"\$cmdline--skip\". 740To restore the original branch and stop patching run \"\$cmdline--abort\"." 741 stop_here$this 742 } 743 rm -f "$dotest/original-commit" "$dotest/author-script" 744 { 745 sed -n '/^Subject/ s/Subject: //p' "$dotest/info" 746 echo 747 cat "$dotest/msg" 748 } | 749 git stripspace > "$dotest/msg-clean" 750 fi 751 ;; 752 esac 753 754 if test -f "$dotest/author-script" 755 then 756 eval$(cat "$dotest/author-script") 757 else 758 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 759 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 760 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 761 fi 762 763 if test -z "$GIT_AUTHOR_EMAIL" 764 then 765 gettextln "Patch does not have a valid e-mail address." 766 stop_here$this 767 fi 768 769 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 770 771 case "$resume" in 772 '') 773 if test '' != "$SIGNOFF" 774 then 775 LAST_SIGNED_OFF_BY=$( 776 sed -ne '/^Signed-off-by: /p' \ 777 "$dotest/msg-clean" | 778 sed -ne '$p' 779 ) 780 ADD_SIGNOFF=$( 781 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 782 test '' = "$LAST_SIGNED_OFF_BY" && echo 783 echo "$SIGNOFF" 784 }) 785 else 786 ADD_SIGNOFF= 787 fi 788 { 789 if test -s "$dotest/msg-clean" 790 then 791 cat "$dotest/msg-clean" 792 fi 793 if test '' != "$ADD_SIGNOFF" 794 then 795 echo "$ADD_SIGNOFF" 796 fi 797 } >"$dotest/final-commit" 798 ;; 799 *) 800 case "$resolved$interactive" in 801 tt) 802 # This is used only for interactive view option. 803 git diff-index -p --cached HEAD -- >"$dotest/patch" 804 ;; 805 esac 806 esac 807 808 resume= 809 if test "$interactive" = t 810 then 811 test -t 0 || 812 die "$(gettext "cannot be interactive without stdin connected to a terminal.")" 813 action=again 814 while test "$action" = again 815 do 816 gettextln "Commit Body is:" 817 echo "--------------------------" 818 cat "$dotest/final-commit" 819 echo "--------------------------" 820 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a] 821 # in your translation. The program will only accept English 822 # input at this point. 823 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 824 read reply 825 case "$reply" in 826 [yY]*) action=yes ;; 827 [aA]*) action=yes interactive= ;; 828 [nN]*) action=skip ;; 829 [eE]*) git_editor "$dotest/final-commit" 830 action=again ;; 831 [vV]*) action=again 832 git_pager "$dotest/patch" ;; 833 *) action=again ;; 834 esac 835 done 836 else 837 action=yes 838 fi 839 840 if test$action= skip 841 then 842 go_next 843 continue 844 fi 845 846 hook="$(git rev-parse --git-path hooks/applypatch-msg)" 847 if test -x "$hook" 848 then 849 "$hook" "$dotest/final-commit" || stop_here$this 850 fi 851 852 if test -f "$dotest/final-commit" 853 then 854 FIRSTLINE=$(sed 1q "$dotest/final-commit") 855 else 856 FIRSTLINE="" 857 fi 858 859 say "$(eval_gettext "Applying: \$FIRSTLINE")" 860 861 case "$resolved" in 862 '') 863 # When we are allowed to fall back to 3-way later, don't give 864 # false errors during the initial attempt. 865 squelch= 866 if test "$threeway" = t 867 then 868 squelch='>/dev/null 2>&1 ' 869 fi 870 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"' 871 apply_status=$? 872 ;; 873 t) 874 # Resolved means the user did all the hard work, and 875 # we do not have to do any patch application. Just 876 # trust what the user has in the index file and the 877 # working tree. 878 resolved= 879 git diff-index --quiet --cached HEAD -- && { 880 gettextln "No changes - did you forget to use 'git add'? 881If there is nothing left to stage, chances are that something else 882already introduced the same changes; you might want to skip this patch." 883 stop_here_user_resolve$this 884 } 885 unmerged=$(git ls-files -u) 886 if test -n "$unmerged" 887 then 888 gettextln "You still have unmerged paths in your index 889did you forget to use 'git add'?" 890 stop_here_user_resolve$this 891 fi 892 apply_status=0 893 git rerere 894 ;; 895 esac 896 897 if test$apply_status!= 0 && test "$threeway" = t 898 then 899 if (fall_back_3way) 900 then 901 # Applying the patch to an earlier tree and merging the 902 # result may have produced the same tree as ours. 903 git diff-index --quiet --cached HEAD -- && { 904 say "$(gettext "No changes -- Patch already applied.")" 905 go_next 906 continue 907 } 908 # clear apply_status -- we have successfully merged. 909 apply_status=0 910 fi 911 fi 912 if test$apply_status!= 0 913 then 914 eval_gettextln 'Patch failed at$msgnum$FIRSTLINE' 915 if test "$(git config --bool advice.amworkdir)" != false 916 then 917 eval_gettextln 'The copy of the patch that failed is found in: 918$dotest/patch' 919 fi 920 stop_here_user_resolve$this 921 fi 922 923 hook="$(git rev-parse --git-path hooks/pre-applypatch)" 924 if test -x "$hook" 925 then 926 "$hook" || stop_here$this 927 fi 928 929 tree=$(git write-tree)&& 930 commit=$( 931 if test -n "$ignore_date" 932 then 933 GIT_AUTHOR_DATE= 934 fi 935 parent=$(git rev-parse --verify -q HEAD)|| 936 say >&2 "$(gettext "applying to an empty history")" 937 938 if test -n "$committer_date_is_author_date" 939 then 940 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" 941 export GIT_COMMITTER_DATE 942 fi && 943 git commit-tree${parent:+-p}$parent${gpg_sign_opt:+"$gpg_sign_opt"}$tree\ 944 <"$dotest/final-commit" 945 ) && 946 git update-ref -m "$GIT_REFLOG_ACTION:$FIRSTLINE" HEAD$commit$parent|| 947 stop_here$this 948 949 if test -f "$dotest/original-commit"; then 950 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten" 951 fi 952 953 hook="$(git rev-parse --git-path hooks/post-applypatch)" 954 test -x "$hook" && "$hook" 955 956 go_next 957done 958 959if test -s "$dotest"/rewritten; then 960 git notes copy --for-rewrite=rebase < "$dotest"/rewritten 961 hook="$(git rev-parse --git-path hooks/post-rewrite)" 962 if test -x "$hook"; then 963 "$hook" rebase < "$dotest"/rewritten 964 fi 965fi 966 967# If am was called with --rebasing (from git-rebase--am), it's up to 968# the caller to take care of housekeeping. 969if ! test -f "$dotest/rebasing" 970then 971 rm -fr "$dotest" 972 git gc --auto 973fi