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 14s,signoff add a Signed-off-by line to the commit message 15u,utf8 recode into utf8 (default) 16k,keep pass -k flag to git-mailinfo 17whitespace= pass it through git-apply 18directory= pass it through git-apply 19C= pass it through git-apply 20p= pass it through git-apply 21reject pass it through git-apply 22resolvemsg= override error message when patch failure occurs 23r,resolved to be used after a patch failure 24skip skip the current patch 25abort restore the original branch and abort the patching operation. 26committer-date-is-author-date lie about committer date 27ignore-date use current timestamp for author date 28rebasing* (internal use for git-rebase)" 29 30. git-sh-setup 31prefix=$(git rev-parse --show-prefix) 32set_reflog_action am 33require_work_tree 34cd_to_toplevel 35 36git var GIT_COMMITTER_IDENT >/dev/null || 37 die "You need to set your committer info first" 38 39if git rev-parse --verify -q HEAD >/dev/null 40then 41 HAS_HEAD=yes 42else 43 HAS_HEAD= 44fi 45 46sq() { 47for sqarg 48do 49printf"%s""$sqarg"| 50sed-e's/'\''/'\''\\'\'''\''/g'-e's/.*/ '\''&'\''/' 51done 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 106echo 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 122echo 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 132 git-merge-recursive$orig_tree-- HEAD $his_tree|| { 133 git rerere 134echo Failed to merge in the changes. 135exit1 136} 137unset GITHEAD_$his_tree 138} 139 140prec=4 141dotest="$GIT_DIR/rebase-apply" 142sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= 143resolvemsg= resume= 144git_apply_opt= 145committer_date_is_author_date= 146ignore_date= 147 148whiletest$#!=0 149do 150case"$1"in 151-i|--interactive) 152 interactive=t ;; 153-b|--binary) 154: ;; 155-3|--3way) 156 threeway=t ;; 157-s|--signoff) 158 sign=t ;; 159-u|--utf8) 160 utf8=t ;;# this is now default 161--no-utf8) 162 utf8= ;; 163-k|--keep) 164 keep=t ;; 165-r|--resolved) 166 resolved=t ;; 167--skip) 168 skip=t ;; 169--abort) 170 abort=t ;; 171--rebasing) 172 rebasing=t threeway=t keep=t ;; 173-d|--dotest) 174 die "-d option is no longer supported. Do not use." 175;; 176--resolvemsg) 177shift; resolvemsg=$1;; 178--whitespace|--directory) 179 git_apply_opt="$git_apply_opt$(sq "$1=$2")";shift;; 180-C|-p) 181 git_apply_opt="$git_apply_opt$(sq "$1$2")";shift;; 182--reject) 183 git_apply_opt="$git_apply_opt$1";; 184--committer-date-is-author-date) 185 committer_date_is_author_date=t ;; 186--ignore-date) 187 ignore_date=t ;; 188--) 189shift;break;; 190*) 191 usage ;; 192esac 193shift 194done 195 196# If the dotest directory exists, but we have finished applying all the 197# patches in them, clear it out. 198iftest -d"$dotest"&& 199 last=$(cat "$dotest/last")&& 200 next=$(cat "$dotest/next")&& 201test$#!=0&& 202test"$next"-gt"$last" 203then 204rm-fr"$dotest" 205fi 206 207iftest -d"$dotest" 208then 209case"$#,$skip$resolved$abort"in 2100,*t*) 211# Explicit resume command and we do not have file, so 212# we are happy. 213: ;; 2140,) 215# No file input but without resume parameters; catch 216# user error to feed us a patch from standard input 217# when there is already $dotest. This is somewhat 218# unreliable -- stdin could be /dev/null for example 219# and the caller did not intend to feed us a patch but 220# wanted to continue unattended. 221test -t0 222;; 223*) 224 false 225;; 226esac|| 227 die "previous rebase directory$doteststill exists but mbox given." 228 resume=yes 229 230case"$skip,$abort"in 231 t,t) 232 die "Please make up your mind. --skip or --abort?" 233;; 234 t,) 235 git rerere clear 236 git read-tree --reset -u HEAD HEAD 237 orig_head=$(cat "$GIT_DIR/ORIG_HEAD") 238 git reset HEAD 239 git update-ref ORIG_HEAD $orig_head 240;; 241,t) 242iftest -f"$dotest/rebasing" 243then 244exec git rebase --abort 245fi 246 git rerere clear 247test -f"$dotest/dirtyindex"|| { 248 git read-tree --reset -u HEAD ORIG_HEAD 249 git reset ORIG_HEAD 250} 251rm-fr"$dotest" 252exit;; 253esac 254rm-f"$dotest/dirtyindex" 255else 256# Make sure we are not given --skip, --resolved, nor --abort 257test"$skip$resolved$abort"=""|| 258 die "Resolve operation not in progress, we are not resuming." 259 260# Start afresh. 261mkdir-p"$dotest"||exit 262 263iftest -n"$prefix"&&test$#!=0 264then 265 first=t 266for arg 267do 268test -n"$first"&& { 269set x 270 first= 271} 272case"$arg"in 273/*) 274set"$@""$arg";; 275*) 276set"$@""$prefix$arg";; 277esac 278done 279shift 280fi 281 git mailsplit -d"$prec"-o"$dotest"-b --"$@">"$dotest/last"|| { 282rm-fr"$dotest" 283exit1 284} 285 286# -s, -u, -k, --whitespace, -3, -C and -p flags are kept 287# for the resuming session after a patch failure. 288# -i can and must be given when resuming. 289echo"$git_apply_opt">"$dotest/apply-opt" 290echo"$threeway">"$dotest/threeway" 291echo"$sign">"$dotest/sign" 292echo"$utf8">"$dotest/utf8" 293echo"$keep">"$dotest/keep" 294echo1>"$dotest/next" 295iftest -n"$rebasing" 296then 297: >"$dotest/rebasing" 298else 299: >"$dotest/applying" 300iftest -n"$HAS_HEAD" 301then 302 git update-ref ORIG_HEAD HEAD 303else 304 git update-ref -d ORIG_HEAD >/dev/null 2>&1 305fi 306fi 307fi 308 309case"$resolved"in 310'') 311case"$HAS_HEAD"in 312'') 313 files=$(git ls-files);; 314 ?*) 315 files=$(git diff-index --cached --name-only HEAD --);; 316esac||exit 317iftest"$files" 318then 319test -n"$HAS_HEAD"&& : >"$dotest/dirtyindex" 320 die "Dirty index: cannot apply patches (dirty:$files)" 321fi 322esac 323 324iftest"$(cat "$dotest/utf8")"= t 325then 326 utf8=-u 327else 328 utf8=-n 329fi 330iftest"$(cat "$dotest/keep")"= t 331then 332 keep=-k 333fi 334iftest"$(cat "$dotest/threeway")"= t 335then 336 threeway=t 337fi 338git_apply_opt=$(cat "$dotest/apply-opt") 339iftest"$(cat "$dotest/sign")"= t 340then 341 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e ' 342 s/>.*/>/ 343 s/^/Signed-off-by: /' 344 ` 345else 346 SIGNOFF= 347fi 348 349last=`cat "$dotest/last"` 350this=`cat "$dotest/next"` 351iftest"$skip"= t 352then 353 this=`expr "$this" + 1` 354 resume= 355fi 356 357iftest"$this"-gt"$last" 358then 359echo Nothing to do. 360rm-fr"$dotest" 361exit 362fi 363 364whiletest"$this"-le"$last" 365do 366 msgnum=`printf "%0${prec}d"$this` 367 next=`expr "$this" + 1` 368test -f"$dotest/$msgnum"|| { 369 resume= 370 go_next 371continue 372} 373 374# If we are not resuming, parse and extract the patch information 375# into separate files: 376# - info records the authorship and title 377# - msg is the rest of commit log message 378# - patch is the patch body. 379# 380# When we are resuming, these files are either already prepared 381# by the user, or the user can tell us to do so by --resolved flag. 382case"$resume"in 383'') 384 git mailinfo $keep $utf8"$dotest/msg""$dotest/patch" \ 385<"$dotest/$msgnum">"$dotest/info"|| 386 stop_here $this 387 388# skip pine's internal folder data 389grep'^Author: Mail System Internal Data$' \ 390<"$dotest"/info >/dev/null && 391 go_next &&continue 392 393test -s"$dotest/patch"|| { 394echo"Patch is empty. Was it split wrong?" 395 stop_here $this 396} 397iftest -f"$dotest/rebasing"&& 398 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \ 399 -e q "$dotest/$msgnum") && 400 test "$(git cat-file -t "$commit")" = commit 401 then 402 git cat-file commit "$commit" | 403 sed -e '1,/^$/d' >"$dotest/msg-clean" 404 else 405 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 406 case "$keep_subject" in -k) SUBJECT="[PATCH]$SUBJECT" ;; esac 407 408 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") | 409 git stripspace > "$dotest/msg-clean" 410 fi 411 ;; 412 esac 413 414 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 415 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 416 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 417 418 if test -z "$GIT_AUTHOR_EMAIL" 419 then 420 echo "Patch does not have a valid e-mail address." 421 stop_here$this 422 fi 423 424 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 425 426 case "$resume" in 427 '') 428 if test '' != "$SIGNOFF" 429 then 430 LAST_SIGNED_OFF_BY=` 431 sed -ne '/^Signed-off-by: /p' \ 432 "$dotest/msg-clean" | 433 sed -ne '$p' 434 ` 435 ADD_SIGNOFF=` 436 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 437 test '' = "$LAST_SIGNED_OFF_BY" && echo 438 echo "$SIGNOFF" 439 }` 440 else 441 ADD_SIGNOFF= 442 fi 443 { 444 if test -s "$dotest/msg-clean" 445 then 446 cat "$dotest/msg-clean" 447 fi 448 if test '' != "$ADD_SIGNOFF" 449 then 450 echo "$ADD_SIGNOFF" 451 fi 452 } >"$dotest/final-commit" 453 ;; 454 *) 455 case "$resolved$interactive" in 456 tt) 457 # This is used only for interactive view option. 458 git diff-index -p --cached HEAD -- >"$dotest/patch" 459 ;; 460 esac 461 esac 462 463 resume= 464 if test "$interactive" = t 465 then 466 test -t 0 || 467 die "cannot be interactive without stdin connected to a terminal." 468 action=again 469 while test "$action" = again 470 do 471 echo "Commit Body is:" 472 echo "--------------------------" 473 cat "$dotest/final-commit" 474 echo "--------------------------" 475 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 476 read reply 477 case "$reply" in 478 [yY]*) action=yes ;; 479 [aA]*) action=yes interactive= ;; 480 [nN]*) action=skip ;; 481 [eE]*) git_editor "$dotest/final-commit" 482 action=again ;; 483 [vV]*) action=again 484 LESS=-S${PAGER:-less}"$dotest/patch" ;; 485 *) action=again ;; 486 esac 487 done 488 else 489 action=yes 490 fi 491 FIRSTLINE=$(sed 1q "$dotest/final-commit") 492 493 if test$action= skip 494 then 495 go_next 496 continue 497 fi 498 499 if test -x "$GIT_DIR"/hooks/applypatch-msg 500 then 501 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || 502 stop_here$this 503 fi 504 505 printf 'Applying: %s\n' "$FIRSTLINE" 506 507 case "$resolved" in 508 '') 509 eval 'git apply '"$git_apply_opt"'--index"$dotest/patch"' 510 apply_status=$? 511 ;; 512 t) 513 # Resolved means the user did all the hard work, and 514 # we do not have to do any patch application. Just 515 # trust what the user has in the index file and the 516 # working tree. 517 resolved= 518 git diff-index --quiet --cached HEAD -- && { 519 echo "No changes - did you forget to use 'git add'?" 520 stop_here_user_resolve$this 521 } 522 unmerged=$(git ls-files -u) 523 if test -n "$unmerged" 524 then 525 echo "You still have unmerged paths in your index" 526 echo "did you forget to use 'git add'?" 527 stop_here_user_resolve$this 528 fi 529 apply_status=0 530 git rerere 531 ;; 532 esac 533 534 if test$apply_status= 1 && test "$threeway" = t 535 then 536 if (fall_back_3way) 537 then 538 # Applying the patch to an earlier tree and merging the 539 # result may have produced the same tree as ours. 540 git diff-index --quiet --cached HEAD -- && { 541 echo No changes -- Patch already applied. 542 go_next 543 continue 544 } 545 # clear apply_status -- we have successfully merged. 546 apply_status=0 547 fi 548 fi 549 if test$apply_status!= 0 550 then 551 printf 'Patch failed at%s %s\n' "$msgnum" "$FIRSTLINE" 552 stop_here_user_resolve$this 553 fi 554 555 if test -x "$GIT_DIR"/hooks/pre-applypatch 556 then 557 "$GIT_DIR"/hooks/pre-applypatch || stop_here$this 558 fi 559 560 tree=$(git write-tree)&& 561 commit=$( 562 if test -n "$ignore_date" 563 then 564 GIT_AUTHOR_DATE= 565 fi 566 parent=$(git rev-parse --verify -q HEAD)|| 567 echo >&2 "applying to an empty history" 568 569 if test -n "$committer_date_is_author_date" 570 then 571 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" 572 export GIT_COMMITTER_DATE 573 fi && 574 git commit-tree$tree${parent:+-p}$parent<"$dotest/final-commit" 575 ) && 576 git update-ref -m "$GIT_REFLOG_ACTION:$FIRSTLINE" HEAD$commit$parent|| 577 stop_here$this 578 579 if test -x "$GIT_DIR"/hooks/post-applypatch 580 then 581 "$GIT_DIR"/hooks/post-applypatch 582 fi 583 584 go_next 585done 586 587git gc --auto 588 589rm -fr "$dotest"