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