clone: the given repository dir should be relative to $PWD
[gitweb.git] / git-merge.sh
index 4609fe54d52843f42d2c1d18a9dc2ff235b5c7ce..5b34b4de99c33a99dfb841795baced8889f74a88 100755 (executable)
@@ -3,19 +3,21 @@
 # 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'
+all_strategies='recur recursive recursive-old octopus resolve stupid ours'
+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'
+       all_strategies='recur recursive resolve octopus stupid ours'
 fi
 
 dropsave() {
@@ -38,31 +40,68 @@ restorestate() {
        fi
 }
 
-finish () {
-       test '' = "$2" || echo "$2"
-       case "$merge_msg" in
+finish_up_to_date () {
+       case "$squash" in
+       t)
+               echo "$1 (nothing to squash)" ;;
        '')
-               echo "No merge message -- not updating HEAD"
+               echo "$1" ;;
+       esac
+       dropsave
+}
+
+squash_message () {
+       echo Squashed commit of the following:
+       echo
+       git-log --no-merges ^"$head" $remote
+}
+
+finish () {
+       if test '' = "$2"
+       then
+               rlogm="$rloga"
+       else
+               echo "$2"
+               rlogm="$rloga: $2"
+       fi
+       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 -m "$rlogm" 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
 }
 
+rloga=
 while case "$#" in 0) break ;; esac
 do
        case "$1" in
        -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 +109,7 @@ do
        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
                case "$#,$1" in
                *,*=*)
-                       strategy=`expr "$1" : '-[^=]*=\(.*\)'` ;;
+                       strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
                1,*)
                        usage ;;
                *)
@@ -84,14 +123,15 @@ do
                        die "available strategies are: $all_strategies" ;;
                esac
                ;;
+       --reflog-action=*)
+               rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
+               ;;
        -*)     usage ;;
        *)      break ;;
        esac
        shift
 done
 
-test "$#" -le 2 && usage ;# we need at least two heads.
-
 merge_msg="$1"
 shift
 head_arg="$1"
@@ -99,6 +139,9 @@ 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.
+test "$rloga" = '' && rloga="merge: $@"
+
 remoteheads=
 for remote
 do
@@ -108,6 +151,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,39 +182,41 @@ 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."
+       echo "Updating from $head to $1"
        git-update-index --refresh 2>/dev/null
        new_head=$(git-rev-parse --verify "$1^0") &&
-       git-read-tree -u -m $head "$new_head" &&
+       git-read-tree -u -v -m $head "$new_head" &&
        finish "$new_head" "Fast forward"
        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
 
        echo "Trying really trivial in-index merge..."
        git-update-index --refresh 2>/dev/null
-       if git-read-tree --trivial -m -u $common $head "$1" &&
+       if git-read-tree --trivial -m -u -v $common $head "$1" &&
           result_tree=$(git-write-tree)
        then
            echo "Wonderful."
@@ -178,8 +244,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 +253,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,13 +324,9 @@ 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."
+    finish "$result_commit" "Merge made by $wt_strategy."
     dropsave
     exit 0
 fi
@@ -298,11 +348,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 +378,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