View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
4    * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5    * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.lib;
48  
49  import static org.junit.Assert.assertEquals;
50  import static org.junit.Assert.assertFalse;
51  import static org.junit.Assert.assertNull;
52  import static org.junit.Assert.assertSame;
53  import static org.junit.Assert.assertTrue;
54  import static org.junit.Assert.fail;
55  
56  import java.io.IOException;
57  
58  import org.eclipse.jgit.api.Git;
59  import org.eclipse.jgit.errors.AmbiguousObjectException;
60  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
61  import org.eclipse.jgit.errors.RevisionSyntaxException;
62  import org.eclipse.jgit.revwalk.RevCommit;
63  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
64  import org.junit.Test;
65  
66  public class RepositoryResolveTest extends SampleDataRepositoryTestCase {
67  
68  	@Test
69  	public void testObjectId_existing() throws IOException {
70  		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0").name());
71  	}
72  
73  	@Test
74  	public void testObjectId_nonexisting() throws IOException {
75  		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c1",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c1").name());
76  	}
77  
78  	@Test
79  	public void testObjectId_objectid_implicit_firstparent() throws IOException {
80  		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^").name());
81  		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^").name());
82  		assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^^").name());
83  	}
84  
85  	@Test
86  	public void testObjectId_objectid_self() throws IOException {
87  		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0").name());
88  		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0^0").name());
89  		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^0^0^0").name());
90  	}
91  
92  	@Test
93  	public void testObjectId_objectid_explicit_firstparent() throws IOException {
94  		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1").name());
95  		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1^1").name());
96  		assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1^1^1").name());
97  	}
98  
99  	@Test
100 	public void testObjectId_objectid_explicit_otherparents() throws IOException {
101 		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^1").name());
102 		assertEquals("f73b95671f326616d66b2afb3bdfcdbbce110b44",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^2").name());
103 		assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^3").name());
104 		assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^03").name());
105 	}
106 
107 	@Test
108 	public void testObjectId_objectid_invalid_explicit_parent() throws IOException {
109 		assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4^1").name());
110 		assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4^2"));
111 		assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^0").name());
112 		assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^1"));
113 		assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^2"));
114 	}
115 
116 	@Test
117 	public void testRef_refname() throws IOException {
118 		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("master^0").name());
119 		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("master^").name());
120 		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("refs/heads/master^1").name());
121 	}
122 
123 	@Test
124 	public void testDistance() throws IOException {
125 		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~0").name());
126 		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~1").name());
127 		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~2").name());
128 		assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~3").name());
129 		assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~03").name());
130 		assertEquals("6e1475206e57110fcef4b92320436c1e9872a322",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~").name());
131 		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~~").name());
132 		assertEquals("bab66b48f836ed950c99134ef666436fb07a09a0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~~~").name());
133 		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~~1").name());
134 		assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~~~0").name());
135 	}
136 
137 	@Test
138 	public void testDistance_past_root() throws IOException {
139 		assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~1").name());
140 		assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~~"));
141 		assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4^^"));
142 		assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~2"));
143 		assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~99"));
144 		assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1~~"));
145 		assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^^"));
146 		assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1~2"));
147 		assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1~99"));
148 		assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("master~6").name());
149 		assertNull(db.resolve("master~7"));
150 		assertNull(db.resolve("master~6~"));
151 	}
152 
153 	@Test
154 	public void testTree() throws IOException {
155 		assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{tree}").name());
156 		assertEquals("02ba32d3649e510002c21651936b7077aa75ffa9",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^^{tree}").name());
157 	}
158 
159 	@Test
160 	public void testHEAD() throws IOException {
161 		assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("HEAD^{tree}").name());
162 	}
163 
164 	@Test
165 	public void testDerefCommit() throws IOException {
166 		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{}").name());
167 		assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{commit}").name());
168 		// double deref
169 		assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{commit}^{tree}").name());
170 	}
171 
172 	@Test
173 	public void testDerefTag() throws IOException {
174 		assertEquals("17768080a2318cd89bba4c8b87834401e2095703",db.resolve("refs/tags/B").name());
175 		assertEquals("d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",db.resolve("refs/tags/B^{commit}").name());
176 		assertEquals("032c063ce34486359e3ee3d4f9e5c225b9e1a4c2",db.resolve("refs/tags/B10th").name());
177 		assertEquals("d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",db.resolve("refs/tags/B10th^{commit}").name());
178 		assertEquals("d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",db.resolve("refs/tags/B10th^{}").name());
179 		assertEquals("d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",db.resolve("refs/tags/B10th^0").name());
180 		assertEquals("d86a2aada2f5e7ccf6f11880bfb9ab404e8a8864",db.resolve("refs/tags/B10th~0").name());
181 		assertEquals("0966a434eb1a025db6b71485ab63a3bfbea520b6",db.resolve("refs/tags/B10th^").name());
182 		assertEquals("2c349335b7f797072cf729c4f3bb0914ecb6dec9",db.resolve("refs/tags/B10th^^").name());
183 		assertEquals("0966a434eb1a025db6b71485ab63a3bfbea520b6",db.resolve("refs/tags/B10th^1").name());
184 		assertEquals("0966a434eb1a025db6b71485ab63a3bfbea520b6",db.resolve("refs/tags/B10th~1").name());
185 		assertEquals("2c349335b7f797072cf729c4f3bb0914ecb6dec9",db.resolve("refs/tags/B10th~2").name());
186 		assertEquals("2c349335b7f797072cf729c4f3bb0914ecb6dec9",db.resolve("refs/tags/B10th^~1").name());
187 	}
188 
189 	@Test
190 	public void testDerefBlob() throws IOException {
191 		assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("spearce-gpg-pub^{}").name());
192 		assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("spearce-gpg-pub^{blob}").name());
193 		assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("fd608fbe625a2b456d9f15c2b1dc41f252057dd7^{}").name());
194 		assertEquals("fd608fbe625a2b456d9f15c2b1dc41f252057dd7",db.resolve("fd608fbe625a2b456d9f15c2b1dc41f252057dd7^{blob}").name());
195 	}
196 
197 	@Test
198 	public void testDerefTree() throws IOException {
199 		assertEquals("032c063ce34486359e3ee3d4f9e5c225b9e1a4c2",db.resolve("refs/tags/B10th").name());
200 		assertEquals("856ec208ae6cadac25a6d74f19b12bb27a24fe24",db.resolve("032c063ce34486359e3ee3d4f9e5c225b9e1a4c2^{tree}").name());
201 		assertEquals("856ec208ae6cadac25a6d74f19b12bb27a24fe24",db.resolve("refs/tags/B10th^{tree}").name());
202 	}
203 
204 	@Test
205 	public void testParseGitDescribeOutput() throws IOException {
206 		ObjectId exp = db.resolve("b");
207 		assertEquals(exp, db.resolve("B-g7f82283")); // old style
208 		assertEquals(exp, db.resolve("B-6-g7f82283")); // new style
209 
210 		assertEquals(exp, db.resolve("B-6-g7f82283^0"));
211 		assertEquals(exp, db.resolve("B-6-g7f82283^{commit}"));
212 
213 		try {
214 			db.resolve("B-6-g7f82283^{blob}");
215 			fail("expected IncorrectObjectTypeException");
216 		} catch (IncorrectObjectTypeException badType) {
217 			// Expected
218 		}
219 
220 		assertEquals(db.resolve("b^1"), db.resolve("B-6-g7f82283^1"));
221 		assertEquals(db.resolve("b~2"), db.resolve("B-6-g7f82283~2"));
222 	}
223 
224 	@Test
225 	public void testParseNonGitDescribe() throws IOException {
226 		ObjectId id = id("49322bb17d3acc9146f98c97d078513228bbf3c0");
227 		RefUpdate ru = db.updateRef("refs/heads/foo-g032c");
228 		ru.setNewObjectId(id);
229 		assertSame(RefUpdate.Result.NEW, ru.update());
230 
231 		assertEquals(id, db.resolve("refs/heads/foo-g032c"));
232 		assertEquals(id, db.resolve("foo-g032c"));
233 		assertNull(db.resolve("foo-g032"));
234 		assertNull(db.resolve("foo-g03"));
235 		assertNull(db.resolve("foo-g0"));
236 		assertNull(db.resolve("foo-g"));
237 
238 		ru = db.updateRef("refs/heads/foo-g032c-dev");
239 		ru.setNewObjectId(id);
240 		assertSame(RefUpdate.Result.NEW, ru.update());
241 
242 		assertEquals(id, db.resolve("refs/heads/foo-g032c-dev"));
243 		assertEquals(id, db.resolve("foo-g032c-dev"));
244 	}
245 
246 	@Test
247 	public void testParseLookupPath() throws IOException {
248 		ObjectId b2_txt = id("10da5895682013006950e7da534b705252b03be6");
249 		ObjectId b3_b2_txt = id("e6bfff5c1d0f0ecd501552b43a1e13d8008abc31");
250 		ObjectId b_root = id("acd0220f06f7e4db50ea5ba242f0dfed297b27af");
251 		ObjectId master_txt = id("82b1d08466e9505f8666b778744f9a3471a70c81");
252 
253 		assertEquals(b2_txt, db.resolve("b:b/b2.txt"));
254 		assertEquals(b_root, db.resolve("b:"));
255 		assertEquals(id("6020a3b8d5d636e549ccbd0c53e2764684bb3125"),
256 				db.resolve("master:"));
257 		assertEquals(id("10da5895682013006950e7da534b705252b03be6"),
258 				db.resolve("master:b/b2.txt"));
259 		assertEquals(master_txt, db.resolve(":master.txt"));
260 		assertEquals(b3_b2_txt, db.resolve("b~3:b/b2.txt"));
261 
262 		assertNull("no FOO", db.resolve("b:FOO"));
263 		assertNull("no b/FOO", db.resolve("b:b/FOO"));
264 		assertNull("no b/FOO", db.resolve(":b/FOO"));
265 		assertNull("no not-a-branch:", db.resolve("not-a-branch:"));
266 	}
267 
268 	@Test
269 	public void resolveExprSimple() throws Exception {
270 		try (Git git = new Git(db)) {
271 			writeTrashFile("file.txt", "content");
272 			git.add().addFilepattern("file.txt").call();
273 			git.commit().setMessage("create file").call();
274 			assertEquals("master", db.simplify("master"));
275 			assertEquals("refs/heads/master", db.simplify("refs/heads/master"));
276 			assertEquals("HEAD", db.simplify("HEAD"));
277 		}
278 	}
279 
280 	@Test
281 	public void resolveUpstream() throws Exception {
282 		try (Git git = new Git(db)) {
283 			writeTrashFile("file.txt", "content");
284 			git.add().addFilepattern("file.txt").call();
285 			RevCommit c1 = git.commit().setMessage("create file").call();
286 			writeTrashFile("file2.txt", "content");
287 			RefUpdate updateRemoteRef = db.updateRef("refs/remotes/origin/main");
288 			updateRemoteRef.setNewObjectId(c1);
289 			updateRemoteRef.update();
290 			db.getConfig().setString("branch", "master", "remote", "origin");
291 			db.getConfig()
292 					.setString("branch", "master", "merge", "refs/heads/main");
293 			db.getConfig().setString("remote", "origin", "url",
294 					"git://example.com/here");
295 			db.getConfig().setString("remote", "origin", "fetch",
296 					"+refs/heads/*:refs/remotes/origin/*");
297 			git.add().addFilepattern("file2.txt").call();
298 			git.commit().setMessage("create file").call();
299 			assertEquals("refs/remotes/origin/main", db.simplify("@{upstream}"));
300 		}
301 	}
302 
303 	@Test
304 	public void invalidNames() throws AmbiguousObjectException, IOException {
305 		assertTrue(Repository.isValidRefName("x/a"));
306 		assertTrue(Repository.isValidRefName("x/a.b"));
307 		assertTrue(Repository.isValidRefName("x/a@b"));
308 		assertTrue(Repository.isValidRefName("x/a@b{x}"));
309 		assertTrue(Repository.isValidRefName("x/a/b"));
310 		assertTrue(Repository.isValidRefName("x/a]b")); // odd, yes..
311 		assertTrue(Repository.isValidRefName("x/\u00a0")); // unicode is fine,
312 															// even hard space
313 		assertFalse(Repository.isValidRefName("x/.a"));
314 		assertFalse(Repository.isValidRefName("x/a."));
315 		assertFalse(Repository.isValidRefName("x/a..b"));
316 		assertFalse(Repository.isValidRefName("x//a"));
317 		assertFalse(Repository.isValidRefName("x/a/"));
318 		assertFalse(Repository.isValidRefName("x/a//b"));
319 		assertFalse(Repository.isValidRefName("x/a[b"));
320 		assertFalse(Repository.isValidRefName("x/a^b"));
321 		assertFalse(Repository.isValidRefName("x/a*b"));
322 		assertFalse(Repository.isValidRefName("x/a?b"));
323 		assertFalse(Repository.isValidRefName("x/a~1"));
324 		assertFalse(Repository.isValidRefName("x/a\\b"));
325 		assertFalse(Repository.isValidRefName("x/a\u0000"));
326 
327 		assertUnparseable(".");
328 		assertUnparseable("x@{3");
329 		assertUnparseable("x[b");
330 		assertUnparseable("x y");
331 		assertUnparseable("x.");
332 		assertUnparseable(".x");
333 		assertUnparseable("a..b");
334 		assertUnparseable("x\\b");
335 		assertUnparseable("a~b");
336 		assertUnparseable("a^b");
337 		assertUnparseable("a\u0000");
338 	}
339 
340 	private void assertUnparseable(String s) throws AmbiguousObjectException,
341 			IOException {
342 		try {
343 			db.resolve(s);
344 			fail("'" + s + "' should be unparseable");
345 		} catch (RevisionSyntaxException e) {
346 			// good
347 		}
348 	}
349 
350 	private static ObjectId id(String name) {
351 		return ObjectId.fromString(name);
352 	}
353 }