git p4: allow short ref names to --branch
authorPete Wyckoff <pw@padd.com>
Tue, 15 Jan 2013 00:47:03 +0000 (19:47 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 15 Jan 2013 17:46:30 +0000 (09:46 -0800)
For a clone or sync, --branch says where the newly imported
branch should go, or which existing branch to sync up. It
takes an argument, which is currently either something that
starts with "refs/", or if not, "refs/heads/p4" is prepended.

Putting it in heads seems like a bad default; these should
go in remotes/p4/ in most situations. Make that the new default,
and be more liberal in the form of the branch name.

Signed-off-by: Pete Wyckoff <pw@padd.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-p4.txt
git-p4.py
t/t9806-git-p4-options.sh
index 7c5230e82ccdcb92924f656ca5225f9ea7a3530c..7bd5c290c4dbe97fee653ea2e5178847d482595e 100644 (file)
@@ -173,8 +173,11 @@ subsequent 'sync' operations.
 
 --branch <branch>::
        Import changes into given branch.  If the branch starts with
-       'refs/', it will be used as is, otherwise the path 'refs/heads/'
-       will be prepended.  The default branch is 'p4/master'.
+       'refs/', it will be used as is.  Otherwise if it does not start
+       with 'p4/', that prefix is added.  The branch is assumed to
+       name a remote tracking, but this can be modified using
+       '--import-local', or by giving a full ref name.  The default
+       branch is 'master'.
 +
 This example imports a new remote "p4/proj2" into an existing
 git repository:
index c59ad9303488066ae11286388def4f51296f3155..2d0b58f0b450dcd8ab2009d2e03e3ba6fcfbc4fc 100755 (executable)
--- a/git-p4.py
+++ b/git-p4.py
@@ -2841,8 +2841,18 @@ def run(self, args):
                 if not self.silent and not self.detectBranches:
                     print "Performing incremental import into %s git branch" % self.branch
 
+        # accept multiple ref name abbreviations:
+        #    refs/foo/bar/branch -> use it exactly
+        #    p4/branch -> prepend refs/remotes/ or refs/heads/
+        #    branch -> prepend refs/remotes/p4/ or refs/heads/p4/
         if not self.branch.startswith("refs/"):
-            self.branch = "refs/heads/" + self.branch
+            if self.importIntoRemotes:
+                prepend = "refs/remotes/"
+            else:
+                prepend = "refs/heads/"
+            if not self.branch.startswith("p4/"):
+                prepend += "p4/"
+            self.branch = prepend + self.branch
 
         if len(args) == 0 and self.depotPaths:
             if not self.silent:
index 2ad3a3e0ca21c467e400536165a324d309ec66d0..c0d44337d2893de9f10deefe80237860c8d40657 100755 (executable)
@@ -51,6 +51,27 @@ test_expect_failure 'sync when branch is not called master should work' '
        )
 '
 
+test_expect_success 'sync --branch builds the full ref name correctly' '
+       test_when_finished cleanup_git &&
+       (
+               cd "$git" &&
+               git init &&
+
+               git p4 sync --branch=b1 //depot &&
+               git rev-parse --verify refs/remotes/p4/b1 &&
+               git p4 sync --branch=p4/b2 //depot &&
+               git rev-parse --verify refs/remotes/p4/b2 &&
+
+               git p4 sync --import-local --branch=h1 //depot &&
+               git rev-parse --verify refs/heads/p4/h1 &&
+               git p4 sync --import-local --branch=p4/h2 //depot &&
+               git rev-parse --verify refs/heads/p4/h2 &&
+
+               git p4 sync --branch=refs/stuff //depot &&
+               git rev-parse --verify refs/stuff
+       )
+'
+
 # engages --detect-branches code, which will do filename filtering so
 # no sync to either b1 or b2
 test_expect_success 'sync when two branches but no master should noop' '