Code

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