1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
75
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
109 assertEquals("master",
110 db.shortenRemoteBranchName("refs/remotes/origin/master"));
111 assertEquals("origin", db.getRemoteName("refs/remotes/origin/master"));
112
113
114 assertEquals("masta/r",
115 db.shortenRemoteBranchName("refs/remotes/origin/masta/r"));
116 assertEquals("origin", db.getRemoteName("refs/remotes/origin/masta/r"));
117
118
119 assertEquals("xmaster",
120 db.shortenRemoteBranchName("refs/remotes/ab/c/xmaster"));
121 assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmaster"));
122
123
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
129 assertNull(db.getRemoteName("refs/remotes/nosuchremote/x"));
130 assertNull(db.shortenRemoteBranchName("refs/remotes/nosuchremote/x"));
131
132
133 assertNull(db.getRemoteName("refs/remotes/abranch"));
134 assertNull(db.shortenRemoteBranchName("refs/remotes/abranch"));
135
136
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);
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
256
257
258
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
277
278
279
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 }