Fix a minor problem in identifying squashes vs. normal splits.
[gitweb.git] / git-subtree.sh
index c5c0201448eb455ef29a25f5a2950d85ea74363f..cccc3400fdd8e3a06ccf8ac8f17589ca08afbffb 100755 (executable)
@@ -27,7 +27,8 @@ rejoin        merge the new branch back into HEAD
 squash        merge subtree changes as a single commit
 "
 eval $(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)
-. $(git --exec-path)/git-sh-setup
+PATH=$(git --exec-path):$PATH
+. git-sh-setup
 require_work_tree
 
 quiet=
@@ -101,7 +102,7 @@ esac
 if [ -z "$prefix" ]; then
        die "You must provide the --prefix option."
 fi
-dir="$prefix"
+dir="$(dirname "$prefix/.")"
 
 if [ "$command" != "pull" ]; then
        revs=$(git rev-parse $default --revs-only "$@") || exit $?
@@ -174,7 +175,7 @@ find_latest_squash()
        sq=
        main=
        sub=
-       git log --grep="^git-subtree-dir: $dir\$" \
+       git log --grep="^git-subtree-dir: $dir/*\$" \
                --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
        while read a b junk; do
                debug "$a $b $junk"
@@ -209,16 +210,18 @@ find_existing_splits()
        revs="$2"
        main=
        sub=
-       git log --grep="^git-subtree-dir: $dir\$" \
+       git log --grep="^git-subtree-dir: $dir/*\$" \
                --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
        while read a b junk; do
                case "$a" in
-                       START) main="$b"; sq="$b" ;;
+                       START) sq="$b" ;;
                        git-subtree-mainline:) main="$b" ;;
                        git-subtree-split:) sub="$b" ;;
                        END)
+                               debug "  Main is: '$main'"
                                if [ -z "$main" -a -n "$sub" ]; then
                                        # squash commits refer to a subtree
+                                       debug "  Squash: $sq from $sub"
                                        cache_set "$sq" "$sub"
                                fi
                                if [ -n "$main" -a -n "$sub" ]; then
@@ -321,6 +324,7 @@ subtree_for_commit()
        git ls-tree "$commit" -- "$dir" |
        while read mode type tree name; do
                assert [ "$name" = "$dir" ]
+               assert [ "$type" = "tree" ]
                echo $tree
                break
        done
@@ -425,7 +429,7 @@ cmd_add()
        
        debug "Adding $dir as '$rev'..."
        git read-tree --prefix="$dir" $rev || exit $?
-       git checkout "$dir" || exit $?
+       git checkout -- "$dir" || exit $?
        tree=$(git write-tree) || exit $?
        
        headrev=$(git rev-parse HEAD) || exit $?