git-am.shon commit Merge branch 'th/mergetools-deltawalker' (f4464a3)
   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                : ;;
 384        -3|--3way)
 385                threeway=t ;;
 386        -s|--signoff)
 387                sign=t ;;
 388        -u|--utf8)
 389                utf8=t ;; # this is now default
 390        --no-utf8)
 391                utf8= ;;
 392        -k|--keep)
 393                keep=t ;;
 394        --keep-non-patch)
 395                keep=b ;;
 396        -c|--scissors)
 397                scissors=t ;;
 398        --no-scissors)
 399                scissors=f ;;
 400        -r|--resolved|--continue)
 401                resolved=t ;;
 402        --skip)
 403                skip=t ;;
 404        --abort)
 405                abort=t ;;
 406        --rebasing)
 407                rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
 408        -d|--dotest)
 409                die "$(gettext "-d option is no longer supported.  Do not use.")"
 410                ;;
 411        --resolvemsg)
 412                shift; resolvemsg=$1 ;;
 413        --whitespace|--directory|--exclude)
 414                git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
 415        -C|-p)
 416                git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
 417        --patch-format)
 418                shift ; patch_format="$1" ;;
 419        --reject|--ignore-whitespace|--ignore-space-change)
 420                git_apply_opt="$git_apply_opt $1" ;;
 421        --committer-date-is-author-date)
 422                committer_date_is_author_date=t ;;
 423        --ignore-date)
 424                ignore_date=t ;;
 425        --rerere-autoupdate|--no-rerere-autoupdate)
 426                allow_rerere_autoupdate="$1" ;;
 427        -q|--quiet)
 428                GIT_QUIET=t ;;
 429        --keep-cr)
 430                keepcr=t ;;
 431        --no-keep-cr)
 432                keepcr=f ;;
 433        --)
 434                shift; break ;;
 435        *)
 436                usage ;;
 437        esac
 438        shift
 439done
 440
 441# If the dotest directory exists, but we have finished applying all the
 442# patches in them, clear it out.
 443if test -d "$dotest" &&
 444   last=$(cat "$dotest/last") &&
 445   next=$(cat "$dotest/next") &&
 446   test $# != 0 &&
 447   test "$next" -gt "$last"
 448then
 449   rm -fr "$dotest"
 450fi
 451
 452if test -d "$dotest"
 453then
 454        case "$#,$skip$resolved$abort" in
 455        0,*t*)
 456                # Explicit resume command and we do not have file, so
 457                # we are happy.
 458                : ;;
 459        0,)
 460                # No file input but without resume parameters; catch
 461                # user error to feed us a patch from standard input
 462                # when there is already $dotest.  This is somewhat
 463                # unreliable -- stdin could be /dev/null for example
 464                # and the caller did not intend to feed us a patch but
 465                # wanted to continue unattended.
 466                test -t 0
 467                ;;
 468        *)
 469                false
 470                ;;
 471        esac ||
 472        die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
 473        resume=yes
 474
 475        case "$skip,$abort" in
 476        t,t)
 477                die "$(gettext "Please make up your mind. --skip or --abort?")"
 478                ;;
 479        t,)
 480                git rerere clear
 481                git read-tree --reset -u HEAD HEAD
 482                orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
 483                git reset HEAD
 484                git update-ref ORIG_HEAD $orig_head
 485                ;;
 486        ,t)
 487                if test -f "$dotest/rebasing"
 488                then
 489                        exec git rebase --abort
 490                fi
 491                git rerere clear
 492                if safe_to_abort
 493                then
 494                        git read-tree --reset -u HEAD ORIG_HEAD
 495                        git reset ORIG_HEAD
 496                fi
 497                rm -fr "$dotest"
 498                exit ;;
 499        esac
 500        rm -f "$dotest/dirtyindex"
 501else
 502        # Make sure we are not given --skip, --resolved, nor --abort
 503        test "$skip$resolved$abort" = "" ||
 504                die "$(gettext "Resolve operation not in progress, we are not resuming.")"
 505
 506        # Start afresh.
 507        mkdir -p "$dotest" || exit
 508
 509        if test -n "$prefix" && test $# != 0
 510        then
 511                first=t
 512                for arg
 513                do
 514                        test -n "$first" && {
 515                                set x
 516                                first=
 517                        }
 518                        if is_absolute_path "$arg"
 519                        then
 520                                set "$@" "$arg"
 521                        else
 522                                set "$@" "$prefix$arg"
 523                        fi
 524                done
 525                shift
 526        fi
 527
 528        check_patch_format "$@"
 529
 530        split_patches "$@"
 531
 532        # -i can and must be given when resuming; everything
 533        # else is kept
 534        echo " $git_apply_opt" >"$dotest/apply-opt"
 535        echo "$threeway" >"$dotest/threeway"
 536        echo "$sign" >"$dotest/sign"
 537        echo "$utf8" >"$dotest/utf8"
 538        echo "$keep" >"$dotest/keep"
 539        echo "$scissors" >"$dotest/scissors"
 540        echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
 541        echo "$GIT_QUIET" >"$dotest/quiet"
 542        echo 1 >"$dotest/next"
 543        if test -n "$rebasing"
 544        then
 545                : >"$dotest/rebasing"
 546        else
 547                : >"$dotest/applying"
 548                if test -n "$HAS_HEAD"
 549                then
 550                        git update-ref ORIG_HEAD HEAD
 551                else
 552                        git update-ref -d ORIG_HEAD >/dev/null 2>&1
 553                fi
 554        fi
 555fi
 556
 557git update-index -q --refresh
 558
 559case "$resolved" in
 560'')
 561        case "$HAS_HEAD" in
 562        '')
 563                files=$(git ls-files) ;;
 564        ?*)
 565                files=$(git diff-index --cached --name-only HEAD --) ;;
 566        esac || exit
 567        if test "$files"
 568        then
 569                test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
 570                die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
 571        fi
 572esac
 573
 574# Now, decide what command line options we will give to the git
 575# commands we invoke, based on the result of parsing command line
 576# options and previous invocation state stored in $dotest/ files.
 577
 578if test "$(cat "$dotest/utf8")" = t
 579then
 580        utf8=-u
 581else
 582        utf8=-n
 583fi
 584keep=$(cat "$dotest/keep")
 585case "$keep" in
 586t)
 587        keep=-k ;;
 588b)
 589        keep=-b ;;
 590*)
 591        keep= ;;
 592esac
 593case "$(cat "$dotest/scissors")" in
 594t)
 595        scissors=--scissors ;;
 596f)
 597        scissors=--no-scissors ;;
 598esac
 599if test "$(cat "$dotest/no_inbody_headers")" = t
 600then
 601        no_inbody_headers=--no-inbody-headers
 602else
 603        no_inbody_headers=
 604fi
 605if test "$(cat "$dotest/quiet")" = t
 606then
 607        GIT_QUIET=t
 608fi
 609if test "$(cat "$dotest/threeway")" = t
 610then
 611        threeway=t
 612fi
 613git_apply_opt=$(cat "$dotest/apply-opt")
 614if test "$(cat "$dotest/sign")" = t
 615then
 616        SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
 617                        s/>.*/>/
 618                        s/^/Signed-off-by: /'
 619                `
 620else
 621        SIGNOFF=
 622fi
 623
 624last=`cat "$dotest/last"`
 625this=`cat "$dotest/next"`
 626if test "$skip" = t
 627then
 628        this=`expr "$this" + 1`
 629        resume=
 630fi
 631
 632while test "$this" -le "$last"
 633do
 634        msgnum=`printf "%0${prec}d" $this`
 635        next=`expr "$this" + 1`
 636        test -f "$dotest/$msgnum" || {
 637                resume=
 638                go_next
 639                continue
 640        }
 641
 642        # If we are not resuming, parse and extract the patch information
 643        # into separate files:
 644        #  - info records the authorship and title
 645        #  - msg is the rest of commit log message
 646        #  - patch is the patch body.
 647        #
 648        # When we are resuming, these files are either already prepared
 649        # by the user, or the user can tell us to do so by --resolved flag.
 650        case "$resume" in
 651        '')
 652                git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
 653                        <"$dotest/$msgnum" >"$dotest/info" ||
 654                        stop_here $this
 655
 656                # skip pine's internal folder data
 657                sane_grep '^Author: Mail System Internal Data$' \
 658                        <"$dotest"/info >/dev/null &&
 659                        go_next && continue
 660
 661                test -s "$dotest/patch" || {
 662                        eval_gettextln "Patch is empty.  Was it split wrong?
 663If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
 664To restore the original branch and stop patching run \"\$cmdline --abort\"."
 665                        stop_here $this
 666                }
 667                rm -f "$dotest/original-commit" "$dotest/author-script"
 668                if test -f "$dotest/rebasing" &&
 669                        commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
 670                                -e q "$dotest/$msgnum") &&
 671                        test "$(git cat-file -t "$commit")" = commit
 672                then
 673                        git cat-file commit "$commit" |
 674                        sed -e '1,/^$/d' >"$dotest/msg-clean"
 675                        echo "$commit" > "$dotest/original-commit"
 676                        get_author_ident_from_commit "$commit" > "$dotest/author-script"
 677                else
 678                        {
 679                                sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
 680                                echo
 681                                cat "$dotest/msg"
 682                        } |
 683                        git stripspace > "$dotest/msg-clean"
 684                fi
 685                ;;
 686        esac
 687
 688        if test -f "$dotest/author-script"
 689        then
 690                eval $(cat "$dotest/author-script")
 691        else
 692                GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
 693                GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
 694                GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
 695        fi
 696
 697        if test -z "$GIT_AUTHOR_EMAIL"
 698        then
 699                gettextln "Patch does not have a valid e-mail address."
 700                stop_here $this
 701        fi
 702
 703        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 704
 705        case "$resume" in
 706        '')
 707            if test '' != "$SIGNOFF"
 708            then
 709                LAST_SIGNED_OFF_BY=`
 710                    sed -ne '/^Signed-off-by: /p' \
 711                    "$dotest/msg-clean" |
 712                    sed -ne '$p'
 713                `
 714                ADD_SIGNOFF=`
 715                    test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
 716                    test '' = "$LAST_SIGNED_OFF_BY" && echo
 717                    echo "$SIGNOFF"
 718                }`
 719            else
 720                ADD_SIGNOFF=
 721            fi
 722            {
 723                if test -s "$dotest/msg-clean"
 724                then
 725                        cat "$dotest/msg-clean"
 726                fi
 727                if test '' != "$ADD_SIGNOFF"
 728                then
 729                        echo "$ADD_SIGNOFF"
 730                fi
 731            } >"$dotest/final-commit"
 732            ;;
 733        *)
 734                case "$resolved$interactive" in
 735                tt)
 736                        # This is used only for interactive view option.
 737                        git diff-index -p --cached HEAD -- >"$dotest/patch"
 738                        ;;
 739                esac
 740        esac
 741
 742        resume=
 743        if test "$interactive" = t
 744        then
 745            test -t 0 ||
 746            die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
 747            action=again
 748            while test "$action" = again
 749            do
 750                gettextln "Commit Body is:"
 751                echo "--------------------------"
 752                cat "$dotest/final-commit"
 753                echo "--------------------------"
 754                # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
 755                # in your translation. The program will only accept English
 756                # input at this point.
 757                gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
 758                read reply
 759                case "$reply" in
 760                [yY]*) action=yes ;;
 761                [aA]*) action=yes interactive= ;;
 762                [nN]*) action=skip ;;
 763                [eE]*) git_editor "$dotest/final-commit"
 764                       action=again ;;
 765                [vV]*) action=again
 766                       git_pager "$dotest/patch" ;;
 767                *)     action=again ;;
 768                esac
 769            done
 770        else
 771            action=yes
 772        fi
 773
 774        if test -f "$dotest/final-commit"
 775        then
 776                FIRSTLINE=$(sed 1q "$dotest/final-commit")
 777        else
 778                FIRSTLINE=""
 779        fi
 780
 781        if test $action = skip
 782        then
 783                go_next
 784                continue
 785        fi
 786
 787        if test -x "$GIT_DIR"/hooks/applypatch-msg
 788        then
 789                "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
 790                stop_here $this
 791        fi
 792
 793        say "$(eval_gettext "Applying: \$FIRSTLINE")"
 794
 795        case "$resolved" in
 796        '')
 797                # When we are allowed to fall back to 3-way later, don't give
 798                # false errors during the initial attempt.
 799                squelch=
 800                if test "$threeway" = t
 801                then
 802                        squelch='>/dev/null 2>&1 '
 803                fi
 804                eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
 805                apply_status=$?
 806                ;;
 807        t)
 808                # Resolved means the user did all the hard work, and
 809                # we do not have to do any patch application.  Just
 810                # trust what the user has in the index file and the
 811                # working tree.
 812                resolved=
 813                git diff-index --quiet --cached HEAD -- && {
 814                        gettextln "No changes - did you forget to use 'git add'?
 815If there is nothing left to stage, chances are that something else
 816already introduced the same changes; you might want to skip this patch."
 817                        stop_here_user_resolve $this
 818                }
 819                unmerged=$(git ls-files -u)
 820                if test -n "$unmerged"
 821                then
 822                        gettextln "You still have unmerged paths in your index
 823did you forget to use 'git add'?"
 824                        stop_here_user_resolve $this
 825                fi
 826                apply_status=0
 827                git rerere
 828                ;;
 829        esac
 830
 831        if test $apply_status != 0 && test "$threeway" = t
 832        then
 833                if (fall_back_3way)
 834                then
 835                    # Applying the patch to an earlier tree and merging the
 836                    # result may have produced the same tree as ours.
 837                    git diff-index --quiet --cached HEAD -- && {
 838                        say "$(gettext "No changes -- Patch already applied.")"
 839                        go_next
 840                        continue
 841                    }
 842                    # clear apply_status -- we have successfully merged.
 843                    apply_status=0
 844                fi
 845        fi
 846        if test $apply_status != 0
 847        then
 848                eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
 849                stop_here_user_resolve $this
 850        fi
 851
 852        if test -x "$GIT_DIR"/hooks/pre-applypatch
 853        then
 854                "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
 855        fi
 856
 857        tree=$(git write-tree) &&
 858        commit=$(
 859                if test -n "$ignore_date"
 860                then
 861                        GIT_AUTHOR_DATE=
 862                fi
 863                parent=$(git rev-parse --verify -q HEAD) ||
 864                say >&2 "$(gettext "applying to an empty history")"
 865
 866                if test -n "$committer_date_is_author_date"
 867                then
 868                        GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 869                        export GIT_COMMITTER_DATE
 870                fi &&
 871                git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
 872        ) &&
 873        git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 874        stop_here $this
 875
 876        if test -f "$dotest/original-commit"; then
 877                echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
 878        fi
 879
 880        if test -x "$GIT_DIR"/hooks/post-applypatch
 881        then
 882                "$GIT_DIR"/hooks/post-applypatch
 883        fi
 884
 885        go_next
 886done
 887
 888if test -s "$dotest"/rewritten; then
 889    git notes copy --for-rewrite=rebase < "$dotest"/rewritten
 890    if test -x "$GIT_DIR"/hooks/post-rewrite; then
 891        "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
 892    fi
 893fi
 894
 895rm -fr "$dotest"
 896git gc --auto