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