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