View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * Copyright (C) 2009, Robin Rosenberg
4    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.lib;
14  
15  import static java.nio.charset.StandardCharsets.UTF_8;
16  import static org.eclipse.jgit.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Optional;
31  import java.util.Set;
32  import java.util.TreeSet;
33  
34  import org.eclipse.jgit.lib.Ref.Storage;
35  import org.eclipse.jgit.lib.RefUpdate.Result;
36  import org.eclipse.jgit.storage.file.FileBasedConfig;
37  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
38  import org.junit.Test;
39  
40  /**
41   * Misc tests for refs. A lot of things are tested elsewhere so not having a
42   * test for a ref related method, does not mean it is untested.
43   */
44  public class RefTest extends SampleDataRepositoryTestCase {
45  
46  	private void writeSymref(String src, String dst) throws IOException {
47  		RefUpdate u = db.updateRef(src);
48  		switch (u.link(dst)) {
49  		case NEW:
50  		case FORCED:
51  		case NO_CHANGE:
52  			break;
53  		default:
54  			fail("link " + src + " to " + dst);
55  		}
56  	}
57  
58  	private void writeNewRef(String name, ObjectId value) throws IOException {
59  		RefUpdate updateRef = db.updateRef(name);
60  		updateRef.setNewObjectId(value);
61  		assertEquals(RefUpdate.Result.NEW, updateRef.update());
62  	}
63  
64  	@Test
65  	public void testRemoteNames() throws Exception {
66  		FileBasedConfig config = db.getConfig();
67  		config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
68  				"origin", "dummy", true);
69  		config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
70  				"ab/c", "dummy", true);
71  		config.save();
72  		assertEquals("[ab/c, origin]",
73  				new TreeSet<>(db.getRemoteNames()).toString());
74  
75  		// one-level deep remote branch
76  		assertEquals("master",
77  				db.shortenRemoteBranchName("refs/remotes/origin/master"));
78  		assertEquals("origin", db.getRemoteName("refs/remotes/origin/master"));
79  
80  		// two-level deep remote branch
81  		assertEquals("masta/r",
82  				db.shortenRemoteBranchName("refs/remotes/origin/masta/r"));
83  		assertEquals("origin", db.getRemoteName("refs/remotes/origin/masta/r"));
84  
85  		// Remote with slash and one-level deep branch name
86  		assertEquals("xmaster",
87  				db.shortenRemoteBranchName("refs/remotes/ab/c/xmaster"));
88  		assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmaster"));
89  
90  		// Remote with slash and two-level deep branch name
91  		assertEquals("xmasta/r",
92  				db.shortenRemoteBranchName("refs/remotes/ab/c/xmasta/r"));
93  		assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmasta/r"));
94  
95  		// no such remote
96  		assertNull(db.getRemoteName("refs/remotes/nosuchremote/x"));
97  		assertNull(db.shortenRemoteBranchName("refs/remotes/nosuchremote/x"));
98  
99  		// no such remote too, no branch name either
100 		assertNull(db.getRemoteName("refs/remotes/abranch"));
101 		assertNull(db.shortenRemoteBranchName("refs/remotes/abranch"));
102 
103 		// // local branch
104 		assertNull(db.getRemoteName("refs/heads/abranch"));
105 		assertNull(db.shortenRemoteBranchName("refs/heads/abranch"));
106 	}
107 
108 	@Test
109 	public void testReadAllIncludingSymrefs() throws Exception {
110 		ObjectId masterId = db.resolve("refs/heads/master");
111 		RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
112 		updateRef.setNewObjectId(masterId);
113 		updateRef.setForceUpdate(true);
114 		updateRef.update();
115 		writeSymref("refs/remotes/origin/HEAD",
116 					"refs/remotes/origin/master");
117 
118 		ObjectId r = db.resolve("refs/remotes/origin/HEAD");
119 		assertEquals(masterId, r);
120 
121 		List<Ref> allRefs = db.getRefDatabase().getRefs();
122 		Optional<Ref> refHEAD = allRefs.stream()
123 				.filter(ref -> ref.getName().equals("refs/remotes/origin/HEAD"))
124 				.findAny();
125 		assertTrue(refHEAD.isPresent());
126 		assertEquals(masterId, refHEAD.get().getObjectId());
127 		assertFalse(refHEAD.get().isPeeled());
128 		assertNull(refHEAD.get().getPeeledObjectId());
129 
130 		Optional<Ref> refmaster = allRefs.stream().filter(
131 				ref -> ref.getName().equals("refs/remotes/origin/master"))
132 				.findAny();
133 		assertTrue(refmaster.isPresent());
134 		assertEquals(masterId, refmaster.get().getObjectId());
135 		assertFalse(refmaster.get().isPeeled());
136 		assertNull(refmaster.get().getPeeledObjectId());
137 	}
138 
139 	@Test
140 	public void testReadSymRefToPacked() throws IOException {
141 		writeSymref("HEAD", "refs/heads/b");
142 		Ref ref = db.exactRef("HEAD");
143 		assertEquals(Ref.Storage.LOOSE, ref.getStorage());
144 		assertTrue("is symref", ref.isSymbolic());
145 		ref = ref.getTarget();
146 		assertEquals("refs/heads/b", ref.getName());
147 		assertEquals(Ref.Storage.PACKED, ref.getStorage());
148 	}
149 
150 	@Test
151 	public void testReadSymRefToLoosePacked() throws IOException {
152 		ObjectId pid = db.resolve("refs/heads/master^");
153 		RefUpdate updateRef = db.updateRef("refs/heads/master");
154 		updateRef.setNewObjectId(pid);
155 		updateRef.setForceUpdate(true);
156 		Result update = updateRef.update();
157 		assertEquals(Result.FORCED, update); // internal
158 
159 		writeSymref("HEAD", "refs/heads/master");
160 		Ref ref = db.exactRef("HEAD");
161 		assertEquals(Ref.Storage.LOOSE, ref.getStorage());
162 		ref = ref.getTarget();
163 		assertEquals("refs/heads/master", ref.getName());
164 		assertEquals(Ref.Storage.LOOSE, ref.getStorage());
165 	}
166 
167 	@Test
168 	public void testReadLooseRef() throws IOException {
169 		RefUpdate updateRef = db.updateRef("ref/heads/new");
170 		updateRef.setNewObjectId(db.resolve("refs/heads/master"));
171 		Result update = updateRef.update();
172 		assertEquals(Result.NEW, update);
173 		Ref ref = db.exactRef("ref/heads/new");
174 		assertEquals(Storage.LOOSE, ref.getStorage());
175 	}
176 
177 	@Test
178 	public void testGetShortRef() throws IOException {
179 		Ref ref = db.exactRef("refs/heads/master");
180 		assertEquals("refs/heads/master", ref.getName());
181 		assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
182 	}
183 
184 	@Test
185 	public void testGetShortExactRef() throws IOException {
186 		assertNull(db.getRefDatabase().exactRef("master"));
187 
188 		Ref ref = db.getRefDatabase().exactRef("HEAD");
189 		assertEquals("HEAD", ref.getName());
190 		assertEquals("refs/heads/master", ref.getTarget().getName());
191 		assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
192 	}
193 
194 	@Test
195 	public void testRefsUnderRefs() throws IOException {
196 		ObjectId masterId = db.resolve("refs/heads/master");
197 		writeNewRef("refs/heads/refs/foo/bar", masterId);
198 
199 		assertNull(db.getRefDatabase().exactRef("refs/foo/bar"));
200 
201 		Ref ref = db.findRef("refs/foo/bar");
202 		assertEquals("refs/heads/refs/foo/bar", ref.getName());
203 		assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
204 	}
205 
206 	@Test
207 	public void testAmbiguousRefsUnderRefs() throws IOException {
208 		ObjectId masterId = db.resolve("refs/heads/master");
209 		writeNewRef("refs/foo/bar", masterId);
210 		writeNewRef("refs/heads/refs/foo/bar", masterId);
211 
212 		Ref exactRef = db.getRefDatabase().exactRef("refs/foo/bar");
213 		assertEquals("refs/foo/bar", exactRef.getName());
214 		assertEquals(masterId, exactRef.getObjectId());
215 
216 		Ref ref = db.findRef("refs/foo/bar");
217 		assertEquals("refs/foo/bar", ref.getName());
218 		assertEquals(masterId, ref.getObjectId());
219 	}
220 
221 	/**
222 	 * Let an "outsider" create a loose ref with the same name as a packed one
223 	 *
224 	 * @throws IOException
225 	 * @throws InterruptedException
226 	 */
227 	@Test
228 	public void testReadLoosePackedRef() throws IOException,
229 			InterruptedException {
230 		Ref ref = db.exactRef("refs/heads/master");
231 		assertEquals(Storage.PACKED, ref.getStorage());
232 		try (FileOutputStream os = new FileOutputStream(
233 				new File(db.getDirectory(), "refs/heads/master"))) {
234 			os.write(ref.getObjectId().name().getBytes(UTF_8));
235 			os.write('\n');
236 		}
237 
238 		ref = db.exactRef("refs/heads/master");
239 		assertEquals(Storage.LOOSE, ref.getStorage());
240 	}
241 
242 	/**
243 	 * Modify a packed ref using the API. This creates a loose ref too, ie.
244 	 * LOOSE_PACKED
245 	 *
246 	 * @throws IOException
247 	 */
248 	@Test
249 	public void testReadSimplePackedRefSameRepo() throws IOException {
250 		Ref ref = db.exactRef("refs/heads/master");
251 		ObjectId pid = db.resolve("refs/heads/master^");
252 		assertEquals(Storage.PACKED, ref.getStorage());
253 		RefUpdate updateRef = db.updateRef("refs/heads/master");
254 		updateRef.setNewObjectId(pid);
255 		updateRef.setForceUpdate(true);
256 		Result update = updateRef.update();
257 		assertEquals(Result.FORCED, update);
258 
259 		ref = db.exactRef("refs/heads/master");
260 		assertEquals(Storage.LOOSE, ref.getStorage());
261 	}
262 
263 	@Test
264 	public void testResolvedNamesBranch() throws IOException {
265 		Ref ref = db.findRef("a");
266 		assertEquals("refs/heads/a", ref.getName());
267 	}
268 
269 	@Test
270 	public void testResolvedSymRef() throws IOException {
271 		Ref ref = db.exactRef(Constants.HEAD);
272 		assertEquals(Constants.HEAD, ref.getName());
273 		assertTrue("is symbolic ref", ref.isSymbolic());
274 		assertSame(Ref.Storage.LOOSE, ref.getStorage());
275 
276 		Ref dst = ref.getTarget();
277 		assertNotNull("has target", dst);
278 		assertEquals("refs/heads/master", dst.getName());
279 
280 		assertSame(dst.getObjectId(), ref.getObjectId());
281 		assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId());
282 		assertEquals(dst.isPeeled(), ref.isPeeled());
283 	}
284 
285 	private static void checkContainsRef(Collection<Ref> haystack, Ref needle) {
286 		for (Ref ref : haystack) {
287 			if (ref.getName().equals(needle.getName()) &&
288 					ref.getObjectId().equals(needle.getObjectId())) {
289 				return;
290 			}
291 		}
292 		fail("list " + haystack + " does not contain ref " + needle);
293 	}
294 
295 	@Test
296 	public void testGetRefsByPrefix() throws IOException {
297 		List<Ref> refs = db.getRefDatabase().getRefsByPrefix("refs/heads/g");
298 		assertEquals(2, refs.size());
299 		checkContainsRef(refs, db.exactRef("refs/heads/g"));
300 		checkContainsRef(refs, db.exactRef("refs/heads/gitlink"));
301 
302 		refs = db.getRefDatabase().getRefsByPrefix("refs/heads/prefix/");
303 		assertEquals(1, refs.size());
304 		checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
305 	}
306 
307 	@Test
308 	public void testGetRefsByPrefixes() throws IOException {
309 		List<Ref> refs = db.getRefDatabase().getRefsByPrefix();
310 		assertEquals(0, refs.size());
311 
312 		refs = db.getRefDatabase().getRefsByPrefix("refs/heads/p",
313 				"refs/tags/A");
314 		assertEquals(3, refs.size());
315 		checkContainsRef(refs, db.exactRef("refs/heads/pa"));
316 		checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
317 		checkContainsRef(refs, db.exactRef("refs/tags/A"));
318 	}
319 
320 	@Test
321 	public void testResolveTipSha1() throws IOException {
322 		ObjectId masterId = db.resolve("refs/heads/master");
323 		Set<Ref> resolved = db.getRefDatabase().getTipsWithSha1(masterId);
324 
325 		assertEquals(2, resolved.size());
326 		checkContainsRef(resolved, db.exactRef("refs/heads/master"));
327 		checkContainsRef(resolved, db.exactRef("HEAD"));
328 
329 		assertEquals(db.getRefDatabase()
330 				.getTipsWithSha1(ObjectId.zeroId()).size(), 0);
331 	}
332 }