]> source.dussan.org Git - jgit.git/commitdiff
Handle short branch/tag name for setBranch in CloneCommand 53/13153/2
authorRobin Stocker <robin@nibor.org>
Sat, 25 May 2013 13:56:50 +0000 (15:56 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Sat, 25 May 2013 23:25:10 +0000 (01:25 +0200)
Before, it was not clear from the documentation what kind of branch name
was accepted. Users specifying "branch" (instead of "refs/heads/branch")
got no error message and ended up with a repository without HEAD and no
checkout.

With this, CloneCommand now tries "$branch", then "refs/heads/$branch"
and then "refs/tags/$branch". C Git only does the last two, but for
compatibility we should still allow "refs/heads/branch".

Bug: 390994
Change-Id: I4be13144f2a21a6583e0942f0c7c40da32f2247a
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index 7422951920b17e11a39c5d0406c2ac5175f86eb6..31f909a5f2a77c3dcfc930f39a1bb5f7bf69c4a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com>
+ * Copyright (C) 2011, 2013 Chris Aniszczyk <caniszczyk@gmail.com>
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -64,7 +64,6 @@ import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Ref;
-import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevBlob;
 import org.eclipse.jgit.revwalk.RevCommit;
@@ -92,11 +91,10 @@ public class CloneCommandTest extends RepositoryTestCase {
                writeTrashFile("Test.txt", "Hello world");
                git.add().addFilepattern("Test.txt").call();
                git.commit().setMessage("Initial commit").call();
+               git.tag().setName("tag-initial").setMessage("Tag initial").call();
 
-               // create a master branch and switch to it
-               git.branchCreate().setName("test").call();
-               RefUpdate rup = db.updateRef(Constants.HEAD);
-               rup.link("refs/heads/test");
+               // create a test branch and switch to it
+               git.checkout().setCreateBranch(true).setName("test").call();
 
                // commit something on the test branch
                writeTrashFile("Test.txt", "Some change");
@@ -205,6 +203,36 @@ public class CloneCommandTest extends RepositoryTestCase {
                                .branchList().setListMode(ListMode.ALL).call()));
        }
 
+       @Test
+       public void testCloneRepositoryWithBranchShortName() throws Exception {
+               File directory = createTempDirectory("testCloneRepositoryWithBranch");
+               CloneCommand command = Git.cloneRepository();
+               command.setBranch("test");
+               command.setDirectory(directory);
+               command.setURI("file://" + git.getRepository().getWorkTree().getPath());
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+
+               assertNotNull(git2);
+               assertEquals("refs/heads/test", git2.getRepository().getFullBranch());
+       }
+
+       @Test
+       public void testCloneRepositoryWithTagName() throws Exception {
+               File directory = createTempDirectory("testCloneRepositoryWithBranch");
+               CloneCommand command = Git.cloneRepository();
+               command.setBranch("tag-initial");
+               command.setDirectory(directory);
+               command.setURI("file://" + git.getRepository().getWorkTree().getPath());
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+
+               assertNotNull(git2);
+               ObjectId taggedCommit = db.resolve("tag-initial^{commit}");
+               assertEquals(taggedCommit.name(), git2
+                               .getRepository().getFullBranch());
+       }
+
        @Test
        public void testCloneRepositoryOnlyOneBranch() throws IOException,
                        JGitInternalException, GitAPIException {
index 65af216ae86bdfda5f0b4ef3b4da3d0d2bac4aef..645d3e781572de89c34018d9c303c20fc2f4ddbb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com>
+ * Copyright (C) 2011, 2013 Chris Aniszczyk <caniszczyk@gmail.com>
  * and other copyright owners as documented in the project's IP log.
  *
  * This program and the accompanying materials are made available
@@ -198,12 +198,19 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                        throws MissingObjectException, IncorrectObjectTypeException,
                        IOException, GitAPIException {
 
-               Ref head = result.getAdvertisedRef(branch);
+               Ref head = null;
                if (branch.equals(Constants.HEAD)) {
                        Ref foundBranch = findBranchToCheckout(result);
                        if (foundBranch != null)
                                head = foundBranch;
                }
+               if (head == null) {
+                       head = result.getAdvertisedRef(branch);
+                       if (head == null)
+                               head = result.getAdvertisedRef(Constants.R_HEADS + branch);
+                       if (head == null)
+                               head = result.getAdvertisedRef(Constants.R_TAGS + branch);
+               }
 
                if (head == null || head.getObjectId() == null)
                        return; // throw exception?
@@ -362,7 +369,9 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
 
        /**
         * @param branch
-        *            the initial branch to check out when cloning the repository
+        *            the initial branch to check out when cloning the repository.
+        *            Can be specified as ref name (<code>refs/heads/master</code>),
+        *            branch name (<code>master</code>) or tag name (<code>v1.2.3</code>).
         * @return this instance
         */
        public CloneCommand setBranch(String branch) {
@@ -409,7 +418,8 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
        /**
         * @param branchesToClone
         *            collection of branches to clone. Ignored when allSelected is
-        *            true.
+        *            true. Must be specified as full ref names (e.g.
+        *            <code>refs/heads/master</code>).
         * @return {@code this}
         */
        public CloneCommand setBranchesToClone(Collection<String> branchesToClone) {