Merge branch 'mw/symlinks'
[gitweb.git] / contrib / subtree / git-subtree.sh
index 8a23f58ba04c4b1402c66d03db69d0c109c90b58..dc59a91031ea6815456e5c86f5494bf56858c0d1 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # git-subtree.sh: split/join git repositories in subdirectories of this one
 #
@@ -9,10 +9,10 @@ if [ $# -eq 0 ]; then
 fi
 OPTS_SPEC="\
 git subtree add   --prefix=<prefix> <commit>
-git subtree add   --prefix=<prefix> <repository> <commit>
+git subtree add   --prefix=<prefix> <repository> <ref>
 git subtree merge --prefix=<prefix> <commit>
-git subtree pull  --prefix=<prefix> <repository> <refspec...>
-git subtree push  --prefix=<prefix> <repository> <refspec...>
+git subtree pull  --prefix=<prefix> <repository> <ref>
+git subtree push  --prefix=<prefix> <repository> <ref>
 git subtree split --prefix=<prefix> <commit...>
 --
 h,help        show the help
@@ -311,7 +311,7 @@ copy_commit()
                        GIT_COMMITTER_NAME \
                        GIT_COMMITTER_EMAIL \
                        GIT_COMMITTER_DATE
-               (echo -n "$annotate"; cat ) |
+               (printf "%s" "$annotate"; cat ) |
                git commit-tree "$2" $3  # reads the rest of stdin
        ) || die "Can't copy commit $1"
 }
@@ -489,6 +489,12 @@ ensure_clean()
        fi
 }
 
+ensure_valid_ref_format()
+{
+       git check-ref-format "refs/heads/$1" ||
+           die "'$1' does not look like a ref"
+}
+
 cmd_add()
 {
        if [ -e "$dir" ]; then
@@ -508,8 +514,7 @@ cmd_add()
            # specified directory.  Allowing a refspec might be
            # misleading because we won't do anything with any other
            # branches fetched via the refspec.
-           git rev-parse -q --verify "$2^{commit}" >/dev/null ||
-           die "'$2' does not refer to a commit"
+           ensure_valid_ref_format "$2"
 
            "cmd_add_repository" "$@"
        else
@@ -699,7 +704,11 @@ cmd_merge()
 
 cmd_pull()
 {
+       if [ $# -ne 2 ]; then
+           die "You must provide <repository> <ref>"
+       fi
        ensure_clean
+       ensure_valid_ref_format "$2"
        git fetch "$@" || exit $?
        revs=FETCH_HEAD
        set -- $revs
@@ -709,13 +718,15 @@ cmd_pull()
 cmd_push()
 {
        if [ $# -ne 2 ]; then
-           die "You must provide <repository> <refspec>"
+           die "You must provide <repository> <ref>"
        fi
+       ensure_valid_ref_format "$2"
        if [ -e "$dir" ]; then
            repository=$1
            refspec=$2
            echo "git push using: " $repository $refspec
-           git push $repository $(git subtree split --prefix=$prefix):refs/heads/$refspec
+           localrev=$(git subtree split --prefix="$prefix") || die
+           git push $repository $localrev:refs/heads/$refspec
        else
            die "'$dir' must already exist. Try 'git subtree add'."
        fi