1#!/bin/sh
2#
3# Copyright (c) 2005 Linus Torvalds
4#
5# Resolve two trees, 'stupid merge'.
6
7# The first parameters up to -- are merge bases; the rest are heads.
8bases= head= remotes= sep_seen=
9for arg
10do
11 case ",$sep_seen,$head,$arg," in
12 *,--,)
13 sep_seen=yes
14 ;;
15 ,yes,,*)
16 head=$arg
17 ;;
18 ,yes,*)
19 remotes="$remotes$arg "
20 ;;
21 *)
22 bases="$bases$arg "
23 ;;
24 esac
25done
26
27# Give up if we are given two or more remotes -- not handling octopus.
28case "$remotes" in
29?*' '?*)
30 exit 2 ;;
31esac
32
33# Find an optimum merge base if there are more than one candidates.
34case "$bases" in
35?*' '?*)
36 echo "Trying to find the optimum merge base."
37 G=.tmp-index$$
38 best=
39 best_cnt=-1
40 for c in $bases
41 do
42 rm -f $G
43 GIT_INDEX_FILE=$G git read-tree -m $c $head $remotes \
44 2>/dev/null || continue
45 # Count the paths that are unmerged.
46 cnt=`GIT_INDEX_FILE=$G git ls-files --unmerged | wc -l`
47 if test $best_cnt -le 0 -o $cnt -le $best_cnt
48 then
49 best=$c
50 best_cnt=$cnt
51 if test "$best_cnt" -eq 0
52 then
53 # Cannot do any better than all trivial merge.
54 break
55 fi
56 fi
57 done
58 rm -f $G
59 common="$best"
60 ;;
61*)
62 common="$bases"
63 ;;
64esac
65
66git update-index --refresh 2>/dev/null
67git read-tree -u -m $common $head $remotes || exit 2
68echo "Trying simple merge."
69if result_tree=$(git write-tree 2>/dev/null)
70then
71 exit 0
72else
73 echo "Simple merge failed, trying Automatic merge."
74 if git-merge-index -o git-merge-one-file -a
75 then
76 exit 0
77 else
78 exit 1
79 fi
80fi