]>
Commit | Line | Data |
---|---|---|
91063bbc JH |
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 () { | |
123ee3ca | 12 | die "git-merge [-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+" |
91063bbc JH |
13 | } |
14 | ||
e4cf17ce | 15 | # all_strategies='resolve recursive stupid octopus' |
91063bbc | 16 | |
64da9e60 | 17 | all_strategies='recursive octopus resolve stupid ours' |
2276aa6c | 18 | default_strategies='resolve octopus' |
91063bbc JH |
19 | use_strategies= |
20 | ||
a9358240 | 21 | dropsave() { |
deca7e8c | 22 | rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \ |
a9358240 JH |
23 | "$GIT_DIR/MERGE_SAVE" || exit 1 |
24 | } | |
25 | ||
26 | savestate() { | |
60fa0560 | 27 | # Stash away any local modifications. |
50b8e355 | 28 | git-diff-index -z --name-only $head | |
88f8f0a5 | 29 | cpio -0 -o >"$GIT_DIR/MERGE_SAVE" |
a9358240 JH |
30 | } |
31 | ||
32 | restorestate() { | |
deca7e8c JH |
33 | if test -f "$GIT_DIR/MERGE_SAVE" |
34 | then | |
35 | git reset --hard $head | |
36 | cpio -iuv <"$GIT_DIR/MERGE_SAVE" | |
37 | git-update-index --refresh >/dev/null | |
38 | fi | |
91063bbc JH |
39 | } |
40 | ||
4f692b19 JH |
41 | finish () { |
42 | test '' = "$2" || echo "$2" | |
43 | case "$merge_msg" in | |
44 | '') | |
45 | echo "No merge message -- not updating HEAD" | |
46 | ;; | |
47 | *) | |
48 | git-update-ref HEAD "$1" "$head" || exit 1 | |
49 | ;; | |
50 | esac | |
51 | ||
91063bbc JH |
52 | case "$no_summary" in |
53 | '') | |
4f692b19 | 54 | git-diff-tree -p -M "$head" "$1" | |
91063bbc JH |
55 | git-apply --stat --summary |
56 | ;; | |
57 | esac | |
58 | } | |
59 | ||
60 | while case "$#" in 0) break ;; esac | |
61 | do | |
62 | case "$1" in | |
63 | -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\ | |
64 | --no-summa|--no-summar|--no-summary) | |
65 | no_summary=t ;; | |
123ee3ca JH |
66 | --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit) |
67 | no_commit=t ;; | |
91063bbc JH |
68 | -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ |
69 | --strateg=*|--strategy=*|\ | |
70 | -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) | |
71 | case "$#,$1" in | |
72 | *,*=*) | |
73 | strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;; | |
f88ed172 | 74 | 1,*) |
91063bbc JH |
75 | usage ;; |
76 | *) | |
77 | strategy="$2" | |
78 | shift ;; | |
79 | esac | |
80 | case " $all_strategies " in | |
81 | *" $strategy "*) | |
82 | use_strategies="$use_strategies$strategy " ;; | |
83 | *) | |
84 | die "available strategies are: $all_strategies" ;; | |
85 | esac | |
86 | ;; | |
87 | -*) usage ;; | |
88 | *) break ;; | |
89 | esac | |
90 | shift | |
91 | done | |
92 | ||
93 | case "$use_strategies" in | |
94 | '') | |
95 | use_strategies=$default_strategies | |
96 | ;; | |
97 | esac | |
98 | test "$#" -le 2 && usage ;# we need at least two heads. | |
99 | ||
100 | merge_msg="$1" | |
101 | shift | |
8cc01e50 | 102 | head_arg="$1" |
91063bbc JH |
103 | head=$(git-rev-parse --verify "$1"^0) || usage |
104 | shift | |
105 | ||
106 | # All the rest are remote heads | |
107 | for remote | |
108 | do | |
109 | git-rev-parse --verify "$remote"^0 >/dev/null || | |
110 | die "$remote - not something we can merge" | |
111 | done | |
112 | ||
13956670 JH |
113 | case "$#" in |
114 | 1) | |
115 | common=$(git-merge-base --all $head "$@") | |
116 | ;; | |
117 | *) | |
118 | common=$(git-show-branch --merge-base $head "$@") | |
119 | ;; | |
120 | esac | |
91063bbc JH |
121 | echo "$head" >"$GIT_DIR/ORIG_HEAD" |
122 | ||
123ee3ca JH |
123 | case "$#,$common,$no_commit" in |
124 | *,'',*) | |
88f8f0a5 | 125 | # No common ancestors found. We need a real merge. |
91063bbc | 126 | ;; |
123ee3ca | 127 | 1,"$1",*) |
91063bbc JH |
128 | # If head can reach all the merge then we are up to date. |
129 | # but first the most common case of merging one remote | |
4f692b19 | 130 | echo "Already up-to-date." |
a9358240 | 131 | dropsave |
91063bbc JH |
132 | exit 0 |
133 | ;; | |
123ee3ca | 134 | 1,"$head",*) |
91063bbc JH |
135 | # Again the most common case of merging one remote. |
136 | echo "Updating from $head to $1." | |
137 | git-update-index --refresh 2>/dev/null | |
bf7960eb | 138 | new_head=$(git-rev-parse --verify "$1^0") && |
4f692b19 JH |
139 | git-read-tree -u -m $head "$new_head" && |
140 | finish "$new_head" "Fast forward" | |
a9358240 | 141 | dropsave |
91063bbc JH |
142 | exit 0 |
143 | ;; | |
123ee3ca | 144 | 1,?*"$LF"?*,*) |
91063bbc JH |
145 | # We are not doing octopus and not fast forward. Need a |
146 | # real merge. | |
147 | ;; | |
123ee3ca | 148 | 1,*,) |
f9d72413 JH |
149 | # We are not doing octopus, not fast forward, and have only |
150 | # one common. See if it is really trivial. | |
151 | echo "Trying really trivial in-index merge..." | |
152 | git-update-index --refresh 2>/dev/null | |
153 | if git-read-tree --trivial -m -u $common $head "$1" && | |
154 | result_tree=$(git-write-tree) | |
155 | then | |
156 | echo "Wonderful." | |
157 | result_commit=$( | |
158 | echo "$merge_msg" | | |
159 | git-commit-tree $result_tree -p HEAD -p "$1" | |
160 | ) || exit | |
4f692b19 | 161 | finish "$result_commit" "In-index merge" |
f9d72413 JH |
162 | dropsave |
163 | exit 0 | |
164 | fi | |
165 | echo "Nope." | |
166 | ;; | |
91063bbc JH |
167 | *) |
168 | # An octopus. If we can reach all the remote we are up to date. | |
169 | up_to_date=t | |
170 | for remote | |
171 | do | |
13956670 | 172 | common_one=$(git-merge-base --all $head $remote) |
91063bbc JH |
173 | if test "$common_one" != "$remote" |
174 | then | |
175 | up_to_date=f | |
176 | break | |
177 | fi | |
178 | done | |
179 | if test "$up_to_date" = t | |
180 | then | |
181 | echo "Already up-to-date. Yeeah!" | |
a9358240 | 182 | dropsave |
91063bbc JH |
183 | exit 0 |
184 | fi | |
185 | ;; | |
186 | esac | |
187 | ||
a9358240 JH |
188 | # At this point, we need a real merge. No matter what strategy |
189 | # we use, it would operate on the index, possibly affecting the | |
190 | # working tree, and when resolved cleanly, have the desired tree | |
191 | # in the index -- this means that the index must be in sync with | |
60fa0560 | 192 | # the $head commit. The strategies are responsible to ensure this. |
91063bbc | 193 | |
a9358240 JH |
194 | case "$use_strategies" in |
195 | ?*' '?*) | |
196 | # Stash away the local changes so that we can try more than one. | |
197 | savestate | |
198 | single_strategy=no | |
199 | ;; | |
200 | *) | |
deca7e8c | 201 | rm -f "$GIT_DIR/MERGE_SAVE" |
a9358240 JH |
202 | single_strategy=yes |
203 | ;; | |
204 | esac | |
91063bbc JH |
205 | |
206 | result_tree= best_cnt=-1 best_strategy= wt_strategy= | |
207 | for strategy in $use_strategies | |
208 | do | |
209 | test "$wt_strategy" = '' || { | |
210 | echo "Rewinding the tree to pristine..." | |
a9358240 | 211 | restorestate |
91063bbc | 212 | } |
a9358240 JH |
213 | case "$single_strategy" in |
214 | no) | |
215 | echo "Trying merge strategy $strategy..." | |
216 | ;; | |
217 | esac | |
218 | ||
219 | # Remember which strategy left the state in the working tree | |
91063bbc | 220 | wt_strategy=$strategy |
a9358240 | 221 | |
123ee3ca JH |
222 | git-merge-$strategy $common -- "$head_arg" "$@" |
223 | exit=$? | |
224 | if test "$no_commit" = t && test "$exit" = 0 | |
225 | then | |
226 | exit=1 ;# pretend it left conflicts. | |
227 | fi | |
228 | ||
229 | test "$exit" = 0 || { | |
91063bbc JH |
230 | |
231 | # The backend exits with 1 when conflicts are left to be resolved, | |
232 | # with 2 when it does not handle the given merge at all. | |
233 | ||
91063bbc JH |
234 | if test "$exit" -eq 1 |
235 | then | |
236 | cnt=`{ | |
237 | git-diff-files --name-only | |
238 | git-ls-files --unmerged | |
239 | } | wc -l` | |
240 | if test $best_cnt -le 0 -o $cnt -le $best_cnt | |
241 | then | |
242 | best_strategy=$strategy | |
243 | best_cnt=$cnt | |
244 | fi | |
245 | fi | |
246 | continue | |
247 | } | |
248 | ||
249 | # Automerge succeeded. | |
250 | result_tree=$(git-write-tree) && break | |
251 | done | |
252 | ||
253 | # If we have a resulting tree, that means the strategy module | |
254 | # auto resolved the merge cleanly. | |
255 | if test '' != "$result_tree" | |
256 | then | |
257 | parents="-p $head" | |
258 | for remote | |
259 | do | |
260 | parents="$parents -p $remote" | |
261 | done | |
bf7960eb | 262 | result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit |
4f692b19 | 263 | finish "$result_commit" "Merge $result_commit, made by $wt_strategy." |
a9358240 | 264 | dropsave |
91063bbc JH |
265 | exit 0 |
266 | fi | |
267 | ||
268 | # Pick the result from the best strategy and have the user fix it up. | |
269 | case "$best_strategy" in | |
270 | '') | |
a9358240 | 271 | restorestate |
91063bbc JH |
272 | die "No merge strategy handled the merge." |
273 | ;; | |
274 | "$wt_strategy") | |
275 | # We already have its result in the working tree. | |
276 | ;; | |
277 | *) | |
278 | echo "Rewinding the tree to pristine..." | |
a9358240 | 279 | restorestate |
91063bbc | 280 | echo "Using the $best_strategy to prepare resolving by hand." |
a9358240 | 281 | git-merge-$best_strategy $common -- "$head_arg" "$@" |
91063bbc JH |
282 | ;; |
283 | esac | |
284 | for remote | |
285 | do | |
286 | echo $remote | |
287 | done >"$GIT_DIR/MERGE_HEAD" | |
deca7e8c JH |
288 | echo $merge_msg >"$GIT_DIR/MERGE_MSG" |
289 | ||
123ee3ca | 290 | die "Automatic merge failed/prevented; fix up by hand" |