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