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>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.lib;
47  
48  import static java.nio.charset.StandardCharsets.UTF_8;
49  import static org.eclipse.jgit.junit.Assert.assertEquals;
50  import static org.junit.Assert.assertEquals;
51  import static org.junit.Assert.assertFalse;
52  import static org.junit.Assert.assertNotNull;
53  import static org.junit.Assert.assertNull;
54  import static org.junit.Assert.assertSame;
55  import static org.junit.Assert.assertTrue;
56  import static org.junit.Assert.fail;
57  
58  import java.io.File;
59  import java.io.FileOutputStream;
60  import java.io.IOException;
61  import java.util.Collection;
62  import java.util.List;
63  import java.util.Optional;
64  import java.util.Set;
65  import java.util.TreeSet;
66  
67  import org.eclipse.jgit.lib.Ref.Storage;
68  import org.eclipse.jgit.lib.RefUpdate.Result;
69  import org.eclipse.jgit.storage.file.FileBasedConfig;
70  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
71  import org.junit.Test;
72  
73  /**
74   * Misc tests for refs. A lot of things are tested elsewhere so not having a
75   * test for a ref related method, does not mean it is untested.
76   */
77  public class RefTest extends SampleDataRepositoryTestCase {
78  
79  	private void writeSymref(String src, String dst) throws IOException {
80  		RefUpdate u = db.updateRef(src);
81  		switch (u.link(dst)) {
82  		case NEW:
83  		case FORCED:
84  		case NO_CHANGE:
85  			break;
86  		default:
87  			fail("link " + src + " to " + dst);
88  		}
89  	}
90  
91  	private void writeNewRef(String name, ObjectId value) throws IOException {
92  		RefUpdate updateRef = db.updateRef(name);
93  		updateRef.setNewObjectId(value);
94  		assertEquals(RefUpdate.Result.NEW, updateRef.update());
95  	}
96  
97  	@Test
98  	public void testRemoteNames() throws Exception {
99  		FileBasedConfig config = db.getConfig();
100 		config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
101 				"origin", "dummy", true);
102 		config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
103 				"ab/c", "dummy", true);
104 		config.save();
105 		assertEquals("[ab/c, origin]",
106 				new TreeSet<>(db.getRemoteNames()).toString());
107 
108 		// one-level deep remote branch
109 		assertEquals("master",
110 				db.shortenRemoteBranchName("refs/remotes/origin/master"));
111 		assertEquals("origin", db.getRemoteName("refs/remotes/origin/master"));
112 
113 		// two-level deep remote branch
114 		assertEquals("masta/r",
115 				db.shortenRemoteBranchName("refs/remotes/origin/masta/r"));
116 		assertEquals("origin", db.getRemoteName("refs/remotes/origin/masta/r"));
117 
118 		// Remote with slash and one-level deep branch name
119 		assertEquals("xmaster",
120 				db.shortenRemoteBranchName("refs/remotes/ab/c/xmaster"));
121 		assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmaster"));
122 
123 		// Remote with slash and two-level deep branch name
124 		assertEquals("xmasta/r",
125 				db.shortenRemoteBranchName("refs/remotes/ab/c/xmasta/r"));
126 		assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmasta/r"));
127 
128 		// no such remote
129 		assertNull(db.getRemoteName("refs/remotes/nosuchremote/x"));
130 		assertNull(db.shortenRemoteBranchName("refs/remotes/nosuchremote/x"));
131 
132 		// no such remote too, no branch name either
133 		assertNull(db.getRemoteName("refs/remotes/abranch"));
134 		assertNull(db.shortenRemoteBranchName("refs/remotes/abranch"));
135 
136 		// // local branch
137 		assertNull(db.getRemoteName("refs/heads/abranch"));
138 		assertNull(db.shortenRemoteBranchName("refs/heads/abranch"));
139 	}
140 
141 	@Test
142 	public void testReadAllIncludingSymrefs() throws Exception {
143 		ObjectId masterId = db.resolve("refs/heads/master");
144 		RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
145 		updateRef.setNewObjectId(masterId);
146 		updateRef.setForceUpdate(true);
147 		updateRef.update();
148 		writeSymref("refs/remotes/origin/HEAD",
149 					"refs/remotes/origin/master");
150 
151 		ObjectId r = db.resolve("refs/remotes/origin/HEAD");
152 		assertEquals(masterId, r);
153 
154 		List<Ref> allRefs = db.getRefDatabase().getRefs();
155 		Optional<Ref> refHEAD = allRefs.stream()
156 				.filter(ref -> ref.getName().equals("refs/remotes/origin/HEAD"))
157 				.findAny();
158 		assertTrue(refHEAD.isPresent());
159 		assertEquals(masterId, refHEAD.get().getObjectId());
160 		assertFalse(refHEAD.get().isPeeled());
161 		assertNull(refHEAD.get().getPeeledObjectId());
162 
163 		Optional<Ref> refmaster = allRefs.stream().filter(
164 				ref -> ref.getName().equals("refs/remotes/origin/master"))
165 				.findAny();
166 		assertTrue(refmaster.isPresent());
167 		assertEquals(masterId, refmaster.get().getObjectId());
168 		assertFalse(refmaster.get().isPeeled());
169 		assertNull(refmaster.get().getPeeledObjectId());
170 	}
171 
172 	@Test
173 	public void testReadSymRefToPacked() throws IOException {
174 		writeSymref("HEAD", "refs/heads/b");
175 		Ref ref = db.exactRef("HEAD");
176 		assertEquals(Ref.Storage.LOOSE, ref.getStorage());
177 		assertTrue("is symref", ref.isSymbolic());
178 		ref = ref.getTarget();
179 		assertEquals("refs/heads/b", ref.getName());
180 		assertEquals(Ref.Storage.PACKED, ref.getStorage());
181 	}
182 
183 	@Test
184 	public void testReadSymRefToLoosePacked() throws IOException {
185 		ObjectId pid = db.resolve("refs/heads/master^");
186 		RefUpdate updateRef = db.updateRef("refs/heads/master");
187 		updateRef.setNewObjectId(pid);
188 		updateRef.setForceUpdate(true);
189 		Result update = updateRef.update();
190 		assertEquals(Result.FORCED, update); // internal
191 
192 		writeSymref("HEAD", "refs/heads/master");
193 		Ref ref = db.exactRef("HEAD");
194 		assertEquals(Ref.Storage.LOOSE, ref.getStorage());
195 		ref = ref.getTarget();
196 		assertEquals("refs/heads/master", ref.getName());
197 		assertEquals(Ref.Storage.LOOSE, ref.getStorage());
198 	}
199 
200 	@Test
201 	public void testReadLooseRef() throws IOException {
202 		RefUpdate updateRef = db.updateRef("ref/heads/new");
203 		updateRef.setNewObjectId(db.resolve("refs/heads/master"));
204 		Result update = updateRef.update();
205 		assertEquals(Result.NEW, update);
206 		Ref ref = db.exactRef("ref/heads/new");
207 		assertEquals(Storage.LOOSE, ref.getStorage());
208 	}
209 
210 	@Test
211 	public void testGetShortRef() throws IOException {
212 		Ref ref = db.exactRef("refs/heads/master");
213 		assertEquals("refs/heads/master", ref.getName());
214 		assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
215 	}
216 
217 	@Test
218 	public void testGetShortExactRef() throws IOException {
219 		assertNull(db.getRefDatabase().exactRef("master"));
220 
221 		Ref ref = db.getRefDatabase().exactRef("HEAD");
222 		assertEquals("HEAD", ref.getName());
223 		assertEquals("refs/heads/master", ref.getTarget().getName());
224 		assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
225 	}
226 
227 	@Test
228 	public void testRefsUnderRefs() throws IOException {
229 		ObjectId masterId = db.resolve("refs/heads/master");
230 		writeNewRef("refs/heads/refs/foo/bar", masterId);
231 
232 		assertNull(db.getRefDatabase().exactRef("refs/foo/bar"));
233 
234 		Ref ref = db.findRef("refs/foo/bar");
235 		assertEquals("refs/heads/refs/foo/bar", ref.getName());
236 		assertEquals(db.resolve("refs/heads/master"), ref.getObjectId());
237 	}
238 
239 	@Test
240 	public void testAmbiguousRefsUnderRefs() throws IOException {
241 		ObjectId masterId = db.resolve("refs/heads/master");
242 		writeNewRef("refs/foo/bar", masterId);
243 		writeNewRef("refs/heads/refs/foo/bar", masterId);
244 
245 		Ref exactRef = db.getRefDatabase().exactRef("refs/foo/bar");
246 		assertEquals("refs/foo/bar", exactRef.getName());
247 		assertEquals(masterId, exactRef.getObjectId());
248 
249 		Ref ref = db.findRef("refs/foo/bar");
250 		assertEquals("refs/foo/bar", ref.getName());
251 		assertEquals(masterId, ref.getObjectId());
252 	}
253 
254 	/**
255 	 * Let an "outsider" create a loose ref with the same name as a packed one
256 	 *
257 	 * @throws IOException
258 	 * @throws InterruptedException
259 	 */
260 	@Test
261 	public void testReadLoosePackedRef() throws IOException,
262 			InterruptedException {
263 		Ref ref = db.exactRef("refs/heads/master");
264 		assertEquals(Storage.PACKED, ref.getStorage());
265 		try (FileOutputStream os = new FileOutputStream(
266 				new File(db.getDirectory(), "refs/heads/master"))) {
267 			os.write(ref.getObjectId().name().getBytes(UTF_8));
268 			os.write('\n');
269 		}
270 
271 		ref = db.exactRef("refs/heads/master");
272 		assertEquals(Storage.LOOSE, ref.getStorage());
273 	}
274 
275 	/**
276 	 * Modify a packed ref using the API. This creates a loose ref too, ie.
277 	 * LOOSE_PACKED
278 	 *
279 	 * @throws IOException
280 	 */
281 	@Test
282 	public void testReadSimplePackedRefSameRepo() throws IOException {
283 		Ref ref = db.exactRef("refs/heads/master");
284 		ObjectId pid = db.resolve("refs/heads/master^");
285 		assertEquals(Storage.PACKED, ref.getStorage());
286 		RefUpdate updateRef = db.updateRef("refs/heads/master");
287 		updateRef.setNewObjectId(pid);
288 		updateRef.setForceUpdate(true);
289 		Result update = updateRef.update();
290 		assertEquals(Result.FORCED, update);
291 
292 		ref = db.exactRef("refs/heads/master");
293 		assertEquals(Storage.LOOSE, ref.getStorage());
294 	}
295 
296 	@Test
297 	public void testResolvedNamesBranch() throws IOException {
298 		Ref ref = db.findRef("a");
299 		assertEquals("refs/heads/a", ref.getName());
300 	}
301 
302 	@Test
303 	public void testResolvedSymRef() throws IOException {
304 		Ref ref = db.exactRef(Constants.HEAD);
305 		assertEquals(Constants.HEAD, ref.getName());
306 		assertTrue("is symbolic ref", ref.isSymbolic());
307 		assertSame(Ref.Storage.LOOSE, ref.getStorage());
308 
309 		Ref dst = ref.getTarget();
310 		assertNotNull("has target", dst);
311 		assertEquals("refs/heads/master", dst.getName());
312 
313 		assertSame(dst.getObjectId(), ref.getObjectId());
314 		assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId());
315 		assertEquals(dst.isPeeled(), ref.isPeeled());
316 	}
317 
318 	private static void checkContainsRef(Collection<Ref> haystack, Ref needle) {
319 		for (Ref ref : haystack) {
320 			if (ref.getName().equals(needle.getName()) &&
321 					ref.getObjectId().equals(needle.getObjectId())) {
322 				return;
323 			}
324 		}
325 		fail("list " + haystack + " does not contain ref " + needle);
326 	}
327 
328 	@Test
329 	public void testGetRefsByPrefix() throws IOException {
330 		List<Ref> refs = db.getRefDatabase().getRefsByPrefix("refs/heads/g");
331 		assertEquals(2, refs.size());
332 		checkContainsRef(refs, db.exactRef("refs/heads/g"));
333 		checkContainsRef(refs, db.exactRef("refs/heads/gitlink"));
334 
335 		refs = db.getRefDatabase().getRefsByPrefix("refs/heads/prefix/");
336 		assertEquals(1, refs.size());
337 		checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
338 	}
339 
340 	@Test
341 	public void testGetRefsByPrefixes() throws IOException {
342 		List<Ref> refs = db.getRefDatabase().getRefsByPrefix();
343 		assertEquals(0, refs.size());
344 
345 		refs = db.getRefDatabase().getRefsByPrefix("refs/heads/p",
346 				"refs/tags/A");
347 		assertEquals(3, refs.size());
348 		checkContainsRef(refs, db.exactRef("refs/heads/pa"));
349 		checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
350 		checkContainsRef(refs, db.exactRef("refs/tags/A"));
351 	}
352 
353 	@Test
354 	public void testResolveTipSha1() throws IOException {
355 		ObjectId masterId = db.resolve("refs/heads/master");
356 		Set<Ref> resolved = db.getRefDatabase().getTipsWithSha1(masterId);
357 
358 		assertEquals(2, resolved.size());
359 		checkContainsRef(resolved, db.exactRef("refs/heads/master"));
360 		checkContainsRef(resolved, db.exactRef("HEAD"));
361 
362 		assertEquals(db.getRefDatabase()
363 				.getTipsWithSha1(ObjectId.zeroId()).size(), 0);
364 	}
365 }