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