added -p alias for --prefix
[gitweb.git] / git-subtree.sh
index c5c0201448eb455ef29a25f5a2950d85ea74363f..28fb8e81fb80349a17763a819666308ebc0a589e 100755 (executable)
@@ -16,7 +16,7 @@ git subtree split --prefix=<prefix> <commit...>
 h,help        show the help
 q             quiet
 d             show debug messages
-prefix=       the name of the subdir to split out
+p,prefix=     the name of the subdir to split out
  options for 'split'
 annotate=     add a prefix to commit message of new commits
 b,branch=     create a new branch from the split subtree
@@ -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=
@@ -75,7 +76,7 @@ while [ $# -gt 0 ]; do
                --annotate) annotate="$1"; shift ;;
                --no-annotate) annotate= ;;
                -b) branch="$1"; shift ;;
-               --prefix) prefix="$1"; shift ;;
+               -p) prefix="$1"; shift ;;
                --no-prefix) prefix= ;;
                --onto) onto="$1"; shift ;;
                --no-onto) onto= ;;
@@ -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 $?
@@ -563,8 +567,9 @@ cmd_merge()
 cmd_pull()
 {
        ensure_clean
-       set -x
-       git pull -s subtree "$@"
+       git fetch "$@" || exit $?
+       revs=FETCH_HEAD
+       cmd_merge
 }
 
 "cmd_$command" "$@"