Code

upload-pack.c: <sys/poll.h> includes <ctype.h> on OpenBSD 3.8
[git.git] / git-merge.sh
index cc0952a97db7d998a3f26e98b93cf2154887abd5..24e3b507ef1f907e7146a2653189f45a92db08fd 100755 (executable)
@@ -3,19 +3,22 @@
 # Copyright (c) 2005 Junio C Hamano
 #
 
-
-USAGE='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+'
+USAGE='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
 . git-sh-setup
 
 LF='
 '
 
 all_strategies='recursive octopus resolve stupid ours'
-default_strategies='recursive'
+default_twohead_strategies='recursive'
+default_octopus_strategies='octopus'
+no_trivial_merge_strategies='ours'
 use_strategies=
+
+index_merge=t
 if test "@@NO_PYTHON@@"; then
        all_strategies='resolve octopus stupid ours'
-       default_strategies='resolve'
+       default_twohead_strategies='resolve'
 fi
 
 dropsave() {
@@ -38,21 +41,49 @@ restorestate() {
        fi
 }
 
+finish_up_to_date () {
+       case "$squash" in
+       t)
+               echo "$1 (nothing to squash)" ;;
+       '')
+               echo "$1" ;;
+       esac
+       dropsave
+}
+
+squash_message () {
+       echo Squashed commit of the following:
+       echo
+       git-log --no-merges ^"$head" $remote
+}
+
 finish () {
        test '' = "$2" || echo "$2"
-       case "$merge_msg" in
-       '')
-               echo "No merge message -- not updating HEAD"
+       case "$squash" in
+       t)
+               echo "Squash commit -- not updating HEAD"
+               squash_message >"$GIT_DIR/SQUASH_MSG"
                ;;
-       *)
-               git-update-ref HEAD "$1" "$head" || exit 1
+       '')
+               case "$merge_msg" in
+               '')
+                       echo "No merge message -- not updating HEAD"
+                       ;;
+               *)
+                       git-update-ref HEAD "$1" "$head" || exit 1
+                       ;;
+               esac
                ;;
        esac
-
-       case "$no_summary" in
+       case "$1" in
        '')
-               git-diff-tree -p -M "$head" "$1" |
-               git-apply --stat --summary
+               ;;
+       ?*)
+               case "$no_summary" in
+               '')
+                       git-diff-tree --stat --summary -M "$head" "$1"
+                       ;;
+               esac
                ;;
        esac
 }
@@ -63,6 +94,8 @@ do
        -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
                --no-summa|--no-summar|--no-summary)
                no_summary=t ;;
+       --sq|--squ|--squa|--squas|--squash)
+               squash=t no_commit=t ;;
        --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
                no_commit=t ;;
        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
@@ -70,7 +103,7 @@ do
        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
                case "$#,$1" in
                *,*=*)
-                       strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
+                       strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
                1,*)
                        usage ;;
                *)
@@ -90,8 +123,6 @@ do
        shift
 done
 
-test "$#" -le 2 && usage ;# we need at least two heads.
-
 merge_msg="$1"
 shift
 head_arg="$1"
@@ -99,6 +130,8 @@ head=$(git-rev-parse --verify "$1"^0) || usage
 shift
 
 # All the rest are remote heads
+test "$#" = 0 && usage ;# we need at least one remote head.
+
 remoteheads=
 for remote
 do
@@ -108,6 +141,27 @@ do
 done
 set x $remoteheads ; shift
 
+case "$use_strategies" in
+'')
+       case "$#" in
+       1)
+               use_strategies="$default_twohead_strategies" ;;
+       *)
+               use_strategies="$default_octopus_strategies" ;;
+       esac
+       ;;
+esac
+
+for s in $use_strategies
+do
+       case " $s " in
+       *" $no_trivial_merge_strategies "*)
+               index_merge=f
+               break
+               ;;
+       esac
+done
+
 case "$#" in
 1)
        common=$(git-merge-base --all $head "$@")
@@ -118,18 +172,20 @@ case "$#" in
 esac
 echo "$head" >"$GIT_DIR/ORIG_HEAD"
 
-case "$#,$common,$no_commit" in
-*,'',*)
+case "$index_merge,$#,$common,$no_commit" in
+f,*)
+       # We've been told not to try anything clever.  Skip to real merge.
+       ;;
+?,*,'',*)
        # No common ancestors found. We need a real merge.
        ;;
-1,"$1",*)
+?,1,"$1",*)
        # If head can reach all the merge then we are up to date.
-       # but first the most common case of merging one remote
-       echo "Already up-to-date."
-       dropsave
+       # but first the most common case of merging one remote.
+       finish_up_to_date "Already up-to-date."
        exit 0
        ;;
-1,"$head",*)
+?,1,"$head",*)
        # Again the most common case of merging one remote.
        echo "Updating from $head to $1"
        git-update-index --refresh 2>/dev/null
@@ -139,11 +195,11 @@ case "$#,$common,$no_commit" in
        dropsave
        exit 0
        ;;
-1,?*"$LF"?*,*)
+?,1,?*"$LF"?*,*)
        # We are not doing octopus and not fast forward.  Need a
        # real merge.
        ;;
-1,*,)
+?,1,*,)
        # We are not doing octopus, not fast forward, and have only
        # one common.  See if it is really trivial.
        git var GIT_COMMITTER_IDENT >/dev/null || exit
@@ -178,8 +234,7 @@ case "$#,$common,$no_commit" in
        done
        if test "$up_to_date" = t
        then
-               echo "Already up-to-date. Yeeah!"
-               dropsave
+               finish_up_to_date "Already up-to-date. Yeeah!"
                exit 0
        fi
        ;;
@@ -188,17 +243,6 @@ esac
 # We are going to make a new commit.
 git var GIT_COMMITTER_IDENT >/dev/null || exit
 
-case "$use_strategies" in
-'')
-       case "$#" in
-       1)
-               use_strategies="$default_strategies" ;;
-       *)
-               use_strategies=octopus ;;
-       esac            
-       ;;
-esac
-
 # At this point, we need a real merge.  No matter what strategy
 # we use, it would operate on the index, possibly affecting the
 # working tree, and when resolved cleanly, have the desired tree
@@ -270,11 +314,7 @@ done
 # auto resolved the merge cleanly.
 if test '' != "$result_tree"
 then
-    parents="-p $head"
-    for remote
-    do
-        parents="$parents -p $remote"
-    done
+    parents=$(git-show-branch --independent "$head" "$@" | sed -e 's/^/-p /')
     result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree $parents) || exit
     finish "$result_commit" "Merge $result_commit, made by $wt_strategy."
     dropsave
@@ -298,11 +338,17 @@ case "$best_strategy" in
        git-merge-$best_strategy $common -- "$head_arg" "$@"
        ;;
 esac
-for remote
-do
-       echo $remote
-done >"$GIT_DIR/MERGE_HEAD"
-echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
+
+if test "$squash" = t
+then
+       finish
+else
+       for remote
+       do
+               echo $remote
+       done >"$GIT_DIR/MERGE_HEAD"
+       echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
+fi
 
 if test "$merge_was_ok" = t
 then
@@ -322,5 +368,5 @@ Conflicts:
        then
                git-rerere
        fi
-       die "Automatic merge failed; fix up by hand"
+       die "Automatic merge failed; fix conflicts and then commit the result."
 fi