git-am.shon commit Merge branch 'fk/usage' (b0d3e9b)
   1#!/bin/sh
   2#
   3#
   4
   5USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way] <mbox>
   6  or, when resuming [--skip | --resolved]'
   7. git-sh-setup
   8
   9stop_here () {
  10    echo "$1" >"$dotest/next"
  11    exit 1
  12}
  13
  14go_next () {
  15        rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
  16                "$dotest/patch" "$dotest/info"
  17        echo "$next" >"$dotest/next"
  18        this=$next
  19}
  20
  21fall_back_3way () {
  22    O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
  23
  24    rm -fr "$dotest"/patch-merge-*
  25    mkdir "$dotest/patch-merge-tmp-dir"
  26
  27    # First see if the patch records the index info that we can use.
  28    if git-apply -z --index-info "$dotest/patch" \
  29        >"$dotest/patch-merge-index-info" 2>/dev/null &&
  30        GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
  31        git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
  32        GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
  33        git-write-tree >"$dotest/patch-merge-base+" &&
  34        # index has the base tree now.
  35        (
  36            cd "$dotest/patch-merge-tmp-dir" &&
  37            GIT_INDEX_FILE="../patch-merge-tmp-index" \
  38            GIT_OBJECT_DIRECTORY="$O_OBJECT" \
  39            git-apply $binary --index <../patch
  40        )
  41    then
  42        echo Using index info to reconstruct a base tree...
  43        mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
  44        mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
  45    else
  46        # Otherwise, try nearby trees that can be used to apply the
  47        # patch.
  48        (
  49            N=10
  50
  51            # Hoping the patch is against our recent commits...
  52            git-rev-list --max-count=$N HEAD
  53
  54            # or hoping the patch is against known tags...
  55            git-ls-remote --tags .
  56        ) |
  57        while read base junk
  58        do
  59            # See if we have it as a tree...
  60            git-cat-file tree "$base" >/dev/null 2>&1 || continue
  61
  62            rm -fr "$dotest"/patch-merge-* &&
  63            mkdir "$dotest/patch-merge-tmp-dir" || break
  64            (
  65                cd "$dotest/patch-merge-tmp-dir" &&
  66                GIT_INDEX_FILE=../patch-merge-tmp-index &&
  67                GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
  68                export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
  69                git-read-tree "$base" &&
  70                git-apply $binary --index &&
  71                mv ../patch-merge-tmp-index ../patch-merge-index &&
  72                echo "$base" >../patch-merge-base
  73            ) <"$dotest/patch"  2>/dev/null && break
  74        done
  75    fi
  76
  77    test -f "$dotest/patch-merge-index" &&
  78    his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
  79    orig_tree=$(cat "$dotest/patch-merge-base") &&
  80    rm -fr "$dotest"/patch-merge-* || exit 1
  81
  82    echo Falling back to patching base and 3-way merge...
  83
  84    # This is not so wrong.  Depending on which base we picked,
  85    # orig_tree may be wildly different from ours, but his_tree
  86    # has the same set of wildly different changes in parts the
  87    # patch did not touch, so resolve ends up cancelling them,
  88    # saying that we reverted all those changes.
  89
  90    git-merge-resolve $orig_tree -- HEAD $his_tree || {
  91            echo Failed to merge in the changes.
  92            exit 1
  93    }
  94}
  95
  96prec=4
  97dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary=
  98
  99while case "$#" in 0) break;; esac
 100do
 101        case "$1" in
 102        -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
 103        dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
 104        -d|--d|--do|--dot|--dote|--dotes|--dotest)
 105        case "$#" in 1) usage ;; esac; shift
 106        dotest="$1"; shift;;
 107
 108        -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
 109        --interacti|--interactiv|--interactive)
 110        interactive=t; shift ;;
 111
 112        -b|--b|--bi|--bin|--bina|--binar|--binary)
 113        binary=t; shift ;;
 114
 115        -3|--3|--3w|--3wa|--3way)
 116        threeway=t; shift ;;
 117        -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
 118        sign=t; shift ;;
 119        -u|--u|--ut|--utf|--utf8)
 120        utf8=t; shift ;;
 121        -k|--k|--ke|--kee|--keep)
 122        keep=t; shift ;;
 123
 124        -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
 125        resolved=t; shift ;;
 126
 127        --sk|--ski|--skip)
 128        skip=t; shift ;;
 129
 130        --)
 131        shift; break ;;
 132        -*)
 133        usage ;;
 134        *)
 135        break ;;
 136        esac
 137done
 138
 139# If the dotest directory exists, but we have finished applying all the
 140# patches in them, clear it out.
 141if test -d "$dotest" &&
 142   last=$(cat "$dotest/last") &&
 143   next=$(cat "$dotest/next") &&
 144   test $# != 0 &&
 145   test "$next" -gt "$last"
 146then
 147   rm -fr "$dotest"
 148fi
 149
 150if test -d "$dotest"
 151then
 152        test ",$#," = ",0," ||
 153        die "previous dotest directory $dotest still exists but mbox given."
 154        resume=yes
 155else
 156        # Make sure we are not given --skip nor --resolved
 157        test ",$skip,$resolved," = ,,, ||
 158                die "we are not resuming."
 159
 160        # Start afresh.
 161        mkdir -p "$dotest" || exit
 162
 163        # cat does the right thing for us, including '-' to mean
 164        # standard input.
 165        cat "$@" |
 166        git-mailsplit -d$prec "$dotest/" >"$dotest/last" || {
 167                rm -fr "$dotest"
 168                exit 1
 169        }
 170
 171        # -b, -s, -u and -k flags are kept for the resuming session after
 172        # a patch failure.
 173        # -3 and -i can and must be given when resuming.
 174        echo "$binary" >"$dotest/binary"
 175        echo "$sign" >"$dotest/sign"
 176        echo "$utf8" >"$dotest/utf8"
 177        echo "$keep" >"$dotest/keep"
 178        echo 1 >"$dotest/next"
 179fi
 180
 181case "$resolved" in
 182'')
 183        files=$(git-diff-index --cached --name-only HEAD) || exit
 184        if [ "$files" ]; then
 185           echo "Dirty index: cannot apply patches (dirty: $files)" >&2
 186           exit 1
 187        fi
 188esac
 189
 190if test "$(cat "$dotest/binary")" = t
 191then
 192        binary=--allow-binary-replacement
 193fi
 194if test "$(cat "$dotest/utf8")" = t
 195then
 196        utf8=-u
 197fi
 198if test "$(cat "$dotest/keep")" = t
 199then
 200        keep=-k
 201fi
 202if test "$(cat "$dotest/sign")" = t
 203then
 204        SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
 205                        s/>.*/>/
 206                        s/^/Signed-off-by: /'
 207                `
 208else
 209        SIGNOFF=
 210fi
 211
 212last=`cat "$dotest/last"`
 213this=`cat "$dotest/next"`
 214if test "$skip" = t
 215then
 216        this=`expr "$this" + 1`
 217fi
 218
 219if test "$this" -gt "$last"
 220then
 221        echo Nothing to do.
 222        rm -fr "$dotest"
 223        exit
 224fi
 225
 226while test "$this" -le "$last"
 227do
 228        msgnum=`printf "%0${prec}d" $this`
 229        next=`expr "$this" + 1`
 230        test -f "$dotest/$msgnum" || {
 231                go_next
 232                continue
 233        }
 234
 235        # If we are not resuming, parse and extract the patch information
 236        # into separate files:
 237        #  - info records the authorship and title
 238        #  - msg is the rest of commit log message
 239        #  - patch is the patch body.
 240        #
 241        # When we are resuming, these files are either already prepared
 242        # by the user, or the user can tell us to do so by --resolved flag.
 243        case "$resume" in
 244        '')
 245                git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
 246                        <"$dotest/$msgnum" >"$dotest/info" ||
 247                        stop_here $this
 248                git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
 249                ;;
 250        esac
 251
 252        GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
 253        GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
 254        GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
 255        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 256
 257        SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
 258        case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
 259
 260        case "$resume" in
 261        '')
 262            if test '' != "$SIGNOFF"
 263            then
 264                LAST_SIGNED_OFF_BY=`
 265                    sed -ne '/^Signed-off-by: /p' \
 266                    "$dotest/msg-clean" |
 267                    tail -n 1
 268                `
 269                ADD_SIGNOFF=`
 270                    test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
 271                    test '' = "$LAST_SIGNED_OFF_BY" && echo
 272                    echo "$SIGNOFF"
 273                }`
 274            else
 275                ADD_SIGNOFF=
 276            fi
 277            {
 278                echo "$SUBJECT"
 279                if test -s "$dotest/msg-clean"
 280                then
 281                        echo
 282                        cat "$dotest/msg-clean"
 283                fi
 284                if test '' != "$ADD_SIGNOFF"
 285                then
 286                        echo "$ADD_SIGNOFF"
 287                fi
 288            } >"$dotest/final-commit"
 289            ;;
 290        *)
 291                case "$resolved,$interactive" in
 292                tt)
 293                        # This is used only for interactive view option.
 294                        git-diff-index -p --cached HEAD >"$dotest/patch"
 295                        ;;
 296                esac
 297        esac
 298
 299        resume=
 300        if test "$interactive" = t
 301        then
 302            test -t 0 ||
 303            die "cannot be interactive without stdin connected to a terminal."
 304            action=again
 305            while test "$action" = again
 306            do
 307                echo "Commit Body is:"
 308                echo "--------------------------"
 309                cat "$dotest/final-commit"
 310                echo "--------------------------"
 311                printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
 312                read reply
 313                case "$reply" in
 314                [yY]*) action=yes ;;
 315                [aA]*) action=yes interactive= ;;
 316                [nN]*) action=skip ;;
 317                [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
 318                       action=again ;;
 319                [vV]*) action=again
 320                       LESS=-S ${PAGER:-less} "$dotest/patch" ;;
 321                *)     action=again ;;
 322                esac
 323            done
 324        else
 325            action=yes
 326        fi
 327
 328        if test $action = skip
 329        then
 330                go_next
 331                continue
 332        fi
 333
 334        if test -x "$GIT_DIR"/hooks/applypatch-msg
 335        then
 336                "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
 337                stop_here $this
 338        fi
 339
 340        echo
 341        echo "Applying '$SUBJECT'"
 342        echo
 343
 344        case "$resolved" in
 345        '')
 346                git-apply $binary --index "$dotest/patch"
 347                apply_status=$?
 348                ;;
 349        t)
 350                # Resolved means the user did all the hard work, and
 351                # we do not have to do any patch application.  Just
 352                # trust what the user has in the index file and the
 353                # working tree.
 354                resolved=
 355                apply_status=0
 356                ;;
 357        esac
 358
 359        if test $apply_status = 1 && test "$threeway" = t
 360        then
 361                if (fall_back_3way)
 362                then
 363                    # Applying the patch to an earlier tree and merging the
 364                    # result may have produced the same tree as ours.
 365                    changed="$(git-diff-index --cached --name-only -z HEAD)"
 366                    if test '' = "$changed"
 367                    then
 368                            echo No changes -- Patch already applied.
 369                            go_next
 370                            continue
 371                    fi
 372                    # clear apply_status -- we have successfully merged.
 373                    apply_status=0
 374                fi
 375        fi
 376        if test $apply_status != 0
 377        then
 378                echo Patch failed at $msgnum.
 379                stop_here $this
 380        fi
 381
 382        if test -x "$GIT_DIR"/hooks/pre-applypatch
 383        then
 384                "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
 385        fi
 386
 387        tree=$(git-write-tree) &&
 388        echo Wrote tree $tree &&
 389        parent=$(git-rev-parse --verify HEAD) &&
 390        commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
 391        echo Committed: $commit &&
 392        git-update-ref HEAD $commit $parent ||
 393        stop_here $this
 394
 395        if test -x "$GIT_DIR"/hooks/post-applypatch
 396        then
 397                "$GIT_DIR"/hooks/post-applypatch
 398        fi
 399
 400        go_next
 401done
 402
 403rm -fr "$dotest"