merge script: merge -X<option>
[git] / contrib / examples / git-merge.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git merge [options] <remote>...
9 git merge [options] <msg> HEAD <remote>
10 --
11 stat                 show a diffstat at the end of the merge
12 n                    don't show a diffstat at the end of the merge
13 summary              (synonym to --stat)
14 log                  add list of one-line log to merge commit message
15 squash               create a single commit instead of doing a merge
16 commit               perform a commit if the merge succeeds (default)
17 ff                   allow fast-forward (default)
18 s,strategy=          merge strategy to use
19 X=                   option for selected merge strategy
20 m,message=           message to be used for the merge commit (if any)
21 "
22
23 SUBDIRECTORY_OK=Yes
24 . git-sh-setup
25 require_work_tree
26 cd_to_toplevel
27
28 test -z "$(git ls-files -u)" ||
29         die "Merge is not possible because you have unmerged files."
30
31 ! test -e "$GIT_DIR/MERGE_HEAD" ||
32         die 'You have not concluded your merge (MERGE_HEAD exists).'
33
34 LF='
35 '
36
37 all_strategies='recur recursive octopus resolve stupid ours subtree'
38 all_strategies="$all_strategies recursive-ours recursive-theirs"
39 default_twohead_strategies='recursive'
40 default_octopus_strategies='octopus'
41 no_fast_forward_strategies='subtree ours'
42 no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
43 use_strategies=
44 xopt=
45
46 allow_fast_forward=t
47 allow_trivial_merge=t
48 squash= no_commit= log_arg=
49
50 dropsave() {
51         rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
52                  "$GIT_DIR/MERGE_STASH" || exit 1
53 }
54
55 savestate() {
56         # Stash away any local modifications.
57         git stash create >"$GIT_DIR/MERGE_STASH"
58 }
59
60 restorestate() {
61         if test -f "$GIT_DIR/MERGE_STASH"
62         then
63                 git reset --hard $head >/dev/null
64                 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
65                 git update-index --refresh >/dev/null
66         fi
67 }
68
69 finish_up_to_date () {
70         case "$squash" in
71         t)
72                 echo "$1 (nothing to squash)" ;;
73         '')
74                 echo "$1" ;;
75         esac
76         dropsave
77 }
78
79 squash_message () {
80         echo Squashed commit of the following:
81         echo
82         git log --no-merges --pretty=medium ^"$head" $remoteheads
83 }
84
85 finish () {
86         if test '' = "$2"
87         then
88                 rlogm="$GIT_REFLOG_ACTION"
89         else
90                 echo "$2"
91                 rlogm="$GIT_REFLOG_ACTION: $2"
92         fi
93         case "$squash" in
94         t)
95                 echo "Squash commit -- not updating HEAD"
96                 squash_message >"$GIT_DIR/SQUASH_MSG"
97                 ;;
98         '')
99                 case "$merge_msg" in
100                 '')
101                         echo "No merge message -- not updating HEAD"
102                         ;;
103                 *)
104                         git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
105                         git gc --auto
106                         ;;
107                 esac
108                 ;;
109         esac
110         case "$1" in
111         '')
112                 ;;
113         ?*)
114                 if test "$show_diffstat" = t
115                 then
116                         # We want color (if set), but no pager
117                         GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
118                 fi
119                 ;;
120         esac
121
122         # Run a post-merge hook
123         if test -x "$GIT_DIR"/hooks/post-merge
124         then
125             case "$squash" in
126             t)
127                 "$GIT_DIR"/hooks/post-merge 1
128                 ;;
129             '')
130                 "$GIT_DIR"/hooks/post-merge 0
131                 ;;
132             esac
133         fi
134 }
135
136 merge_name () {
137         remote="$1"
138         rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
139         if truname=$(expr "$remote" : '\(.*\)~[0-9]*$') &&
140                 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
141         then
142                 echo "$rh               branch '$truname' (early part) of ."
143                 return
144         fi
145         if found_ref=$(git rev-parse --symbolic-full-name --verify \
146                                                         "$remote" 2>/dev/null)
147         then
148                 if test "${found_ref#refs/heads/}" != "$found_ref"
149                 then
150                         echo "$rh               branch '$remote' of ."
151                         return
152                 elif test "${found_ref#refs/remotes/}" != "$found_ref"
153                 then
154                         echo "$rh               remote branch '$remote' of ."
155                         return
156                 fi
157         fi
158         if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
159         then
160                 sed -e 's/      not-for-merge   /               /' -e 1q \
161                         "$GIT_DIR/FETCH_HEAD"
162                 return
163         fi
164         echo "$rh               commit '$remote'"
165 }
166
167 parse_config () {
168         while test $# != 0; do
169                 case "$1" in
170                 -n|--no-stat|--no-summary)
171                         show_diffstat=false ;;
172                 --stat|--summary)
173                         show_diffstat=t ;;
174                 --log|--no-log)
175                         log_arg=$1 ;;
176                 --squash)
177                         test "$allow_fast_forward" = t ||
178                                 die "You cannot combine --squash with --no-ff."
179                         squash=t no_commit=t ;;
180                 --no-squash)
181                         squash= no_commit= ;;
182                 --commit)
183                         no_commit= ;;
184                 --no-commit)
185                         no_commit=t ;;
186                 --ff)
187                         allow_fast_forward=t ;;
188                 --no-ff)
189                         test "$squash" != t ||
190                                 die "You cannot combine --squash with --no-ff."
191                         allow_fast_forward=f ;;
192                 -s|--strategy)
193                         shift
194                         case " $all_strategies " in
195                         *" $1 "*)
196                                 use_strategies="$use_strategies$1 " ;;
197                         *)
198                                 die "available strategies are: $all_strategies" ;;
199                         esac
200                         ;;
201                 -X)
202                         shift
203                         xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
204                         ;;
205                 -m|--message)
206                         shift
207                         merge_msg="$1"
208                         have_message=t
209                         ;;
210                 --)
211                         shift
212                         break ;;
213                 *)      usage ;;
214                 esac
215                 shift
216         done
217         args_left=$#
218 }
219
220 test $# != 0 || usage
221
222 have_message=
223
224 if branch=$(git-symbolic-ref -q HEAD)
225 then
226         mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
227         if test -n "$mergeopts"
228         then
229                 parse_config $mergeopts --
230         fi
231 fi
232
233 parse_config "$@"
234 while test $args_left -lt $#; do shift; done
235
236 if test -z "$show_diffstat"; then
237     test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
238     test "$(git config --bool merge.stat)" = false && show_diffstat=false
239     test -z "$show_diffstat" && show_diffstat=t
240 fi
241
242 # This could be traditional "merge <msg> HEAD <commit>..."  and the
243 # way we can tell it is to see if the second token is HEAD, but some
244 # people might have misused the interface and used a committish that
245 # is the same as HEAD there instead.  Traditional format never would
246 # have "-m" so it is an additional safety measure to check for it.
247
248 if test -z "$have_message" &&
249         second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
250         head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
251         test "$second_token" = "$head_commit"
252 then
253         merge_msg="$1"
254         shift
255         head_arg="$1"
256         shift
257 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
258 then
259         # If the merged head is a valid one there is no reason to
260         # forbid "git merge" into a branch yet to be born.  We do
261         # the same for "git pull".
262         if test 1 -ne $#
263         then
264                 echo >&2 "Can merge only exactly one commit into empty head"
265                 exit 1
266         fi
267
268         test "$squash" != t ||
269                 die "Squash commit into empty head not supported yet"
270         test "$allow_fast_forward" = t ||
271                 die "Non-fast-forward into an empty head does not make sense"
272         rh=$(git rev-parse --verify "$1^0") ||
273                 die "$1 - not something we can merge"
274
275         git update-ref -m "initial pull" HEAD "$rh" "" &&
276         git read-tree --reset -u HEAD
277         exit
278
279 else
280         # We are invoked directly as the first-class UI.
281         head_arg=HEAD
282
283         # All the rest are the commits being merged; prepare
284         # the standard merge summary message to be appended to
285         # the given message.  If remote is invalid we will die
286         # later in the common codepath so we discard the error
287         # in this loop.
288         merge_name=$(for remote
289                 do
290                         merge_name "$remote"
291                 done | git fmt-merge-msg $log_arg
292         )
293         merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
294 fi
295 head=$(git rev-parse --verify "$head_arg"^0) || usage
296
297 # All the rest are remote heads
298 test "$#" = 0 && usage ;# we need at least one remote head.
299 set_reflog_action "merge $*"
300
301 remoteheads=
302 for remote
303 do
304         remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
305             die "$remote - not something we can merge"
306         remoteheads="${remoteheads}$remotehead "
307         eval GITHEAD_$remotehead='"$remote"'
308         export GITHEAD_$remotehead
309 done
310 set x $remoteheads ; shift
311
312 case "$use_strategies" in
313 '')
314         case "$#" in
315         1)
316                 var="`git config --get pull.twohead`"
317                 if test -n "$var"
318                 then
319                         use_strategies="$var"
320                 else
321                         use_strategies="$default_twohead_strategies"
322                 fi ;;
323         *)
324                 var="`git config --get pull.octopus`"
325                 if test -n "$var"
326                 then
327                         use_strategies="$var"
328                 else
329                         use_strategies="$default_octopus_strategies"
330                 fi ;;
331         esac
332         ;;
333 esac
334
335 for s in $use_strategies
336 do
337         for ss in $no_fast_forward_strategies
338         do
339                 case " $s " in
340                 *" $ss "*)
341                         allow_fast_forward=f
342                         break
343                         ;;
344                 esac
345         done
346         for ss in $no_trivial_strategies
347         do
348                 case " $s " in
349                 *" $ss "*)
350                         allow_trivial_merge=f
351                         break
352                         ;;
353                 esac
354         done
355 done
356
357 case "$#" in
358 1)
359         common=$(git merge-base --all $head "$@")
360         ;;
361 *)
362         common=$(git show-branch --merge-base $head "$@")
363         ;;
364 esac
365 echo "$head" >"$GIT_DIR/ORIG_HEAD"
366
367 case "$allow_fast_forward,$#,$common,$no_commit" in
368 ?,*,'',*)
369         # No common ancestors found. We need a real merge.
370         ;;
371 ?,1,"$1",*)
372         # If head can reach all the merge then we are up to date.
373         # but first the most common case of merging one remote.
374         finish_up_to_date "Already up-to-date."
375         exit 0
376         ;;
377 t,1,"$head",*)
378         # Again the most common case of merging one remote.
379         echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
380         git update-index --refresh 2>/dev/null
381         msg="Fast-forward"
382         if test -n "$have_message"
383         then
384                 msg="$msg (no commit created; -m option ignored)"
385         fi
386         new_head=$(git rev-parse --verify "$1^0") &&
387         git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
388         finish "$new_head" "$msg" || exit
389         dropsave
390         exit 0
391         ;;
392 ?,1,?*"$LF"?*,*)
393         # We are not doing octopus and not fast-forward.  Need a
394         # real merge.
395         ;;
396 ?,1,*,)
397         # We are not doing octopus, not fast-forward, and have only
398         # one common.
399         git update-index --refresh 2>/dev/null
400         case "$allow_trivial_merge" in
401         t)
402                 # See if it is really trivial.
403                 git var GIT_COMMITTER_IDENT >/dev/null || exit
404                 echo "Trying really trivial in-index merge..."
405                 if git read-tree --trivial -m -u -v $common $head "$1" &&
406                    result_tree=$(git write-tree)
407                 then
408                         echo "Wonderful."
409                         result_commit=$(
410                                 printf '%s\n' "$merge_msg" |
411                                 git commit-tree $result_tree -p HEAD -p "$1"
412                         ) || exit
413                         finish "$result_commit" "In-index merge"
414                         dropsave
415                         exit 0
416                 fi
417                 echo "Nope."
418         esac
419         ;;
420 *)
421         # An octopus.  If we can reach all the remote we are up to date.
422         up_to_date=t
423         for remote
424         do
425                 common_one=$(git merge-base --all $head $remote)
426                 if test "$common_one" != "$remote"
427                 then
428                         up_to_date=f
429                         break
430                 fi
431         done
432         if test "$up_to_date" = t
433         then
434                 finish_up_to_date "Already up-to-date. Yeeah!"
435                 exit 0
436         fi
437         ;;
438 esac
439
440 # We are going to make a new commit.
441 git var GIT_COMMITTER_IDENT >/dev/null || exit
442
443 # At this point, we need a real merge.  No matter what strategy
444 # we use, it would operate on the index, possibly affecting the
445 # working tree, and when resolved cleanly, have the desired tree
446 # in the index -- this means that the index must be in sync with
447 # the $head commit.  The strategies are responsible to ensure this.
448
449 case "$use_strategies" in
450 ?*' '?*)
451     # Stash away the local changes so that we can try more than one.
452     savestate
453     single_strategy=no
454     ;;
455 *)
456     rm -f "$GIT_DIR/MERGE_STASH"
457     single_strategy=yes
458     ;;
459 esac
460
461 result_tree= best_cnt=-1 best_strategy= wt_strategy=
462 merge_was_ok=
463 for strategy in $use_strategies
464 do
465     test "$wt_strategy" = '' || {
466         echo "Rewinding the tree to pristine..."
467         restorestate
468     }
469     case "$single_strategy" in
470     no)
471         echo "Trying merge strategy $strategy..."
472         ;;
473     esac
474
475     # Remember which strategy left the state in the working tree
476     wt_strategy=$strategy
477
478     eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
479     exit=$?
480     if test "$no_commit" = t && test "$exit" = 0
481     then
482         merge_was_ok=t
483         exit=1 ;# pretend it left conflicts.
484     fi
485
486     test "$exit" = 0 || {
487
488         # The backend exits with 1 when conflicts are left to be resolved,
489         # with 2 when it does not handle the given merge at all.
490
491         if test "$exit" -eq 1
492         then
493             cnt=`{
494                 git diff-files --name-only
495                 git ls-files --unmerged
496             } | wc -l`
497             if test $best_cnt -le 0 -o $cnt -le $best_cnt
498             then
499                 best_strategy=$strategy
500                 best_cnt=$cnt
501             fi
502         fi
503         continue
504     }
505
506     # Automerge succeeded.
507     result_tree=$(git write-tree) && break
508 done
509
510 # If we have a resulting tree, that means the strategy module
511 # auto resolved the merge cleanly.
512 if test '' != "$result_tree"
513 then
514     if test "$allow_fast_forward" = "t"
515     then
516         parents=$(git show-branch --independent "$head" "$@")
517     else
518         parents=$(git rev-parse "$head" "$@")
519     fi
520     parents=$(echo "$parents" | sed -e 's/^/-p /')
521     result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
522     finish "$result_commit" "Merge made by $wt_strategy."
523     dropsave
524     exit 0
525 fi
526
527 # Pick the result from the best strategy and have the user fix it up.
528 case "$best_strategy" in
529 '')
530         restorestate
531         case "$use_strategies" in
532         ?*' '?*)
533                 echo >&2 "No merge strategy handled the merge."
534                 ;;
535         *)
536                 echo >&2 "Merge with strategy $use_strategies failed."
537                 ;;
538         esac
539         exit 2
540         ;;
541 "$wt_strategy")
542         # We already have its result in the working tree.
543         ;;
544 *)
545         echo "Rewinding the tree to pristine..."
546         restorestate
547         echo "Using the $best_strategy to prepare resolving by hand."
548         git-merge-$best_strategy $common -- "$head_arg" "$@"
549         ;;
550 esac
551
552 if test "$squash" = t
553 then
554         finish
555 else
556         for remote
557         do
558                 echo $remote
559         done >"$GIT_DIR/MERGE_HEAD"
560         printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
561 fi
562
563 if test "$merge_was_ok" = t
564 then
565         echo >&2 \
566         "Automatic merge went well; stopped before committing as requested"
567         exit 0
568 else
569         {
570             echo '
571 Conflicts:
572 '
573                 git ls-files --unmerged |
574                 sed -e 's/^[^   ]*      /       /' |
575                 uniq
576         } >>"$GIT_DIR/MERGE_MSG"
577         git rerere
578         die "Automatic merge failed; fix conflicts and then commit the result."
579 fi