84c3acfe63081b1c10ec171fa10246e0330efac8
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
6 USAGE='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
7 . git-sh-setup
9 LF='
10 '
12 all_strategies='recur recursive octopus resolve stupid ours'
13 default_twohead_strategies='recursive'
14 default_octopus_strategies='octopus'
15 no_trivial_merge_strategies='ours'
16 use_strategies=
18 index_merge=t
20 dropsave() {
21 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
22 "$GIT_DIR/MERGE_SAVE" || exit 1
23 }
25 savestate() {
26 # Stash away any local modifications.
27 git-diff-index -z --name-only $head |
28 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
29 }
31 restorestate() {
32 if test -f "$GIT_DIR/MERGE_SAVE"
33 then
34 git reset --hard $head
35 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
36 git-update-index --refresh >/dev/null
37 fi
38 }
40 finish_up_to_date () {
41 case "$squash" in
42 t)
43 echo "$1 (nothing to squash)" ;;
44 '')
45 echo "$1" ;;
46 esac
47 dropsave
48 }
50 squash_message () {
51 echo Squashed commit of the following:
52 echo
53 git-log --no-merges ^"$head" $remote
54 }
56 finish () {
57 if test '' = "$2"
58 then
59 rlogm="$rloga"
60 else
61 echo "$2"
62 rlogm="$rloga: $2"
63 fi
64 case "$squash" in
65 t)
66 echo "Squash commit -- not updating HEAD"
67 squash_message >"$GIT_DIR/SQUASH_MSG"
68 ;;
69 '')
70 case "$merge_msg" in
71 '')
72 echo "No merge message -- not updating HEAD"
73 ;;
74 *)
75 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
76 ;;
77 esac
78 ;;
79 esac
80 case "$1" in
81 '')
82 ;;
83 ?*)
84 case "$no_summary" in
85 '')
86 git-diff-tree --stat --summary -M "$head" "$1"
87 ;;
88 esac
89 ;;
90 esac
91 }
93 case "$#" in 0) usage ;; esac
95 rloga=
96 while case "$#" in 0) break ;; esac
97 do
98 case "$1" in
99 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
100 --no-summa|--no-summar|--no-summary)
101 no_summary=t ;;
102 --sq|--squ|--squa|--squas|--squash)
103 squash=t no_commit=t ;;
104 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
105 no_commit=t ;;
106 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
107 --strateg=*|--strategy=*|\
108 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
109 case "$#,$1" in
110 *,*=*)
111 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
112 1,*)
113 usage ;;
114 *)
115 strategy="$2"
116 shift ;;
117 esac
118 case " $all_strategies " in
119 *" $strategy "*)
120 use_strategies="$use_strategies$strategy " ;;
121 *)
122 die "available strategies are: $all_strategies" ;;
123 esac
124 ;;
125 --reflog-action=*)
126 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
127 ;;
128 -*) usage ;;
129 *) break ;;
130 esac
131 shift
132 done
134 merge_msg="$1"
135 shift
136 head_arg="$1"
137 head=$(git-rev-parse --verify "$1"^0) || usage
138 shift
140 # All the rest are remote heads
141 test "$#" = 0 && usage ;# we need at least one remote head.
142 test "$rloga" = '' && rloga="merge: $@"
144 remoteheads=
145 for remote
146 do
147 remotehead=$(git-rev-parse --verify "$remote"^0) ||
148 die "$remote - not something we can merge"
149 remoteheads="${remoteheads}$remotehead "
150 done
151 set x $remoteheads ; shift
153 case "$use_strategies" in
154 '')
155 case "$#" in
156 1)
157 use_strategies="$default_twohead_strategies" ;;
158 *)
159 use_strategies="$default_octopus_strategies" ;;
160 esac
161 ;;
162 esac
164 for s in $use_strategies
165 do
166 case " $s " in
167 *" $no_trivial_merge_strategies "*)
168 index_merge=f
169 break
170 ;;
171 esac
172 done
174 case "$#" in
175 1)
176 common=$(git-merge-base --all $head "$@")
177 ;;
178 *)
179 common=$(git-show-branch --merge-base $head "$@")
180 ;;
181 esac
182 echo "$head" >"$GIT_DIR/ORIG_HEAD"
184 case "$index_merge,$#,$common,$no_commit" in
185 f,*)
186 # We've been told not to try anything clever. Skip to real merge.
187 ;;
188 ?,*,'',*)
189 # No common ancestors found. We need a real merge.
190 ;;
191 ?,1,"$1",*)
192 # If head can reach all the merge then we are up to date.
193 # but first the most common case of merging one remote.
194 finish_up_to_date "Already up-to-date."
195 exit 0
196 ;;
197 ?,1,"$head",*)
198 # Again the most common case of merging one remote.
199 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
200 git-update-index --refresh 2>/dev/null
201 new_head=$(git-rev-parse --verify "$1^0") &&
202 git-read-tree -u -v -m $head "$new_head" &&
203 finish "$new_head" "Fast forward"
204 dropsave
205 exit 0
206 ;;
207 ?,1,?*"$LF"?*,*)
208 # We are not doing octopus and not fast forward. Need a
209 # real merge.
210 ;;
211 ?,1,*,)
212 # We are not doing octopus, not fast forward, and have only
213 # one common. See if it is really trivial.
214 git var GIT_COMMITTER_IDENT >/dev/null || exit
216 echo "Trying really trivial in-index merge..."
217 git-update-index --refresh 2>/dev/null
218 if git-read-tree --trivial -m -u -v $common $head "$1" &&
219 result_tree=$(git-write-tree)
220 then
221 echo "Wonderful."
222 result_commit=$(
223 echo "$merge_msg" |
224 git-commit-tree $result_tree -p HEAD -p "$1"
225 ) || exit
226 finish "$result_commit" "In-index merge"
227 dropsave
228 exit 0
229 fi
230 echo "Nope."
231 ;;
232 *)
233 # An octopus. If we can reach all the remote we are up to date.
234 up_to_date=t
235 for remote
236 do
237 common_one=$(git-merge-base --all $head $remote)
238 if test "$common_one" != "$remote"
239 then
240 up_to_date=f
241 break
242 fi
243 done
244 if test "$up_to_date" = t
245 then
246 finish_up_to_date "Already up-to-date. Yeeah!"
247 exit 0
248 fi
249 ;;
250 esac
252 # We are going to make a new commit.
253 git var GIT_COMMITTER_IDENT >/dev/null || exit
255 # At this point, we need a real merge. No matter what strategy
256 # we use, it would operate on the index, possibly affecting the
257 # working tree, and when resolved cleanly, have the desired tree
258 # in the index -- this means that the index must be in sync with
259 # the $head commit. The strategies are responsible to ensure this.
261 case "$use_strategies" in
262 ?*' '?*)
263 # Stash away the local changes so that we can try more than one.
264 savestate
265 single_strategy=no
266 ;;
267 *)
268 rm -f "$GIT_DIR/MERGE_SAVE"
269 single_strategy=yes
270 ;;
271 esac
273 result_tree= best_cnt=-1 best_strategy= wt_strategy=
274 merge_was_ok=
275 for strategy in $use_strategies
276 do
277 test "$wt_strategy" = '' || {
278 echo "Rewinding the tree to pristine..."
279 restorestate
280 }
281 case "$single_strategy" in
282 no)
283 echo "Trying merge strategy $strategy..."
284 ;;
285 esac
287 # Remember which strategy left the state in the working tree
288 wt_strategy=$strategy
290 git-merge-$strategy $common -- "$head_arg" "$@"
291 exit=$?
292 if test "$no_commit" = t && test "$exit" = 0
293 then
294 merge_was_ok=t
295 exit=1 ;# pretend it left conflicts.
296 fi
298 test "$exit" = 0 || {
300 # The backend exits with 1 when conflicts are left to be resolved,
301 # with 2 when it does not handle the given merge at all.
303 if test "$exit" -eq 1
304 then
305 cnt=`{
306 git-diff-files --name-only
307 git-ls-files --unmerged
308 } | wc -l`
309 if test $best_cnt -le 0 -o $cnt -le $best_cnt
310 then
311 best_strategy=$strategy
312 best_cnt=$cnt
313 fi
314 fi
315 continue
316 }
318 # Automerge succeeded.
319 result_tree=$(git-write-tree) && break
320 done
322 # If we have a resulting tree, that means the strategy module
323 # auto resolved the merge cleanly.
324 if test '' != "$result_tree"
325 then
326 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
327 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
328 finish "$result_commit" "Merge made by $wt_strategy."
329 dropsave
330 exit 0
331 fi
333 # Pick the result from the best strategy and have the user fix it up.
334 case "$best_strategy" in
335 '')
336 restorestate
337 echo >&2 "No merge strategy handled the merge."
338 exit 2
339 ;;
340 "$wt_strategy")
341 # We already have its result in the working tree.
342 ;;
343 *)
344 echo "Rewinding the tree to pristine..."
345 restorestate
346 echo "Using the $best_strategy to prepare resolving by hand."
347 git-merge-$best_strategy $common -- "$head_arg" "$@"
348 ;;
349 esac
351 if test "$squash" = t
352 then
353 finish
354 else
355 for remote
356 do
357 echo $remote
358 done >"$GIT_DIR/MERGE_HEAD"
359 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
360 fi
362 if test "$merge_was_ok" = t
363 then
364 echo >&2 \
365 "Automatic merge went well; stopped before committing as requested"
366 exit 0
367 else
368 {
369 echo '
370 Conflicts:
371 '
372 git ls-files --unmerged |
373 sed -e 's/^[^ ]* / /' |
374 uniq
375 } >>"$GIT_DIR/MERGE_MSG"
376 if test -d "$GIT_DIR/rr-cache"
377 then
378 git-rerere
379 fi
380 die "Automatic merge failed; fix conflicts and then commit the result."
381 fi