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