]> source.dussan.org Git - jgit.git/commitdiff
Add parameters for timeout and branches to clone 09/3009/2
authorStefan Lay <stefan.lay@sap.com>
Wed, 6 Apr 2011 12:08:25 +0000 (14:08 +0200)
committerStefan Lay <stefan.lay@sap.com>
Wed, 6 Apr 2011 13:00:22 +0000 (15:00 +0200)
The timeout is also used in the FetchCommand called by the
CloneCommand.

The possibility to provide a list of branches to fetch initially is a
feature offered by EGit. To implement it here is a prerequisite for
EGit to be able to use the CloneCommand.

Change-Id: I21453de22e9ca61919a7c3386fcc526024742f5f
Signed-off-by: Stefan Lay <stefan.lay@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index f2ad9e5f117fb109bf6b71831ea0d2951175fae9..f21dc4a0be0680f105e84f168fdcc138997b0743 100644 (file)
@@ -48,7 +48,9 @@ import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 
+import org.eclipse.jgit.api.ListBranchCommand.ListMode;
 import org.eclipse.jgit.junit.TestRepository;
 import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.lib.Constants;
@@ -114,6 +116,8 @@ public class CloneCommandTest extends RepositoryTestCase {
                                                        .getConfig()
                                                        .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
                                                                        "test", ConfigConstants.CONFIG_KEY_MERGE));
+                       assertEquals(2, git2.branchList().setListMode(ListMode.REMOTE)
+                                       .call().size());
                } catch (Exception e) {
                        fail(e.getMessage());
                }
@@ -137,6 +141,28 @@ public class CloneCommandTest extends RepositoryTestCase {
                }
        }
 
+       @Test
+       public void testCloneRepositoryOnlyOneBranch() {
+               try {
+                       File directory = createTempDirectory("testCloneRepositoryWithBranch");
+                       CloneCommand command = Git.cloneRepository();
+                       command.setBranch("refs/heads/master");
+                       command.setBranchesToClone(Collections
+                                       .singletonList("refs/heads/master"));
+                       command.setDirectory(directory);
+                       command.setURI("file://"
+                                       + git.getRepository().getWorkTree().getPath());
+                       Git git2 = command.call();
+                       assertNotNull(git2);
+                       assertEquals(git2.getRepository().getFullBranch(),
+                                       "refs/heads/master");
+                       assertEquals(1, git2.branchList().setListMode(ListMode.REMOTE)
+                                       .call().size());
+               } catch (Exception e) {
+                       fail(e.getMessage());
+               }
+       }
+
        public static File createTempDirectory(String name) throws IOException {
                final File temp;
                temp = File.createTempFile(name, Long.toString(System.nanoTime()));
index 52d9e21b09e76bf1bc2e15742953e00c48d0e0fa..39652306b0ebf70ff2b9f6e0d868accb4cfb93a8 100644 (file)
@@ -45,6 +45,9 @@ package org.eclipse.jgit.api;
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 import java.util.concurrent.Callable;
 
 import org.eclipse.jgit.api.errors.InvalidRemoteException;
@@ -91,6 +94,12 @@ public class CloneCommand implements Callable<Git> {
 
        private CredentialsProvider credentialsProvider;
 
+       private int timeout;
+
+       private boolean cloneAllBranches;
+
+       private Collection<String> branchesToClone;
+
        /**
         * Executes the {@code Clone} command.
         *
@@ -146,11 +155,32 @@ public class CloneCommand implements Callable<Git> {
                command.setRemote(remote);
                command.setProgressMonitor(monitor);
                command.setTagOpt(TagOpt.FETCH_TAGS);
+               command.setTimeout(timeout);
                if (credentialsProvider != null)
                        command.setCredentialsProvider(credentialsProvider);
+
+               List<RefSpec> specs = calculateRefSpecs(dst);
+               command.setRefSpecs(specs);
+
                return command.call();
        }
 
+       private List<RefSpec> calculateRefSpecs(final String dst) {
+               RefSpec wcrs = new RefSpec();
+               wcrs = wcrs.setForceUpdate(true);
+               wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
+               List<RefSpec> specs = new ArrayList<RefSpec>();
+               if (cloneAllBranches)
+                       specs.add(wcrs);
+               else if (branchesToClone != null
+                               && branchesToClone.size() > 0) {
+                       for (final String selectedRef : branchesToClone)
+                               if (wcrs.matchSource(selectedRef))
+                                       specs.add(wcrs.expandFromSource(selectedRef));
+               }
+               return specs;
+       }
+
        private void checkout(Repository repo, FetchResult result)
                        throws JGitInternalException,
                        MissingObjectException, IncorrectObjectTypeException, IOException {
@@ -306,4 +336,36 @@ public class CloneCommand implements Callable<Git> {
                return this;
        }
 
+       /**
+        * @param timeout
+        *            the timeout used for the fetch step
+        * @return {@code this}
+        */
+       public CloneCommand setTimeout(int timeout) {
+               this.timeout = timeout;
+               return this;
+       }
+
+       /**
+        * @param cloneAllBranches
+        *            true when all branches have to be fetched (indicates wildcard
+        *            in created fetch refspec), false otherwise.
+        * @return {@code this}
+        */
+       public CloneCommand setCloneAllBranches(boolean cloneAllBranches) {
+               this.cloneAllBranches = cloneAllBranches;
+               return this;
+       }
+
+       /**
+        * @param branchesToClone
+        *            collection of branches to clone. Ignored when allSelected is
+        *            true.
+        * @return {@code this}
+        */
+       public CloneCommand setBranchesToClone(Collection<String> branchesToClone) {
+               this.branchesToClone = branchesToClone;
+               return this;
+       }
+
 }