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