]> git.ipfire.org Git - thirdparty/git.git/blame - git-merge.sh
git-merge --squash
[thirdparty/git.git] / git-merge.sh
CommitLineData
91063bbc
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
7d0c6887 6USAGE='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
ae2b0f15 7. git-sh-setup
91063bbc
JH
8
9LF='
10'
11
64da9e60 12all_strategies='recursive octopus resolve stupid ours'
6ea23343
JH
13default_twohead_strategies='recursive'
14default_octopus_strategies='octopus'
15no_trivial_merge_strategies='ours'
91063bbc 16use_strategies=
6ea23343
JH
17
18index_merge=t
abb7c7b3
JS
19if test "@@NO_PYTHON@@"; then
20 all_strategies='resolve octopus stupid ours'
6ea23343 21 default_twohead_strategies='resolve'
abb7c7b3 22fi
91063bbc 23
a9358240 24dropsave() {
deca7e8c 25 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
a9358240
JH
26 "$GIT_DIR/MERGE_SAVE" || exit 1
27}
28
29savestate() {
60fa0560 30 # Stash away any local modifications.
50b8e355 31 git-diff-index -z --name-only $head |
88f8f0a5 32 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
a9358240
JH
33}
34
35restorestate() {
deca7e8c
JH
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
41 fi
91063bbc
JH
42}
43
7d0c6887
JH
44finish_up_to_date () {
45 case "$squash" in
46 t)
47 echo "$1 (nothing to squash)" ;;
48 '')
49 echo "$1" ;;
50 esac
51 dropsave
52}
53
54squash_message () {
55 echo Squashed commit of the following:
56 echo
57 git-log --no-merges ^"$head" $remote
58}
59
4f692b19
JH
60finish () {
61 test '' = "$2" || echo "$2"
7d0c6887
JH
62 case "$squash" in
63 t)
64 echo "Squash commit -- not updating HEAD"
65 squash_message >"$GIT_DIR/SQUASH_MSG"
4f692b19 66 ;;
7d0c6887
JH
67 '')
68 case "$merge_msg" in
69 '')
70 echo "No merge message -- not updating HEAD"
71 ;;
72 *)
73 git-update-ref HEAD "$1" "$head" || exit 1
74 ;;
75 esac
4f692b19
JH
76 ;;
77 esac
7d0c6887 78 case "$1" in
91063bbc 79 '')
7d0c6887
JH
80 ;;
81 ?*)
82 case "$no_summary" in
83 '')
84 git-diff-tree --stat --summary -M "$head" "$1"
85 ;;
86 esac
91063bbc
JH
87 ;;
88 esac
89}
90
91while case "$#" in 0) break ;; esac
92do
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 ;;
7d0c6887
JH
97 --sq|--squ|--squa|--squas|--squash)
98 squash=t no_commit=t ;;
123ee3ca
JH
99 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
100 no_commit=t ;;
91063bbc
JH
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 "$1" : '-[^=]*=\(.*\)'` ;;
f88ed172 107 1,*)
91063bbc
JH
108 usage ;;
109 *)
110 strategy="$2"
111 shift ;;
112 esac
113 case " $all_strategies " in
114 *" $strategy "*)
115 use_strategies="$use_strategies$strategy " ;;
116 *)
117 die "available strategies are: $all_strategies" ;;
118 esac
119 ;;
120 -*) usage ;;
121 *) break ;;
122 esac
123 shift
124done
125
91063bbc
JH
126merge_msg="$1"
127shift
8cc01e50 128head_arg="$1"
91063bbc
JH
129head=$(git-rev-parse --verify "$1"^0) || usage
130shift
131
132# All the rest are remote heads
6ea23343
JH
133test "$#" = 0 && usage ;# we need at least one remote head.
134
9954f5b8 135remoteheads=
91063bbc
JH
136for remote
137do
9954f5b8 138 remotehead=$(git-rev-parse --verify "$remote"^0) ||
91063bbc 139 die "$remote - not something we can merge"
9954f5b8 140 remoteheads="${remoteheads}$remotehead "
91063bbc 141done
9954f5b8 142set x $remoteheads ; shift
91063bbc 143
6ea23343
JH
144case "$use_strategies" in
145'')
146 case "$#" in
147 1)
148 use_strategies="$default_twohead_strategies" ;;
149 *)
150 use_strategies="$default_octopus_strategies" ;;
151 esac
152 ;;
153esac
154
155for s in $use_strategies
156do
157 case " $s " in
158 *" $no_trivial_merge_strategies "*)
159 index_merge=f
160 break
161 ;;
162 esac
163done
164
13956670
JH
165case "$#" in
1661)
167 common=$(git-merge-base --all $head "$@")
168 ;;
169*)
170 common=$(git-show-branch --merge-base $head "$@")
171 ;;
172esac
91063bbc
JH
173echo "$head" >"$GIT_DIR/ORIG_HEAD"
174
6ea23343
JH
175case "$index_merge,$#,$common,$no_commit" in
176f,*)
177 # We've been told not to try anything clever. Skip to real merge.
178 ;;
179?,*,'',*)
88f8f0a5 180 # No common ancestors found. We need a real merge.
91063bbc 181 ;;
6ea23343 182?,1,"$1",*)
91063bbc 183 # If head can reach all the merge then we are up to date.
6ea23343 184 # but first the most common case of merging one remote.
7d0c6887 185 finish_up_to_date "Already up-to-date."
91063bbc
JH
186 exit 0
187 ;;
6ea23343 188?,1,"$head",*)
91063bbc 189 # Again the most common case of merging one remote.
180b0d74 190 echo "Updating from $head to $1"
91063bbc 191 git-update-index --refresh 2>/dev/null
bf7960eb 192 new_head=$(git-rev-parse --verify "$1^0") &&
744633cb 193 git-read-tree -u -v -m $head "$new_head" &&
4f692b19 194 finish "$new_head" "Fast forward"
a9358240 195 dropsave
91063bbc
JH
196 exit 0
197 ;;
6ea23343 198?,1,?*"$LF"?*,*)
91063bbc
JH
199 # We are not doing octopus and not fast forward. Need a
200 # real merge.
201 ;;
6ea23343 202?,1,*,)
f9d72413
JH
203 # We are not doing octopus, not fast forward, and have only
204 # one common. See if it is really trivial.
e3b59a44
JH
205 git var GIT_COMMITTER_IDENT >/dev/null || exit
206
f9d72413
JH
207 echo "Trying really trivial in-index merge..."
208 git-update-index --refresh 2>/dev/null
744633cb 209 if git-read-tree --trivial -m -u -v $common $head "$1" &&
f9d72413
JH
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
4f692b19 217 finish "$result_commit" "In-index merge"
f9d72413
JH
218 dropsave
219 exit 0
220 fi
221 echo "Nope."
222 ;;
91063bbc
JH
223*)
224 # An octopus. If we can reach all the remote we are up to date.
225 up_to_date=t
226 for remote
227 do
13956670 228 common_one=$(git-merge-base --all $head $remote)
91063bbc
JH
229 if test "$common_one" != "$remote"
230 then
231 up_to_date=f
232 break
233 fi
234 done
235 if test "$up_to_date" = t
236 then
7d0c6887 237 finish_up_to_date "Already up-to-date. Yeeah!"
91063bbc
JH
238 exit 0
239 fi
240 ;;
241esac
242
e3b59a44
JH
243# We are going to make a new commit.
244git var GIT_COMMITTER_IDENT >/dev/null || exit
245
a9358240
JH
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
60fa0560 250# the $head commit. The strategies are responsible to ensure this.
91063bbc 251
a9358240
JH
252case "$use_strategies" in
253?*' '?*)
254 # Stash away the local changes so that we can try more than one.
255 savestate
256 single_strategy=no
257 ;;
258*)
deca7e8c 259 rm -f "$GIT_DIR/MERGE_SAVE"
a9358240
JH
260 single_strategy=yes
261 ;;
262esac
91063bbc
JH
263
264result_tree= best_cnt=-1 best_strategy= wt_strategy=
695bf722 265merge_was_ok=
91063bbc
JH
266for strategy in $use_strategies
267do
268 test "$wt_strategy" = '' || {
269 echo "Rewinding the tree to pristine..."
a9358240 270 restorestate
91063bbc 271 }
a9358240
JH
272 case "$single_strategy" in
273 no)
274 echo "Trying merge strategy $strategy..."
275 ;;
276 esac
277
278 # Remember which strategy left the state in the working tree
91063bbc 279 wt_strategy=$strategy
a9358240 280
123ee3ca
JH
281 git-merge-$strategy $common -- "$head_arg" "$@"
282 exit=$?
283 if test "$no_commit" = t && test "$exit" = 0
284 then
695bf722 285 merge_was_ok=t
123ee3ca
JH
286 exit=1 ;# pretend it left conflicts.
287 fi
288
289 test "$exit" = 0 || {
91063bbc
JH
290
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.
293
91063bbc
JH
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
304 fi
305 fi
306 continue
307 }
308
309 # Automerge succeeded.
310 result_tree=$(git-write-tree) && break
311done
312
313# If we have a resulting tree, that means the strategy module
314# auto resolved the merge cleanly.
315if test '' != "$result_tree"
316then
6ea23343 317 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
bf7960eb 318 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
4f692b19 319 finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
a9358240 320 dropsave
91063bbc
JH
321 exit 0
322fi
323
324# Pick the result from the best strategy and have the user fix it up.
325case "$best_strategy" in
326'')
a9358240 327 restorestate
4275df51
FK
328 echo >&2 "No merge strategy handled the merge."
329 exit 2
91063bbc
JH
330 ;;
331"$wt_strategy")
332 # We already have its result in the working tree.
333 ;;
334*)
335 echo "Rewinding the tree to pristine..."
a9358240 336 restorestate
91063bbc 337 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 338 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
339 ;;
340esac
7d0c6887
JH
341
342if test "$squash" = t
343then
344 finish
345else
346 for remote
347 do
348 echo $remote
349 done >"$GIT_DIR/MERGE_HEAD"
350 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
351fi
deca7e8c 352
695bf722
JH
353if test "$merge_was_ok" = t
354then
355 echo >&2 \
356 "Automatic merge went well; stopped before committing as requested"
357 exit 0
358else
6b94f1e4
JH
359 {
360 echo '
361Conflicts:
362'
363 git ls-files --unmerged |
364 sed -e 's/^[^ ]* / /' |
365 uniq
366 } >>"$GIT_DIR/MERGE_MSG"
1536dd9c
JH
367 if test -d "$GIT_DIR/rr-cache"
368 then
369 git-rerere
370 fi
50ac7408 371 die "Automatic merge failed; fix conflicts and then commit the result."
695bf722 372fi