git-merge.shon commit Future-proof source for changes in xdemitconf_t (30b2501)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6USAGE='[-n] [--summary] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
   7
   8SUBDIRECTORY_OK=Yes
   9. git-sh-setup
  10require_work_tree
  11cd_to_toplevel
  12
  13test -z "$(git ls-files -u)" ||
  14        die "You are in the middle of a conflicted merge."
  15
  16LF='
  17'
  18
  19all_strategies='recur recursive octopus resolve stupid ours subtree'
  20default_twohead_strategies='recursive'
  21default_octopus_strategies='octopus'
  22no_trivial_merge_strategies='ours subtree'
  23use_strategies=
  24
  25index_merge=t
  26
  27dropsave() {
  28        rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
  29                 "$GIT_DIR/MERGE_SAVE" || exit 1
  30}
  31
  32savestate() {
  33        # Stash away any local modifications.
  34        git diff-index -z --name-only $head |
  35        cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
  36}
  37
  38restorestate() {
  39        if test -f "$GIT_DIR/MERGE_SAVE"
  40        then
  41                git reset --hard $head >/dev/null
  42                cpio -iuv <"$GIT_DIR/MERGE_SAVE"
  43                git update-index --refresh >/dev/null
  44        fi
  45}
  46
  47finish_up_to_date () {
  48        case "$squash" in
  49        t)
  50                echo "$1 (nothing to squash)" ;;
  51        '')
  52                echo "$1" ;;
  53        esac
  54        dropsave
  55}
  56
  57squash_message () {
  58        echo Squashed commit of the following:
  59        echo
  60        git log --no-merges ^"$head" $remote
  61}
  62
  63finish () {
  64        if test '' = "$2"
  65        then
  66                rlogm="$GIT_REFLOG_ACTION"
  67        else
  68                echo "$2"
  69                rlogm="$GIT_REFLOG_ACTION: $2"
  70        fi
  71        case "$squash" in
  72        t)
  73                echo "Squash commit -- not updating HEAD"
  74                squash_message >"$GIT_DIR/SQUASH_MSG"
  75                ;;
  76        '')
  77                case "$merge_msg" in
  78                '')
  79                        echo "No merge message -- not updating HEAD"
  80                        ;;
  81                *)
  82                        git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
  83                        ;;
  84                esac
  85                ;;
  86        esac
  87        case "$1" in
  88        '')
  89                ;;
  90        ?*)
  91                if test "$show_diffstat" = t
  92                then
  93                        # We want color (if set), but no pager
  94                        GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
  95                fi
  96                ;;
  97        esac
  98}
  99
 100merge_name () {
 101        remote="$1"
 102        rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
 103        bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
 104        if test "$rh" = "$bh"
 105        then
 106                echo "$rh               branch '$remote' of ."
 107        elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
 108                git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
 109        then
 110                echo "$rh               branch '$truname' (early part) of ."
 111        elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
 112        then
 113                sed -e 's/      not-for-merge   /               /' -e 1q \
 114                        "$GIT_DIR/FETCH_HEAD"
 115        else
 116                echo "$rh               commit '$remote'"
 117        fi
 118}
 119
 120case "$#" in 0) usage ;; esac
 121
 122have_message=
 123while case "$#" in 0) break ;; esac
 124do
 125        case "$1" in
 126        -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
 127                --no-summa|--no-summar|--no-summary)
 128                show_diffstat=false ;;
 129        --summary)
 130                show_diffstat=t ;;
 131        --sq|--squ|--squa|--squas|--squash)
 132                squash=t no_commit=t ;;
 133        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
 134                no_commit=t ;;
 135        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
 136                --strateg=*|--strategy=*|\
 137        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
 138                case "$#,$1" in
 139                *,*=*)
 140                        strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
 141                1,*)
 142                        usage ;;
 143                *)
 144                        strategy="$2"
 145                        shift ;;
 146                esac
 147                case " $all_strategies " in
 148                *" $strategy "*)
 149                        use_strategies="$use_strategies$strategy " ;;
 150                *)
 151                        die "available strategies are: $all_strategies" ;;
 152                esac
 153                ;;
 154        -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
 155                merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
 156                have_message=t
 157                ;;
 158        -m|--m|--me|--mes|--mess|--messa|--messag|--message)
 159                shift
 160                case "$#" in
 161                1)      usage ;;
 162                esac
 163                merge_msg="$1"
 164                have_message=t
 165                ;;
 166        -*)     usage ;;
 167        *)      break ;;
 168        esac
 169        shift
 170done
 171
 172if test -z "$show_diffstat"; then
 173    test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
 174    test -z "$show_diffstat" && show_diffstat=t
 175fi
 176
 177# This could be traditional "merge <msg> HEAD <commit>..."  and the
 178# way we can tell it is to see if the second token is HEAD, but some
 179# people might have misused the interface and used a committish that
 180# is the same as HEAD there instead.  Traditional format never would
 181# have "-m" so it is an additional safety measure to check for it.
 182
 183if test -z "$have_message" &&
 184        second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
 185        head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
 186        test "$second_token" = "$head_commit"
 187then
 188        merge_msg="$1"
 189        shift
 190        head_arg="$1"
 191        shift
 192elif ! git rev-parse --verify HEAD >/dev/null 2>&1
 193then
 194        # If the merged head is a valid one there is no reason to
 195        # forbid "git merge" into a branch yet to be born.  We do
 196        # the same for "git pull".
 197        if test 1 -ne $#
 198        then
 199                echo >&2 "Can merge only exactly one commit into empty head"
 200                exit 1
 201        fi
 202
 203        rh=$(git rev-parse --verify "$1^0") ||
 204                die "$1 - not something we can merge"
 205
 206        git update-ref -m "initial pull" HEAD "$rh" "" &&
 207        git read-tree --reset -u HEAD
 208        exit
 209
 210else
 211        # We are invoked directly as the first-class UI.
 212        head_arg=HEAD
 213
 214        # All the rest are the commits being merged; prepare
 215        # the standard merge summary message to be appended to
 216        # the given message.  If remote is invalid we will die
 217        # later in the common codepath so we discard the error
 218        # in this loop.
 219        merge_name=$(for remote
 220                do
 221                        merge_name "$remote"
 222                done | git fmt-merge-msg
 223        )
 224        merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
 225fi
 226head=$(git rev-parse --verify "$head_arg"^0) || usage
 227
 228# All the rest are remote heads
 229test "$#" = 0 && usage ;# we need at least one remote head.
 230set_reflog_action "merge $*"
 231
 232remoteheads=
 233for remote
 234do
 235        remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
 236            die "$remote - not something we can merge"
 237        remoteheads="${remoteheads}$remotehead "
 238        eval GITHEAD_$remotehead='"$remote"'
 239        export GITHEAD_$remotehead
 240done
 241set x $remoteheads ; shift
 242
 243case "$use_strategies" in
 244'')
 245        case "$#" in
 246        1)
 247                var="`git config --get pull.twohead`"
 248                if test -n "$var"
 249                then
 250                        use_strategies="$var"
 251                else
 252                        use_strategies="$default_twohead_strategies"
 253                fi ;;
 254        *)
 255                var="`git config --get pull.octopus`"
 256                if test -n "$var"
 257                then
 258                        use_strategies="$var"
 259                else
 260                        use_strategies="$default_octopus_strategies"
 261                fi ;;
 262        esac
 263        ;;
 264esac
 265
 266for s in $use_strategies
 267do
 268        for nt in $no_trivial_merge_strategies
 269        do
 270                case " $s " in
 271                *" $nt "*)
 272                        index_merge=f
 273                        break
 274                        ;;
 275                esac
 276        done
 277done
 278
 279case "$#" in
 2801)
 281        common=$(git merge-base --all $head "$@")
 282        ;;
 283*)
 284        common=$(git show-branch --merge-base $head "$@")
 285        ;;
 286esac
 287echo "$head" >"$GIT_DIR/ORIG_HEAD"
 288
 289case "$index_merge,$#,$common,$no_commit" in
 290f,*)
 291        # We've been told not to try anything clever.  Skip to real merge.
 292        ;;
 293?,*,'',*)
 294        # No common ancestors found. We need a real merge.
 295        ;;
 296?,1,"$1",*)
 297        # If head can reach all the merge then we are up to date.
 298        # but first the most common case of merging one remote.
 299        finish_up_to_date "Already up-to-date."
 300        exit 0
 301        ;;
 302?,1,"$head",*)
 303        # Again the most common case of merging one remote.
 304        echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
 305        git update-index --refresh 2>/dev/null
 306        msg="Fast forward"
 307        if test -n "$have_message"
 308        then
 309                msg="$msg (no commit created; -m option ignored)"
 310        fi
 311        new_head=$(git rev-parse --verify "$1^0") &&
 312        git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
 313        finish "$new_head" "$msg" || exit
 314        dropsave
 315        exit 0
 316        ;;
 317?,1,?*"$LF"?*,*)
 318        # We are not doing octopus and not fast forward.  Need a
 319        # real merge.
 320        ;;
 321?,1,*,)
 322        # We are not doing octopus, not fast forward, and have only
 323        # one common.
 324        git update-index --refresh 2>/dev/null
 325        case " $use_strategies " in
 326        *' recursive '*|*' recur '*)
 327                : run merge later
 328                ;;
 329        *)
 330                # See if it is really trivial.
 331                git var GIT_COMMITTER_IDENT >/dev/null || exit
 332                echo "Trying really trivial in-index merge..."
 333                if git read-tree --trivial -m -u -v $common $head "$1" &&
 334                   result_tree=$(git write-tree)
 335                then
 336                        echo "Wonderful."
 337                        result_commit=$(
 338                                printf '%s\n' "$merge_msg" |
 339                                git commit-tree $result_tree -p HEAD -p "$1"
 340                        ) || exit
 341                        finish "$result_commit" "In-index merge"
 342                        dropsave
 343                        exit 0
 344                fi
 345                echo "Nope."
 346        esac
 347        ;;
 348*)
 349        # An octopus.  If we can reach all the remote we are up to date.
 350        up_to_date=t
 351        for remote
 352        do
 353                common_one=$(git merge-base --all $head $remote)
 354                if test "$common_one" != "$remote"
 355                then
 356                        up_to_date=f
 357                        break
 358                fi
 359        done
 360        if test "$up_to_date" = t
 361        then
 362                finish_up_to_date "Already up-to-date. Yeeah!"
 363                exit 0
 364        fi
 365        ;;
 366esac
 367
 368# We are going to make a new commit.
 369git var GIT_COMMITTER_IDENT >/dev/null || exit
 370
 371# At this point, we need a real merge.  No matter what strategy
 372# we use, it would operate on the index, possibly affecting the
 373# working tree, and when resolved cleanly, have the desired tree
 374# in the index -- this means that the index must be in sync with
 375# the $head commit.  The strategies are responsible to ensure this.
 376
 377case "$use_strategies" in
 378?*' '?*)
 379    # Stash away the local changes so that we can try more than one.
 380    savestate
 381    single_strategy=no
 382    ;;
 383*)
 384    rm -f "$GIT_DIR/MERGE_SAVE"
 385    single_strategy=yes
 386    ;;
 387esac
 388
 389result_tree= best_cnt=-1 best_strategy= wt_strategy=
 390merge_was_ok=
 391for strategy in $use_strategies
 392do
 393    test "$wt_strategy" = '' || {
 394        echo "Rewinding the tree to pristine..."
 395        restorestate
 396    }
 397    case "$single_strategy" in
 398    no)
 399        echo "Trying merge strategy $strategy..."
 400        ;;
 401    esac
 402
 403    # Remember which strategy left the state in the working tree
 404    wt_strategy=$strategy
 405
 406    git-merge-$strategy $common -- "$head_arg" "$@"
 407    exit=$?
 408    if test "$no_commit" = t && test "$exit" = 0
 409    then
 410        merge_was_ok=t
 411        exit=1 ;# pretend it left conflicts.
 412    fi
 413
 414    test "$exit" = 0 || {
 415
 416        # The backend exits with 1 when conflicts are left to be resolved,
 417        # with 2 when it does not handle the given merge at all.
 418
 419        if test "$exit" -eq 1
 420        then
 421            cnt=`{
 422                git diff-files --name-only
 423                git ls-files --unmerged
 424            } | wc -l`
 425            if test $best_cnt -le 0 -o $cnt -le $best_cnt
 426            then
 427                best_strategy=$strategy
 428                best_cnt=$cnt
 429            fi
 430        fi
 431        continue
 432    }
 433
 434    # Automerge succeeded.
 435    result_tree=$(git write-tree) && break
 436done
 437
 438# If we have a resulting tree, that means the strategy module
 439# auto resolved the merge cleanly.
 440if test '' != "$result_tree"
 441then
 442    parents=$(git show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
 443    result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
 444    finish "$result_commit" "Merge made by $wt_strategy."
 445    dropsave
 446    exit 0
 447fi
 448
 449# Pick the result from the best strategy and have the user fix it up.
 450case "$best_strategy" in
 451'')
 452        restorestate
 453        case "$use_strategies" in
 454        ?*' '?*)
 455                echo >&2 "No merge strategy handled the merge."
 456                ;;
 457        *)
 458                echo >&2 "Merge with strategy $use_strategies failed."
 459                ;;
 460        esac
 461        exit 2
 462        ;;
 463"$wt_strategy")
 464        # We already have its result in the working tree.
 465        ;;
 466*)
 467        echo "Rewinding the tree to pristine..."
 468        restorestate
 469        echo "Using the $best_strategy to prepare resolving by hand."
 470        git-merge-$best_strategy $common -- "$head_arg" "$@"
 471        ;;
 472esac
 473
 474if test "$squash" = t
 475then
 476        finish
 477else
 478        for remote
 479        do
 480                echo $remote
 481        done >"$GIT_DIR/MERGE_HEAD"
 482        printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
 483fi
 484
 485if test "$merge_was_ok" = t
 486then
 487        echo >&2 \
 488        "Automatic merge went well; stopped before committing as requested"
 489        exit 0
 490else
 491        {
 492            echo '
 493Conflicts:
 494'
 495                git ls-files --unmerged |
 496                sed -e 's/^[^   ]*      /       /' |
 497                uniq
 498        } >>"$GIT_DIR/MERGE_MSG"
 499        if test -d "$GIT_DIR/rr-cache"
 500        then
 501                git rerere
 502        fi
 503        die "Automatic merge failed; fix conflicts and then commit the result."
 504fi