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) 200 git mailsplit -d"$prec"-o"$dotest"-b --"$@">"$dotest/last"|| 201 clean_abort 202;; 203 stgit-series) 204iftest$#-ne1 205then 206 clean_abort "Only one StGIT patch series can be applied at once" 207fi 208 series_dir=`dirname "$1"` 209 series_file="$1" 210shift 211{ 212set x 213whileread filename 214do 215set"$@""$series_dir/$filename" 216done 217# remove the safety x 218shift 219# remove the arg coming from the first-line comment 220shift 221} <"$series_file"|| clean_abort 222# set the patch format appropriately 223 patch_format=stgit 224# now handle the actual StGIT patches 225 split_patches "$@" 226;; 227 stgit) 228 this=0 229for stgit in"$@" 230do 231 this=`expr "$this" + 1` 232 msgnum=`printf "%0${prec}d"$this` 233# Perl version of StGIT parse_patch. The first nonemptyline 234# not starting with Author, From or Date is the 235# subject, and the body starts with the next nonempty 236# line not starting with Author, From or Date 237 perl -ne'BEGIN {$subject= 0 } 238 if ($subject> 1) { print ; } 239 elsif (/^\s+$/) { next ; } 240 elsif (/^Author:/) { print s/Author/From/ ; } 241 elsif (/^(From|Date)/) { print ; } 242 elsif ($subject) { 243$subject= 2 ; 244 print "\n" ; 245 print ; 246 } else { 247 print "Subject: ",$_; 248$subject= 1; 249 } 250 '<"$stgit">"$dotest/$msgnum"|| clean_abort 251done 252echo"$this">"$dotest/last" 253 this= 254 msgnum= 255;; 256*) 257 clean_abort "Patch format$patch_formatis not supported." 258;; 259esac 260} 261 262prec=4 263dotest="$GIT_DIR/rebase-apply" 264sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= 265resolvemsg= resume= 266git_apply_opt= 267committer_date_is_author_date= 268ignore_date= 269 270whiletest$#!=0 271do 272case"$1"in 273-i|--interactive) 274 interactive=t ;; 275-b|--binary) 276: ;; 277-3|--3way) 278 threeway=t ;; 279-s|--signoff) 280 sign=t ;; 281-u|--utf8) 282 utf8=t ;;# this is now default 283--no-utf8) 284 utf8= ;; 285-k|--keep) 286 keep=t ;; 287-r|--resolved) 288 resolved=t ;; 289--skip) 290 skip=t ;; 291--abort) 292 abort=t ;; 293--rebasing) 294 rebasing=t threeway=t keep=t ;; 295-d|--dotest) 296 die "-d option is no longer supported. Do not use." 297;; 298--resolvemsg) 299shift; resolvemsg=$1;; 300--whitespace|--directory) 301 git_apply_opt="$git_apply_opt$(sq "$1=$2")";shift;; 302-C|-p) 303 git_apply_opt="$git_apply_opt$(sq "$1$2")";shift;; 304--patch-format) 305shift; patch_format="$1";; 306--reject) 307 git_apply_opt="$git_apply_opt$1";; 308--committer-date-is-author-date) 309 committer_date_is_author_date=t ;; 310--ignore-date) 311 ignore_date=t ;; 312-q|--quiet) 313 GIT_QUIET=t ;; 314--) 315shift;break;; 316*) 317 usage ;; 318esac 319shift 320done 321 322# If the dotest directory exists, but we have finished applying all the 323# patches in them, clear it out. 324iftest -d"$dotest"&& 325 last=$(cat "$dotest/last")&& 326 next=$(cat "$dotest/next")&& 327test$#!=0&& 328test"$next"-gt"$last" 329then 330rm-fr"$dotest" 331fi 332 333iftest -d"$dotest" 334then 335case"$#,$skip$resolved$abort"in 3360,*t*) 337# Explicit resume command and we do not have file, so 338# we are happy. 339: ;; 3400,) 341# No file input but without resume parameters; catch 342# user error to feed us a patch from standard input 343# when there is already $dotest. This is somewhat 344# unreliable -- stdin could be /dev/null for example 345# and the caller did not intend to feed us a patch but 346# wanted to continue unattended. 347test -t0 348;; 349*) 350 false 351;; 352esac|| 353 die "previous rebase directory$doteststill exists but mbox given." 354 resume=yes 355 356case"$skip,$abort"in 357 t,t) 358 die "Please make up your mind. --skip or --abort?" 359;; 360 t,) 361 git rerere clear 362 git read-tree --reset -u HEAD HEAD 363 orig_head=$(cat "$GIT_DIR/ORIG_HEAD") 364 git reset HEAD 365 git update-ref ORIG_HEAD $orig_head 366;; 367,t) 368iftest -f"$dotest/rebasing" 369then 370exec git rebase --abort 371fi 372 git rerere clear 373test -f"$dotest/dirtyindex"|| { 374 git read-tree --reset -u HEAD ORIG_HEAD 375 git reset ORIG_HEAD 376} 377rm-fr"$dotest" 378exit;; 379esac 380rm-f"$dotest/dirtyindex" 381else 382# Make sure we are not given --skip, --resolved, nor --abort 383test"$skip$resolved$abort"=""|| 384 die "Resolve operation not in progress, we are not resuming." 385 386# Start afresh. 387mkdir-p"$dotest"||exit 388 389iftest -n"$prefix"&&test$#!=0 390then 391 first=t 392for arg 393do 394test -n"$first"&& { 395set x 396 first= 397} 398case"$arg"in 399/*) 400set"$@""$arg";; 401*) 402set"$@""$prefix$arg";; 403esac 404done 405shift 406fi 407 408 check_patch_format "$@" 409 410 split_patches "$@" 411 412# -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept 413# for the resuming session after a patch failure. 414# -i can and must be given when resuming. 415echo"$git_apply_opt">"$dotest/apply-opt" 416echo"$threeway">"$dotest/threeway" 417echo"$sign">"$dotest/sign" 418echo"$utf8">"$dotest/utf8" 419echo"$keep">"$dotest/keep" 420echo"$GIT_QUIET">"$dotest/quiet" 421echo1>"$dotest/next" 422iftest -n"$rebasing" 423then 424: >"$dotest/rebasing" 425else 426: >"$dotest/applying" 427iftest -n"$HAS_HEAD" 428then 429 git update-ref ORIG_HEAD HEAD 430else 431 git update-ref -d ORIG_HEAD >/dev/null 2>&1 432fi 433fi 434fi 435 436case"$resolved"in 437'') 438case"$HAS_HEAD"in 439'') 440 files=$(git ls-files);; 441 ?*) 442 files=$(git diff-index --cached --name-only HEAD --);; 443esac||exit 444iftest"$files" 445then 446test -n"$HAS_HEAD"&& : >"$dotest/dirtyindex" 447 die "Dirty index: cannot apply patches (dirty:$files)" 448fi 449esac 450 451iftest"$(cat "$dotest/utf8")"= t 452then 453 utf8=-u 454else 455 utf8=-n 456fi 457iftest"$(cat "$dotest/keep")"= t 458then 459 keep=-k 460fi 461iftest"$(cat "$dotest/quiet")"= t 462then 463 GIT_QUIET=t 464fi 465iftest"$(cat "$dotest/threeway")"= t 466then 467 threeway=t 468fi 469git_apply_opt=$(cat "$dotest/apply-opt") 470iftest"$(cat "$dotest/sign")"= t 471then 472 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e ' 473 s/>.*/>/ 474 s/^/Signed-off-by: /' 475 ` 476else 477 SIGNOFF= 478fi 479 480last=`cat "$dotest/last"` 481this=`cat "$dotest/next"` 482iftest"$skip"= t 483then 484 this=`expr "$this" + 1` 485 resume= 486fi 487 488iftest"$this"-gt"$last" 489then 490 say Nothing to do. 491rm-fr"$dotest" 492exit 493fi 494 495whiletest"$this"-le"$last" 496do 497 msgnum=`printf "%0${prec}d"$this` 498 next=`expr "$this" + 1` 499test -f"$dotest/$msgnum"|| { 500 resume= 501 go_next 502continue 503} 504 505# If we are not resuming, parse and extract the patch information 506# into separate files: 507# - info records the authorship and title 508# - msg is the rest of commit log message 509# - patch is the patch body. 510# 511# When we are resuming, these files are either already prepared 512# by the user, or the user can tell us to do so by --resolved flag. 513case"$resume"in 514'') 515 git mailinfo $keep $utf8"$dotest/msg""$dotest/patch" \ 516<"$dotest/$msgnum">"$dotest/info"|| 517 stop_here $this 518 519# skip pine's internal folder data 520grep'^Author: Mail System Internal Data$' \ 521<"$dotest"/info >/dev/null && 522 go_next &&continue 523 524test -s"$dotest/patch"|| { 525echo"Patch is empty. Was it split wrong?" 526 stop_here $this 527} 528iftest -f"$dotest/rebasing"&& 529 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \ 530 -e q "$dotest/$msgnum") && 531 test "$(git cat-file -t "$commit")" = commit 532 then 533 git cat-file commit "$commit" | 534 sed -e '1,/^$/d' >"$dotest/msg-clean" 535 else 536 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 537 case "$keep_subject" in -k) SUBJECT="[PATCH]$SUBJECT" ;; esac 538 539 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") | 540 git stripspace > "$dotest/msg-clean" 541 fi 542 ;; 543 esac 544 545 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 546 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 547 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 548 549 if test -z "$GIT_AUTHOR_EMAIL" 550 then 551 echo "Patch does not have a valid e-mail address." 552 stop_here$this 553 fi 554 555 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 556 557 case "$resume" in 558 '') 559 if test '' != "$SIGNOFF" 560 then 561 LAST_SIGNED_OFF_BY=` 562 sed -ne '/^Signed-off-by: /p' \ 563 "$dotest/msg-clean" | 564 sed -ne '$p' 565 ` 566 ADD_SIGNOFF=` 567 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 568 test '' = "$LAST_SIGNED_OFF_BY" && echo 569 echo "$SIGNOFF" 570 }` 571 else 572 ADD_SIGNOFF= 573 fi 574 { 575 if test -s "$dotest/msg-clean" 576 then 577 cat "$dotest/msg-clean" 578 fi 579 if test '' != "$ADD_SIGNOFF" 580 then 581 echo "$ADD_SIGNOFF" 582 fi 583 } >"$dotest/final-commit" 584 ;; 585 *) 586 case "$resolved$interactive" in 587 tt) 588 # This is used only for interactive view option. 589 git diff-index -p --cached HEAD -- >"$dotest/patch" 590 ;; 591 esac 592 esac 593 594 resume= 595 if test "$interactive" = t 596 then 597 test -t 0 || 598 die "cannot be interactive without stdin connected to a terminal." 599 action=again 600 while test "$action" = again 601 do 602 echo "Commit Body is:" 603 echo "--------------------------" 604 cat "$dotest/final-commit" 605 echo "--------------------------" 606 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 607 read reply 608 case "$reply" in 609 [yY]*) action=yes ;; 610 [aA]*) action=yes interactive= ;; 611 [nN]*) action=skip ;; 612 [eE]*) git_editor "$dotest/final-commit" 613 action=again ;; 614 [vV]*) action=again 615 LESS=-S${PAGER:-less}"$dotest/patch" ;; 616 *) action=again ;; 617 esac 618 done 619 else 620 action=yes 621 fi 622 FIRSTLINE=$(sed 1q "$dotest/final-commit") 623 624 if test$action= skip 625 then 626 go_next 627 continue 628 fi 629 630 if test -x "$GIT_DIR"/hooks/applypatch-msg 631 then 632 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || 633 stop_here$this 634 fi 635 636 say "Applying:$FIRSTLINE" 637 638 case "$resolved" in 639 '') 640 # When we are allowed to fall back to 3-way later, don't give 641# false errors during the initial attempt. 642 squelch= 643iftest"$threeway"= t 644then 645 squelch='>/dev/null 2>&1 ' 646fi 647eval"git apply$squelch$git_apply_opt"' --index "$dotest/patch"' 648 apply_status=$? 649;; 650 t) 651# Resolved means the user did all the hard work, and 652# we do not have to do any patch application. Just 653# trust what the user has in the index file and the 654# working tree. 655 resolved= 656 git diff-index --quiet --cached HEAD --&& { 657echo"No changes - did you forget to use 'git add'?" 658 stop_here_user_resolve $this 659} 660 unmerged=$(git ls-files -u) 661iftest -n"$unmerged" 662then 663echo"You still have unmerged paths in your index" 664echo"did you forget to use 'git add'?" 665 stop_here_user_resolve $this 666fi 667 apply_status=0 668 git rerere 669;; 670esac 671 672iftest$apply_status=1&&test"$threeway"= t 673then 674if(fall_back_3way) 675then 676# Applying the patch to an earlier tree and merging the 677# result may have produced the same tree as ours. 678 git diff-index --quiet --cached HEAD --&& { 679 say No changes -- Patch already applied. 680 go_next 681continue 682} 683# clear apply_status -- we have successfully merged. 684 apply_status=0 685fi 686fi 687iftest$apply_status!=0 688then 689printf'Patch failed at %s %s\n'"$msgnum""$FIRSTLINE" 690 stop_here_user_resolve $this 691fi 692 693iftest -x"$GIT_DIR"/hooks/pre-applypatch 694then 695"$GIT_DIR"/hooks/pre-applypatch|| stop_here $this 696fi 697 698 tree=$(git write-tree)&& 699 commit=$( 700iftest -n"$ignore_date" 701then 702 GIT_AUTHOR_DATE= 703fi 704 parent=$(git rev-parse --verify -q HEAD)|| 705 say >&2"applying to an empty history" 706 707iftest -n"$committer_date_is_author_date" 708then 709 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" 710export GIT_COMMITTER_DATE 711fi&& 712 git commit-tree$tree ${parent:+-p} $parent<"$dotest/final-commit" 713) && 714 git update-ref -m"$GIT_REFLOG_ACTION:$FIRSTLINE" HEAD $commit $parent|| 715 stop_here $this 716 717iftest -x"$GIT_DIR"/hooks/post-applypatch 718then 719"$GIT_DIR"/hooks/post-applypatch 720fi 721 722 go_next 723done 724 725git gc --auto 726 727rm-fr"$dotest"