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