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