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