git-am.shon commit Merge branch 'ph/rerere-doc' (9d9bfea)
   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
  18keep-non-patch  pass -b flag to git-mailinfo
  19keep-cr         pass --keep-cr flag to git-mailsplit for mbox format
  20no-keep-cr      do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
  21c,scissors      strip everything before a scissors line
  22whitespace=     pass it through git-apply
  23ignore-space-change pass it through git-apply
  24ignore-whitespace pass it through git-apply
  25directory=      pass it through git-apply
  26exclude=        pass it through git-apply
  27C=              pass it through git-apply
  28p=              pass it through git-apply
  29patch-format=   format the patch(es) are in
  30reject          pass it through git-apply
  31resolvemsg=     override error message when patch failure occurs
  32continue        continue applying patches after resolving a conflict
  33r,resolved      synonyms for --continue
  34skip            skip the current patch
  35abort           restore the original branch and abort the patching operation.
  36committer-date-is-author-date    lie about committer date
  37ignore-date     use current timestamp for author date
  38rerere-autoupdate update the index with reused conflict resolution if possible
  39rebasing*       (internal use for git-rebase)"
  40
  41. git-sh-setup
  42. git-sh-i18n
  43prefix=$(git rev-parse --show-prefix)
  44set_reflog_action am
  45require_work_tree
  46cd_to_toplevel
  47
  48git var GIT_COMMITTER_IDENT >/dev/null ||
  49        die "$(gettext "You need to set your committer info first")"
  50
  51if git rev-parse --verify -q HEAD >/dev/null
  52then
  53        HAS_HEAD=yes
  54else
  55        HAS_HEAD=
  56fi
  57
  58cmdline="git am"
  59if test '' != "$interactive"
  60then
  61        cmdline="$cmdline -i"
  62fi
  63if test '' != "$threeway"
  64then
  65        cmdline="$cmdline -3"
  66fi
  67
  68sq () {
  69        git rev-parse --sq-quote "$@"
  70}
  71
  72stop_here () {
  73    echo "$1" >"$dotest/next"
  74    git rev-parse --verify -q HEAD >"$dotest/abort-safety"
  75    exit 1
  76}
  77
  78safe_to_abort () {
  79        if test -f "$dotest/dirtyindex"
  80        then
  81                return 1
  82        fi
  83
  84        if ! test -s "$dotest/abort-safety"
  85        then
  86                return 0
  87        fi
  88
  89        abort_safety=$(cat "$dotest/abort-safety")
  90        if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
  91        then
  92                return 0
  93        fi
  94                gettextln "You seem to have moved HEAD since the last 'am' failure.
  95Not rewinding to ORIG_HEAD" >&2
  96        return 1
  97}
  98
  99stop_here_user_resolve () {
 100    if [ -n "$resolvemsg" ]; then
 101            printf '%s\n' "$resolvemsg"
 102            stop_here $1
 103    fi
 104    eval_gettextln "When you have resolved this problem run \"\$cmdline --resolved\".
 105If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
 106To restore the original branch and stop patching run \"\$cmdline --abort\"."
 107
 108    stop_here $1
 109}
 110
 111go_next () {
 112        rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
 113                "$dotest/patch" "$dotest/info"
 114        echo "$next" >"$dotest/next"
 115        this=$next
 116}
 117
 118cannot_fallback () {
 119        echo "$1"
 120        gettextln "Cannot fall back to three-way merge."
 121        exit 1
 122}
 123
 124fall_back_3way () {
 125    O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
 126
 127    rm -fr "$dotest"/patch-merge-*
 128    mkdir "$dotest/patch-merge-tmp-dir"
 129
 130    # First see if the patch records the index info that we can use.
 131    cmd="git apply $git_apply_opt --build-fake-ancestor" &&
 132    cmd="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
 133    eval "$cmd" &&
 134    GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
 135    git write-tree >"$dotest/patch-merge-base+" ||
 136    cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")"
 137
 138    say Using index info to reconstruct a base tree...
 139
 140    cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
 141    cmd="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
 142    if eval "$cmd"
 143    then
 144        mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
 145        mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
 146    else
 147        cannot_fallback "$(gettext "Did you hand edit your patch?
 148It does not apply to blobs recorded in its index.")"
 149    fi
 150
 151    test -f "$dotest/patch-merge-index" &&
 152    his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
 153    orig_tree=$(cat "$dotest/patch-merge-base") &&
 154    rm -fr "$dotest"/patch-merge-* || exit 1
 155
 156    say "$(gettext "Falling back to patching base and 3-way merge...")"
 157
 158    # This is not so wrong.  Depending on which base we picked,
 159    # orig_tree may be wildly different from ours, but his_tree
 160    # has the same set of wildly different changes in parts the
 161    # patch did not touch, so recursive ends up canceling them,
 162    # saying that we reverted all those changes.
 163
 164    eval GITHEAD_$his_tree='"$FIRSTLINE"'
 165    export GITHEAD_$his_tree
 166    if test -n "$GIT_QUIET"
 167    then
 168            GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
 169    fi
 170    git-merge-recursive $orig_tree -- HEAD $his_tree || {
 171            git rerere $allow_rerere_autoupdate
 172            echo Failed to merge in the changes.
 173            exit 1
 174    }
 175    unset GITHEAD_$his_tree
 176}
 177
 178clean_abort () {
 179        test $# = 0 || echo >&2 "$@"
 180        rm -fr "$dotest"
 181        exit 1
 182}
 183
 184patch_format=
 185
 186check_patch_format () {
 187        # early return if patch_format was set from the command line
 188        if test -n "$patch_format"
 189        then
 190                return 0
 191        fi
 192
 193        # we default to mbox format if input is from stdin and for
 194        # directories
 195        if test $# = 0 || test "x$1" = "x-" || test -d "$1"
 196        then
 197                patch_format=mbox
 198                return 0
 199        fi
 200
 201        # otherwise, check the first few non-blank lines of the first
 202        # patch to try to detect its format
 203        {
 204                # Start from first line containing non-whitespace
 205                l1=
 206                while test -z "$l1"
 207                do
 208                        read l1 || break
 209                done
 210                read l2
 211                read l3
 212                case "$l1" in
 213                "From "* | "From: "*)
 214                        patch_format=mbox
 215                        ;;
 216                '# This series applies on GIT commit'*)
 217                        patch_format=stgit-series
 218                        ;;
 219                "# HG changeset patch")
 220                        patch_format=hg
 221                        ;;
 222                *)
 223                        # if the second line is empty and the third is
 224                        # a From, Author or Date entry, this is very
 225                        # likely an StGIT patch
 226                        case "$l2,$l3" in
 227                        ,"From: "* | ,"Author: "* | ,"Date: "*)
 228                                patch_format=stgit
 229                                ;;
 230                        *)
 231                                ;;
 232                        esac
 233                        ;;
 234                esac
 235                if test -z "$patch_format" &&
 236                        test -n "$l1" &&
 237                        test -n "$l2" &&
 238                        test -n "$l3"
 239                then
 240                        # This begins with three non-empty lines.  Is this a
 241                        # piece of e-mail a-la RFC2822?  Grab all the headers,
 242                        # discarding the indented remainder of folded lines,
 243                        # and see if it looks like that they all begin with the
 244                        # header field names...
 245                        tr -d '\015' <"$1" |
 246                        sed -n -e '/^$/q' -e '/^[       ]/d' -e p |
 247                        sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
 248                        patch_format=mbox
 249                fi
 250        } < "$1" || clean_abort
 251}
 252
 253split_patches () {
 254        case "$patch_format" in
 255        mbox)
 256                if test -n "$rebasing" || test t = "$keepcr"
 257                then
 258                    keep_cr=--keep-cr
 259                else
 260                    keep_cr=
 261                fi
 262                git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
 263                clean_abort
 264                ;;
 265        stgit-series)
 266                if test $# -ne 1
 267                then
 268                        clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
 269                fi
 270                series_dir=`dirname "$1"`
 271                series_file="$1"
 272                shift
 273                {
 274                        set x
 275                        while read filename
 276                        do
 277                                set "$@" "$series_dir/$filename"
 278                        done
 279                        # remove the safety x
 280                        shift
 281                        # remove the arg coming from the first-line comment
 282                        shift
 283                } < "$series_file" || clean_abort
 284                # set the patch format appropriately
 285                patch_format=stgit
 286                # now handle the actual StGIT patches
 287                split_patches "$@"
 288                ;;
 289        stgit)
 290                this=0
 291                for stgit in "$@"
 292                do
 293                        this=`expr "$this" + 1`
 294                        msgnum=`printf "%0${prec}d" $this`
 295                        # Perl version of StGIT parse_patch. The first nonemptyline
 296                        # not starting with Author, From or Date is the
 297                        # subject, and the body starts with the next nonempty
 298                        # line not starting with Author, From or Date
 299                        perl -ne 'BEGIN { $subject = 0 }
 300                                if ($subject > 1) { print ; }
 301                                elsif (/^\s+$/) { next ; }
 302                                elsif (/^Author:/) { s/Author/From/ ; print ;}
 303                                elsif (/^(From|Date)/) { print ; }
 304                                elsif ($subject) {
 305                                        $subject = 2 ;
 306                                        print "\n" ;
 307                                        print ;
 308                                } else {
 309                                        print "Subject: ", $_ ;
 310                                        $subject = 1;
 311                                }
 312                        ' < "$stgit" > "$dotest/$msgnum" || clean_abort
 313                done
 314                echo "$this" > "$dotest/last"
 315                this=
 316                msgnum=
 317                ;;
 318        hg)
 319                this=0
 320                for hg in "$@"
 321                do
 322                        this=$(( $this + 1 ))
 323                        msgnum=$(printf "%0${prec}d" $this)
 324                        # hg stores changeset metadata in #-commented lines preceding
 325                        # the commit message and diff(s). The only metadata we care about
 326                        # are the User and Date (Node ID and Parent are hashes which are
 327                        # only relevant to the hg repository and thus not useful to us)
 328                        # Since we cannot guarantee that the commit message is in
 329                        # git-friendly format, we put no Subject: line and just consume
 330                        # all of the message as the body
 331                        perl -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
 332                                if ($subject) { print ; }
 333                                elsif (/^\# User /) { s/\# User/From:/ ; print ; }
 334                                elsif (/^\# Date /) {
 335                                        my ($hashsign, $str, $time, $tz) = split ;
 336                                        $tz = sprintf "%+05d", (0-$tz)/36;
 337                                        print "Date: " .
 338                                              strftime("%a, %d %b %Y %H:%M:%S ",
 339                                                       localtime($time))
 340                                              . "$tz\n";
 341                                } elsif (/^\# /) { next ; }
 342                                else {
 343                                        print "\n", $_ ;
 344                                        $subject = 1;
 345                                }
 346                        ' <"$hg" >"$dotest/$msgnum" || clean_abort
 347                done
 348                echo "$this" >"$dotest/last"
 349                this=
 350                msgnum=
 351                ;;
 352        *)
 353                if test -n "$patch_format"
 354                then
 355                        clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
 356                else
 357                        clean_abort "$(gettext "Patch format detection failed.")"
 358                fi
 359                ;;
 360        esac
 361}
 362
 363prec=4
 364dotest="$GIT_DIR/rebase-apply"
 365sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
 366resolvemsg= resume= scissors= no_inbody_headers=
 367git_apply_opt=
 368committer_date_is_author_date=
 369ignore_date=
 370allow_rerere_autoupdate=
 371
 372if test "$(git config --bool --get am.keepcr)" = true
 373then
 374    keepcr=t
 375fi
 376
 377while test $# != 0
 378do
 379        case "$1" in
 380        -i|--interactive)
 381                interactive=t ;;
 382        -b|--binary)
 383                echo >&2 "The $1 option has been a no-op for long time, and"
 384                echo >&2 "it will be removed. Please do not use it anymore."
 385                ;;
 386        -3|--3way)
 387                threeway=t ;;
 388        -s|--signoff)
 389                sign=t ;;
 390        -u|--utf8)
 391                utf8=t ;; # this is now default
 392        --no-utf8)
 393                utf8= ;;
 394        -k|--keep)
 395                keep=t ;;
 396        --keep-non-patch)
 397                keep=b ;;
 398        -c|--scissors)
 399                scissors=t ;;
 400        --no-scissors)
 401                scissors=f ;;
 402        -r|--resolved|--continue)
 403                resolved=t ;;
 404        --skip)
 405                skip=t ;;
 406        --abort)
 407                abort=t ;;
 408        --rebasing)
 409                rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
 410        -d|--dotest)
 411                die "$(gettext "-d option is no longer supported.  Do not use.")"
 412                ;;
 413        --resolvemsg)
 414                shift; resolvemsg=$1 ;;
 415        --whitespace|--directory|--exclude)
 416                git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
 417        -C|-p)
 418                git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
 419        --patch-format)
 420                shift ; patch_format="$1" ;;
 421        --reject|--ignore-whitespace|--ignore-space-change)
 422                git_apply_opt="$git_apply_opt $1" ;;
 423        --committer-date-is-author-date)
 424                committer_date_is_author_date=t ;;
 425        --ignore-date)
 426                ignore_date=t ;;
 427        --rerere-autoupdate|--no-rerere-autoupdate)
 428                allow_rerere_autoupdate="$1" ;;
 429        -q|--quiet)
 430                GIT_QUIET=t ;;
 431        --keep-cr)
 432                keepcr=t ;;
 433        --no-keep-cr)
 434                keepcr=f ;;
 435        --)
 436                shift; break ;;
 437        *)
 438                usage ;;
 439        esac
 440        shift
 441done
 442
 443# If the dotest directory exists, but we have finished applying all the
 444# patches in them, clear it out.
 445if test -d "$dotest" &&
 446   last=$(cat "$dotest/last") &&
 447   next=$(cat "$dotest/next") &&
 448   test $# != 0 &&
 449   test "$next" -gt "$last"
 450then
 451   rm -fr "$dotest"
 452fi
 453
 454if test -d "$dotest"
 455then
 456        case "$#,$skip$resolved$abort" in
 457        0,*t*)
 458                # Explicit resume command and we do not have file, so
 459                # we are happy.
 460                : ;;
 461        0,)
 462                # No file input but without resume parameters; catch
 463                # user error to feed us a patch from standard input
 464                # when there is already $dotest.  This is somewhat
 465                # unreliable -- stdin could be /dev/null for example
 466                # and the caller did not intend to feed us a patch but
 467                # wanted to continue unattended.
 468                test -t 0
 469                ;;
 470        *)
 471                false
 472                ;;
 473        esac ||
 474        die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
 475        resume=yes
 476
 477        case "$skip,$abort" in
 478        t,t)
 479                die "$(gettext "Please make up your mind. --skip or --abort?")"
 480                ;;
 481        t,)
 482                git rerere clear
 483                git read-tree --reset -u HEAD HEAD
 484                orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
 485                git reset HEAD
 486                git update-ref ORIG_HEAD $orig_head
 487                ;;
 488        ,t)
 489                if test -f "$dotest/rebasing"
 490                then
 491                        exec git rebase --abort
 492                fi
 493                git rerere clear
 494                if safe_to_abort
 495                then
 496                        git read-tree --reset -u HEAD ORIG_HEAD
 497                        git reset ORIG_HEAD
 498                fi
 499                rm -fr "$dotest"
 500                exit ;;
 501        esac
 502        rm -f "$dotest/dirtyindex"
 503else
 504        # Make sure we are not given --skip, --resolved, nor --abort
 505        test "$skip$resolved$abort" = "" ||
 506                die "$(gettext "Resolve operation not in progress, we are not resuming.")"
 507
 508        # Start afresh.
 509        mkdir -p "$dotest" || exit
 510
 511        if test -n "$prefix" && test $# != 0
 512        then
 513                first=t
 514                for arg
 515                do
 516                        test -n "$first" && {
 517                                set x
 518                                first=
 519                        }
 520                        if is_absolute_path "$arg"
 521                        then
 522                                set "$@" "$arg"
 523                        else
 524                                set "$@" "$prefix$arg"
 525                        fi
 526                done
 527                shift
 528        fi
 529
 530        check_patch_format "$@"
 531
 532        split_patches "$@"
 533
 534        # -i can and must be given when resuming; everything
 535        # else is kept
 536        echo " $git_apply_opt" >"$dotest/apply-opt"
 537        echo "$threeway" >"$dotest/threeway"
 538        echo "$sign" >"$dotest/sign"
 539        echo "$utf8" >"$dotest/utf8"
 540        echo "$keep" >"$dotest/keep"
 541        echo "$scissors" >"$dotest/scissors"
 542        echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
 543        echo "$GIT_QUIET" >"$dotest/quiet"
 544        echo 1 >"$dotest/next"
 545        if test -n "$rebasing"
 546        then
 547                : >"$dotest/rebasing"
 548        else
 549                : >"$dotest/applying"
 550                if test -n "$HAS_HEAD"
 551                then
 552                        git update-ref ORIG_HEAD HEAD
 553                else
 554                        git update-ref -d ORIG_HEAD >/dev/null 2>&1
 555                fi
 556        fi
 557fi
 558
 559git update-index -q --refresh
 560
 561case "$resolved" in
 562'')
 563        case "$HAS_HEAD" in
 564        '')
 565                files=$(git ls-files) ;;
 566        ?*)
 567                files=$(git diff-index --cached --name-only HEAD --) ;;
 568        esac || exit
 569        if test "$files"
 570        then
 571                test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
 572                die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
 573        fi
 574esac
 575
 576# Now, decide what command line options we will give to the git
 577# commands we invoke, based on the result of parsing command line
 578# options and previous invocation state stored in $dotest/ files.
 579
 580if test "$(cat "$dotest/utf8")" = t
 581then
 582        utf8=-u
 583else
 584        utf8=-n
 585fi
 586keep=$(cat "$dotest/keep")
 587case "$keep" in
 588t)
 589        keep=-k ;;
 590b)
 591        keep=-b ;;
 592*)
 593        keep= ;;
 594esac
 595case "$(cat "$dotest/scissors")" in
 596t)
 597        scissors=--scissors ;;
 598f)
 599        scissors=--no-scissors ;;
 600esac
 601if test "$(cat "$dotest/no_inbody_headers")" = t
 602then
 603        no_inbody_headers=--no-inbody-headers
 604else
 605        no_inbody_headers=
 606fi
 607if test "$(cat "$dotest/quiet")" = t
 608then
 609        GIT_QUIET=t
 610fi
 611if test "$(cat "$dotest/threeway")" = t
 612then
 613        threeway=t
 614fi
 615git_apply_opt=$(cat "$dotest/apply-opt")
 616if test "$(cat "$dotest/sign")" = t
 617then
 618        SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
 619                        s/>.*/>/
 620                        s/^/Signed-off-by: /'
 621                `
 622else
 623        SIGNOFF=
 624fi
 625
 626last=`cat "$dotest/last"`
 627this=`cat "$dotest/next"`
 628if test "$skip" = t
 629then
 630        this=`expr "$this" + 1`
 631        resume=
 632fi
 633
 634while test "$this" -le "$last"
 635do
 636        msgnum=`printf "%0${prec}d" $this`
 637        next=`expr "$this" + 1`
 638        test -f "$dotest/$msgnum" || {
 639                resume=
 640                go_next
 641                continue
 642        }
 643
 644        # If we are not resuming, parse and extract the patch information
 645        # into separate files:
 646        #  - info records the authorship and title
 647        #  - msg is the rest of commit log message
 648        #  - patch is the patch body.
 649        #
 650        # When we are resuming, these files are either already prepared
 651        # by the user, or the user can tell us to do so by --resolved flag.
 652        case "$resume" in
 653        '')
 654                git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
 655                        <"$dotest/$msgnum" >"$dotest/info" ||
 656                        stop_here $this
 657
 658                # skip pine's internal folder data
 659                sane_grep '^Author: Mail System Internal Data$' \
 660                        <"$dotest"/info >/dev/null &&
 661                        go_next && continue
 662
 663                test -s "$dotest/patch" || {
 664                        eval_gettextln "Patch is empty.  Was it split wrong?
 665If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
 666To restore the original branch and stop patching run \"\$cmdline --abort\"."
 667                        stop_here $this
 668                }
 669                rm -f "$dotest/original-commit" "$dotest/author-script"
 670                if test -f "$dotest/rebasing" &&
 671                        commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
 672                                -e q "$dotest/$msgnum") &&
 673                        test "$(git cat-file -t "$commit")" = commit
 674                then
 675                        git cat-file commit "$commit" |
 676                        sed -e '1,/^$/d' >"$dotest/msg-clean"
 677                        echo "$commit" > "$dotest/original-commit"
 678                        get_author_ident_from_commit "$commit" > "$dotest/author-script"
 679                else
 680                        {
 681                                sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
 682                                echo
 683                                cat "$dotest/msg"
 684                        } |
 685                        git stripspace > "$dotest/msg-clean"
 686                fi
 687                ;;
 688        esac
 689
 690        if test -f "$dotest/author-script"
 691        then
 692                eval $(cat "$dotest/author-script")
 693        else
 694                GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
 695                GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
 696                GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
 697        fi
 698
 699        if test -z "$GIT_AUTHOR_EMAIL"
 700        then
 701                gettextln "Patch does not have a valid e-mail address."
 702                stop_here $this
 703        fi
 704
 705        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 706
 707        case "$resume" in
 708        '')
 709            if test '' != "$SIGNOFF"
 710            then
 711                LAST_SIGNED_OFF_BY=`
 712                    sed -ne '/^Signed-off-by: /p' \
 713                    "$dotest/msg-clean" |
 714                    sed -ne '$p'
 715                `
 716                ADD_SIGNOFF=`
 717                    test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
 718                    test '' = "$LAST_SIGNED_OFF_BY" && echo
 719                    echo "$SIGNOFF"
 720                }`
 721            else
 722                ADD_SIGNOFF=
 723            fi
 724            {
 725                if test -s "$dotest/msg-clean"
 726                then
 727                        cat "$dotest/msg-clean"
 728                fi
 729                if test '' != "$ADD_SIGNOFF"
 730                then
 731                        echo "$ADD_SIGNOFF"
 732                fi
 733            } >"$dotest/final-commit"
 734            ;;
 735        *)
 736                case "$resolved$interactive" in
 737                tt)
 738                        # This is used only for interactive view option.
 739                        git diff-index -p --cached HEAD -- >"$dotest/patch"
 740                        ;;
 741                esac
 742        esac
 743
 744        resume=
 745        if test "$interactive" = t
 746        then
 747            test -t 0 ||
 748            die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
 749            action=again
 750            while test "$action" = again
 751            do
 752                gettextln "Commit Body is:"
 753                echo "--------------------------"
 754                cat "$dotest/final-commit"
 755                echo "--------------------------"
 756                # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
 757                # in your translation. The program will only accept English
 758                # input at this point.
 759                gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
 760                read reply
 761                case "$reply" in
 762                [yY]*) action=yes ;;
 763                [aA]*) action=yes interactive= ;;
 764                [nN]*) action=skip ;;
 765                [eE]*) git_editor "$dotest/final-commit"
 766                       action=again ;;
 767                [vV]*) action=again
 768                       git_pager "$dotest/patch" ;;
 769                *)     action=again ;;
 770                esac
 771            done
 772        else
 773            action=yes
 774        fi
 775
 776        if test -f "$dotest/final-commit"
 777        then
 778                FIRSTLINE=$(sed 1q "$dotest/final-commit")
 779        else
 780                FIRSTLINE=""
 781        fi
 782
 783        if test $action = skip
 784        then
 785                go_next
 786                continue
 787        fi
 788
 789        if test -x "$GIT_DIR"/hooks/applypatch-msg
 790        then
 791                "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
 792                stop_here $this
 793        fi
 794
 795        say "$(eval_gettext "Applying: \$FIRSTLINE")"
 796
 797        case "$resolved" in
 798        '')
 799                # When we are allowed to fall back to 3-way later, don't give
 800                # false errors during the initial attempt.
 801                squelch=
 802                if test "$threeway" = t
 803                then
 804                        squelch='>/dev/null 2>&1 '
 805                fi
 806                eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
 807                apply_status=$?
 808                ;;
 809        t)
 810                # Resolved means the user did all the hard work, and
 811                # we do not have to do any patch application.  Just
 812                # trust what the user has in the index file and the
 813                # working tree.
 814                resolved=
 815                git diff-index --quiet --cached HEAD -- && {
 816                        gettextln "No changes - did you forget to use 'git add'?
 817If there is nothing left to stage, chances are that something else
 818already introduced the same changes; you might want to skip this patch."
 819                        stop_here_user_resolve $this
 820                }
 821                unmerged=$(git ls-files -u)
 822                if test -n "$unmerged"
 823                then
 824                        gettextln "You still have unmerged paths in your index
 825did you forget to use 'git add'?"
 826                        stop_here_user_resolve $this
 827                fi
 828                apply_status=0
 829                git rerere
 830                ;;
 831        esac
 832
 833        if test $apply_status != 0 && test "$threeway" = t
 834        then
 835                if (fall_back_3way)
 836                then
 837                    # Applying the patch to an earlier tree and merging the
 838                    # result may have produced the same tree as ours.
 839                    git diff-index --quiet --cached HEAD -- && {
 840                        say "$(gettext "No changes -- Patch already applied.")"
 841                        go_next
 842                        continue
 843                    }
 844                    # clear apply_status -- we have successfully merged.
 845                    apply_status=0
 846                fi
 847        fi
 848        if test $apply_status != 0
 849        then
 850                eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
 851                stop_here_user_resolve $this
 852        fi
 853
 854        if test -x "$GIT_DIR"/hooks/pre-applypatch
 855        then
 856                "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
 857        fi
 858
 859        tree=$(git write-tree) &&
 860        commit=$(
 861                if test -n "$ignore_date"
 862                then
 863                        GIT_AUTHOR_DATE=
 864                fi
 865                parent=$(git rev-parse --verify -q HEAD) ||
 866                say >&2 "$(gettext "applying to an empty history")"
 867
 868                if test -n "$committer_date_is_author_date"
 869                then
 870                        GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 871                        export GIT_COMMITTER_DATE
 872                fi &&
 873                git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
 874        ) &&
 875        git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 876        stop_here $this
 877
 878        if test -f "$dotest/original-commit"; then
 879                echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
 880        fi
 881
 882        if test -x "$GIT_DIR"/hooks/post-applypatch
 883        then
 884                "$GIT_DIR"/hooks/post-applypatch
 885        fi
 886
 887        go_next
 888done
 889
 890if test -s "$dotest"/rewritten; then
 891    git notes copy --for-rewrite=rebase < "$dotest"/rewritten
 892    if test -x "$GIT_DIR"/hooks/post-rewrite; then
 893        "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
 894    fi
 895fi
 896
 897rm -fr "$dotest"
 898git gc --auto