Fix errno usage in connect.c
[git/dscho.git] / git-merge.sh
blob24e3b507ef1f907e7146a2653189f45a92db08fd
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 test '' = "$2" || echo "$2"
62 case "$squash" in
64 echo "Squash commit -- not updating HEAD"
65 squash_message >"$GIT_DIR/SQUASH_MSG"
67 '')
68 case "$merge_msg" in
69 '')
70 echo "No merge message -- not updating HEAD"
73 git-update-ref HEAD "$1" "$head" || exit 1
75 esac
77 esac
78 case "$1" in
79 '')
81 ?*)
82 case "$no_summary" in
83 '')
84 git-diff-tree --stat --summary -M "$head" "$1"
86 esac
88 esac
91 while case "$#" in 0) break ;; esac
93 case "$1" in
94 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
95 --no-summa|--no-summar|--no-summary)
96 no_summary=t ;;
97 --sq|--squ|--squa|--squas|--squash)
98 squash=t no_commit=t ;;
99 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
100 no_commit=t ;;
101 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
102 --strateg=*|--strategy=*|\
103 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
104 case "$#,$1" in
105 *,*=*)
106 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
107 1,*)
108 usage ;;
110 strategy="$2"
111 shift ;;
112 esac
113 case " $all_strategies " in
114 *" $strategy "*)
115 use_strategies="$use_strategies$strategy " ;;
117 die "available strategies are: $all_strategies" ;;
118 esac
120 -*) usage ;;
121 *) break ;;
122 esac
123 shift
124 done
126 merge_msg="$1"
127 shift
128 head_arg="$1"
129 head=$(git-rev-parse --verify "$1"^0) || usage
130 shift
132 # All the rest are remote heads
133 test "$#" = 0 && usage ;# we need at least one remote head.
135 remoteheads=
136 for remote
138 remotehead=$(git-rev-parse --verify "$remote"^0) ||
139 die "$remote - not something we can merge"
140 remoteheads="${remoteheads}$remotehead "
141 done
142 set x $remoteheads ; shift
144 case "$use_strategies" in
146 case "$#" in
148 use_strategies="$default_twohead_strategies" ;;
150 use_strategies="$default_octopus_strategies" ;;
151 esac
153 esac
155 for s in $use_strategies
157 case " $s " in
158 *" $no_trivial_merge_strategies "*)
159 index_merge=f
160 break
162 esac
163 done
165 case "$#" in
167 common=$(git-merge-base --all $head "$@")
170 common=$(git-show-branch --merge-base $head "$@")
172 esac
173 echo "$head" >"$GIT_DIR/ORIG_HEAD"
175 case "$index_merge,$#,$common,$no_commit" in
176 f,*)
177 # We've been told not to try anything clever. Skip to real merge.
179 ?,*,'',*)
180 # No common ancestors found. We need a real merge.
182 ?,1,"$1",*)
183 # If head can reach all the merge then we are up to date.
184 # but first the most common case of merging one remote.
185 finish_up_to_date "Already up-to-date."
186 exit 0
188 ?,1,"$head",*)
189 # Again the most common case of merging one remote.
190 echo "Updating from $head to $1"
191 git-update-index --refresh 2>/dev/null
192 new_head=$(git-rev-parse --verify "$1^0") &&
193 git-read-tree -u -v -m $head "$new_head" &&
194 finish "$new_head" "Fast forward"
195 dropsave
196 exit 0
198 ?,1,?*"$LF"?*,*)
199 # We are not doing octopus and not fast forward. Need a
200 # real merge.
202 ?,1,*,)
203 # We are not doing octopus, not fast forward, and have only
204 # one common. See if it is really trivial.
205 git var GIT_COMMITTER_IDENT >/dev/null || exit
207 echo "Trying really trivial in-index merge..."
208 git-update-index --refresh 2>/dev/null
209 if git-read-tree --trivial -m -u -v $common $head "$1" &&
210 result_tree=$(git-write-tree)
211 then
212 echo "Wonderful."
213 result_commit=$(
214 echo "$merge_msg" |
215 git-commit-tree $result_tree -p HEAD -p "$1"
216 ) || exit
217 finish "$result_commit" "In-index merge"
218 dropsave
219 exit 0
221 echo "Nope."
224 # An octopus. If we can reach all the remote we are up to date.
225 up_to_date=t
226 for remote
228 common_one=$(git-merge-base --all $head $remote)
229 if test "$common_one" != "$remote"
230 then
231 up_to_date=f
232 break
234 done
235 if test "$up_to_date" = t
236 then
237 finish_up_to_date "Already up-to-date. Yeeah!"
238 exit 0
241 esac
243 # We are going to make a new commit.
244 git var GIT_COMMITTER_IDENT >/dev/null || exit
246 # At this point, we need a real merge. No matter what strategy
247 # we use, it would operate on the index, possibly affecting the
248 # working tree, and when resolved cleanly, have the desired tree
249 # in the index -- this means that the index must be in sync with
250 # the $head commit. The strategies are responsible to ensure this.
252 case "$use_strategies" in
253 ?*' '?*)
254 # Stash away the local changes so that we can try more than one.
255 savestate
256 single_strategy=no
259 rm -f "$GIT_DIR/MERGE_SAVE"
260 single_strategy=yes
262 esac
264 result_tree= best_cnt=-1 best_strategy= wt_strategy=
265 merge_was_ok=
266 for strategy in $use_strategies
268 test "$wt_strategy" = '' || {
269 echo "Rewinding the tree to pristine..."
270 restorestate
272 case "$single_strategy" in
274 echo "Trying merge strategy $strategy..."
276 esac
278 # Remember which strategy left the state in the working tree
279 wt_strategy=$strategy
281 git-merge-$strategy $common -- "$head_arg" "$@"
282 exit=$?
283 if test "$no_commit" = t && test "$exit" = 0
284 then
285 merge_was_ok=t
286 exit=1 ;# pretend it left conflicts.
289 test "$exit" = 0 || {
291 # The backend exits with 1 when conflicts are left to be resolved,
292 # with 2 when it does not handle the given merge at all.
294 if test "$exit" -eq 1
295 then
296 cnt=`{
297 git-diff-files --name-only
298 git-ls-files --unmerged
299 } | wc -l`
300 if test $best_cnt -le 0 -o $cnt -le $best_cnt
301 then
302 best_strategy=$strategy
303 best_cnt=$cnt
306 continue
309 # Automerge succeeded.
310 result_tree=$(git-write-tree) && break
311 done
313 # If we have a resulting tree, that means the strategy module
314 # auto resolved the merge cleanly.
315 if test '' != "$result_tree"
316 then
317 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
318 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
319 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
320 dropsave
321 exit 0
324 # Pick the result from the best strategy and have the user fix it up.
325 case "$best_strategy" in
327 restorestate
328 echo >&2 "No merge strategy handled the merge."
329 exit 2
331 "$wt_strategy")
332 # We already have its result in the working tree.
335 echo "Rewinding the tree to pristine..."
336 restorestate
337 echo "Using the $best_strategy to prepare resolving by hand."
338 git-merge-$best_strategy $common -- "$head_arg" "$@"
340 esac
342 if test "$squash" = t
343 then
344 finish
345 else
346 for remote
348 echo $remote
349 done >"$GIT_DIR/MERGE_HEAD"
350 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
353 if test "$merge_was_ok" = t
354 then
355 echo >&2 \
356 "Automatic merge went well; stopped before committing as requested"
357 exit 0
358 else
360 echo '
361 Conflicts:
363 git ls-files --unmerged |
364 sed -e 's/^[^ ]* / /' |
365 uniq
366 } >>"$GIT_DIR/MERGE_MSG"
367 if test -d "$GIT_DIR/rr-cache"
368 then
369 git-rerere
371 die "Automatic merge failed; fix conflicts and then commit the result."