]> source.dussan.org Git - jgit.git/commitdiff
tag option for clone command 70/159170/3
authorAlexander Nittka <alex@nittka.de>
Wed, 11 Mar 2020 10:58:03 +0000 (11:58 +0100)
committerAlexander Nittka <alex@nittka.de>
Sun, 15 Mar 2020 14:34:21 +0000 (15:34 +0100)
Allow explicitly setting the tag option for the remote configuration
when cloning a repository.

Bug: 561021
Change-Id: Iac43268a2bb231ae7599c3255bf555883d34fa32
Signed-off-by: Alexander Nittka <alex@nittka.de>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index 3d0dacab3dd161b8643504c97c41aba81305e0b9..b737bbec0ee51cf33a88d7f14e56497d3076169d 100644 (file)
@@ -44,6 +44,7 @@ import org.eclipse.jgit.submodule.SubmoduleStatusType;
 import org.eclipse.jgit.submodule.SubmoduleWalk;
 import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.transport.RemoteConfig;
+import org.eclipse.jgit.transport.TagOpt;
 import org.eclipse.jgit.transport.URIish;
 import org.eclipse.jgit.util.SystemReader;
 import org.junit.Test;
@@ -111,6 +112,7 @@ public class CloneCommandTest extends RepositoryTestCase {
                                .size());
                assertEquals(new RefSpec("+refs/heads/*:refs/remotes/origin/*"),
                                fetchRefSpec(git2.getRepository()));
+               assertTagOption(git2.getRepository(), TagOpt.AUTO_FOLLOW);
        }
 
        @Test
@@ -801,6 +803,50 @@ public class CloneCommandTest extends RepositoryTestCase {
                }
        }
 
+       @Test
+       public void testCloneNoTags() throws IOException, JGitInternalException,
+                       GitAPIException, URISyntaxException {
+               File directory = createTempDirectory("testCloneRepository");
+               CloneCommand command = Git.cloneRepository();
+               command.setDirectory(directory);
+               command.setURI(fileUri());
+               command.setNoTags();
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+               assertNotNull(git2);
+               assertNotNull(git2.getRepository().resolve("refs/heads/test"));
+               assertNull(git2.getRepository().resolve("tag-initial"));
+               assertNull(git2.getRepository().resolve("tag-for-blob"));
+               assertTagOption(git2.getRepository(), TagOpt.NO_TAGS);
+       }
+
+       @Test
+       public void testCloneFollowTags() throws IOException, JGitInternalException,
+                       GitAPIException, URISyntaxException {
+               File directory = createTempDirectory("testCloneRepository");
+               CloneCommand command = Git.cloneRepository();
+               command.setDirectory(directory);
+               command.setURI(fileUri());
+               command.setBranch("refs/heads/master");
+               command.setBranchesToClone(
+                               Collections.singletonList("refs/heads/master"));
+               command.setTagOption(TagOpt.FETCH_TAGS);
+               Git git2 = command.call();
+               addRepoToClose(git2.getRepository());
+               assertNotNull(git2);
+               assertNull(git2.getRepository().resolve("refs/heads/test"));
+               assertNotNull(git2.getRepository().resolve("tag-initial"));
+               assertNotNull(git2.getRepository().resolve("tag-for-blob"));
+               assertTagOption(git2.getRepository(), TagOpt.FETCH_TAGS);
+       }
+
+       private void assertTagOption(Repository repo, TagOpt expectedTagOption)
+                       throws URISyntaxException {
+               RemoteConfig remoteConfig = new RemoteConfig(
+                               repo.getConfig(), "origin");
+               assertEquals(expectedTagOption, remoteConfig.getTagOpt());
+       }
+
        private String fileUri() {
                return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
        }
index 78afe18f39ad80b527407d91d03972c71805aaed..30d7f9adc43f9bd548ed4d302b4b9f5f404256ef 100644 (file)
@@ -89,6 +89,8 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
 
        private FETCH_TYPE fetchType;
 
+       private TagOpt tagOption;
+
        private enum FETCH_TYPE {
                MULTIPLE_BRANCHES, ALL_BRANCHES, MIRROR
        }
@@ -278,6 +280,9 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
 
                config.setFetchRefSpecs(calculateRefSpecs(fetchType, config.getName()));
                config.setMirror(fetchType == FETCH_TYPE.MIRROR);
+               if (tagOption != null) {
+                       config.setTagOpt(tagOption);
+               }
                config.update(clonedRepo.getConfig());
 
                clonedRepo.getConfig().save();
@@ -286,7 +291,12 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                FetchCommand command = new FetchCommand(clonedRepo);
                command.setRemote(remote);
                command.setProgressMonitor(monitor);
-               command.setTagOpt(fetchAll ? TagOpt.FETCH_TAGS : TagOpt.AUTO_FOLLOW);
+               if (tagOption != null) {
+                       command.setTagOpt(tagOption);
+               } else {
+                       command.setTagOpt(
+                                       fetchAll ? TagOpt.FETCH_TAGS : TagOpt.AUTO_FOLLOW);
+               }
                configure(command);
 
                return command.call();
@@ -663,6 +673,30 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                return this;
        }
 
+       /**
+        * Set the tag option used for the remote configuration explicitly.
+        *
+        * @param tagOption
+        *            tag option to be used for the remote config
+        * @return {@code this}
+        * @since 5.8
+        */
+       public CloneCommand setTagOption(TagOpt tagOption) {
+               this.tagOption = tagOption;
+               return this;
+       }
+
+       /**
+        * Set the --no-tags option. Tags are not cloned now and the remote
+        * configuration is initialized with the --no-tags option as well.
+        *
+        * @return {@code this}
+        * @since 5.8
+        */
+       public CloneCommand setNoTags() {
+               return setTagOption(TagOpt.NO_TAGS);
+       }
+
        /**
         * Set whether to skip checking out a branch
         *