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