git-merge: make it usable as the first class UI
[git/git.git] / git-merge.sh
CommitLineData
91063bbc
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
17bcdad3
JH
6USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [--reflog-action=<action>] [-m=<merge-message>] <commit>+'
7
ae2b0f15 8. git-sh-setup
91063bbc
JH
9
10LF='
11'
12
7cdbff14 13all_strategies='recur recursive octopus resolve stupid ours'
a06f678e 14default_twohead_strategies='recursive'
6ea23343
JH
15default_octopus_strategies='octopus'
16no_trivial_merge_strategies='ours'
91063bbc 17use_strategies=
6ea23343
JH
18
19index_merge=t
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
7d0c6887
JH
41finish_up_to_date () {
42 case "$squash" in
43 t)
44 echo "$1 (nothing to squash)" ;;
45 '')
46 echo "$1" ;;
47 esac
48 dropsave
49}
50
51squash_message () {
52 echo Squashed commit of the following:
53 echo
54 git-log --no-merges ^"$head" $remote
55}
56
4f692b19 57finish () {
e1447e38
SP
58 if test '' = "$2"
59 then
60 rlogm="$rloga"
61 else
62 echo "$2"
63 rlogm="$rloga: $2"
64 fi
7d0c6887
JH
65 case "$squash" in
66 t)
67 echo "Squash commit -- not updating HEAD"
68 squash_message >"$GIT_DIR/SQUASH_MSG"
4f692b19 69 ;;
7d0c6887
JH
70 '')
71 case "$merge_msg" in
72 '')
73 echo "No merge message -- not updating HEAD"
74 ;;
75 *)
e1447e38 76 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
7d0c6887
JH
77 ;;
78 esac
4f692b19
JH
79 ;;
80 esac
7d0c6887 81 case "$1" in
91063bbc 82 '')
7d0c6887
JH
83 ;;
84 ?*)
85 case "$no_summary" in
86 '')
87 git-diff-tree --stat --summary -M "$head" "$1"
88 ;;
89 esac
91063bbc
JH
90 ;;
91 esac
92}
93
fc61e313
RS
94case "$#" in 0) usage ;; esac
95
17bcdad3 96rloga= have_message=
91063bbc
JH
97while case "$#" in 0) break ;; esac
98do
99 case "$1" in
100 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
101 --no-summa|--no-summar|--no-summary)
102 no_summary=t ;;
7d0c6887
JH
103 --sq|--squ|--squa|--squas|--squash)
104 squash=t no_commit=t ;;
123ee3ca
JH
105 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
106 no_commit=t ;;
91063bbc
JH
107 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
108 --strateg=*|--strategy=*|\
109 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
110 case "$#,$1" in
111 *,*=*)
8096fae7 112 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
f88ed172 113 1,*)
91063bbc
JH
114 usage ;;
115 *)
116 strategy="$2"
117 shift ;;
118 esac
119 case " $all_strategies " in
120 *" $strategy "*)
121 use_strategies="$use_strategies$strategy " ;;
122 *)
123 die "available strategies are: $all_strategies" ;;
124 esac
125 ;;
e1447e38
SP
126 --reflog-action=*)
127 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
128 ;;
17bcdad3
JH
129 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
130 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
131 have_message=t
132 ;;
133 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
134 shift
135 case "$#" in
136 1) usage ;;
137 esac
138 merge_msg="$1"
139 have_message=t
140 ;;
91063bbc
JH
141 -*) usage ;;
142 *) break ;;
143 esac
144 shift
145done
146
17bcdad3
JH
147# This could be traditional "merge <msg> HEAD <commit>..." and the
148# way we can tell it is to see if the second token is HEAD, but some
149# people might have misused the interface and used a committish that
150# is the same as HEAD there instead. Traditional format never would
151# have "-m" so it is an additional safety measure to check for it.
152
153if test -z "$have_message" &&
154 second_token=$(git-rev-parse --verify "$2^0" 2>/dev/null) &&
155 head_commit=$(git-rev-parse --verify "HEAD" 2>/dev/null) &&
156 test "$second_token" = "$head_commit"
157then
158 merge_msg="$1"
159 shift
160 head_arg="$1"
161 shift
162else
163 # We are invoked directly as the first-class UI.
164 head_arg=HEAD
165
166 # All the rest are the commits being merged; prepare
167 # the standard merge summary message to be appended to
168 # the given message. If remote is invalid we will die
169 # later in the common codepath so we discard the error
170 # in this loop.
171 merge_name=$(for remote
172 do
173 rh=$(git-rev-parse --verify "$remote"^0 2>/dev/null)
174 if git show-ref -q --verify "refs/heads/$remote"
175 then
176 what=branch
177 else
178 what=commit
179 fi
180 echo "$rh $what '$remote'"
181 done | git-fmt-merge-msg
182 )
183 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
184fi
185head=$(git-rev-parse --verify "$head_arg"^0) || usage
91063bbc
JH
186
187# All the rest are remote heads
6ea23343 188test "$#" = 0 && usage ;# we need at least one remote head.
e1447e38 189test "$rloga" = '' && rloga="merge: $@"
6ea23343 190
9954f5b8 191remoteheads=
91063bbc
JH
192for remote
193do
9954f5b8 194 remotehead=$(git-rev-parse --verify "$remote"^0) ||
91063bbc 195 die "$remote - not something we can merge"
9954f5b8 196 remoteheads="${remoteheads}$remotehead "
91063bbc 197done
9954f5b8 198set x $remoteheads ; shift
91063bbc 199
6ea23343
JH
200case "$use_strategies" in
201'')
202 case "$#" in
203 1)
204 use_strategies="$default_twohead_strategies" ;;
205 *)
206 use_strategies="$default_octopus_strategies" ;;
207 esac
208 ;;
209esac
210
211for s in $use_strategies
212do
213 case " $s " in
214 *" $no_trivial_merge_strategies "*)
215 index_merge=f
216 break
217 ;;
218 esac
219done
220
13956670
JH
221case "$#" in
2221)
223 common=$(git-merge-base --all $head "$@")
224 ;;
225*)
226 common=$(git-show-branch --merge-base $head "$@")
227 ;;
228esac
91063bbc
JH
229echo "$head" >"$GIT_DIR/ORIG_HEAD"
230
6ea23343
JH
231case "$index_merge,$#,$common,$no_commit" in
232f,*)
233 # We've been told not to try anything clever. Skip to real merge.
234 ;;
235?,*,'',*)
88f8f0a5 236 # No common ancestors found. We need a real merge.
91063bbc 237 ;;
6ea23343 238?,1,"$1",*)
91063bbc 239 # If head can reach all the merge then we are up to date.
6ea23343 240 # but first the most common case of merging one remote.
7d0c6887 241 finish_up_to_date "Already up-to-date."
91063bbc
JH
242 exit 0
243 ;;
6ea23343 244?,1,"$head",*)
91063bbc 245 # Again the most common case of merging one remote.
ba0ac36e 246 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
91063bbc 247 git-update-index --refresh 2>/dev/null
bf7960eb 248 new_head=$(git-rev-parse --verify "$1^0") &&
744633cb 249 git-read-tree -u -v -m $head "$new_head" &&
4f692b19 250 finish "$new_head" "Fast forward"
a9358240 251 dropsave
91063bbc
JH
252 exit 0
253 ;;
6ea23343 254?,1,?*"$LF"?*,*)
91063bbc
JH
255 # We are not doing octopus and not fast forward. Need a
256 # real merge.
257 ;;
6ea23343 258?,1,*,)
f9d72413
JH
259 # We are not doing octopus, not fast forward, and have only
260 # one common. See if it is really trivial.
e3b59a44
JH
261 git var GIT_COMMITTER_IDENT >/dev/null || exit
262
f9d72413
JH
263 echo "Trying really trivial in-index merge..."
264 git-update-index --refresh 2>/dev/null
744633cb 265 if git-read-tree --trivial -m -u -v $common $head "$1" &&
f9d72413
JH
266 result_tree=$(git-write-tree)
267 then
268 echo "Wonderful."
269 result_commit=$(
270 echo "$merge_msg" |
271 git-commit-tree $result_tree -p HEAD -p "$1"
272 ) || exit
4f692b19 273 finish "$result_commit" "In-index merge"
f9d72413
JH
274 dropsave
275 exit 0
276 fi
277 echo "Nope."
278 ;;
91063bbc
JH
279*)
280 # An octopus. If we can reach all the remote we are up to date.
281 up_to_date=t
282 for remote
283 do
13956670 284 common_one=$(git-merge-base --all $head $remote)
91063bbc
JH
285 if test "$common_one" != "$remote"
286 then
287 up_to_date=f
288 break
289 fi
290 done
291 if test "$up_to_date" = t
292 then
7d0c6887 293 finish_up_to_date "Already up-to-date. Yeeah!"
91063bbc
JH
294 exit 0
295 fi
296 ;;
297esac
298
e3b59a44
JH
299# We are going to make a new commit.
300git var GIT_COMMITTER_IDENT >/dev/null || exit
301
a9358240
JH
302# At this point, we need a real merge. No matter what strategy
303# we use, it would operate on the index, possibly affecting the
304# working tree, and when resolved cleanly, have the desired tree
305# in the index -- this means that the index must be in sync with
60fa0560 306# the $head commit. The strategies are responsible to ensure this.
91063bbc 307
a9358240
JH
308case "$use_strategies" in
309?*' '?*)
310 # Stash away the local changes so that we can try more than one.
311 savestate
312 single_strategy=no
313 ;;
314*)
deca7e8c 315 rm -f "$GIT_DIR/MERGE_SAVE"
a9358240
JH
316 single_strategy=yes
317 ;;
318esac
91063bbc
JH
319
320result_tree= best_cnt=-1 best_strategy= wt_strategy=
695bf722 321merge_was_ok=
91063bbc
JH
322for strategy in $use_strategies
323do
324 test "$wt_strategy" = '' || {
325 echo "Rewinding the tree to pristine..."
a9358240 326 restorestate
91063bbc 327 }
a9358240
JH
328 case "$single_strategy" in
329 no)
330 echo "Trying merge strategy $strategy..."
331 ;;
332 esac
333
334 # Remember which strategy left the state in the working tree
91063bbc 335 wt_strategy=$strategy
a9358240 336
123ee3ca
JH
337 git-merge-$strategy $common -- "$head_arg" "$@"
338 exit=$?
339 if test "$no_commit" = t && test "$exit" = 0
340 then
695bf722 341 merge_was_ok=t
123ee3ca
JH
342 exit=1 ;# pretend it left conflicts.
343 fi
344
345 test "$exit" = 0 || {
91063bbc
JH
346
347 # The backend exits with 1 when conflicts are left to be resolved,
348 # with 2 when it does not handle the given merge at all.
349
91063bbc
JH
350 if test "$exit" -eq 1
351 then
352 cnt=`{
353 git-diff-files --name-only
354 git-ls-files --unmerged
355 } | wc -l`
356 if test $best_cnt -le 0 -o $cnt -le $best_cnt
357 then
358 best_strategy=$strategy
359 best_cnt=$cnt
360 fi
361 fi
362 continue
363 }
364
365 # Automerge succeeded.
366 result_tree=$(git-write-tree) && break
367done
368
369# If we have a resulting tree, that means the strategy module
370# auto resolved the merge cleanly.
371if test '' != "$result_tree"
372then
6ea23343 373 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
bf7960eb 374 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
e1447e38 375 finish "$result_commit" "Merge made by $wt_strategy."
a9358240 376 dropsave
91063bbc
JH
377 exit 0
378fi
379
380# Pick the result from the best strategy and have the user fix it up.
381case "$best_strategy" in
382'')
a9358240 383 restorestate
4275df51
FK
384 echo >&2 "No merge strategy handled the merge."
385 exit 2
91063bbc
JH
386 ;;
387"$wt_strategy")
388 # We already have its result in the working tree.
389 ;;
390*)
391 echo "Rewinding the tree to pristine..."
a9358240 392 restorestate
91063bbc 393 echo "Using the $best_strategy to prepare resolving by hand."
a9358240 394 git-merge-$best_strategy $common -- "$head_arg" "$@"
91063bbc
JH
395 ;;
396esac
7d0c6887
JH
397
398if test "$squash" = t
399then
400 finish
401else
402 for remote
403 do
404 echo $remote
405 done >"$GIT_DIR/MERGE_HEAD"
406 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
407fi
deca7e8c 408
695bf722
JH
409if test "$merge_was_ok" = t
410then
411 echo >&2 \
412 "Automatic merge went well; stopped before committing as requested"
413 exit 0
414else
6b94f1e4
JH
415 {
416 echo '
417Conflicts:
418'
419 git ls-files --unmerged |
420 sed -e 's/^[^ ]* / /' |
421 uniq
422 } >>"$GIT_DIR/MERGE_MSG"
1536dd9c
JH
423 if test -d "$GIT_DIR/rr-cache"
424 then
425 git-rerere
426 fi
50ac7408 427 die "Automatic merge failed; fix conflicts and then commit the result."
695bf722 428fi