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