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