git-merge.shon commit Optionally work without python (abb7c7b)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6
   7USAGE='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+'
   8. git-sh-setup
   9
  10LF='
  11'
  12
  13all_strategies='recursive octopus resolve stupid ours'
  14default_strategies='recursive'
  15use_strategies=
  16if test "@@NO_PYTHON@@"; then
  17        all_strategies='resolve octopus stupid ours'
  18        default_strategies='resolve'
  19fi
  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 () {
  42        test '' = "$2" || echo "$2"
  43        case "$merge_msg" in
  44        '')
  45                echo "No merge message -- not updating HEAD"
  46                ;;
  47        *)
  48                git-update-ref HEAD "$1" "$head" || exit 1
  49                ;;
  50        esac
  51
  52        case "$no_summary" in
  53        '')
  54                git-diff-tree -p -M "$head" "$1" |
  55                git-apply --stat --summary
  56                ;;
  57        esac
  58}
  59
  60while case "$#" in 0) break ;; esac
  61do
  62        case "$1" in
  63        -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
  64                --no-summa|--no-summar|--no-summary)
  65                no_summary=t ;;
  66        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
  67                no_commit=t ;;
  68        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
  69                --strateg=*|--strategy=*|\
  70        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
  71                case "$#,$1" in
  72                *,*=*)
  73                        strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
  74                1,*)
  75                        usage ;;
  76                *)
  77                        strategy="$2"
  78                        shift ;;
  79                esac
  80                case " $all_strategies " in
  81                *" $strategy "*)
  82                        use_strategies="$use_strategies$strategy " ;;
  83                *)
  84                        die "available strategies are: $all_strategies" ;;
  85                esac
  86                ;;
  87        -*)     usage ;;
  88        *)      break ;;
  89        esac
  90        shift
  91done
  92
  93test "$#" -le 2 && usage ;# we need at least two heads.
  94
  95merge_msg="$1"
  96shift
  97head_arg="$1"
  98head=$(git-rev-parse --verify "$1"^0) || usage
  99shift
 100
 101# All the rest are remote heads
 102remoteheads=
 103for remote
 104do
 105        remotehead=$(git-rev-parse --verify "$remote"^0) ||
 106            die "$remote - not something we can merge"
 107        remoteheads="${remoteheads}$remotehead "
 108done
 109set x $remoteheads ; shift
 110
 111case "$#" in
 1121)
 113        common=$(git-merge-base --all $head "$@")
 114        ;;
 115*)
 116        common=$(git-show-branch --merge-base $head "$@")
 117        ;;
 118esac
 119echo "$head" >"$GIT_DIR/ORIG_HEAD"
 120
 121case "$#,$common,$no_commit" in
 122*,'',*)
 123        # No common ancestors found. We need a real merge.
 124        ;;
 1251,"$1",*)
 126        # If head can reach all the merge then we are up to date.
 127        # but first the most common case of merging one remote
 128        echo "Already up-to-date."
 129        dropsave
 130        exit 0
 131        ;;
 1321,"$head",*)
 133        # Again the most common case of merging one remote.
 134        echo "Updating from $head to $1."
 135        git-update-index --refresh 2>/dev/null
 136        new_head=$(git-rev-parse --verify "$1^0") &&
 137        git-read-tree -u -m $head "$new_head" &&
 138        finish "$new_head" "Fast forward"
 139        dropsave
 140        exit 0
 141        ;;
 1421,?*"$LF"?*,*)
 143        # We are not doing octopus and not fast forward.  Need a
 144        # real merge.
 145        ;;
 1461,*,)
 147        # We are not doing octopus, not fast forward, and have only
 148        # one common.  See if it is really trivial.
 149        echo "Trying really trivial in-index merge..."
 150        git-update-index --refresh 2>/dev/null
 151        if git-read-tree --trivial -m -u $common $head "$1" &&
 152           result_tree=$(git-write-tree)
 153        then
 154            echo "Wonderful."
 155            result_commit=$(
 156                echo "$merge_msg" |
 157                git-commit-tree $result_tree -p HEAD -p "$1"
 158            ) || exit
 159            finish "$result_commit" "In-index merge"
 160            dropsave
 161            exit 0
 162        fi
 163        echo "Nope."
 164        ;;
 165*)
 166        # An octopus.  If we can reach all the remote we are up to date.
 167        up_to_date=t
 168        for remote
 169        do
 170                common_one=$(git-merge-base --all $head $remote)
 171                if test "$common_one" != "$remote"
 172                then
 173                        up_to_date=f
 174                        break
 175                fi
 176        done
 177        if test "$up_to_date" = t
 178        then
 179                echo "Already up-to-date. Yeeah!"
 180                dropsave
 181                exit 0
 182        fi
 183        ;;
 184esac
 185
 186case "$use_strategies" in
 187'')
 188        case "$#" in
 189        1)
 190                use_strategies="$default_strategies" ;;
 191        *)
 192                use_strategies=octopus ;;
 193        esac            
 194        ;;
 195esac
 196
 197# At this point, we need a real merge.  No matter what strategy
 198# we use, it would operate on the index, possibly affecting the
 199# working tree, and when resolved cleanly, have the desired tree
 200# in the index -- this means that the index must be in sync with
 201# the $head commit.  The strategies are responsible to ensure this.
 202
 203case "$use_strategies" in
 204?*' '?*)
 205    # Stash away the local changes so that we can try more than one.
 206    savestate
 207    single_strategy=no
 208    ;;
 209*)
 210    rm -f "$GIT_DIR/MERGE_SAVE"
 211    single_strategy=yes
 212    ;;
 213esac
 214
 215result_tree= best_cnt=-1 best_strategy= wt_strategy=
 216merge_was_ok=
 217for strategy in $use_strategies
 218do
 219    test "$wt_strategy" = '' || {
 220        echo "Rewinding the tree to pristine..."
 221        restorestate
 222    }
 223    case "$single_strategy" in
 224    no)
 225        echo "Trying merge strategy $strategy..."
 226        ;;
 227    esac
 228
 229    # Remember which strategy left the state in the working tree
 230    wt_strategy=$strategy
 231
 232    git-merge-$strategy $common -- "$head_arg" "$@"
 233    exit=$?
 234    if test "$no_commit" = t && test "$exit" = 0
 235    then
 236        merge_was_ok=t
 237        exit=1 ;# pretend it left conflicts.
 238    fi
 239
 240    test "$exit" = 0 || {
 241
 242        # The backend exits with 1 when conflicts are left to be resolved,
 243        # with 2 when it does not handle the given merge at all.
 244
 245        if test "$exit" -eq 1
 246        then
 247            cnt=`{
 248                git-diff-files --name-only
 249                git-ls-files --unmerged
 250            } | wc -l`
 251            if test $best_cnt -le 0 -o $cnt -le $best_cnt
 252            then
 253                best_strategy=$strategy
 254                best_cnt=$cnt
 255            fi
 256        fi
 257        continue
 258    }
 259
 260    # Automerge succeeded.
 261    result_tree=$(git-write-tree) && break
 262done
 263
 264# If we have a resulting tree, that means the strategy module
 265# auto resolved the merge cleanly.
 266if test '' != "$result_tree"
 267then
 268    parents="-p $head"
 269    for remote
 270    do
 271        parents="$parents -p $remote"
 272    done
 273    result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
 274    finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
 275    dropsave
 276    exit 0
 277fi
 278
 279# Pick the result from the best strategy and have the user fix it up.
 280case "$best_strategy" in
 281'')
 282        restorestate
 283        echo >&2 "No merge strategy handled the merge."
 284        exit 2
 285        ;;
 286"$wt_strategy")
 287        # We already have its result in the working tree.
 288        ;;
 289*)
 290        echo "Rewinding the tree to pristine..."
 291        restorestate
 292        echo "Using the $best_strategy to prepare resolving by hand."
 293        git-merge-$best_strategy $common -- "$head_arg" "$@"
 294        ;;
 295esac
 296for remote
 297do
 298        echo $remote
 299done >"$GIT_DIR/MERGE_HEAD"
 300echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
 301
 302if test "$merge_was_ok" = t
 303then
 304        echo >&2 \
 305        "Automatic merge went well; stopped before committing as requested"
 306        exit 0
 307else
 308        {
 309            echo '
 310Conflicts:
 311'
 312                git ls-files --unmerged |
 313                sed -e 's/^[^   ]*      /       /' |
 314                uniq
 315        } >>"$GIT_DIR/MERGE_MSG"
 316        if test -d "$GIT_DIR/rr-cache"
 317        then
 318                git-rerere
 319        fi
 320        die "Automatic merge failed; fix up by hand"
 321fi