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