# [PATCH v2] contrib/subtree: allow addition of remote branch with name not locally present

1 messages from 2013-02-28 to 2013-02-28. Participants: Paul Campbell.
Thread: https://gitlist.dev/t/33041

## Paul Campbell, 2013-02-28 21:41

Subject: [PATCH v2] contrib/subtree: allow addition of remote branch with name not locally present
Message-ID: <CALeLG_m3A_52z=+NDrpttmTavzw68hnuWJZk8XHcN-smsQJupg@mail.gmail.com>
URL: https://gitlist.dev/e/CALeLG_m3A_52z%3D%2BNDrpttmTavzw68hnuWJZk8XHcN-smsQJupg%40mail.gmail.com

```
cmd_add() attempts to check for the validity of refspec for the repository
it is about to add as a subtree. It tries to do so before contacting the
repository. If the refspec happens to exist locally (say 'master') then
the test passes and the repo is fetched. If the refspec doesn't exist
locally then the test fails and the remote repo is never contacted.

Removing the tests still works as the git fetch command fails with the
perfectly accurate error:

  fatal: Couldn't find remote ref <refspec>

New tests check to prevent pulling in multiple branches or creating a local
branch for the subtree.

Signed-off-by: Paul Campbell <pcampbell@carnegiecollege.ac.uk>
---

I've rerolled this with alternate tests.

Is there anything else that should be tested for?

 contrib/subtree/git-subtree.sh | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 8a23f58..d04bd25 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -503,13 +503,14 @@ cmd_add()

 	    "cmd_add_commit" "$@"
 	elif [ $# -eq 2 ]; then
-	    # Technically we could accept a refspec here but we're
-	    # just going to turn around and add FETCH_HEAD under the
-	    # 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"
+	    case "$2" in
+		*\**) # Avoid pulling in multiple branches
+			die "'$2' contains a wildcard"
+			;;
+		*\:*) # Don't create a local branch for the subtree
+			die "'$2' contains a local branch name"
+			;;
+	    esac

 	    "cmd_add_repository" "$@"
 	else
-- 
1.8.2.rc1

```
