Documentation: i18n commit log message notes.
[tgit.git] / git-merge.sh
blobba42260426296b63070a1fbdc22a9b77ea5ac412
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
8 . git-sh-setup
9 set_reflog_action "merge $*"
11 LF='
14 all_strategies='recur recursive octopus resolve stupid ours'
15 default_twohead_strategies='recursive'
16 default_octopus_strategies='octopus'
17 no_trivial_merge_strategies='ours'
18 use_strategies=
20 index_merge=t
22 dropsave() {
23 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
24 "$GIT_DIR/MERGE_SAVE" || exit 1
27 savestate() {
28 # Stash away any local modifications.
29 git-diff-index -z --name-only $head |
30 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
33 restorestate() {
34 if test -f "$GIT_DIR/MERGE_SAVE"
35 then
36 git reset --hard $head >/dev/null
37 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
38 git-update-index --refresh >/dev/null
42 finish_up_to_date () {
43 case "$squash" in
45 echo "$1 (nothing to squash)" ;;
46 '')
47 echo "$1" ;;
48 esac
49 dropsave
52 squash_message () {
53 echo Squashed commit of the following:
54 echo
55 git-log --no-merges ^"$head" $remote
58 finish () {
59 if test '' = "$2"
60 then
61 rlogm="$GIT_REFLOG_ACTION"
62 else
63 echo "$2"
64 rlogm="$GIT_REFLOG_ACTION: $2"
66 case "$squash" in
68 echo "Squash commit -- not updating HEAD"
69 squash_message >"$GIT_DIR/SQUASH_MSG"
71 '')
72 case "$merge_msg" in
73 '')
74 echo "No merge message -- not updating HEAD"
77 git-update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
79 esac
81 esac
82 case "$1" in
83 '')
85 ?*)
86 case "$no_summary" in
87 '')
88 git-diff-tree --stat --summary -M "$head" "$1"
90 esac
92 esac
95 merge_name () {
96 remote="$1"
97 rh=$(git-rev-parse --verify "$remote^0" 2>/dev/null) || return
98 bh=$(git-show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
99 if test "$rh" = "$bh"
100 then
101 echo "$rh branch '$remote' of ."
102 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
103 git-show-ref -q --verify "refs/heads/$truname" 2>/dev/null
104 then
105 echo "$rh branch '$truname' (early part) of ."
106 else
107 echo "$rh commit '$remote'"
111 case "$#" in 0) usage ;; esac
113 have_message=
114 while case "$#" in 0) break ;; esac
116 case "$1" in
117 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
118 --no-summa|--no-summar|--no-summary)
119 no_summary=t ;;
120 --sq|--squ|--squa|--squas|--squash)
121 squash=t no_commit=t ;;
122 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
123 no_commit=t ;;
124 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
125 --strateg=*|--strategy=*|\
126 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
127 case "$#,$1" in
128 *,*=*)
129 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
130 1,*)
131 usage ;;
133 strategy="$2"
134 shift ;;
135 esac
136 case " $all_strategies " in
137 *" $strategy "*)
138 use_strategies="$use_strategies$strategy " ;;
140 die "available strategies are: $all_strategies" ;;
141 esac
143 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
144 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
145 have_message=t
147 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
148 shift
149 case "$#" in
150 1) usage ;;
151 esac
152 merge_msg="$1"
153 have_message=t
155 -*) usage ;;
156 *) break ;;
157 esac
158 shift
159 done
161 # This could be traditional "merge <msg> HEAD <commit>..." and the
162 # way we can tell it is to see if the second token is HEAD, but some
163 # people might have misused the interface and used a committish that
164 # is the same as HEAD there instead. Traditional format never would
165 # have "-m" so it is an additional safety measure to check for it.
167 if test -z "$have_message" &&
168 second_token=$(git-rev-parse --verify "$2^0" 2>/dev/null) &&
169 head_commit=$(git-rev-parse --verify "HEAD" 2>/dev/null) &&
170 test "$second_token" = "$head_commit"
171 then
172 merge_msg="$1"
173 shift
174 head_arg="$1"
175 shift
176 elif ! git-rev-parse --verify HEAD >/dev/null 2>&1
177 then
178 # If the merged head is a valid one there is no reason to
179 # forbid "git merge" into a branch yet to be born. We do
180 # the same for "git pull".
181 if test 1 -ne $#
182 then
183 echo >&2 "Can merge only exactly one commit into empty head"
184 exit 1
187 rh=$(git rev-parse --verify "$1^0") ||
188 die "$1 - not something we can merge"
190 git-update-ref -m "initial pull" HEAD "$rh" "" &&
191 git-read-tree --reset -u HEAD
192 exit
194 else
195 # We are invoked directly as the first-class UI.
196 head_arg=HEAD
198 # All the rest are the commits being merged; prepare
199 # the standard merge summary message to be appended to
200 # the given message. If remote is invalid we will die
201 # later in the common codepath so we discard the error
202 # in this loop.
203 merge_name=$(for remote
205 merge_name "$remote"
206 done | git-fmt-merge-msg
208 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
210 head=$(git-rev-parse --verify "$head_arg"^0) || usage
212 # All the rest are remote heads
213 test "$#" = 0 && usage ;# we need at least one remote head.
215 remoteheads=
216 for remote
218 remotehead=$(git-rev-parse --verify "$remote"^0 2>/dev/null) ||
219 die "$remote - not something we can merge"
220 remoteheads="${remoteheads}$remotehead "
221 eval GITHEAD_$remotehead='"$remote"'
222 export GITHEAD_$remotehead
223 done
224 set x $remoteheads ; shift
226 case "$use_strategies" in
228 case "$#" in
230 var="`git-repo-config --get pull.twohead`"
231 if test -n "$var"
232 then
233 use_strategies="$var"
234 else
235 use_strategies="$default_twohead_strategies"
236 fi ;;
238 var="`git-repo-config --get pull.octopus`"
239 if test -n "$var"
240 then
241 use_strategies="$var"
242 else
243 use_strategies="$default_octopus_strategies"
244 fi ;;
245 esac
247 esac
249 for s in $use_strategies
251 case " $s " in
252 *" $no_trivial_merge_strategies "*)
253 index_merge=f
254 break
256 esac
257 done
259 case "$#" in
261 common=$(git-merge-base --all $head "$@")
264 common=$(git-show-branch --merge-base $head "$@")
266 esac
267 echo "$head" >"$GIT_DIR/ORIG_HEAD"
269 case "$index_merge,$#,$common,$no_commit" in
270 f,*)
271 # We've been told not to try anything clever. Skip to real merge.
273 ?,*,'',*)
274 # No common ancestors found. We need a real merge.
276 ?,1,"$1",*)
277 # If head can reach all the merge then we are up to date.
278 # but first the most common case of merging one remote.
279 finish_up_to_date "Already up-to-date."
280 exit 0
282 ?,1,"$head",*)
283 # Again the most common case of merging one remote.
284 echo "Updating $(git-rev-parse --short $head)..$(git-rev-parse --short $1)"
285 git-update-index --refresh 2>/dev/null
286 new_head=$(git-rev-parse --verify "$1^0") &&
287 git-read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
288 finish "$new_head" "Fast forward"
289 dropsave
290 exit 0
292 ?,1,?*"$LF"?*,*)
293 # We are not doing octopus and not fast forward. Need a
294 # real merge.
296 ?,1,*,)
297 # We are not doing octopus, not fast forward, and have only
298 # one common. See if it is really trivial.
299 git var GIT_COMMITTER_IDENT >/dev/null || exit
301 echo "Trying really trivial in-index merge..."
302 git-update-index --refresh 2>/dev/null
303 if git-read-tree --trivial -m -u -v $common $head "$1" &&
304 result_tree=$(git-write-tree)
305 then
306 echo "Wonderful."
307 result_commit=$(
308 echo "$merge_msg" |
309 git-commit-tree $result_tree -p HEAD -p "$1"
310 ) || exit
311 finish "$result_commit" "In-index merge"
312 dropsave
313 exit 0
315 echo "Nope."
318 # An octopus. If we can reach all the remote we are up to date.
319 up_to_date=t
320 for remote
322 common_one=$(git-merge-base --all $head $remote)
323 if test "$common_one" != "$remote"
324 then
325 up_to_date=f
326 break
328 done
329 if test "$up_to_date" = t
330 then
331 finish_up_to_date "Already up-to-date. Yeeah!"
332 exit 0
335 esac
337 # We are going to make a new commit.
338 git var GIT_COMMITTER_IDENT >/dev/null || exit
340 # At this point, we need a real merge. No matter what strategy
341 # we use, it would operate on the index, possibly affecting the
342 # working tree, and when resolved cleanly, have the desired tree
343 # in the index -- this means that the index must be in sync with
344 # the $head commit. The strategies are responsible to ensure this.
346 case "$use_strategies" in
347 ?*' '?*)
348 # Stash away the local changes so that we can try more than one.
349 savestate
350 single_strategy=no
353 rm -f "$GIT_DIR/MERGE_SAVE"
354 single_strategy=yes
356 esac
358 result_tree= best_cnt=-1 best_strategy= wt_strategy=
359 merge_was_ok=
360 for strategy in $use_strategies
362 test "$wt_strategy" = '' || {
363 echo "Rewinding the tree to pristine..."
364 restorestate
366 case "$single_strategy" in
368 echo "Trying merge strategy $strategy..."
370 esac
372 # Remember which strategy left the state in the working tree
373 wt_strategy=$strategy
375 git-merge-$strategy $common -- "$head_arg" "$@"
376 exit=$?
377 if test "$no_commit" = t && test "$exit" = 0
378 then
379 merge_was_ok=t
380 exit=1 ;# pretend it left conflicts.
383 test "$exit" = 0 || {
385 # The backend exits with 1 when conflicts are left to be resolved,
386 # with 2 when it does not handle the given merge at all.
388 if test "$exit" -eq 1
389 then
390 cnt=`{
391 git-diff-files --name-only
392 git-ls-files --unmerged
393 } | wc -l`
394 if test $best_cnt -le 0 -o $cnt -le $best_cnt
395 then
396 best_strategy=$strategy
397 best_cnt=$cnt
400 continue
403 # Automerge succeeded.
404 result_tree=$(git-write-tree) && break
405 done
407 # If we have a resulting tree, that means the strategy module
408 # auto resolved the merge cleanly.
409 if test '' != "$result_tree"
410 then
411 parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
412 result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
413 finish "$result_commit" "Merge made by $wt_strategy."
414 dropsave
415 exit 0
418 # Pick the result from the best strategy and have the user fix it up.
419 case "$best_strategy" in
421 restorestate
422 case "$use_strategies" in
423 ?*' '?*)
424 echo >&2 "No merge strategy handled the merge."
427 echo >&2 "Merge with strategy $use_strategies failed."
429 esac
430 exit 2
432 "$wt_strategy")
433 # We already have its result in the working tree.
436 echo "Rewinding the tree to pristine..."
437 restorestate
438 echo "Using the $best_strategy to prepare resolving by hand."
439 git-merge-$best_strategy $common -- "$head_arg" "$@"
441 esac
443 if test "$squash" = t
444 then
445 finish
446 else
447 for remote
449 echo $remote
450 done >"$GIT_DIR/MERGE_HEAD"
451 echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
454 if test "$merge_was_ok" = t
455 then
456 echo >&2 \
457 "Automatic merge went well; stopped before committing as requested"
458 exit 0
459 else
461 echo '
462 Conflicts:
464 git ls-files --unmerged |
465 sed -e 's/^[^ ]* / /' |
466 uniq
467 } >>"$GIT_DIR/MERGE_MSG"
468 if test -d "$GIT_DIR/rr-cache"
469 then
470 git-rerere
472 die "Automatic merge failed; fix conflicts and then commit the result."