git-am.shon commit git-merge-tree: generalize the "traverse <n> trees in sync" functionality (164dcb9)
   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            if test -d "$GIT_DIR/rr-cache"
  92            then
  93                git-rerere
  94            fi
  95            echo Failed to merge in the changes.
  96            exit 1
  97    }
  98}
  99
 100prec=4
 101dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary=
 102
 103while case "$#" in 0) break;; esac
 104do
 105        case "$1" in
 106        -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
 107        dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
 108        -d|--d|--do|--dot|--dote|--dotes|--dotest)
 109        case "$#" in 1) usage ;; esac; shift
 110        dotest="$1"; shift;;
 111
 112        -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
 113        --interacti|--interactiv|--interactive)
 114        interactive=t; shift ;;
 115
 116        -b|--b|--bi|--bin|--bina|--binar|--binary)
 117        binary=t; shift ;;
 118
 119        -3|--3|--3w|--3wa|--3way)
 120        threeway=t; shift ;;
 121        -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
 122        sign=t; shift ;;
 123        -u|--u|--ut|--utf|--utf8)
 124        utf8=t; shift ;;
 125        -k|--k|--ke|--kee|--keep)
 126        keep=t; shift ;;
 127
 128        -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
 129        resolved=t; shift ;;
 130
 131        --sk|--ski|--skip)
 132        skip=t; shift ;;
 133
 134        --)
 135        shift; break ;;
 136        -*)
 137        usage ;;
 138        *)
 139        break ;;
 140        esac
 141done
 142
 143# If the dotest directory exists, but we have finished applying all the
 144# patches in them, clear it out.
 145if test -d "$dotest" &&
 146   last=$(cat "$dotest/last") &&
 147   next=$(cat "$dotest/next") &&
 148   test $# != 0 &&
 149   test "$next" -gt "$last"
 150then
 151   rm -fr "$dotest"
 152fi
 153
 154if test -d "$dotest"
 155then
 156        test ",$#," = ",0," ||
 157        die "previous dotest directory $dotest still exists but mbox given."
 158        resume=yes
 159else
 160        # Make sure we are not given --skip nor --resolved
 161        test ",$skip,$resolved," = ,,, ||
 162                die "we are not resuming."
 163
 164        # Start afresh.
 165        mkdir -p "$dotest" || exit
 166
 167        git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||  {
 168                rm -fr "$dotest"
 169                exit 1
 170        }
 171
 172        # -b, -s, -u and -k flags are kept for the resuming session after
 173        # a patch failure.
 174        # -3 and -i can and must be given when resuming.
 175        echo "$binary" >"$dotest/binary"
 176        echo "$sign" >"$dotest/sign"
 177        echo "$utf8" >"$dotest/utf8"
 178        echo "$keep" >"$dotest/keep"
 179        echo 1 >"$dotest/next"
 180fi
 181
 182case "$resolved" in
 183'')
 184        files=$(git-diff-index --cached --name-only HEAD) || exit
 185        if [ "$files" ]; then
 186           echo "Dirty index: cannot apply patches (dirty: $files)" >&2
 187           exit 1
 188        fi
 189esac
 190
 191if test "$(cat "$dotest/binary")" = t
 192then
 193        binary=--allow-binary-replacement
 194fi
 195if test "$(cat "$dotest/utf8")" = t
 196then
 197        utf8=-u
 198fi
 199if test "$(cat "$dotest/keep")" = t
 200then
 201        keep=-k
 202fi
 203if test "$(cat "$dotest/sign")" = t
 204then
 205        SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
 206                        s/>.*/>/
 207                        s/^/Signed-off-by: /'
 208                `
 209else
 210        SIGNOFF=
 211fi
 212
 213last=`cat "$dotest/last"`
 214this=`cat "$dotest/next"`
 215if test "$skip" = t
 216then
 217        this=`expr "$this" + 1`
 218        resume=
 219fi
 220
 221if test "$this" -gt "$last"
 222then
 223        echo Nothing to do.
 224        rm -fr "$dotest"
 225        exit
 226fi
 227
 228while test "$this" -le "$last"
 229do
 230        msgnum=`printf "%0${prec}d" $this`
 231        next=`expr "$this" + 1`
 232        test -f "$dotest/$msgnum" || {
 233                resume=
 234                go_next
 235                continue
 236        }
 237
 238        # If we are not resuming, parse and extract the patch information
 239        # into separate files:
 240        #  - info records the authorship and title
 241        #  - msg is the rest of commit log message
 242        #  - patch is the patch body.
 243        #
 244        # When we are resuming, these files are either already prepared
 245        # by the user, or the user can tell us to do so by --resolved flag.
 246        case "$resume" in
 247        '')
 248                git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
 249                        <"$dotest/$msgnum" >"$dotest/info" ||
 250                        stop_here $this
 251                git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
 252                ;;
 253        esac
 254
 255        GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
 256        GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
 257        GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
 258
 259        if test -z "$GIT_AUTHOR_EMAIL"
 260        then
 261                echo "Patch does not have a valid e-mail address."
 262                stop_here $this
 263        fi
 264
 265        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 266
 267        SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
 268        case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
 269
 270        case "$resume" in
 271        '')
 272            if test '' != "$SIGNOFF"
 273            then
 274                LAST_SIGNED_OFF_BY=`
 275                    sed -ne '/^Signed-off-by: /p' \
 276                    "$dotest/msg-clean" |
 277                    tail -n 1
 278                `
 279                ADD_SIGNOFF=`
 280                    test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
 281                    test '' = "$LAST_SIGNED_OFF_BY" && echo
 282                    echo "$SIGNOFF"
 283                }`
 284            else
 285                ADD_SIGNOFF=
 286            fi
 287            {
 288                echo "$SUBJECT"
 289                if test -s "$dotest/msg-clean"
 290                then
 291                        echo
 292                        cat "$dotest/msg-clean"
 293                fi
 294                if test '' != "$ADD_SIGNOFF"
 295                then
 296                        echo "$ADD_SIGNOFF"
 297                fi
 298            } >"$dotest/final-commit"
 299            ;;
 300        *)
 301                case "$resolved,$interactive" in
 302                tt)
 303                        # This is used only for interactive view option.
 304                        git-diff-index -p --cached HEAD >"$dotest/patch"
 305                        ;;
 306                esac
 307        esac
 308
 309        resume=
 310        if test "$interactive" = t
 311        then
 312            test -t 0 ||
 313            die "cannot be interactive without stdin connected to a terminal."
 314            action=again
 315            while test "$action" = again
 316            do
 317                echo "Commit Body is:"
 318                echo "--------------------------"
 319                cat "$dotest/final-commit"
 320                echo "--------------------------"
 321                printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
 322                read reply
 323                case "$reply" in
 324                [yY]*) action=yes ;;
 325                [aA]*) action=yes interactive= ;;
 326                [nN]*) action=skip ;;
 327                [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
 328                       action=again ;;
 329                [vV]*) action=again
 330                       LESS=-S ${PAGER:-less} "$dotest/patch" ;;
 331                *)     action=again ;;
 332                esac
 333            done
 334        else
 335            action=yes
 336        fi
 337
 338        if test $action = skip
 339        then
 340                go_next
 341                continue
 342        fi
 343
 344        if test -x "$GIT_DIR"/hooks/applypatch-msg
 345        then
 346                "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
 347                stop_here $this
 348        fi
 349
 350        echo
 351        echo "Applying '$SUBJECT'"
 352        echo
 353
 354        case "$resolved" in
 355        '')
 356                git-apply $binary --index "$dotest/patch"
 357                apply_status=$?
 358                ;;
 359        t)
 360                # Resolved means the user did all the hard work, and
 361                # we do not have to do any patch application.  Just
 362                # trust what the user has in the index file and the
 363                # working tree.
 364                resolved=
 365                apply_status=0
 366                ;;
 367        esac
 368
 369        if test $apply_status = 1 && test "$threeway" = t
 370        then
 371                if (fall_back_3way)
 372                then
 373                    # Applying the patch to an earlier tree and merging the
 374                    # result may have produced the same tree as ours.
 375                    changed="$(git-diff-index --cached --name-only -z HEAD)"
 376                    if test '' = "$changed"
 377                    then
 378                            echo No changes -- Patch already applied.
 379                            go_next
 380                            continue
 381                    fi
 382                    # clear apply_status -- we have successfully merged.
 383                    apply_status=0
 384                fi
 385        fi
 386        if test $apply_status != 0
 387        then
 388                echo Patch failed at $msgnum.
 389                stop_here $this
 390        fi
 391
 392        if test -x "$GIT_DIR"/hooks/pre-applypatch
 393        then
 394                "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
 395        fi
 396
 397        tree=$(git-write-tree) &&
 398        echo Wrote tree $tree &&
 399        parent=$(git-rev-parse --verify HEAD) &&
 400        commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
 401        echo Committed: $commit &&
 402        git-update-ref HEAD $commit $parent ||
 403        stop_here $this
 404
 405        if test -x "$GIT_DIR"/hooks/post-applypatch
 406        then
 407                "$GIT_DIR"/hooks/post-applypatch
 408        fi
 409
 410        go_next
 411done
 412
 413rm -fr "$dotest"