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