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