View Javadoc
1   /*
2    * Copyright (C) 2011, 2013 Chris Aniszczyk <caniszczyk@gmail.com> and others. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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.eclipse.jgit.lib.StoredConfig;
25  import org.eclipse.jgit.util.SystemReader;
26  import org.junit.Test;
27  
28  public class LsRemoteCommandTest extends RepositoryTestCase {
29  
30  	private Git git;
31  
32  	@Override
33  	public void setUp() throws Exception {
34  		super.setUp();
35  		git = new Git(db);
36  		// commit something
37  		writeTrashFile("Test.txt", "Hello world");
38  		git.add().addFilepattern("Test.txt").call();
39  		git.commit().setMessage("Initial commit").call();
40  
41  		// create a test branch and switch to it
42  		git.branchCreate().setName("test").call();
43  		RefUpdate rup = db.updateRef(Constants.HEAD);
44  		rup.link("refs/heads/test");
45  
46  		// tags
47  		git.tag().setName("tag1").call();
48  		git.tag().setName("tag2").call();
49  		git.tag().setName("tag3").call();
50  	}
51  
52  	@Test
53  	public void testLsRemote() throws Exception {
54  		File directory = createTempDirectory("testRepository");
55  		CloneCommand command = Git.cloneRepository();
56  		command.setDirectory(directory);
57  		command.setURI(fileUri());
58  		command.setCloneAllBranches(true);
59  		Git git2 = command.call();
60  		addRepoToClose(git2.getRepository());
61  
62  
63  		LsRemoteCommand lsRemoteCommand = git2.lsRemote();
64  		Collection<Ref> refs = lsRemoteCommand.call();
65  		assertNotNull(refs);
66  		assertEquals(6, refs.size());
67  	}
68  
69  	@Test
70  	public void testLsRemoteWithTags() throws Exception {
71  		File directory = createTempDirectory("testRepository");
72  		CloneCommand command = Git.cloneRepository();
73  		command.setDirectory(directory);
74  		command.setURI(fileUri());
75  		command.setCloneAllBranches(true);
76  		Git git2 = command.call();
77  		addRepoToClose(git2.getRepository());
78  
79  		LsRemoteCommand lsRemoteCommand = git2.lsRemote();
80  		lsRemoteCommand.setTags(true);
81  		Collection<Ref> refs = lsRemoteCommand.call();
82  		assertNotNull(refs);
83  		assertEquals(3, refs.size());
84  	}
85  
86  	@Test
87  	public void testLsRemoteWithHeads() throws Exception {
88  		File directory = createTempDirectory("testRepository");
89  		CloneCommand command = Git.cloneRepository();
90  		command.setDirectory(directory);
91  		command.setURI(fileUri());
92  		command.setCloneAllBranches(true);
93  		Git git2 = command.call();
94  		addRepoToClose(git2.getRepository());
95  
96  		LsRemoteCommand lsRemoteCommand = git2.lsRemote();
97  		lsRemoteCommand.setHeads(true);
98  		Collection<Ref> refs = lsRemoteCommand.call();
99  		assertNotNull(refs);
100 		assertEquals(2, refs.size());
101 	}
102 
103 	@Test
104 	public void testLsRemoteWithoutLocalRepository() throws Exception {
105 		String uri = fileUri();
106 		Collection<Ref> refs = Git.lsRemoteRepository().setRemote(uri).setHeads(true).call();
107 		assertNotNull(refs);
108 		assertEquals(2, refs.size());
109 	}
110 
111 	@Test
112 	public void testLsRemoteWithoutLocalRepositoryUrlInsteadOf()
113 			throws Exception {
114 		String uri = fileUri();
115 		StoredConfig userConfig = SystemReader.getInstance().getUserConfig();
116 		userConfig.load();
117 		userConfig.setString("url", uri, "insteadOf", "file:///foo");
118 		userConfig.save();
119 		Collection<Ref> refs = Git.lsRemoteRepository().setRemote("file:///foo")
120 				.setHeads(true).call();
121 		assertNotNull(refs);
122 		assertEquals(2, refs.size());
123 	}
124 
125 	@Test
126 	public void testLsRemoteWithSymRefs() throws Exception {
127 		File directory = createTempDirectory("testRepository");
128 		CloneCommand command = Git.cloneRepository();
129 		command.setDirectory(directory);
130 		command.setURI(fileUri());
131 		command.setCloneAllBranches(true);
132 		Git git2 = command.call();
133 		addRepoToClose(git2.getRepository());
134 
135 
136 		LsRemoteCommand lsRemoteCommand = git2.lsRemote();
137 		Collection<Ref> refs = lsRemoteCommand.call();
138 		assertNotNull(refs);
139 		assertEquals(6, refs.size());
140 
141 		Optional<Ref> headRef = refs.stream().filter(ref -> ref.getName().equals(Constants.HEAD)).findFirst();
142 		assertTrue("expected a HEAD Ref", headRef.isPresent());
143 		assertTrue("expected HEAD Ref to be a Symbolic", headRef.get().isSymbolic());
144 		assertEquals("refs/heads/test", headRef.get().getTarget().getName());
145 	}
146 
147 	private String fileUri() {
148 		return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
149 	}
150 
151 }