remove merge-recursive-old
[git/git.git] / git-merge.sh
CommitLineData
91063bbc
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
7d0c6887 6USAGE='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
ae2b0f15 7. git-sh-setup
91063bbc
JH
8
9LF='
10'
11
7cdbff14 12all_strategies='recur recursive octopus resolve stupid ours'
a06f678e 13default_twohead_strategies='recursive'
6ea23343
JH
14default_octopus_strategies='octopus'
15no_trivial_merge_strategies='ours'
91063bbc 16use_strategies=
6ea23343
JH
17
18index_merge=t
91063bbc 19
a9358240 20dropsave() {
deca7e8c 21 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
a9358240
JH
22 "$GIT_DIR/MERGE_SAVE" || exit 1
23}
24
25savestate() {
60fa0560 26 # Stash away any local modifications.
50b8e355 27 git-diff-index -z --name-only $head |
88f8f0a5 28 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
a9358240
JH
29}
30
31restorestate() {
deca7e8c
JH
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
91063bbc
JH
38}
39
7d0c6887
JH
40finish_up_to_date () {
41 case "$squash" in
42 t)
43 echo "$1 (nothing to squash)" ;;
44 '')
45 echo "$1" ;;
46 esac
47 dropsave
48}
49
50squash_message () {
51 echo Squashed commit of the following:
52 echo
53 git-log --no-merges ^"$head" $remote
54}
55
4f692b19 56finish () {
e1447e38
SP
57 if test '' = "$2"
58 then
59 rlogm="$rloga"
60 else
61 echo "$2"
62 rlogm="$rloga: $2"
63 fi
7d0c6887
JH
64 case "$squash" in
65 t)
66 echo "Squash commit -- not updating HEAD"
67 squash_message >"$GIT_DIR/SQUASH_MSG"
4f692b19 68 ;;
7d0c6887
JH
69 '')
70 case "$merge_msg" in
71 '')
72 echo "No merge message -- not updating HEAD"
73 ;;
74 *)
e1447e38 75 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
7d0c6887
JH
76 ;;
77 esac
4f692b19
JH
78 ;;
79 esac
7d0c6887 80 case "$1" in
91063bbc 81 '')
7d0c6887
JH
82 ;;
83 ?*)
84 case "$no_summary" in
85 '')
86 git-diff-tree --stat --summary -M "$head" "$1"
87 ;;
88 esac
91063bbc
JH
89 ;;
90 esac
91}
92
fc61e313
RS
93case "$#" in 0) usage ;; esac
94
e1447e38 95rloga=
91063bbc
JH
96while case "$#" in 0) break ;; esac
97do
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 ;;
7d0c6887
JH
102 --sq|--squ|--squa|--squas|--squash)
103 squash=t no_commit=t ;;
123ee3ca
JH
104 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
105 no_commit=t ;;
91063bbc
JH
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 *,*=*)
8096fae7 111 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
f88ed172 112 1,*)
91063bbc
JH
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 ;;
e1447e38
SP
125 --reflog-action=*)
126 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
127 ;;
91063bbc
JH
128 -*) usage ;;
129 *) break ;;
130 esac
131 shift
132done
133
91063bbc
JH
134merge_msg="$1"
135shift
8cc01e50 136head_arg="$1"
91063bbc
JH
137head=$(git-rev-parse --verify "$1"^0) || usage
138shift
139
140# All the rest are remote heads
6ea23343 141test "$#" = 0 && usage ;# we need at least one remote head.
e1447e38 142test "$rloga" = '' && rloga="merge: $@"
6ea23343 143
9954f5b8 144remoteheads=
91063bbc
JH
145for remote
146do
9954f5b8 147 remotehead=$(git-rev-parse --verify "$remote"^0) ||
91063bbc 148 die "$remote - not something we can merge"
9954f5b8 149 remoteheads="${remoteheads}$remotehead "
91063bbc 150done
9954f5b8 151set x $remoteheads ; shift
91063bbc 152
6ea23343
JH
153case "$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 ;;
162esac
163
164for s in $use_strategies
165do
166 case " $s " in
167 *" $no_trivial_merge_strategies "*)
168 index_merge=f
169 break
170 ;;
171 esac
172done
173
13956670
JH
174case "$#" in
1751)
176 common=$(git-merge-base --all $head "$@")
177 ;;
178*)
179 common=$(git-show-branch --merge-base $head "$@")
180 ;;
181esac
91063bbc
JH
182echo "$head" >"$GIT_DIR/ORIG_HEAD"
183
6ea23343
JH
184case "$index_merge,$#,$common,$no_commit" in
185f,*)
186 # We've been told not to try anything clever. Skip to real merge.
187 ;;
188?,*,'',*)
88f8f0a5 189 # No common ancestors found. We need a real merge.
91063bbc 190 ;;
6ea23343 191?,1,"$1",*)
91063bbc 192 # If head can reach all the merge then we are up to date.
6ea23343 193 # but first the most common case of merging one remote.
7d0c6887 194 finish_up_to_date "Already up-to-date."
91063bbc
JH
195 exit 0
196 ;;
6ea23343 197?,1,"$head",*)
91063bbc 198 # Again the most common case of merging one remote.
ba0ac36e 199 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
91063bbc 200 git-update-index --refresh 2>/dev/null
bf7960eb 201 new_head=$(git-rev-parse --verify "$1^0") &&
744633cb 202 git-read-tree -u -v -m $head "$new_head" &&
4f692b19 203 finish "$new_head" "Fast forward"
a9358240 204 dropsave
91063bbc
JH
205 exit 0
206 ;;
6ea23343 207?,1,?*"$LF"?*,*)
91063bbc
JH
208 # We are not doing octopus and not fast forward. Need a
209 # real merge.
210 ;;
6ea23343 211?,1,*,)
f9d72413
JH
212 # We are not doing octopus, not fast forward, and have only
213 # one common. See if it is really trivial.
e3b59a44
JH
214 git var GIT_COMMITTER_IDENT >/dev/null || exit
215
f9d72413
JH
216 echo "Trying really trivial in-index merge..."
217 git-update-index --refresh 2>/dev/null
744633cb 218 if git-read-tree --trivial -m -u -v $common $head "$1" &&
f9d72413
JH
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
4f692b19 226 finish "$result_commit" "In-index merge"
f9d72413
JH
227 dropsave
228 exit 0
229 fi
230 echo "Nope."
231 ;;
91063bbc
JH
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
13956670 237 common_one=$(git-merge-base --all $head $remote)
91063bbc
JH
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
7d0c6887 246 finish_up_to_date "Already up-to-date. Yeeah!"
91063bbc
JH
247 exit 0
248 fi
249 ;;
250esac
251
e3b59a44
JH
252# We are going to make a new commit.
253git var GIT_COMMITTER_IDENT >/dev/null || exit
254
a9358240
JH
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
60fa0560 259# the $head commit. The strategies are responsible to ensure this.
91063bbc 260
a9358240
JH
261case "$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*)
deca7e8c 268 rm -f "$GIT_DIR/MERGE_SAVE"
a9358240
JH
269 single_strategy=yes
270 ;;
271esac
91063bbc
JH
272
273result_tree= best_cnt=-1 best_strategy= wt_strategy=
695bf722 274merge_was_ok=
91063bbc
JH
275for strategy in $use_strategies
276do
277 test "$wt_strategy" = '' || {
278 echo "Rewinding the tree to pristine..."
a9358240 279 restorestate
91063bbc 280 }
a9358240
JH
281 case "$single_strategy" in
282 no)
283 echo "Trying merge strategy $strategy..."
284 ;;
285 esac
286
287 # Remember which strategy left the state in the working tree
91063bbc 288 wt_strategy=$strategy
a9358240 289
123ee3ca
JH
290 git-merge-$strategy $common -- "$head_arg" "$@"
291 exit=$?
292 if test "$no_commit" = t && test "$exit" = 0
293 then
695bf722 294 merge_was_ok=t
123ee3ca
JH
295 exit=1 ;# pretend it left conflicts.
296 fi
297
298 test "$exit" = 0 || {
91063bbc
JH
299
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.
302
91063bbc
JH
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 }
317
318 # Automerge succeeded.
319 result_tree=$(git-write-tree) && break
320done
321
322# If we have a resulting tree, that means the strategy module
323# auto resolved the merge cleanly.
324if test '' != "$result_tree"
325then
6ea23343 326 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
bf7960eb 327 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
e1447e38 328 finish "$result_commit" "Merge made by $wt_strategy."
a9358240 329 dropsave
91063bbc
JH
330 exit 0
331fi
332
333# Pick the result from the best strategy and have the user fix it up.
334case "$best_strategy" in
335'')
a9358240 336 restorestate
4275df51
FK
337 echo >&2 "No merge strategy handled the merge."
338 exit 2
91063bbc
JH
339 ;;
340"$wt_strategy")
341 # We already have its result in the working tree.
342 ;;
343*)
344 echo "Rewinding the tree to pristine..."
a9358240 345 restorestate
91063bbc 346 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 347 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
348 ;;
349esac
7d0c6887
JH
350
351if test "$squash" = t
352then
353 finish
354else
355 for remote
356 do
357 echo $remote
358 done >"$GIT_DIR/MERGE_HEAD"
359 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
360fi
deca7e8c 361
695bf722
JH
362if test "$merge_was_ok" = t
363then
364 echo >&2 \
365 "Automatic merge went well; stopped before committing as requested"
366 exit 0
367else
6b94f1e4
JH
368 {
369 echo '
370Conflicts:
371'
372 git ls-files --unmerged |
373 sed -e 's/^[^ ]* / /' |
374 uniq
375 } >>"$GIT_DIR/MERGE_MSG"
1536dd9c
JH
376 if test -d "$GIT_DIR/rr-cache"
377 then
378 git-rerere
379 fi
50ac7408 380 die "Automatic merge failed; fix conflicts and then commit the result."
695bf722 381fi