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