blob: 7adffdc7950baaf1da5b36913f78404c48c6d270 [file] [log] [blame]
Junio C Hamano91063bb2005-09-08 13:47:12 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5# Resolve two or more trees.
6#
7
Junio C Hamano17cf9392005-11-09 22:37:14 -08008LF='
9'
10
Junio C Hamanod165fa12005-11-27 23:33:54 -080011die () {
12 echo >&2 "$*"
13 exit 1
14}
15
Junio C Hamano91063bb2005-09-08 13:47:12 -070016# The first parameters up to -- are merge bases; the rest are heads.
17bases= head= remotes= sep_seen=
18for arg
19do
20 case ",$sep_seen,$head,$arg," in
21 *,--,)
22 sep_seen=yes
23 ;;
24 ,yes,,*)
25 head=$arg
26 ;;
27 ,yes,*)
28 remotes="$remotes$arg "
29 ;;
30 *)
31 bases="$bases$arg "
32 ;;
33 esac
34done
35
36# Reject if this is not an Octopus -- resolve should be used instead.
37case "$remotes" in
38?*' '?*)
39 ;;
40*)
41 exit 2 ;;
42esac
43
44# MRC is the current "merge reference commit"
45# MRT is the current "merge result tree"
46
47MRC=$head MSG= PARENT="-p $head"
48MRT=$(git-write-tree)
49CNT=1 ;# counting our head
50NON_FF_MERGE=0
51for SHA1 in $remotes
52do
Junio C Hamano17cf9392005-11-09 22:37:14 -080053 common=$(git-merge-base --all $MRC $SHA1) ||
Junio C Hamano91063bb2005-09-08 13:47:12 -070054 die "Unable to find common commit with $SHA1"
55
Junio C Hamano17cf9392005-11-09 22:37:14 -080056 case "$common" in
57 ?*"$LF"?*)
58 die "Not trivially mergeable."
59 ;;
60 $SHA1)
Junio C Hamano91063bb2005-09-08 13:47:12 -070061 echo "Already up-to-date with $SHA1"
62 continue
Junio C Hamano17cf9392005-11-09 22:37:14 -080063 ;;
64 esac
Junio C Hamano91063bb2005-09-08 13:47:12 -070065
66 CNT=`expr $CNT + 1`
67 PARENT="$PARENT -p $SHA1"
68
69 if test "$common,$NON_FF_MERGE" = "$MRC,0"
70 then
71 # The first head being merged was a fast-forward.
72 # Advance MRC to the head being merged, and use that
73 # tree as the intermediate result of the merge.
74 # We still need to count this as part of the parent set.
75
76 echo "Fast forwarding to: $SHA1"
77 git-read-tree -u -m $head $SHA1 || exit
78 MRC=$SHA1 MRT=$(git-write-tree)
79 continue
80 fi
81
82 NON_FF_MERGE=1
83
84 echo "Trying simple merge with $SHA1"
85 git-read-tree -u -m $common $MRT $SHA1 || exit 2
86 next=$(git-write-tree 2>/dev/null)
87 if test $? -ne 0
88 then
89 echo "Simple merge did not work, trying automatic merge."
90 git-merge-index -o git-merge-one-file -a ||
91 exit 2 ; # Automatic merge failed; should not be doing Octopus
92 next=$(git-write-tree 2>/dev/null)
93 fi
Junio C Hamano17cf9392005-11-09 22:37:14 -080094
95 # We have merged the other branch successfully. Ideally
96 # we could implement OR'ed heads in merge-base, and keep
97 # a list of commits we have merged so far in MRC to feed
98 # them to merge-base, but we approximate it by keep using
99 # the current MRC. We used to update it to $common, which
100 # was incorrectly doing AND'ed merge-base here, which was
101 # unneeded.
102
Junio C Hamano91063bb2005-09-08 13:47:12 -0700103 MRT=$next
104done
105
106exit 0