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