t3200: branch --help does not die anymore.
[git/git.git] / git-merge.sh
CommitLineData
91063bbc
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
ae2b0f15 6. git-sh-setup
91063bbc
JH
7
8LF='
9'
10
11usage () {
123ee3ca 12 die "git-merge [-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+"
91063bbc
JH
13}
14
64da9e60 15all_strategies='recursive octopus resolve stupid ours'
fbf8ac21 16default_strategies='recursive'
91063bbc
JH
17use_strategies=
18
a9358240 19dropsave() {
deca7e8c 20 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
a9358240
JH
21 "$GIT_DIR/MERGE_SAVE" || exit 1
22}
23
24savestate() {
60fa0560 25 # Stash away any local modifications.
50b8e355 26 git-diff-index -z --name-only $head |
88f8f0a5 27 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
a9358240
JH
28}
29
30restorestate() {
deca7e8c
JH
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
36 fi
91063bbc
JH
37}
38
4f692b19
JH
39finish () {
40 test '' = "$2" || echo "$2"
41 case "$merge_msg" in
42 '')
43 echo "No merge message -- not updating HEAD"
44 ;;
45 *)
46 git-update-ref HEAD "$1" "$head" || exit 1
47 ;;
48 esac
49
91063bbc
JH
50 case "$no_summary" in
51 '')
4f692b19 52 git-diff-tree -p -M "$head" "$1" |
91063bbc
JH
53 git-apply --stat --summary
54 ;;
55 esac
56}
57
58while case "$#" in 0) break ;; esac
59do
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 ;;
123ee3ca
JH
64 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
65 no_commit=t ;;
91063bbc
JH
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" : '-[^=]*=\(.*\)'` ;;
f88ed172 72 1,*)
91063bbc
JH
73 usage ;;
74 *)
75 strategy="$2"
76 shift ;;
77 esac
78 case " $all_strategies " in
79 *" $strategy "*)
80 use_strategies="$use_strategies$strategy " ;;
81 *)
82 die "available strategies are: $all_strategies" ;;
83 esac
84 ;;
85 -*) usage ;;
86 *) break ;;
87 esac
88 shift
89done
90
91063bbc
JH
91test "$#" -le 2 && usage ;# we need at least two heads.
92
93merge_msg="$1"
94shift
8cc01e50 95head_arg="$1"
91063bbc
JH
96head=$(git-rev-parse --verify "$1"^0) || usage
97shift
98
99# All the rest are remote heads
9954f5b8 100remoteheads=
91063bbc
JH
101for remote
102do
9954f5b8 103 remotehead=$(git-rev-parse --verify "$remote"^0) ||
91063bbc 104 die "$remote - not something we can merge"
9954f5b8 105 remoteheads="${remoteheads}$remotehead "
91063bbc 106done
9954f5b8 107set x $remoteheads ; shift
91063bbc 108
13956670
JH
109case "$#" in
1101)
111 common=$(git-merge-base --all $head "$@")
112 ;;
113*)
114 common=$(git-show-branch --merge-base $head "$@")
115 ;;
116esac
91063bbc
JH
117echo "$head" >"$GIT_DIR/ORIG_HEAD"
118
123ee3ca
JH
119case "$#,$common,$no_commit" in
120*,'',*)
88f8f0a5 121 # No common ancestors found. We need a real merge.
91063bbc 122 ;;
123ee3ca 1231,"$1",*)
91063bbc
JH
124 # If head can reach all the merge then we are up to date.
125 # but first the most common case of merging one remote
4f692b19 126 echo "Already up-to-date."
a9358240 127 dropsave
91063bbc
JH
128 exit 0
129 ;;
123ee3ca 1301,"$head",*)
91063bbc
JH
131 # Again the most common case of merging one remote.
132 echo "Updating from $head to $1."
133 git-update-index --refresh 2>/dev/null
bf7960eb 134 new_head=$(git-rev-parse --verify "$1^0") &&
4f692b19
JH
135 git-read-tree -u -m $head "$new_head" &&
136 finish "$new_head" "Fast forward"
a9358240 137 dropsave
91063bbc
JH
138 exit 0
139 ;;
123ee3ca 1401,?*"$LF"?*,*)
91063bbc
JH
141 # We are not doing octopus and not fast forward. Need a
142 # real merge.
143 ;;
123ee3ca 1441,*,)
f9d72413
JH
145 # We are not doing octopus, not fast forward, and have only
146 # one common. See if it is really trivial.
147 echo "Trying really trivial in-index merge..."
148 git-update-index --refresh 2>/dev/null
149 if git-read-tree --trivial -m -u $common $head "$1" &&
150 result_tree=$(git-write-tree)
151 then
152 echo "Wonderful."
153 result_commit=$(
154 echo "$merge_msg" |
155 git-commit-tree $result_tree -p HEAD -p "$1"
156 ) || exit
4f692b19 157 finish "$result_commit" "In-index merge"
f9d72413
JH
158 dropsave
159 exit 0
160 fi
161 echo "Nope."
162 ;;
91063bbc
JH
163*)
164 # An octopus. If we can reach all the remote we are up to date.
165 up_to_date=t
166 for remote
167 do
13956670 168 common_one=$(git-merge-base --all $head $remote)
91063bbc
JH
169 if test "$common_one" != "$remote"
170 then
171 up_to_date=f
172 break
173 fi
174 done
175 if test "$up_to_date" = t
176 then
177 echo "Already up-to-date. Yeeah!"
a9358240 178 dropsave
91063bbc
JH
179 exit 0
180 fi
181 ;;
182esac
183
fbf8ac21
JH
184case "$use_strategies" in
185'')
186 case "$#" in
187 1)
188 use_strategies="$default_strategies" ;;
189 *)
190 use_strategies=octopus ;;
191 esac
192 ;;
193esac
194
a9358240
JH
195# At this point, we need a real merge. No matter what strategy
196# we use, it would operate on the index, possibly affecting the
197# working tree, and when resolved cleanly, have the desired tree
198# in the index -- this means that the index must be in sync with
60fa0560 199# the $head commit. The strategies are responsible to ensure this.
91063bbc 200
a9358240
JH
201case "$use_strategies" in
202?*' '?*)
203 # Stash away the local changes so that we can try more than one.
204 savestate
205 single_strategy=no
206 ;;
207*)
deca7e8c 208 rm -f "$GIT_DIR/MERGE_SAVE"
a9358240
JH
209 single_strategy=yes
210 ;;
211esac
91063bbc
JH
212
213result_tree= best_cnt=-1 best_strategy= wt_strategy=
214for strategy in $use_strategies
215do
216 test "$wt_strategy" = '' || {
217 echo "Rewinding the tree to pristine..."
a9358240 218 restorestate
91063bbc 219 }
a9358240
JH
220 case "$single_strategy" in
221 no)
222 echo "Trying merge strategy $strategy..."
223 ;;
224 esac
225
226 # Remember which strategy left the state in the working tree
91063bbc 227 wt_strategy=$strategy
a9358240 228
123ee3ca
JH
229 git-merge-$strategy $common -- "$head_arg" "$@"
230 exit=$?
231 if test "$no_commit" = t && test "$exit" = 0
232 then
233 exit=1 ;# pretend it left conflicts.
234 fi
235
236 test "$exit" = 0 || {
91063bbc
JH
237
238 # The backend exits with 1 when conflicts are left to be resolved,
239 # with 2 when it does not handle the given merge at all.
240
91063bbc
JH
241 if test "$exit" -eq 1
242 then
243 cnt=`{
244 git-diff-files --name-only
245 git-ls-files --unmerged
246 } | wc -l`
247 if test $best_cnt -le 0 -o $cnt -le $best_cnt
248 then
249 best_strategy=$strategy
250 best_cnt=$cnt
251 fi
252 fi
253 continue
254 }
255
256 # Automerge succeeded.
257 result_tree=$(git-write-tree) && break
258done
259
260# If we have a resulting tree, that means the strategy module
261# auto resolved the merge cleanly.
262if test '' != "$result_tree"
263then
264 parents="-p $head"
265 for remote
266 do
267 parents="$parents -p $remote"
268 done
bf7960eb 269 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
4f692b19 270 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
a9358240 271 dropsave
91063bbc
JH
272 exit 0
273fi
274
275# Pick the result from the best strategy and have the user fix it up.
276case "$best_strategy" in
277'')
a9358240 278 restorestate
4275df51
FK
279 echo >&2 "No merge strategy handled the merge."
280 exit 2
91063bbc
JH
281 ;;
282"$wt_strategy")
283 # We already have its result in the working tree.
284 ;;
285*)
286 echo "Rewinding the tree to pristine..."
a9358240 287 restorestate
91063bbc 288 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 289 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
290 ;;
291esac
292for remote
293do
294 echo $remote
295done >"$GIT_DIR/MERGE_HEAD"
deca7e8c
JH
296echo $merge_msg >"$GIT_DIR/MERGE_MSG"
297
123ee3ca 298die "Automatic merge failed/prevented; fix up by hand"