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