1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.File;
17 import java.util.Collection;
18 import java.util.Optional;
19
20 import org.eclipse.jgit.junit.RepositoryTestCase;
21 import org.eclipse.jgit.lib.Constants;
22 import org.eclipse.jgit.lib.Ref;
23 import org.eclipse.jgit.lib.RefUpdate;
24 import org.junit.Test;
25
26 public class LsRemoteCommandTest extends RepositoryTestCase {
27
28 private Git git;
29
30 @Override
31 public void setUp() throws Exception {
32 super.setUp();
33 git = new Git(db);
34
35 writeTrashFile("Test.txt", "Hello world");
36 git.add().addFilepattern("Test.txt").call();
37 git.commit().setMessage("Initial commit").call();
38
39
40 git.branchCreate().setName("test").call();
41 RefUpdate rup = db.updateRef(Constants.HEAD);
42 rup.link("refs/heads/test");
43
44
45 git.tag().setName("tag1").call();
46 git.tag().setName("tag2").call();
47 git.tag().setName("tag3").call();
48 }
49
50 @Test
51 public void testLsRemote() throws Exception {
52 File directory = createTempDirectory("testRepository");
53 CloneCommand command = Git.cloneRepository();
54 command.setDirectory(directory);
55 command.setURI(fileUri());
56 command.setCloneAllBranches(true);
57 Git git2 = command.call();
58 addRepoToClose(git2.getRepository());
59
60
61 LsRemoteCommand lsRemoteCommand = git2.lsRemote();
62 Collection<Ref> refs = lsRemoteCommand.call();
63 assertNotNull(refs);
64 assertEquals(6, refs.size());
65 }
66
67 @Test
68 public void testLsRemoteWithTags() throws Exception {
69 File directory = createTempDirectory("testRepository");
70 CloneCommand command = Git.cloneRepository();
71 command.setDirectory(directory);
72 command.setURI(fileUri());
73 command.setCloneAllBranches(true);
74 Git git2 = command.call();
75 addRepoToClose(git2.getRepository());
76
77 LsRemoteCommand lsRemoteCommand = git2.lsRemote();
78 lsRemoteCommand.setTags(true);
79 Collection<Ref> refs = lsRemoteCommand.call();
80 assertNotNull(refs);
81 assertEquals(3, refs.size());
82 }
83
84 @Test
85 public void testLsRemoteWithHeads() throws Exception {
86 File directory = createTempDirectory("testRepository");
87 CloneCommand command = Git.cloneRepository();
88 command.setDirectory(directory);
89 command.setURI(fileUri());
90 command.setCloneAllBranches(true);
91 Git git2 = command.call();
92 addRepoToClose(git2.getRepository());
93
94 LsRemoteCommand lsRemoteCommand = git2.lsRemote();
95 lsRemoteCommand.setHeads(true);
96 Collection<Ref> refs = lsRemoteCommand.call();
97 assertNotNull(refs);
98 assertEquals(2, refs.size());
99 }
100
101 @Test
102 public void testLsRemoteWithoutLocalRepository() throws Exception {
103 String uri = fileUri();
104 Collection<Ref> refs = Git.lsRemoteRepository().setRemote(uri).setHeads(true).call();
105 assertNotNull(refs);
106 assertEquals(2, refs.size());
107 }
108
109 @Test
110 public void testLsRemoteWithSymRefs() throws Exception {
111 File directory = createTempDirectory("testRepository");
112 CloneCommand command = Git.cloneRepository();
113 command.setDirectory(directory);
114 command.setURI(fileUri());
115 command.setCloneAllBranches(true);
116 Git git2 = command.call();
117 addRepoToClose(git2.getRepository());
118
119
120 LsRemoteCommand lsRemoteCommand = git2.lsRemote();
121 Collection<Ref> refs = lsRemoteCommand.call();
122 assertNotNull(refs);
123 assertEquals(6, refs.size());
124
125 Optional<Ref> headRef = refs.stream().filter(ref -> ref.getName().equals(Constants.HEAD)).findFirst();
126 assertTrue("expected a HEAD Ref", headRef.isPresent());
127 assertTrue("expected HEAD Ref to be a Symbolic", headRef.get().isSymbolic());
128 assertEquals("refs/heads/test", headRef.get().getTarget().getName());
129 }
130
131 private String fileUri() {
132 return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
133 }
134
135 }