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