Document new --date=<format>
[git/mingw/4msysgit/wingit-dll.git] / git-merge.sh
blob5ccf28251d51d3ee10675d48cf04829f5d6d8c06
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n] [--summary] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commit>+'
8 SUBDIRECTORY_OK=Yes
9 . git-sh-setup
10 require_work_tree
11 cd_to_toplevel
13 test -z "$(git ls-files -u)" ||
14 die "You are in the middle of a conflicted merge."
16 LF='
19 all_strategies='recur recursive octopus resolve stupid ours subtree'
20 default_twohead_strategies='recursive'
21 default_octopus_strategies='octopus'
22 no_trivial_merge_strategies='ours subtree'
23 use_strategies=
25 index_merge=t
27 dropsave() {
28 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
29 "$GIT_DIR/MERGE_SAVE" || exit 1
32 savestate() {
33 # Stash away any local modifications.
34 git diff-index -z --name-only $head |
35 cpio -0 -o >"$GIT_DIR/MERGE_SAVE"
38 restorestate() {
39 if test -f "$GIT_DIR/MERGE_SAVE"
40 then
41 git reset --hard $head >/dev/null
42 cpio -iuv <"$GIT_DIR/MERGE_SAVE"
43 git update-index --refresh >/dev/null
47 finish_up_to_date () {
48 case "$squash" in
50 echo "$1 (nothing to squash)" ;;
51 '')
52 echo "$1" ;;
53 esac
54 dropsave
57 squash_message () {
58 echo Squashed commit of the following:
59 echo
60 git log --no-merges ^"$head" $remote
63 finish () {
64 if test '' = "$2"
65 then
66 rlogm="$GIT_REFLOG_ACTION"
67 else
68 echo "$2"
69 rlogm="$GIT_REFLOG_ACTION: $2"
71 case "$squash" in
73 echo "Squash commit -- not updating HEAD"
74 squash_message >"$GIT_DIR/SQUASH_MSG"
76 '')
77 case "$merge_msg" in
78 '')
79 echo "No merge message -- not updating HEAD"
82 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
84 esac
86 esac
87 case "$1" in
88 '')
90 ?*)
91 if test "$show_diffstat" = t
92 then
93 # We want color (if set), but no pager
94 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
97 esac
100 merge_name () {
101 remote="$1"
102 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
103 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
104 if test "$rh" = "$bh"
105 then
106 echo "$rh branch '$remote' of ."
107 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
108 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
109 then
110 echo "$rh branch '$truname' (early part) of ."
111 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
112 then
113 sed -e 's/ not-for-merge / /' -e 1q \
114 "$GIT_DIR/FETCH_HEAD"
115 else
116 echo "$rh commit '$remote'"
120 case "$#" in 0) usage ;; esac
122 have_message=
123 while case "$#" in 0) break ;; esac
125 case "$1" in
126 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
127 --no-summa|--no-summar|--no-summary)
128 show_diffstat=false ;;
129 --summary)
130 show_diffstat=t ;;
131 --sq|--squ|--squa|--squas|--squash)
132 squash=t no_commit=t ;;
133 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
134 no_commit=t ;;
135 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
136 --strateg=*|--strategy=*|\
137 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
138 case "$#,$1" in
139 *,*=*)
140 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
141 1,*)
142 usage ;;
144 strategy="$2"
145 shift ;;
146 esac
147 case " $all_strategies " in
148 *" $strategy "*)
149 use_strategies="$use_strategies$strategy " ;;
151 die "available strategies are: $all_strategies" ;;
152 esac
154 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
155 merge_msg=`expr "z$1" : 'z-[^=]*=\(.*\)'`
156 have_message=t
158 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
159 shift
160 case "$#" in
161 1) usage ;;
162 esac
163 merge_msg="$1"
164 have_message=t
166 -*) usage ;;
167 *) break ;;
168 esac
169 shift
170 done
172 if test -z "$show_diffstat"; then
173 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
174 test -z "$show_diffstat" && show_diffstat=t
177 # This could be traditional "merge <msg> HEAD <commit>..." and the
178 # way we can tell it is to see if the second token is HEAD, but some
179 # people might have misused the interface and used a committish that
180 # is the same as HEAD there instead. Traditional format never would
181 # have "-m" so it is an additional safety measure to check for it.
183 if test -z "$have_message" &&
184 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
185 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
186 test "$second_token" = "$head_commit"
187 then
188 merge_msg="$1"
189 shift
190 head_arg="$1"
191 shift
192 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
193 then
194 # If the merged head is a valid one there is no reason to
195 # forbid "git merge" into a branch yet to be born. We do
196 # the same for "git pull".
197 if test 1 -ne $#
198 then
199 echo >&2 "Can merge only exactly one commit into empty head"
200 exit 1
203 rh=$(git rev-parse --verify "$1^0") ||
204 die "$1 - not something we can merge"
206 git update-ref -m "initial pull" HEAD "$rh" "" &&
207 git read-tree --reset -u HEAD
208 exit
210 else
211 # We are invoked directly as the first-class UI.
212 head_arg=HEAD
214 # All the rest are the commits being merged; prepare
215 # the standard merge summary message to be appended to
216 # the given message. If remote is invalid we will die
217 # later in the common codepath so we discard the error
218 # in this loop.
219 merge_name=$(for remote
221 merge_name "$remote"
222 done | git fmt-merge-msg
224 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
226 head=$(git rev-parse --verify "$head_arg"^0) || usage
228 # All the rest are remote heads
229 test "$#" = 0 && usage ;# we need at least one remote head.
230 set_reflog_action "merge $*"
232 remoteheads=
233 for remote
235 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
236 die "$remote - not something we can merge"
237 remoteheads="${remoteheads}$remotehead "
238 eval GITHEAD_$remotehead='"$remote"'
239 export GITHEAD_$remotehead
240 done
241 set x $remoteheads ; shift
243 case "$use_strategies" in
245 case "$#" in
247 var="`git config --get pull.twohead`"
248 if test -n "$var"
249 then
250 use_strategies="$var"
251 else
252 use_strategies="$default_twohead_strategies"
253 fi ;;
255 var="`git config --get pull.octopus`"
256 if test -n "$var"
257 then
258 use_strategies="$var"
259 else
260 use_strategies="$default_octopus_strategies"
261 fi ;;
262 esac
264 esac
266 for s in $use_strategies
268 for nt in $no_trivial_merge_strategies
270 case " $s " in
271 *" $nt "*)
272 index_merge=f
273 break
275 esac
276 done
277 done
279 case "$#" in
281 common=$(git merge-base --all $head "$@")
284 common=$(git show-branch --merge-base $head "$@")
286 esac
287 echo "$head" >"$GIT_DIR/ORIG_HEAD"
289 case "$index_merge,$#,$common,$no_commit" in
290 f,*)
291 # We've been told not to try anything clever. Skip to real merge.
293 ?,*,'',*)
294 # No common ancestors found. We need a real merge.
296 ?,1,"$1",*)
297 # If head can reach all the merge then we are up to date.
298 # but first the most common case of merging one remote.
299 finish_up_to_date "Already up-to-date."
300 exit 0
302 ?,1,"$head",*)
303 # Again the most common case of merging one remote.
304 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
305 git update-index --refresh 2>/dev/null
306 msg="Fast forward"
307 if test -n "$have_message"
308 then
309 msg="$msg (no commit created; -m option ignored)"
311 new_head=$(git rev-parse --verify "$1^0") &&
312 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
313 finish "$new_head" "$msg" || exit
314 dropsave
315 exit 0
317 ?,1,?*"$LF"?*,*)
318 # We are not doing octopus and not fast forward. Need a
319 # real merge.
321 ?,1,*,)
322 # We are not doing octopus, not fast forward, and have only
323 # one common.
324 git update-index --refresh 2>/dev/null
325 case " $use_strategies " in
326 *' recursive '*|*' recur '*)
327 : run merge later
330 # See if it is really trivial.
331 git var GIT_COMMITTER_IDENT >/dev/null || exit
332 echo "Trying really trivial in-index merge..."
333 if git read-tree --trivial -m -u -v $common $head "$1" &&
334 result_tree=$(git write-tree)
335 then
336 echo "Wonderful."
337 result_commit=$(
338 printf '%s\n' "$merge_msg" |
339 git commit-tree $result_tree -p HEAD -p "$1"
340 ) || exit
341 finish "$result_commit" "In-index merge"
342 dropsave
343 exit 0
345 echo "Nope."
346 esac
349 # An octopus. If we can reach all the remote we are up to date.
350 up_to_date=t
351 for remote
353 common_one=$(git merge-base --all $head $remote)
354 if test "$common_one" != "$remote"
355 then
356 up_to_date=f
357 break
359 done
360 if test "$up_to_date" = t
361 then
362 finish_up_to_date "Already up-to-date. Yeeah!"
363 exit 0
366 esac
368 # We are going to make a new commit.
369 git var GIT_COMMITTER_IDENT >/dev/null || exit
371 # At this point, we need a real merge. No matter what strategy
372 # we use, it would operate on the index, possibly affecting the
373 # working tree, and when resolved cleanly, have the desired tree
374 # in the index -- this means that the index must be in sync with
375 # the $head commit. The strategies are responsible to ensure this.
377 case "$use_strategies" in
378 ?*' '?*)
379 # Stash away the local changes so that we can try more than one.
380 savestate
381 single_strategy=no
384 rm -f "$GIT_DIR/MERGE_SAVE"
385 single_strategy=yes
387 esac
389 result_tree= best_cnt=-1 best_strategy= wt_strategy=
390 merge_was_ok=
391 for strategy in $use_strategies
393 test "$wt_strategy" = '' || {
394 echo "Rewinding the tree to pristine..."
395 restorestate
397 case "$single_strategy" in
399 echo "Trying merge strategy $strategy..."
401 esac
403 # Remember which strategy left the state in the working tree
404 wt_strategy=$strategy
406 git-merge-$strategy $common -- "$head_arg" "$@"
407 exit=$?
408 if test "$no_commit" = t && test "$exit" = 0
409 then
410 merge_was_ok=t
411 exit=1 ;# pretend it left conflicts.
414 test "$exit" = 0 || {
416 # The backend exits with 1 when conflicts are left to be resolved,
417 # with 2 when it does not handle the given merge at all.
419 if test "$exit" -eq 1
420 then
421 cnt=`{
422 git diff-files --name-only
423 git ls-files --unmerged
424 } | wc -l`
425 if test $best_cnt -le 0 -o $cnt -le $best_cnt
426 then
427 best_strategy=$strategy
428 best_cnt=$cnt
431 continue
434 # Automerge succeeded.
435 result_tree=$(git write-tree) && break
436 done
438 # If we have a resulting tree, that means the strategy module
439 # auto resolved the merge cleanly.
440 if test '' != "$result_tree"
441 then
442 parents=$(git show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
443 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
444 finish "$result_commit" "Merge made by $wt_strategy."
445 dropsave
446 exit 0
449 # Pick the result from the best strategy and have the user fix it up.
450 case "$best_strategy" in
452 restorestate
453 case "$use_strategies" in
454 ?*' '?*)
455 echo >&2 "No merge strategy handled the merge."
458 echo >&2 "Merge with strategy $use_strategies failed."
460 esac
461 exit 2
463 "$wt_strategy")
464 # We already have its result in the working tree.
467 echo "Rewinding the tree to pristine..."
468 restorestate
469 echo "Using the $best_strategy to prepare resolving by hand."
470 git-merge-$best_strategy $common -- "$head_arg" "$@"
472 esac
474 if test "$squash" = t
475 then
476 finish
477 else
478 for remote
480 echo $remote
481 done >"$GIT_DIR/MERGE_HEAD"
482 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
485 if test "$merge_was_ok" = t
486 then
487 echo >&2 \
488 "Automatic merge went well; stopped before committing as requested"
489 exit 0
490 else
492 echo '
493 Conflicts:
495 git ls-files --unmerged |
496 sed -e 's/^[^ ]* / /' |
497 uniq
498 } >>"$GIT_DIR/MERGE_MSG"
499 git rerere
500 die "Automatic merge failed; fix conflicts and then commit the result."