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