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