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