git-resolve.shon commit Merge branch 'master' into next (d55e0ff)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Linus Torvalds
   4#
   5# Resolve two trees.
   6#
   7
   8USAGE='<head> <remote> <merge-message>'
   9. git-sh-setup
  10
  11dropheads() {
  12        rm -f -- "$GIT_DIR/MERGE_HEAD" \
  13                "$GIT_DIR/LAST_MERGE" || exit 1
  14}
  15
  16head=$(git-rev-parse --verify "$1"^0) &&
  17merge=$(git-rev-parse --verify "$2"^0) &&
  18merge_msg="$3" || usage
  19
  20#
  21# The remote name is just used for the message,
  22# but we do want it.
  23#
  24if [ -z "$head" -o -z "$merge" -o -z "$merge_msg" ]; then
  25        usage
  26fi
  27
  28dropheads
  29echo $head > "$GIT_DIR"/ORIG_HEAD
  30echo $merge > "$GIT_DIR"/LAST_MERGE
  31
  32common=$(git-merge-base $head $merge)
  33if [ -z "$common" ]; then
  34        die "Unable to find common commit between" $merge $head
  35fi
  36
  37case "$common" in
  38"$merge")
  39        echo "Already up-to-date. Yeeah!"
  40        dropheads
  41        exit 0
  42        ;;
  43"$head")
  44        echo "Updating from $head to $merge."
  45        git-read-tree -u -m $head $merge || exit 1
  46        git-update-ref HEAD "$merge" "$head"
  47        git-diff-tree -p $head $merge | git-apply --stat
  48        dropheads
  49        exit 0
  50        ;;
  51esac
  52
  53# We are going to make a new commit.
  54git var GIT_COMMITTER_IDENT >/dev/null || exit
  55
  56# Find an optimum merge base if there are more than one candidates.
  57LF='
  58'
  59common=$(git-merge-base -a $head $merge)
  60case "$common" in
  61?*"$LF"?*)
  62        echo "Trying to find the optimum merge base."
  63        G=.tmp-index$$
  64        best=
  65        best_cnt=-1
  66        for c in $common
  67        do
  68                rm -f $G
  69                GIT_INDEX_FILE=$G git-read-tree -m $c $head $merge \
  70                        2>/dev/null || continue
  71                # Count the paths that are unmerged.
  72                cnt=`GIT_INDEX_FILE=$G git-ls-files --unmerged | wc -l`
  73                if test $best_cnt -le 0 -o $cnt -le $best_cnt
  74                then
  75                        best=$c
  76                        best_cnt=$cnt
  77                        if test "$best_cnt" -eq 0
  78                        then
  79                                # Cannot do any better than all trivial merge.
  80                                break
  81                        fi
  82                fi
  83        done
  84        rm -f $G
  85        common="$best"
  86esac
  87
  88echo "Trying to merge $merge into $head using $common."
  89git-update-index --refresh 2>/dev/null
  90git-read-tree -u -m $common $head $merge || exit 1
  91result_tree=$(git-write-tree  2> /dev/null)
  92if [ $? -ne 0 ]; then
  93        echo "Simple merge failed, trying Automatic merge"
  94        git-merge-index -o git-merge-one-file -a
  95        if [ $? -ne 0 ]; then
  96                echo $merge > "$GIT_DIR"/MERGE_HEAD
  97                die "Automatic merge failed, fix up by hand"
  98        fi
  99        result_tree=$(git-write-tree) || exit 1
 100fi
 101result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
 102echo "Committed merge $result_commit"
 103git-update-ref HEAD "$result_commit" "$head"
 104git-diff-tree -p $head $result_commit | git-apply --stat
 105dropheads