@matheusbsbs / matheus-bagliano / commits / 91063bbc6c
Fork 0

Multi-backend merge driver.

The new command 'git merge' takes the current head and one or more remote heads, with the commit log message for the automated case. If the heads being merged are simple fast-forwards, it acts the same way as the current 'git resolve'. Otherwise, it tries different merge strategies and takes the result from the one that succeeded auto-merging, if there is any. If no merge strategy succeeds auto-merging, their results are evaluated for number of paths needed for hand resolving, and the one with the least number of such paths is left in the working tree. The user is asked to resolve them by hand and make a commit manually. The calling convention from the 'git merge' driver to merge strategy programs is very simple: - A strategy program is to be called 'git-merge-<strategy>'. - They take input of this form: <common1> <common2> ... '--' <head> <remote1> <remote2>... That is, one or more the common ancestors, double dash, the current head, and one or more remote heads being merged into the current branch. - Before a strategy program is called, the working tree is matched to the current <head>. - The strategy program exits with status code 0 when it successfully auto-merges the given heads. It should do update-cache for all the merged paths when it does so -- the index file will be used to record the merge result as a commit by the driver. - The strategy program exits with status code 1 when it leaves conflicts behind. It should do update-cache for all the merged paths that it successfully auto-merged, and leave the cache entry in the index file as the same as <head> for paths it could not auto-merge, and leave its best-effort result with conflict markers in the working tree when it does so. - The strategy program exists with status code other than 0 or 1 if it does not handle the given merge at all. As examples, this commit comes with merge strategies based on 'git resolve' and 'git octopus'. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 8, 2005 at 13:47 UTC 91063bbc6c4187184e94ebc4ab2b807e6d53cd0a
5 files changed +381 -2
Makefile
+2 -1
@@ -73,7 +73,8 @@ SCRIPT_SH = \
73 git-repack.sh git-request-pull.sh git-reset.sh \
74 git-resolve.sh git-revert.sh git-sh-setup.sh git-status.sh \
75 git-tag.sh git-verify-tag.sh git-whatchanged.sh git.sh \
76 - git-applymbox.sh git-applypatch.sh
76 + git-applymbox.sh git-applypatch.sh \
77 + git-merge.sh git-merge-resolve.sh git-merge-octopus.sh
78
79 SCRIPT_PERL = \
80 git-archimport.perl git-cvsimport.perl git-relink.perl \
git-commit.sh
+1 -1
@@ -158,7 +158,7 @@ if [ ! -r "$GIT_DIR/HEAD" ]; then
158 PARENTS=""
159 else
160 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
161 - PARENTS="-p HEAD -p MERGE_HEAD"
161 + PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
162 fi
163 if test "$use_commit" != ""
164 then
git-merge-octopus.sh new
+86
@@ -0,0 +1,86 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2005 Junio C Hamano
4 +#
5 +# Resolve two or more trees.
6 +#
7 +
8 +# The first parameters up to -- are merge bases; the rest are heads.
9 +bases= head= remotes= sep_seen=
10 +for arg
11 +do
12 + case ",$sep_seen,$head,$arg," in
13 + *,--,)
14 + sep_seen=yes
15 + ;;
16 + ,yes,,*)
17 + head=$arg
18 + ;;
19 + ,yes,*)
20 + remotes="$remotes$arg "
21 + ;;
22 + *)
23 + bases="$bases$arg "
24 + ;;
25 + esac
26 +done
27 +
28 +# Reject if this is not an Octopus -- resolve should be used instead.
29 +case "$remotes" in
30 +?*' '?*)
31 + ;;
32 +*)
33 + exit 2 ;;
34 +esac
35 +
36 +# MRC is the current "merge reference commit"
37 +# MRT is the current "merge result tree"
38 +
39 +MRC=$head MSG= PARENT="-p $head"
40 +MRT=$(git-write-tree)
41 +CNT=1 ;# counting our head
42 +NON_FF_MERGE=0
43 +for SHA1 in $remotes
44 +do
45 + common=$(git-merge-base $MRC $SHA1) ||
46 + die "Unable to find common commit with $SHA1"
47 +
48 + if test "$common" = $SHA1
49 + then
50 + echo "Already up-to-date with $SHA1"
51 + continue
52 + fi
53 +
54 + CNT=`expr $CNT + 1`
55 + PARENT="$PARENT -p $SHA1"
56 +
57 + if test "$common,$NON_FF_MERGE" = "$MRC,0"
58 + then
59 + # The first head being merged was a fast-forward.
60 + # Advance MRC to the head being merged, and use that
61 + # tree as the intermediate result of the merge.
62 + # We still need to count this as part of the parent set.
63 +
64 + echo "Fast forwarding to: $SHA1"
65 + git-read-tree -u -m $head $SHA1 || exit
66 + MRC=$SHA1 MRT=$(git-write-tree)
67 + continue
68 + fi
69 +
70 + NON_FF_MERGE=1
71 +
72 + echo "Trying simple merge with $SHA1"
73 + git-read-tree -u -m $common $MRT $SHA1 || exit 2
74 + next=$(git-write-tree 2>/dev/null)
75 + if test $? -ne 0
76 + then
77 + echo "Simple merge did not work, trying automatic merge."
78 + git-merge-index -o git-merge-one-file -a ||
79 + exit 2 ; # Automatic merge failed; should not be doing Octopus
80 + next=$(git-write-tree 2>/dev/null)
81 + fi
82 + MRC=$common
83 + MRT=$next
84 +done
85 +
86 +exit 0
git-merge-resolve.sh new
+80
@@ -0,0 +1,80 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2005 Linus Torvalds
4 +#
5 +# Resolve two trees.
6 +
7 +# The first parameters up to -- are merge bases; the rest are heads.
8 +bases= head= remotes= sep_seen=
9 +for arg
10 +do
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
25 +done
26 +
27 +# Give up if we are given more than two remotes -- not handling octopus.
28 +case "$remotes" in
29 +?*' '?*)
30 + exit 2 ;;
31 +esac
32 +
33 +# Find an optimum merge base if there are more than one candidates.
34 +case "$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 + ;;
64 +esac
65 +
66 +git-update-index --refresh 2>/dev/null
67 +git-read-tree -u -m $common $head $remotes || exit 2
68 +echo "Trying simple merge."
69 +if result_tree=$(git-write-tree 2>/dev/null)
70 +then
71 + exit 0
72 +else
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
80 +fi
git-merge.sh new
+212
@@ -0,0 +1,212 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2005 Junio C Hamano
4 +#
5 +
6 +. git-sh-setup || die "Not a git archive"
7 +
8 +LF='
9 +'
10 +
11 +usage () {
12 + die "git-merge [-n] [-s <strategy>]... <merge-message> <head> <remote>+"
13 +}
14 +
15 +# all_strategies='resolve multibase fredrik octopus'
16 +
17 +all_strategies='resolve multibase octopus'
18 +default_strategies='resolve multibase octopus'
19 +use_strategies=
20 +
21 +dropheads() {
22 + rm -f -- "$GIT_DIR/MERGE_HEAD" || exit 1
23 +}
24 +
25 +summary() {
26 + case "$no_summary" in
27 + '')
28 + git-diff-tree -p -M $head "$1" |
29 + git-apply --stat --summary
30 + ;;
31 + esac
32 +}
33 +
34 +while case "$#" in 0) break ;; esac
35 +do
36 + case "$1" in
37 + -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
38 + --no-summa|--no-summar|--no-summary)
39 + no_summary=t ;;
40 + -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
41 + --strateg=*|--strategy=*|\
42 + -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
43 + case "$#,$1" in
44 + *,*=*)
45 + strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
46 + 0,*)
47 + usage ;;
48 + *)
49 + strategy="$2"
50 + shift ;;
51 + esac
52 + case " $all_strategies " in
53 + *" $strategy "*)
54 + use_strategies="$use_strategies$strategy " ;;
55 + *)
56 + die "available strategies are: $all_strategies" ;;
57 + esac
58 + ;;
59 + -*) usage ;;
60 + *) break ;;
61 + esac
62 + shift
63 +done
64 +
65 +case "$use_strategies" in
66 +'')
67 + use_strategies=$default_strategies
68 + ;;
69 +esac
70 +test "$#" -le 2 && usage ;# we need at least two heads.
71 +
72 +merge_msg="$1"
73 +shift
74 +head=$(git-rev-parse --verify "$1"^0) || usage
75 +shift
76 +
77 +# All the rest are remote heads
78 +for remote
79 +do
80 + git-rev-parse --verify "$remote"^0 >/dev/null ||
81 + die "$remote - not something we can merge"
82 +done
83 +
84 +common=$(git-show-branch --merge-base $head "$@")
85 +echo "$head" >"$GIT_DIR/ORIG_HEAD"
86 +
87 +case "$#,$common" in
88 +*,'')
89 + die "Unable to find common commit between $head and $*"
90 + ;;
91 +1,"$1")
92 + # If head can reach all the merge then we are up to date.
93 + # but first the most common case of merging one remote
94 + echo "Already up-to-date. Yeeah!"
95 + dropheads
96 + exit 0
97 + ;;
98 +1,"$head")
99 + # Again the most common case of merging one remote.
100 + echo "Updating from $head to $1."
101 + git-update-index --refresh 2>/dev/null
102 + git-read-tree -u -m $head "$1" || exit 1
103 + git-rev-parse --verify "$1^0" > "$GIT_DIR/HEAD"
104 + summary "$1"
105 + dropheads
106 + exit 0
107 + ;;
108 +1,*)
109 + # We are not doing octopus and not fast forward. Need a
110 + # real merge.
111 + ;;
112 +*)
113 + # An octopus. If we can reach all the remote we are up to date.
114 + up_to_date=t
115 + for remote
116 + do
117 + common_one=$(git-merge-base $head $remote)
118 + if test "$common_one" != "$remote"
119 + then
120 + up_to_date=f
121 + break
122 + fi
123 + done
124 + if test "$up_to_date" = t
125 + then
126 + echo "Already up-to-date. Yeeah!"
127 + dropheads
128 + exit 0
129 + fi
130 + ;;
131 +esac
132 +
133 +# At this point we need a real merge. Require that the tree matches
134 +# exactly our head.
135 +
136 +git-update-index --refresh &&
137 +test '' = "`git-diff-index --cached --name-only $head`" || {
138 + die "Need real merge but the working tree has local changes."
139 +}
140 +
141 +result_tree= best_cnt=-1 best_strategy= wt_strategy=
142 +for strategy in $use_strategies
143 +do
144 + test "$wt_strategy" = '' || {
145 + echo "Rewinding the tree to pristine..."
146 + git reset --hard $head
147 + }
148 + echo "Trying merge strategy $strategy..."
149 + wt_strategy=$strategy
150 + git-merge-$strategy $common -- $head "$@" || {
151 +
152 + # The backend exits with 1 when conflicts are left to be resolved,
153 + # with 2 when it does not handle the given merge at all.
154 +
155 + exit=$?
156 + if test "$exit" -eq 1
157 + then
158 + cnt=`{
159 + git-diff-files --name-only
160 + git-ls-files --unmerged
161 + } | wc -l`
162 + if test $best_cnt -le 0 -o $cnt -le $best_cnt
163 + then
164 + best_strategy=$strategy
165 + best_cnt=$cnt
166 + fi
167 + fi
168 + continue
169 + }
170 +
171 + # Automerge succeeded.
172 + result_tree=$(git-write-tree) && break
173 +done
174 +
175 +# If we have a resulting tree, that means the strategy module
176 +# auto resolved the merge cleanly.
177 +if test '' != "$result_tree"
178 +then
179 + parents="-p $head"
180 + for remote
181 + do
182 + parents="$parents -p $remote"
183 + done
184 + result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents)
185 + echo "Committed merge $result_commit, made by $wt_strategy."
186 + echo $result_commit >"$GIT_DIR/HEAD"
187 + summary $result_commit
188 + dropheads
189 + exit 0
190 +fi
191 +
192 +# Pick the result from the best strategy and have the user fix it up.
193 +case "$best_strategy" in
194 +'')
195 + git reset --hard $head
196 + die "No merge strategy handled the merge."
197 + ;;
198 +"$wt_strategy")
199 + # We already have its result in the working tree.
200 + ;;
201 +*)
202 + echo "Rewinding the tree to pristine..."
203 + git reset --hard $head
204 + echo "Using the $best_strategy to prepare resolving by hand."
205 + git-merge-$best_strategy $common -- $head "$@"
206 + ;;
207 +esac
208 +for remote
209 +do
210 + echo $remote
211 +done >"$GIT_DIR/MERGE_HEAD"
212 +die "Automatic merge failed; fix up by hand"