Makefile: say the default target upfront.
[tgit.git] / git-merge.sh
blobd352a3cf65e45e0da0a8ee1989e9acb522e132b5
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup
8 LF='
11 usage () {
12 die "git-merge [-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+"
15 all_strategies='recursive octopus resolve stupid ours'
16 default_strategies='recursive'
17 use_strategies=
19 dropsave() {
20 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
21 "$GIT_DIR/MERGE_SAVE" || exit 1
24 savestate() {
25 # Stash away any local modifications.
26 git-diff-index -z --name-only $head |
27 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
30 restorestate() {
31 if test -f "$GIT_DIR/MERGE_SAVE"
32 then
33 git reset --hard $head
34 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
35 git-update-index --refresh >/dev/null
39 finish () {
40 test '' = "$2" || echo "$2"
41 case "$merge_msg" in
42 '')
43 echo "No merge message -- not updating HEAD"
46 git-update-ref HEAD "$1" "$head" || exit 1
48 esac
50 case "$no_summary" in
51 '')
52 git-diff-tree -p -M "$head" "$1" |
53 git-apply --stat --summary
55 esac
58 while case "$#" in 0) break ;; esac
60 case "$1" in
61 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
62 --no-summa|--no-summar|--no-summary)
63 no_summary=t ;;
64 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
65 no_commit=t ;;
66 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
67 --strateg=*|--strategy=*|\
68 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
69 case "$#,$1" in
70 *,*=*)
71 strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
72 1,*)
73 usage ;;
75 strategy="$2"
76 shift ;;
77 esac
78 case " $all_strategies " in
79 *" $strategy "*)
80 use_strategies="$use_strategies$strategy " ;;
82 die "available strategies are: $all_strategies" ;;
83 esac
85 -*) usage ;;
86 *) break ;;
87 esac
88 shift
89 done
91 test "$#" -le 2 && usage ;# we need at least two heads.
93 merge_msg="$1"
94 shift
95 head_arg="$1"
96 head=$(git-rev-parse --verify "$1"^0) || usage
97 shift
99 # All the rest are remote heads
100 for remote
102 git-rev-parse --verify "$remote"^0 >/dev/null ||
103 die "$remote - not something we can merge"
104 done
106 case "$#" in
108 common=$(git-merge-base --all $head "$@")
111 common=$(git-show-branch --merge-base $head "$@")
113 esac
114 echo "$head" >"$GIT_DIR/ORIG_HEAD"
116 case "$#,$common,$no_commit" in
117 *,'',*)
118 # No common ancestors found. We need a real merge.
120 1,"$1",*)
121 # If head can reach all the merge then we are up to date.
122 # but first the most common case of merging one remote
123 echo "Already up-to-date."
124 dropsave
125 exit 0
127 1,"$head",*)
128 # Again the most common case of merging one remote.
129 echo "Updating from $head to $1."
130 git-update-index --refresh 2>/dev/null
131 new_head=$(git-rev-parse --verify "$1^0") &&
132 git-read-tree -u -m $head "$new_head" &&
133 finish "$new_head" "Fast forward"
134 dropsave
135 exit 0
137 1,?*"$LF"?*,*)
138 # We are not doing octopus and not fast forward. Need a
139 # real merge.
141 1,*,)
142 # We are not doing octopus, not fast forward, and have only
143 # one common. See if it is really trivial.
144 echo "Trying really trivial in-index merge..."
145 git-update-index --refresh 2>/dev/null
146 if git-read-tree --trivial -m -u $common $head "$1" &&
147 result_tree=$(git-write-tree)
148 then
149 echo "Wonderful."
150 result_commit=$(
151 echo "$merge_msg" |
152 git-commit-tree $result_tree -p HEAD -p "$1"
153 ) || exit
154 finish "$result_commit" "In-index merge"
155 dropsave
156 exit 0
158 echo "Nope."
161 # An octopus. If we can reach all the remote we are up to date.
162 up_to_date=t
163 for remote
165 common_one=$(git-merge-base --all $head $remote)
166 if test "$common_one" != "$remote"
167 then
168 up_to_date=f
169 break
171 done
172 if test "$up_to_date" = t
173 then
174 echo "Already up-to-date. Yeeah!"
175 dropsave
176 exit 0
179 esac
181 case "$use_strategies" in
183 case "$#" in
185 use_strategies="$default_strategies" ;;
187 use_strategies=octopus ;;
188 esac
190 esac
192 # At this point, we need a real merge. No matter what strategy
193 # we use, it would operate on the index, possibly affecting the
194 # working tree, and when resolved cleanly, have the desired tree
195 # in the index -- this means that the index must be in sync with
196 # the $head commit. The strategies are responsible to ensure this.
198 case "$use_strategies" in
199 ?*' '?*)
200 # Stash away the local changes so that we can try more than one.
201 savestate
202 single_strategy=no
205 rm -f "$GIT_DIR/MERGE_SAVE"
206 single_strategy=yes
208 esac
210 result_tree= best_cnt=-1 best_strategy= wt_strategy=
211 for strategy in $use_strategies
213 test "$wt_strategy" = '' || {
214 echo "Rewinding the tree to pristine..."
215 restorestate
217 case "$single_strategy" in
219 echo "Trying merge strategy $strategy..."
221 esac
223 # Remember which strategy left the state in the working tree
224 wt_strategy=$strategy
226 git-merge-$strategy $common -- "$head_arg" "$@"
227 exit=$?
228 if test "$no_commit" = t && test "$exit" = 0
229 then
230 exit=1 ;# pretend it left conflicts.
233 test "$exit" = 0 || {
235 # The backend exits with 1 when conflicts are left to be resolved,
236 # with 2 when it does not handle the given merge at all.
238 if test "$exit" -eq 1
239 then
240 cnt=`{
241 git-diff-files --name-only
242 git-ls-files --unmerged
243 } | wc -l`
244 if test $best_cnt -le 0 -o $cnt -le $best_cnt
245 then
246 best_strategy=$strategy
247 best_cnt=$cnt
250 continue
253 # Automerge succeeded.
254 result_tree=$(git-write-tree) && break
255 done
257 # If we have a resulting tree, that means the strategy module
258 # auto resolved the merge cleanly.
259 if test '' != "$result_tree"
260 then
261 parents="-p $head"
262 for remote
264 parents="$parents -p $remote"
265 done
266 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
267 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
268 dropsave
269 exit 0
272 # Pick the result from the best strategy and have the user fix it up.
273 case "$best_strategy" in
275 restorestate
276 die "No merge strategy handled the merge."
278 "$wt_strategy")
279 # We already have its result in the working tree.
282 echo "Rewinding the tree to pristine..."
283 restorestate
284 echo "Using the $best_strategy to prepare resolving by hand."
285 git-merge-$best_strategy $common -- "$head_arg" "$@"
287 esac
288 for remote
290 echo $remote
291 done >"$GIT_DIR/MERGE_HEAD"
292 echo $merge_msg >"$GIT_DIR/MERGE_MSG"
294 die "Automatic merge failed/prevented; fix up by hand"