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 39sq() { 40for sqarg 41do 42printf"%s""$sqarg"| 43sed-e's/'\''/'\''\\'\'''\''/g'-e's/.*/ '\''&'\''/' 44done 45} 46 47stop_here () { 48echo"$1">"$dotest/next" 49exit1 50} 51 52stop_here_user_resolve () { 53if[-n"$resolvemsg"];then 54printf'%s\n'"$resolvemsg" 55 stop_here $1 56fi 57 cmdline="git am" 58iftest''!="$interactive" 59then 60 cmdline="$cmdline-i" 61fi 62iftest''!="$threeway" 63then 64 cmdline="$cmdline-3" 65fi 66echo"When you have resolved this problem run\"$cmdline--resolved\"." 67echo"If you would prefer to skip this patch, instead run\"$cmdline--skip\"." 68echo"To restore the original branch and stop patching run\"$cmdline--abort\"." 69 70 stop_here $1 71} 72 73go_next () { 74rm-f"$dotest/$msgnum""$dotest/msg""$dotest/msg-clean" \ 75"$dotest/patch""$dotest/info" 76echo"$next">"$dotest/next" 77 this=$next 78} 79 80cannot_fallback () { 81echo"$1" 82echo"Cannot fall back to three-way merge." 83exit1 84} 85 86fall_back_3way () { 87 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd` 88 89rm-fr"$dotest"/patch-merge-* 90mkdir"$dotest/patch-merge-tmp-dir" 91 92# First see if the patch records the index info that we can use. 93 git apply --build-fake-ancestor"$dotest/patch-merge-tmp-index" \ 94"$dotest/patch"&& 95 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ 96 git write-tree>"$dotest/patch-merge-base+"|| 97 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge." 98 99echo Using index info to reconstruct a base tree... 100if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \ 101 git apply --cached<"$dotest/patch" 102then 103mv"$dotest/patch-merge-base+""$dotest/patch-merge-base" 104mv"$dotest/patch-merge-tmp-index""$dotest/patch-merge-index" 105else 106 cannot_fallback "Did you hand edit your patch? 107It does not apply to blobs recorded in its index." 108fi 109 110test -f"$dotest/patch-merge-index"&& 111 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree)&& 112 orig_tree=$(cat "$dotest/patch-merge-base")&& 113rm-fr"$dotest"/patch-merge-* ||exit1 114 115echo Falling back to patching base and 3-way merge... 116 117# This is not so wrong. Depending on which base we picked, 118# orig_tree may be wildly different from ours, but his_tree 119# has the same set of wildly different changes in parts the 120# patch did not touch, so recursive ends up canceling them, 121# saying that we reverted all those changes. 122 123eval GITHEAD_$his_tree='"$FIRSTLINE"' 124export GITHEAD_$his_tree 125 git-merge-recursive$orig_tree-- HEAD $his_tree|| { 126 git rerere 127echo Failed to merge in the changes. 128exit1 129} 130unset GITHEAD_$his_tree 131} 132 133prec=4 134dotest="$GIT_DIR/rebase-apply" 135sign= utf8=t keep= skip= interactive= resolved= rebasing= abort= 136resolvemsg= resume= 137git_apply_opt= 138committer_date_is_author_date= 139ignore_date= 140 141whiletest$#!=0 142do 143case"$1"in 144-i|--interactive) 145 interactive=t ;; 146-b|--binary) 147: ;; 148-3|--3way) 149 threeway=t ;; 150-s|--signoff) 151 sign=t ;; 152-u|--utf8) 153 utf8=t ;;# this is now default 154--no-utf8) 155 utf8= ;; 156-k|--keep) 157 keep=t ;; 158-r|--resolved) 159 resolved=t ;; 160--skip) 161 skip=t ;; 162--abort) 163 abort=t ;; 164--rebasing) 165 rebasing=t threeway=t keep=t ;; 166-d|--dotest) 167 die "-d option is no longer supported. Do not use." 168;; 169--resolvemsg) 170shift; resolvemsg=$1;; 171--whitespace|--directory) 172 git_apply_opt="$git_apply_opt$(sq "$1=$2")";shift;; 173-C|-p) 174 git_apply_opt="$git_apply_opt$(sq "$1$2")";shift;; 175--reject) 176 git_apply_opt="$git_apply_opt$1";; 177--committer-date-is-author-date) 178 committer_date_is_author_date=t ;; 179--ignore-date) 180 ignore_date=t ;; 181--) 182shift;break;; 183*) 184 usage ;; 185esac 186shift 187done 188 189# If the dotest directory exists, but we have finished applying all the 190# patches in them, clear it out. 191iftest -d"$dotest"&& 192 last=$(cat "$dotest/last")&& 193 next=$(cat "$dotest/next")&& 194test$#!=0&& 195test"$next"-gt"$last" 196then 197rm-fr"$dotest" 198fi 199 200iftest -d"$dotest" 201then 202case"$#,$skip$resolved$abort"in 2030,*t*) 204# Explicit resume command and we do not have file, so 205# we are happy. 206: ;; 2070,) 208# No file input but without resume parameters; catch 209# user error to feed us a patch from standard input 210# when there is already $dotest. This is somewhat 211# unreliable -- stdin could be /dev/null for example 212# and the caller did not intend to feed us a patch but 213# wanted to continue unattended. 214test -t0 215;; 216*) 217 false 218;; 219esac|| 220 die "previous rebase directory$doteststill exists but mbox given." 221 resume=yes 222 223case"$skip,$abort"in 224 t,) 225 git rerere clear 226 git read-tree --reset -u HEAD HEAD 227 orig_head=$(cat "$GIT_DIR/ORIG_HEAD") 228 git reset HEAD 229 git update-ref ORIG_HEAD $orig_head 230;; 231,t) 232 git rerere clear 233 git read-tree --reset -u HEAD ORIG_HEAD 234 git reset ORIG_HEAD 235rm-fr"$dotest" 236exit;; 237esac 238else 239# Make sure we are not given --skip, --resolved, nor --abort 240test"$skip$resolved$abort"=""|| 241 die "Resolve operation not in progress, we are not resuming." 242 243# Start afresh. 244mkdir-p"$dotest"||exit 245 246iftest -n"$prefix"&&test$#!=0 247then 248 first=t 249for arg 250do 251test -n"$first"&& { 252set x 253 first= 254} 255case"$arg"in 256/*) 257set"$@""$arg";; 258*) 259set"$@""$prefix$arg";; 260esac 261done 262shift 263fi 264 git mailsplit -d"$prec"-o"$dotest"-b --"$@">"$dotest/last"|| { 265rm-fr"$dotest" 266exit1 267} 268 269# -s, -u, -k, --whitespace, -3, -C and -p flags are kept 270# for the resuming session after a patch failure. 271# -i can and must be given when resuming. 272echo"$git_apply_opt">"$dotest/apply-opt" 273echo"$threeway">"$dotest/threeway" 274echo"$sign">"$dotest/sign" 275echo"$utf8">"$dotest/utf8" 276echo"$keep">"$dotest/keep" 277echo1>"$dotest/next" 278iftest -n"$rebasing" 279then 280: >"$dotest/rebasing" 281else 282: >"$dotest/applying" 283 git update-ref ORIG_HEAD HEAD 284fi 285fi 286 287case"$resolved"in 288'') 289 files=$(git diff-index --cached --name-only HEAD --)||exit 290test"$files"&& die "Dirty index: cannot apply patches (dirty:$files)" 291esac 292 293iftest"$(cat "$dotest/utf8")"= t 294then 295 utf8=-u 296else 297 utf8=-n 298fi 299iftest"$(cat "$dotest/keep")"= t 300then 301 keep=-k 302fi 303iftest"$(cat "$dotest/threeway")"= t 304then 305 threeway=t 306fi 307git_apply_opt=$(cat "$dotest/apply-opt") 308iftest"$(cat "$dotest/sign")"= t 309then 310 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e ' 311 s/>.*/>/ 312 s/^/Signed-off-by: /' 313 ` 314else 315 SIGNOFF= 316fi 317 318last=`cat "$dotest/last"` 319this=`cat "$dotest/next"` 320iftest"$skip"= t 321then 322 this=`expr "$this" + 1` 323 resume= 324fi 325 326iftest"$this"-gt"$last" 327then 328echo Nothing to do. 329rm-fr"$dotest" 330exit 331fi 332 333whiletest"$this"-le"$last" 334do 335 msgnum=`printf "%0${prec}d"$this` 336 next=`expr "$this" + 1` 337test -f"$dotest/$msgnum"|| { 338 resume= 339 go_next 340continue 341} 342 343# If we are not resuming, parse and extract the patch information 344# into separate files: 345# - info records the authorship and title 346# - msg is the rest of commit log message 347# - patch is the patch body. 348# 349# When we are resuming, these files are either already prepared 350# by the user, or the user can tell us to do so by --resolved flag. 351case"$resume"in 352'') 353 git mailinfo $keep $utf8"$dotest/msg""$dotest/patch" \ 354<"$dotest/$msgnum">"$dotest/info"|| 355 stop_here $this 356 357# skip pine's internal folder data 358grep'^Author: Mail System Internal Data$' \ 359<"$dotest"/info >/dev/null && 360 go_next &&continue 361 362test -s"$dotest/patch"|| { 363echo"Patch is empty. Was it split wrong?" 364 stop_here $this 365} 366iftest -f"$dotest/rebasing"&& 367 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \ 368 -e q "$dotest/$msgnum") && 369 test "$(git cat-file -t "$commit")" = commit 370 then 371 git cat-file commit "$commit" | 372 sed -e '1,/^$/d' >"$dotest/msg-clean" 373 else 374 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")" 375 case "$keep_subject" in -k) SUBJECT="[PATCH]$SUBJECT" ;; esac 376 377 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") | 378 git stripspace > "$dotest/msg-clean" 379 fi 380 ;; 381 esac 382 383 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")" 384 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")" 385 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")" 386 387 if test -z "$GIT_AUTHOR_EMAIL" 388 then 389 echo "Patch does not have a valid e-mail address." 390 stop_here$this 391 fi 392 393 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 394 395 case "$resume" in 396 '') 397 if test '' != "$SIGNOFF" 398 then 399 LAST_SIGNED_OFF_BY=` 400 sed -ne '/^Signed-off-by: /p' \ 401 "$dotest/msg-clean" | 402 sed -ne '$p' 403 ` 404 ADD_SIGNOFF=` 405 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || { 406 test '' = "$LAST_SIGNED_OFF_BY" && echo 407 echo "$SIGNOFF" 408 }` 409 else 410 ADD_SIGNOFF= 411 fi 412 { 413 if test -s "$dotest/msg-clean" 414 then 415 cat "$dotest/msg-clean" 416 fi 417 if test '' != "$ADD_SIGNOFF" 418 then 419 echo "$ADD_SIGNOFF" 420 fi 421 } >"$dotest/final-commit" 422 ;; 423 *) 424 case "$resolved$interactive" in 425 tt) 426 # This is used only for interactive view option. 427 git diff-index -p --cached HEAD -- >"$dotest/patch" 428 ;; 429 esac 430 esac 431 432 resume= 433 if test "$interactive" = t 434 then 435 test -t 0 || 436 die "cannot be interactive without stdin connected to a terminal." 437 action=again 438 while test "$action" = again 439 do 440 echo "Commit Body is:" 441 echo "--------------------------" 442 cat "$dotest/final-commit" 443 echo "--------------------------" 444 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all " 445 read reply 446 case "$reply" in 447 [yY]*) action=yes ;; 448 [aA]*) action=yes interactive= ;; 449 [nN]*) action=skip ;; 450 [eE]*) git_editor "$dotest/final-commit" 451 action=again ;; 452 [vV]*) action=again 453 LESS=-S${PAGER:-less}"$dotest/patch" ;; 454 *) action=again ;; 455 esac 456 done 457 else 458 action=yes 459 fi 460 FIRSTLINE=$(sed 1q "$dotest/final-commit") 461 462 if test$action= skip 463 then 464 go_next 465 continue 466 fi 467 468 if test -x "$GIT_DIR"/hooks/applypatch-msg 469 then 470 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" || 471 stop_here$this 472 fi 473 474 printf 'Applying: %s\n' "$FIRSTLINE" 475 476 case "$resolved" in 477 '') 478 eval 'git apply '"$git_apply_opt"'--index"$dotest/patch"' 479 apply_status=$? 480 ;; 481 t) 482 # Resolved means the user did all the hard work, and 483 # we do not have to do any patch application. Just 484 # trust what the user has in the index file and the 485 # working tree. 486 resolved= 487 git diff-index --quiet --cached HEAD -- && { 488 echo "No changes - did you forget to use 'git add'?" 489 stop_here_user_resolve$this 490 } 491 unmerged=$(git ls-files -u) 492 if test -n "$unmerged" 493 then 494 echo "You still have unmerged paths in your index" 495 echo "did you forget to use 'git add'?" 496 stop_here_user_resolve$this 497 fi 498 apply_status=0 499 git rerere 500 ;; 501 esac 502 503 if test$apply_status= 1 && test "$threeway" = t 504 then 505 if (fall_back_3way) 506 then 507 # Applying the patch to an earlier tree and merging the 508 # result may have produced the same tree as ours. 509 git diff-index --quiet --cached HEAD -- && { 510 echo No changes -- Patch already applied. 511 go_next 512 continue 513 } 514 # clear apply_status -- we have successfully merged. 515 apply_status=0 516 fi 517 fi 518 if test$apply_status!= 0 519 then 520 printf 'Patch failed at%s %s\n' "$msgnum" "$FIRSTLINE" 521 stop_here_user_resolve$this 522 fi 523 524 if test -x "$GIT_DIR"/hooks/pre-applypatch 525 then 526 "$GIT_DIR"/hooks/pre-applypatch || stop_here$this 527 fi 528 529 tree=$(git write-tree)&& 530 parent=$(git rev-parse --verify HEAD)&& 531 commit=$( 532 if test -n "$ignore_date" 533 then 534 GIT_AUTHOR_DATE= 535 fi 536 if test -n "$committer_date_is_author_date" 537 then 538 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" 539 export GIT_COMMITTER_DATE 540 fi && 541 git commit-tree$tree-p$parent<"$dotest/final-commit" 542 ) && 543 git update-ref -m "$GIT_REFLOG_ACTION:$FIRSTLINE" HEAD$commit$parent|| 544 stop_here$this 545 546 if test -x "$GIT_DIR"/hooks/post-applypatch 547 then 548 "$GIT_DIR"/hooks/post-applypatch 549 fi 550 551 go_next 552done 553 554git gc --auto 555 556rm -fr "$dotest"