1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.file;
12
13 import static org.eclipse.jgit.lib.Constants.HEAD;
14 import static org.eclipse.jgit.lib.Constants.R_HEADS;
15 import static org.eclipse.jgit.lib.Constants.R_TAGS;
16 import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
17 import static org.eclipse.jgit.lib.Ref.Storage.NEW;
18 import static org.junit.Assert.assertArrayEquals;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNotSame;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.time.Instant;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.atomic.AtomicInteger;
36 import java.util.concurrent.atomic.AtomicReference;
37
38 import org.eclipse.jgit.errors.LockFailedException;
39 import org.eclipse.jgit.events.ListenerHandle;
40 import org.eclipse.jgit.events.RefsChangedEvent;
41 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
42 import org.eclipse.jgit.junit.Repeat;
43 import org.eclipse.jgit.junit.TestRepository;
44 import org.eclipse.jgit.lib.AnyObjectId;
45 import org.eclipse.jgit.lib.Ref;
46 import org.eclipse.jgit.lib.Ref.Storage;
47 import org.eclipse.jgit.lib.RefDatabase;
48 import org.eclipse.jgit.lib.Repository;
49 import org.eclipse.jgit.revwalk.RevCommit;
50 import org.eclipse.jgit.revwalk.RevTag;
51 import org.eclipse.jgit.util.FS;
52 import org.junit.Before;
53 import org.junit.Test;
54
55 @SuppressWarnings("boxing")
56 public class RefDirectoryTest extends LocalDiskRepositoryTestCase {
57 private Repository diskRepo;
58
59 private TestRepository<Repository> repo;
60
61 private RefDirectory refdir;
62
63 private RevCommit A;
64
65 private RevCommit B;
66
67 private RevTag v1_0;
68
69 @Override
70 @Before
71 public void setUp() throws Exception {
72 super.setUp();
73
74 diskRepo = createBareRepository();
75 refdir = (RefDirectory) diskRepo.getRefDatabase();
76
77 repo = new TestRepository<>(diskRepo);
78 A = repo.commit().create();
79 B = repo.commit(repo.getRevWalk().parseCommit(A));
80 v1_0 = repo.tag("v1_0", B);
81 repo.getRevWalk().parseBody(v1_0);
82 }
83
84 @Test
85 public void testCreate() throws IOException {
86
87 File d = diskRepo.getDirectory();
88 assertSame(diskRepo, refdir.getRepository());
89
90 assertTrue(new File(d, "refs").isDirectory());
91 assertTrue(new File(d, "logs").isDirectory());
92 assertTrue(new File(d, "logs/refs").isDirectory());
93 assertFalse(new File(d, "packed-refs").exists());
94
95 assertTrue(new File(d, "refs/heads").isDirectory());
96 assertTrue(new File(d, "refs/tags").isDirectory());
97 assertEquals(2, new File(d, "refs").list().length);
98 assertEquals(0, new File(d, "refs/heads").list().length);
99 assertEquals(0, new File(d, "refs/tags").list().length);
100
101 assertTrue(new File(d, "logs/refs/heads").isDirectory());
102 assertFalse(new File(d, "logs/HEAD").exists());
103 assertEquals(0, new File(d, "logs/refs/heads").list().length);
104
105 assertEquals("ref: refs/heads/master\n", read(new File(d, HEAD)));
106 }
107
108 @Test(expected = UnsupportedOperationException.class)
109 public void testVersioningNotImplemented_exactRef() throws IOException {
110 assertFalse(refdir.hasVersioning());
111
112 Ref ref = refdir.exactRef(HEAD);
113 assertNotNull(ref);
114 ref.getUpdateIndex();
115 }
116
117 @Test
118 public void testVersioningNotImplemented_getRefs() throws Exception {
119 assertFalse(refdir.hasVersioning());
120
121 RevCommit C = repo.commit().parent(B).create();
122 repo.update("master", C);
123 List<Ref> refs = refdir.getRefs();
124
125 for (Ref ref : refs) {
126 try {
127 ref.getUpdateIndex();
128 fail("FS doesn't implement ref versioning");
129 } catch (UnsupportedOperationException e) {
130
131 }
132 }
133 }
134
135 @Test
136 public void testGetRefs_EmptyDatabase() throws IOException {
137 Map<String, Ref> all;
138
139 all = refdir.getRefs(RefDatabase.ALL);
140 assertTrue("no references", all.isEmpty());
141
142 all = refdir.getRefs(R_HEADS);
143 assertTrue("no references", all.isEmpty());
144
145 all = refdir.getRefs(R_TAGS);
146 assertTrue("no references", all.isEmpty());
147 }
148
149 @Test
150 public void testGetRefs_HeadOnOneBranch() throws IOException {
151 Map<String, Ref> all;
152 Ref head, master;
153
154 writeLooseRef("refs/heads/master", A);
155
156 all = refdir.getRefs(RefDatabase.ALL);
157 assertEquals(2, all.size());
158 assertTrue("has HEAD", all.containsKey(HEAD));
159 assertTrue("has master", all.containsKey("refs/heads/master"));
160
161 head = all.get(HEAD);
162 master = all.get("refs/heads/master");
163
164 assertEquals(HEAD, head.getName());
165 assertTrue(head.isSymbolic());
166 assertSame(LOOSE, head.getStorage());
167 assertSame("uses same ref as target", master, head.getTarget());
168
169 assertEquals("refs/heads/master", master.getName());
170 assertFalse(master.isSymbolic());
171 assertSame(LOOSE, master.getStorage());
172 assertEquals(A, master.getObjectId());
173 }
174
175 @Test
176 public void testGetRefs_DeatchedHead1() throws IOException {
177 Map<String, Ref> all;
178 Ref head;
179
180 writeLooseRef(HEAD, A);
181
182 all = refdir.getRefs(RefDatabase.ALL);
183 assertEquals(1, all.size());
184 assertTrue("has HEAD", all.containsKey(HEAD));
185
186 head = all.get(HEAD);
187
188 assertEquals(HEAD, head.getName());
189 assertFalse(head.isSymbolic());
190 assertSame(LOOSE, head.getStorage());
191 assertEquals(A, head.getObjectId());
192 }
193
194 @Test
195 public void testGetRefs_DeatchedHead2() throws IOException {
196 Map<String, Ref> all;
197 Ref head, master;
198
199 writeLooseRef(HEAD, A);
200 writeLooseRef("refs/heads/master", B);
201
202 all = refdir.getRefs(RefDatabase.ALL);
203 assertEquals(2, all.size());
204
205 head = all.get(HEAD);
206 master = all.get("refs/heads/master");
207
208 assertEquals(HEAD, head.getName());
209 assertFalse(head.isSymbolic());
210 assertSame(LOOSE, head.getStorage());
211 assertEquals(A, head.getObjectId());
212
213 assertEquals("refs/heads/master", master.getName());
214 assertFalse(master.isSymbolic());
215 assertSame(LOOSE, master.getStorage());
216 assertEquals(B, master.getObjectId());
217 }
218
219 @Test
220 public void testGetRefs_DeeplyNestedBranch() throws IOException {
221 String name = "refs/heads/a/b/c/d/e/f/g/h/i/j/k";
222 Map<String, Ref> all;
223 Ref r;
224
225 writeLooseRef(name, A);
226
227 all = refdir.getRefs(RefDatabase.ALL);
228 assertEquals(1, all.size());
229
230 r = all.get(name);
231 assertEquals(name, r.getName());
232 assertFalse(r.isSymbolic());
233 assertSame(LOOSE, r.getStorage());
234 assertEquals(A, r.getObjectId());
235 }
236
237 @Test
238 public void testGetRefs_HeadBranchNotBorn() throws IOException {
239 Map<String, Ref> all;
240 Ref a, b;
241
242 writeLooseRef("refs/heads/A", A);
243 writeLooseRef("refs/heads/B", B);
244
245 all = refdir.getRefs(RefDatabase.ALL);
246 assertEquals(2, all.size());
247 assertFalse("no HEAD", all.containsKey(HEAD));
248
249 a = all.get("refs/heads/A");
250 b = all.get("refs/heads/B");
251
252 assertEquals(A, a.getObjectId());
253 assertEquals(B, b.getObjectId());
254
255 assertEquals("refs/heads/A", a.getName());
256 assertEquals("refs/heads/B", b.getName());
257 }
258
259 @Test
260 public void testGetRefs_LooseOverridesPacked() throws IOException {
261 Map<String, Ref> heads;
262 Ref a;
263
264 writeLooseRef("refs/heads/master", B);
265 writePackedRef("refs/heads/master", A);
266
267 heads = refdir.getRefs(R_HEADS);
268 assertEquals(1, heads.size());
269
270 a = heads.get("master");
271 assertEquals("refs/heads/master", a.getName());
272 assertEquals(B, a.getObjectId());
273 }
274
275 @Test
276 public void testGetRefs_IgnoresGarbageRef1() throws IOException {
277 Map<String, Ref> heads;
278 Ref a;
279
280 writeLooseRef("refs/heads/A", A);
281 write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
282
283 heads = refdir.getRefs(RefDatabase.ALL);
284 assertEquals(1, heads.size());
285
286 a = heads.get("refs/heads/A");
287 assertEquals("refs/heads/A", a.getName());
288 assertEquals(A, a.getObjectId());
289 }
290
291 @Test
292 public void testGetRefs_IgnoresGarbageRef2() throws IOException {
293 Map<String, Ref> heads;
294 Ref a;
295
296 writeLooseRef("refs/heads/A", A);
297 write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "");
298
299 heads = refdir.getRefs(RefDatabase.ALL);
300 assertEquals(1, heads.size());
301
302 a = heads.get("refs/heads/A");
303 assertEquals("refs/heads/A", a.getName());
304 assertEquals(A, a.getObjectId());
305 }
306
307 @Test
308 public void testGetRefs_IgnoresGarbageRef3() throws IOException {
309 Map<String, Ref> heads;
310 Ref a;
311
312 writeLooseRef("refs/heads/A", A);
313 write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "\n");
314
315 heads = refdir.getRefs(RefDatabase.ALL);
316 assertEquals(1, heads.size());
317
318 a = heads.get("refs/heads/A");
319 assertEquals("refs/heads/A", a.getName());
320 assertEquals(A, a.getObjectId());
321 }
322
323 @Test
324 public void testGetRefs_IgnoresGarbageRef4() throws IOException {
325 Map<String, Ref> heads;
326 Ref a, b, c;
327
328 writeLooseRef("refs/heads/A", A);
329 writeLooseRef("refs/heads/B", B);
330 writeLooseRef("refs/heads/C", A);
331 heads = refdir.getRefs(RefDatabase.ALL);
332 assertEquals(3, heads.size());
333 assertTrue(heads.containsKey("refs/heads/A"));
334 assertTrue(heads.containsKey("refs/heads/B"));
335 assertTrue(heads.containsKey("refs/heads/C"));
336
337 writeLooseRef("refs/heads/B", "FAIL\n");
338
339 heads = refdir.getRefs(RefDatabase.ALL);
340 assertEquals(2, heads.size());
341
342 a = heads.get("refs/heads/A");
343 b = heads.get("refs/heads/B");
344 c = heads.get("refs/heads/C");
345
346 assertEquals("refs/heads/A", a.getName());
347 assertEquals(A, a.getObjectId());
348
349 assertNull("no refs/heads/B", b);
350
351 assertEquals("refs/heads/C", c.getName());
352 assertEquals(A, c.getObjectId());
353 }
354
355 @Test
356 public void testFirstExactRef_IgnoresGarbageRef() throws IOException {
357 writeLooseRef("refs/heads/A", A);
358 write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
359
360 Ref a = refdir.firstExactRef("refs/heads/bad", "refs/heads/A");
361 assertEquals("refs/heads/A", a.getName());
362 assertEquals(A, a.getObjectId());
363 }
364
365 @Test
366 public void testExactRef_IgnoresGarbageRef() throws IOException {
367 writeLooseRef("refs/heads/A", A);
368 write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
369
370 Map<String, Ref> refs =
371 refdir.exactRef("refs/heads/bad", "refs/heads/A");
372
373 assertNull("no refs/heads/bad", refs.get("refs/heads/bad"));
374
375 Ref a = refs.get("refs/heads/A");
376 assertEquals("refs/heads/A", a.getName());
377 assertEquals(A, a.getObjectId());
378
379 assertEquals(1, refs.size());
380 }
381
382 @Test
383 public void testGetRefs_InvalidName() throws IOException {
384 writeLooseRef("refs/heads/A", A);
385
386 assertTrue("empty refs/heads", refdir.getRefs("refs/heads").isEmpty());
387 assertTrue("empty objects", refdir.getRefs("objects").isEmpty());
388 assertTrue("empty objects/", refdir.getRefs("objects/").isEmpty());
389 }
390
391 @Test
392 public void testReadNotExistingBranchConfig() throws IOException {
393 assertNull("find branch config", refdir.findRef("config"));
394 assertNull("find branch config", refdir.findRef("refs/heads/config"));
395 }
396
397 @Test
398 public void testReadBranchConfig() throws IOException {
399 writeLooseRef("refs/heads/config", A);
400
401 assertNotNull("find branch config", refdir.findRef("config"));
402 }
403
404 @Test
405 public void testGetRefs_HeadsOnly_AllLoose() throws IOException {
406 Map<String, Ref> heads;
407 Ref a, b;
408
409 writeLooseRef("refs/heads/A", A);
410 writeLooseRef("refs/heads/B", B);
411 writeLooseRef("refs/tags/v1.0", v1_0);
412
413 heads = refdir.getRefs(R_HEADS);
414 assertEquals(2, heads.size());
415
416 a = heads.get("A");
417 b = heads.get("B");
418
419 assertEquals("refs/heads/A", a.getName());
420 assertEquals("refs/heads/B", b.getName());
421
422 assertEquals(A, a.getObjectId());
423 assertEquals(B, b.getObjectId());
424 }
425
426 @Test
427 public void testGetRefs_HeadsOnly_AllPacked1() throws IOException {
428 Map<String, Ref> heads;
429 Ref a;
430
431 deleteLooseRef(HEAD);
432 writePackedRef("refs/heads/A", A);
433
434 heads = refdir.getRefs(R_HEADS);
435 assertEquals(1, heads.size());
436
437 a = heads.get("A");
438
439 assertEquals("refs/heads/A", a.getName());
440 assertEquals(A, a.getObjectId());
441 }
442
443 @Test
444 public void testGetRefs_HeadsOnly_SymrefToPacked() throws IOException {
445 Map<String, Ref> heads;
446 Ref master, other;
447
448 writeLooseRef("refs/heads/other", "ref: refs/heads/master\n");
449 writePackedRef("refs/heads/master", A);
450
451 heads = refdir.getRefs(R_HEADS);
452 assertEquals(2, heads.size());
453
454 master = heads.get("master");
455 other = heads.get("other");
456
457 assertEquals("refs/heads/master", master.getName());
458 assertEquals(A, master.getObjectId());
459
460 assertEquals("refs/heads/other", other.getName());
461 assertEquals(A, other.getObjectId());
462 assertSame(master, other.getTarget());
463 }
464
465 @Test
466 public void testGetRefs_HeadsOnly_Mixed() throws IOException {
467 Map<String, Ref> heads;
468 Ref a, b;
469
470 writeLooseRef("refs/heads/A", A);
471 writeLooseRef("refs/heads/B", B);
472 writePackedRef("refs/tags/v1.0", v1_0);
473
474 heads = refdir.getRefs(R_HEADS);
475 assertEquals(2, heads.size());
476
477 a = heads.get("A");
478 b = heads.get("B");
479
480 assertEquals("refs/heads/A", a.getName());
481 assertEquals("refs/heads/B", b.getName());
482
483 assertEquals(A, a.getObjectId());
484 assertEquals(B, b.getObjectId());
485 }
486
487 @Test
488 public void testFirstExactRef_Mixed() throws IOException {
489 writeLooseRef("refs/heads/A", A);
490 writePackedRef("refs/tags/v1.0", v1_0);
491
492 Ref a = refdir.firstExactRef("refs/heads/A", "refs/tags/v1.0");
493 Ref one = refdir.firstExactRef("refs/tags/v1.0", "refs/heads/A");
494
495 assertEquals("refs/heads/A", a.getName());
496 assertEquals("refs/tags/v1.0", one.getName());
497
498 assertEquals(A, a.getObjectId());
499 assertEquals(v1_0, one.getObjectId());
500 }
501
502 @Test
503 public void testGetRefs_TagsOnly_AllLoose() throws IOException {
504 Map<String, Ref> tags;
505 Ref a;
506
507 writeLooseRef("refs/heads/A", A);
508 writeLooseRef("refs/tags/v1.0", v1_0);
509
510 tags = refdir.getRefs(R_TAGS);
511 assertEquals(1, tags.size());
512
513 a = tags.get("v1.0");
514
515 assertEquals("refs/tags/v1.0", a.getName());
516 assertEquals(v1_0, a.getObjectId());
517 }
518
519 @Test
520 public void testGetRefs_LooseSortedCorrectly() throws IOException {
521 Map<String, Ref> refs;
522
523 writeLooseRef("refs/heads/project1/A", A);
524 writeLooseRef("refs/heads/project1-B", B);
525
526 refs = refdir.getRefs(RefDatabase.ALL);
527 assertEquals(2, refs.size());
528 assertEquals(A, refs.get("refs/heads/project1/A").getObjectId());
529 assertEquals(B, refs.get("refs/heads/project1-B").getObjectId());
530 }
531
532 @Test
533 public void testGetRefs_LooseSorting_Bug_348834() throws IOException {
534 Map<String, Ref> refs;
535
536 writeLooseRef("refs/heads/my/a+b", A);
537 writeLooseRef("refs/heads/my/a/b/c", B);
538
539 final int[] count = new int[1];
540
541 ListenerHandle listener = Repository.getGlobalListenerList()
542 .addRefsChangedListener((RefsChangedEvent event) -> {
543 count[0]++;
544 });
545
546 refs = refdir.getRefs(RefDatabase.ALL);
547 refs = refdir.getRefs(RefDatabase.ALL);
548 listener.remove();
549 assertEquals(1, count[0]);
550 assertEquals(2, refs.size());
551 assertEquals(A, refs.get("refs/heads/my/a+b").getObjectId());
552 assertEquals(B, refs.get("refs/heads/my/a/b/c").getObjectId());
553
554 }
555
556 @Test
557 public void testGetRefs_TagsOnly_AllPacked() throws IOException {
558 Map<String, Ref> tags;
559 Ref a;
560
561 deleteLooseRef(HEAD);
562 writePackedRef("refs/tags/v1.0", v1_0);
563
564 tags = refdir.getRefs(R_TAGS);
565 assertEquals(1, tags.size());
566
567 a = tags.get("v1.0");
568
569 assertEquals("refs/tags/v1.0", a.getName());
570 assertEquals(v1_0, a.getObjectId());
571 }
572
573 @Test
574 public void testGetRefs_DiscoversNewLoose1() throws IOException {
575 Map<String, Ref> orig, next;
576 Ref orig_r, next_r;
577
578 writeLooseRef("refs/heads/master", A);
579 orig = refdir.getRefs(RefDatabase.ALL);
580
581 writeLooseRef("refs/heads/next", B);
582 next = refdir.getRefs(RefDatabase.ALL);
583
584 assertEquals(2, orig.size());
585 assertEquals(3, next.size());
586
587 assertFalse(orig.containsKey("refs/heads/next"));
588 assertTrue(next.containsKey("refs/heads/next"));
589
590 orig_r = orig.get("refs/heads/master");
591 next_r = next.get("refs/heads/master");
592 assertEquals(A, orig_r.getObjectId());
593 assertSame("uses cached instance", orig_r, next_r);
594 assertSame("same HEAD", orig_r, orig.get(HEAD).getTarget());
595 assertSame("same HEAD", orig_r, next.get(HEAD).getTarget());
596
597 next_r = next.get("refs/heads/next");
598 assertSame(LOOSE, next_r.getStorage());
599 assertEquals(B, next_r.getObjectId());
600 }
601
602 @Test
603 public void testGetRefs_DiscoversNewLoose2() throws IOException {
604 Map<String, Ref> orig, next, news;
605
606 writeLooseRef("refs/heads/pu", A);
607 orig = refdir.getRefs(RefDatabase.ALL);
608
609 writeLooseRef("refs/heads/new/B", B);
610 news = refdir.getRefs("refs/heads/new/");
611 next = refdir.getRefs(RefDatabase.ALL);
612
613 assertEquals(1, orig.size());
614 assertEquals(2, next.size());
615 assertEquals(1, news.size());
616
617 assertTrue(orig.containsKey("refs/heads/pu"));
618 assertTrue(next.containsKey("refs/heads/pu"));
619 assertFalse(news.containsKey("refs/heads/pu"));
620
621 assertFalse(orig.containsKey("refs/heads/new/B"));
622 assertTrue(next.containsKey("refs/heads/new/B"));
623 assertTrue(news.containsKey("B"));
624 }
625
626 @Test
627 public void testGetRefs_DiscoversModifiedLoose() throws IOException {
628 Map<String, Ref> all;
629
630 writeLooseRef("refs/heads/master", A);
631 all = refdir.getRefs(RefDatabase.ALL);
632 assertEquals(A, all.get(HEAD).getObjectId());
633
634 writeLooseRef("refs/heads/master", B);
635 all = refdir.getRefs(RefDatabase.ALL);
636 assertEquals(B, all.get(HEAD).getObjectId());
637 }
638
639 @Repeat(n = 100, abortOnFailure = false)
640 @Test
641 public void testFindRef_DiscoversModifiedLoose() throws IOException {
642 Map<String, Ref> all;
643
644 writeLooseRef("refs/heads/master", A);
645 all = refdir.getRefs(RefDatabase.ALL);
646 assertEquals(A, all.get(HEAD).getObjectId());
647
648 writeLooseRef("refs/heads/master", B);
649
650 Ref master = refdir.findRef("refs/heads/master");
651 assertEquals(B, master.getObjectId());
652 }
653
654 @Test
655 public void testGetRefs_DiscoversDeletedLoose1() throws IOException {
656 Map<String, Ref> orig, next;
657 Ref orig_r, next_r;
658
659 writeLooseRef("refs/heads/B", B);
660 writeLooseRef("refs/heads/master", A);
661 orig = refdir.getRefs(RefDatabase.ALL);
662
663 deleteLooseRef("refs/heads/B");
664 next = refdir.getRefs(RefDatabase.ALL);
665
666 assertEquals(3, orig.size());
667 assertEquals(2, next.size());
668
669 assertTrue(orig.containsKey("refs/heads/B"));
670 assertFalse(next.containsKey("refs/heads/B"));
671
672 orig_r = orig.get("refs/heads/master");
673 next_r = next.get("refs/heads/master");
674 assertEquals(A, orig_r.getObjectId());
675 assertSame("uses cached instance", orig_r, next_r);
676 assertSame("same HEAD", orig_r, orig.get(HEAD).getTarget());
677 assertSame("same HEAD", orig_r, next.get(HEAD).getTarget());
678
679 orig_r = orig.get("refs/heads/B");
680 assertSame(LOOSE, orig_r.getStorage());
681 assertEquals(B, orig_r.getObjectId());
682 }
683
684 @Test
685 public void testFindRef_DiscoversDeletedLoose() throws IOException {
686 Map<String, Ref> all;
687
688 writeLooseRef("refs/heads/master", A);
689 all = refdir.getRefs(RefDatabase.ALL);
690 assertEquals(A, all.get(HEAD).getObjectId());
691
692 deleteLooseRef("refs/heads/master");
693 assertNull(refdir.findRef("refs/heads/master"));
694 assertTrue(refdir.getRefs(RefDatabase.ALL).isEmpty());
695 }
696
697 @Test
698 public void testGetRefs_DiscoversDeletedLoose2() throws IOException {
699 Map<String, Ref> orig, next;
700
701 writeLooseRef("refs/heads/master", A);
702 writeLooseRef("refs/heads/pu", B);
703 orig = refdir.getRefs(RefDatabase.ALL);
704
705 deleteLooseRef("refs/heads/pu");
706 next = refdir.getRefs(RefDatabase.ALL);
707
708 assertEquals(3, orig.size());
709 assertEquals(2, next.size());
710
711 assertTrue(orig.containsKey("refs/heads/pu"));
712 assertFalse(next.containsKey("refs/heads/pu"));
713 }
714
715 @Test
716 public void testGetRefs_DiscoversDeletedLoose3() throws IOException {
717 Map<String, Ref> orig, next;
718
719 writeLooseRef("refs/heads/master", A);
720 writeLooseRef("refs/heads/next", B);
721 writeLooseRef("refs/heads/pu", B);
722 writeLooseRef("refs/tags/v1.0", v1_0);
723 orig = refdir.getRefs(RefDatabase.ALL);
724
725 deleteLooseRef("refs/heads/pu");
726 deleteLooseRef("refs/heads/next");
727 next = refdir.getRefs(RefDatabase.ALL);
728
729 assertEquals(5, orig.size());
730 assertEquals(3, next.size());
731
732 assertTrue(orig.containsKey("refs/heads/pu"));
733 assertTrue(orig.containsKey("refs/heads/next"));
734 assertFalse(next.containsKey("refs/heads/pu"));
735 assertFalse(next.containsKey("refs/heads/next"));
736 }
737
738 @Test
739 public void testGetRefs_DiscoversDeletedLoose4() throws IOException {
740 Map<String, Ref> orig, next;
741 Ref orig_r, next_r;
742
743 writeLooseRef("refs/heads/B", B);
744 writeLooseRef("refs/heads/master", A);
745 orig = refdir.getRefs(RefDatabase.ALL);
746
747 deleteLooseRef("refs/heads/master");
748 next = refdir.getRefs("refs/heads/");
749
750 assertEquals(3, orig.size());
751 assertEquals(1, next.size());
752
753 assertTrue(orig.containsKey("refs/heads/B"));
754 assertTrue(orig.containsKey("refs/heads/master"));
755 assertTrue(next.containsKey("B"));
756 assertFalse(next.containsKey("master"));
757
758 orig_r = orig.get("refs/heads/B");
759 next_r = next.get("B");
760 assertEquals(B, orig_r.getObjectId());
761 assertSame("uses cached instance", orig_r, next_r);
762 }
763
764 @Test
765 public void testGetRefs_DiscoversDeletedLoose5() throws IOException {
766 Map<String, Ref> orig, next;
767
768 writeLooseRef("refs/heads/master", A);
769 writeLooseRef("refs/heads/pu", B);
770 orig = refdir.getRefs(RefDatabase.ALL);
771
772 deleteLooseRef("refs/heads/pu");
773 writeLooseRef("refs/tags/v1.0", v1_0);
774 next = refdir.getRefs(RefDatabase.ALL);
775
776 assertEquals(3, orig.size());
777 assertEquals(3, next.size());
778
779 assertTrue(orig.containsKey("refs/heads/pu"));
780 assertFalse(orig.containsKey("refs/tags/v1.0"));
781 assertFalse(next.containsKey("refs/heads/pu"));
782 assertTrue(next.containsKey("refs/tags/v1.0"));
783 }
784
785 @Test
786 public void testGetRefs_SkipsLockFiles() throws IOException {
787 Map<String, Ref> all;
788
789 writeLooseRef("refs/heads/master", A);
790 writeLooseRef("refs/heads/pu.lock", B);
791 all = refdir.getRefs(RefDatabase.ALL);
792
793 assertEquals(2, all.size());
794
795 assertTrue(all.containsKey(HEAD));
796 assertTrue(all.containsKey("refs/heads/master"));
797 assertFalse(all.containsKey("refs/heads/pu.lock"));
798 }
799
800 @Test
801 public void testGetRefs_CycleInSymbolicRef() throws IOException {
802 Map<String, Ref> all;
803 Ref r;
804
805 writeLooseRef("refs/1", "ref: refs/2\n");
806 writeLooseRef("refs/2", "ref: refs/3\n");
807 writeLooseRef("refs/3", "ref: refs/4\n");
808 writeLooseRef("refs/4", "ref: refs/5\n");
809 writeLooseRef("refs/5", "ref: refs/end\n");
810 writeLooseRef("refs/end", A);
811
812 all = refdir.getRefs(RefDatabase.ALL);
813 r = all.get("refs/1");
814 assertNotNull("has 1", r);
815
816 assertEquals("refs/1", r.getName());
817 assertEquals(A, r.getObjectId());
818 assertTrue(r.isSymbolic());
819
820 r = r.getTarget();
821 assertEquals("refs/2", r.getName());
822 assertEquals(A, r.getObjectId());
823 assertTrue(r.isSymbolic());
824
825 r = r.getTarget();
826 assertEquals("refs/3", r.getName());
827 assertEquals(A, r.getObjectId());
828 assertTrue(r.isSymbolic());
829
830 r = r.getTarget();
831 assertEquals("refs/4", r.getName());
832 assertEquals(A, r.getObjectId());
833 assertTrue(r.isSymbolic());
834
835 r = r.getTarget();
836 assertEquals("refs/5", r.getName());
837 assertEquals(A, r.getObjectId());
838 assertTrue(r.isSymbolic());
839
840 r = r.getTarget();
841 assertEquals("refs/end", r.getName());
842 assertEquals(A, r.getObjectId());
843 assertFalse(r.isSymbolic());
844
845 writeLooseRef("refs/5", "ref: refs/6\n");
846 writeLooseRef("refs/6", "ref: refs/end\n");
847 all = refdir.getRefs(RefDatabase.ALL);
848 r = all.get("refs/1");
849 assertNull("mising 1 due to cycle", r);
850 }
851
852 @Test
853 public void testFindRef_CycleInSymbolicRef() throws IOException {
854 Ref r;
855
856 writeLooseRef("refs/1", "ref: refs/2\n");
857 writeLooseRef("refs/2", "ref: refs/3\n");
858 writeLooseRef("refs/3", "ref: refs/4\n");
859 writeLooseRef("refs/4", "ref: refs/5\n");
860 writeLooseRef("refs/5", "ref: refs/end\n");
861 writeLooseRef("refs/end", A);
862
863 r = refdir.findRef("1");
864 assertEquals("refs/1", r.getName());
865 assertEquals(A, r.getObjectId());
866 assertTrue(r.isSymbolic());
867
868 writeLooseRef("refs/5", "ref: refs/6\n");
869 writeLooseRef("refs/6", "ref: refs/end\n");
870
871 r = refdir.findRef("1");
872 assertNull("missing 1 due to cycle", r);
873
874 writeLooseRef("refs/heads/1", B);
875
876 r = refdir.findRef("1");
877 assertEquals("refs/heads/1", r.getName());
878 assertEquals(B, r.getObjectId());
879 assertFalse(r.isSymbolic());
880 }
881
882 @Test
883 public void testGetRefs_PackedNotPeeled_Sorted() throws IOException {
884 Map<String, Ref> all;
885
886 writePackedRefs("" +
887 A.name() + " refs/heads/master\n" +
888 B.name() + " refs/heads/other\n" +
889 v1_0.name() + " refs/tags/v1.0\n");
890 all = refdir.getRefs(RefDatabase.ALL);
891
892 assertEquals(4, all.size());
893 final Ref head = all.get(HEAD);
894 final Ref master = all.get("refs/heads/master");
895 final Ref other = all.get("refs/heads/other");
896 final Ref tag = all.get("refs/tags/v1.0");
897
898 assertEquals(A, master.getObjectId());
899 assertFalse(master.isPeeled());
900 assertNull(master.getPeeledObjectId());
901
902 assertEquals(B, other.getObjectId());
903 assertFalse(other.isPeeled());
904 assertNull(other.getPeeledObjectId());
905
906 assertSame(master, head.getTarget());
907 assertEquals(A, head.getObjectId());
908 assertFalse(head.isPeeled());
909 assertNull(head.getPeeledObjectId());
910
911 assertEquals(v1_0, tag.getObjectId());
912 assertFalse(tag.isPeeled());
913 assertNull(tag.getPeeledObjectId());
914 }
915
916 @Test
917 public void testFindRef_PackedNotPeeled_WrongSort() throws IOException {
918 writePackedRefs("" +
919 v1_0.name() + " refs/tags/v1.0\n" +
920 B.name() + " refs/heads/other\n" +
921 A.name() + " refs/heads/master\n");
922
923 final Ref head = refdir.findRef(HEAD);
924 final Ref master = refdir.findRef("refs/heads/master");
925 final Ref other = refdir.findRef("refs/heads/other");
926 final Ref tag = refdir.findRef("refs/tags/v1.0");
927
928 assertEquals(A, master.getObjectId());
929 assertFalse(master.isPeeled());
930 assertNull(master.getPeeledObjectId());
931
932 assertEquals(B, other.getObjectId());
933 assertFalse(other.isPeeled());
934 assertNull(other.getPeeledObjectId());
935
936 assertSame(master, head.getTarget());
937 assertEquals(A, head.getObjectId());
938 assertFalse(head.isPeeled());
939 assertNull(head.getPeeledObjectId());
940
941 assertEquals(v1_0, tag.getObjectId());
942 assertFalse(tag.isPeeled());
943 assertNull(tag.getPeeledObjectId());
944 }
945
946 @Test
947 public void testGetRefs_PackedWithPeeled() throws IOException {
948 Map<String, Ref> all;
949
950 writePackedRefs("# pack-refs with: peeled \n" +
951 A.name() + " refs/heads/master\n" +
952 B.name() + " refs/heads/other\n" +
953 v1_0.name() + " refs/tags/v1.0\n" +
954 "^" + v1_0.getObject().name() + "\n");
955 all = refdir.getRefs(RefDatabase.ALL);
956
957 assertEquals(4, all.size());
958 final Ref head = all.get(HEAD);
959 final Ref master = all.get("refs/heads/master");
960 final Ref other = all.get("refs/heads/other");
961 final Ref tag = all.get("refs/tags/v1.0");
962
963 assertEquals(A, master.getObjectId());
964 assertTrue(master.isPeeled());
965 assertNull(master.getPeeledObjectId());
966
967 assertEquals(B, other.getObjectId());
968 assertTrue(other.isPeeled());
969 assertNull(other.getPeeledObjectId());
970
971 assertSame(master, head.getTarget());
972 assertEquals(A, head.getObjectId());
973 assertTrue(head.isPeeled());
974 assertNull(head.getPeeledObjectId());
975
976 assertEquals(v1_0, tag.getObjectId());
977 assertTrue(tag.isPeeled());
978 assertEquals(v1_0.getObject(), tag.getPeeledObjectId());
979 }
980
981 @Test
982 public void test_repack() throws Exception {
983 Map<String, Ref> all;
984
985 writePackedRefs("# pack-refs with: peeled \n" +
986 A.name() + " refs/heads/master\n" +
987 B.name() + " refs/heads/other\n" +
988 v1_0.name() + " refs/tags/v1.0\n" +
989 "^" + v1_0.getObject().name() + "\n");
990 all = refdir.getRefs(RefDatabase.ALL);
991
992 assertEquals(4, all.size());
993 assertEquals(Storage.LOOSE, all.get(HEAD).getStorage());
994 assertEquals(Storage.PACKED, all.get("refs/heads/master").getStorage());
995 assertEquals(A.getId(), all.get("refs/heads/master").getObjectId());
996 assertEquals(Storage.PACKED, all.get("refs/heads/other").getStorage());
997 assertEquals(Storage.PACKED, all.get("refs/tags/v1.0").getStorage());
998
999 repo.update("refs/heads/master", B.getId());
1000 RevTag v0_1 = repo.tag("v0.1", A);
1001 repo.update("refs/tags/v0.1", v0_1);
1002
1003 all = refdir.getRefs(RefDatabase.ALL);
1004 assertEquals(5, all.size());
1005 assertEquals(Storage.LOOSE, all.get(HEAD).getStorage());
1006
1007 assertEquals(Storage.LOOSE, all.get("refs/heads/master")
1008 .getStorage());
1009 assertEquals(B.getId(), all.get("refs/heads/master").getObjectId());
1010 assertEquals(Storage.PACKED, all.get("refs/heads/other").getStorage());
1011 assertEquals(Storage.PACKED, all.get("refs/tags/v1.0").getStorage());
1012 assertEquals(Storage.LOOSE, all.get("refs/tags/v0.1").getStorage());
1013 assertEquals(v0_1.getId(), all.get("refs/tags/v0.1").getObjectId());
1014
1015 all = refdir.getRefs(RefDatabase.ALL);
1016 refdir.pack(new ArrayList<>(all.keySet()));
1017
1018 all = refdir.getRefs(RefDatabase.ALL);
1019 assertEquals(5, all.size());
1020 assertEquals(Storage.LOOSE, all.get(HEAD).getStorage());
1021
1022 assertEquals(Storage.PACKED, all.get("refs/heads/master").getStorage());
1023 assertEquals(B.getId(), all.get("refs/heads/master").getObjectId());
1024 assertEquals(Storage.PACKED, all.get("refs/heads/other").getStorage());
1025 assertEquals(Storage.PACKED, all.get("refs/tags/v1.0").getStorage());
1026 assertEquals(Storage.PACKED, all.get("refs/tags/v0.1").getStorage());
1027 assertEquals(v0_1.getId(), all.get("refs/tags/v0.1").getObjectId());
1028 }
1029
1030 @Test
1031 public void testFindRef_EmptyDatabase() throws IOException {
1032 Ref r;
1033
1034 r = refdir.findRef(HEAD);
1035 assertTrue(r.isSymbolic());
1036 assertSame(LOOSE, r.getStorage());
1037 assertEquals("refs/heads/master", r.getTarget().getName());
1038 assertSame(NEW, r.getTarget().getStorage());
1039 assertNull(r.getTarget().getObjectId());
1040
1041 assertNull(refdir.findRef("refs/heads/master"));
1042 assertNull(refdir.findRef("refs/tags/v1.0"));
1043 assertNull(refdir.findRef("FETCH_HEAD"));
1044 assertNull(refdir.findRef("NOT.A.REF.NAME"));
1045 assertNull(refdir.findRef("master"));
1046 assertNull(refdir.findRef("v1.0"));
1047 }
1048
1049 @Test
1050 public void testExactRef_EmptyDatabase() throws IOException {
1051 Ref r;
1052
1053 r = refdir.exactRef(HEAD);
1054 assertTrue(r.isSymbolic());
1055 assertSame(LOOSE, r.getStorage());
1056 assertEquals("refs/heads/master", r.getTarget().getName());
1057 assertSame(NEW, r.getTarget().getStorage());
1058 assertNull(r.getTarget().getObjectId());
1059
1060 assertNull(refdir.exactRef("refs/heads/master"));
1061 assertNull(refdir.exactRef("refs/tags/v1.0"));
1062 assertNull(refdir.exactRef("FETCH_HEAD"));
1063 assertNull(refdir.exactRef("NOT.A.REF.NAME"));
1064 assertNull(refdir.exactRef("master"));
1065 assertNull(refdir.exactRef("v1.0"));
1066 }
1067
1068 @Test
1069 public void testGetAdditionalRefs_OrigHead() throws IOException {
1070 writeLooseRef("ORIG_HEAD", A);
1071
1072 List<Ref> refs = refdir.getAdditionalRefs();
1073 assertEquals(1, refs.size());
1074
1075 Ref r = refs.get(0);
1076 assertFalse(r.isSymbolic());
1077 assertEquals(A, r.getObjectId());
1078 assertEquals("ORIG_HEAD", r.getName());
1079 assertFalse(r.isPeeled());
1080 assertNull(r.getPeeledObjectId());
1081 }
1082
1083 @Test
1084 public void testGetAdditionalRefs_OrigHeadBranch() throws IOException {
1085 writeLooseRef("refs/heads/ORIG_HEAD", A);
1086 List<Ref> refs = refdir.getAdditionalRefs();
1087 assertArrayEquals(new Ref[0], refs.toArray());
1088 }
1089
1090 @Test
1091 public void testFindRef_FetchHead() throws IOException {
1092
1093
1094
1095 write(new File(diskRepo.getDirectory(), "FETCH_HEAD"), A.name()
1096 + "\tnot-for-merge"
1097 + "\tbranch 'master' of git://egit.eclipse.org/jgit\n");
1098
1099 Ref r = refdir.findRef("FETCH_HEAD");
1100 assertFalse(r.isSymbolic());
1101 assertEquals(A, r.getObjectId());
1102 assertEquals("FETCH_HEAD", r.getName());
1103 assertFalse(r.isPeeled());
1104 assertNull(r.getPeeledObjectId());
1105 }
1106
1107 @Test
1108 public void testExactRef_FetchHead() throws IOException {
1109
1110
1111
1112 write(new File(diskRepo.getDirectory(), "FETCH_HEAD"), A.name()
1113 + "\tnot-for-merge"
1114 + "\tbranch 'master' of git://egit.eclipse.org/jgit\n");
1115
1116 Ref r = refdir.exactRef("FETCH_HEAD");
1117 assertFalse(r.isSymbolic());
1118 assertEquals(A, r.getObjectId());
1119 assertEquals("FETCH_HEAD", r.getName());
1120 assertFalse(r.isPeeled());
1121 assertNull(r.getPeeledObjectId());
1122 }
1123
1124 @Test
1125 public void testFindRef_AnyHeadWithGarbage() throws IOException {
1126 write(new File(diskRepo.getDirectory(), "refs/heads/A"), A.name()
1127 + "012345 . this is not a standard reference\n"
1128 + "#and even more junk\n");
1129
1130 Ref r = refdir.findRef("refs/heads/A");
1131 assertFalse(r.isSymbolic());
1132 assertEquals(A, r.getObjectId());
1133 assertEquals("refs/heads/A", r.getName());
1134 assertFalse(r.isPeeled());
1135 assertNull(r.getPeeledObjectId());
1136 }
1137
1138 @Test
1139 public void testGetRefs_CorruptSymbolicReference() throws IOException {
1140 String name = "refs/heads/A";
1141 writeLooseRef(name, "ref: \n");
1142 assertTrue(refdir.getRefs(RefDatabase.ALL).isEmpty());
1143 }
1144
1145 @Test
1146 public void testFindRef_CorruptSymbolicReference() throws IOException {
1147 String name = "refs/heads/A";
1148 writeLooseRef(name, "ref: \n");
1149 try {
1150 refdir.findRef(name);
1151 fail("read an invalid reference");
1152 } catch (IOException err) {
1153 String msg = err.getMessage();
1154 assertEquals("Not a ref: " + name + ": ref:", msg);
1155 }
1156 }
1157
1158 @Test
1159 public void testGetRefs_CorruptObjectIdReference() throws IOException {
1160 String name = "refs/heads/A";
1161 String content = "zoo" + A.name();
1162 writeLooseRef(name, content + "\n");
1163 assertTrue(refdir.getRefs(RefDatabase.ALL).isEmpty());
1164 }
1165
1166 @Test
1167 public void testFindRef_CorruptObjectIdReference() throws IOException {
1168 String name = "refs/heads/A";
1169 String content = "zoo" + A.name();
1170 writeLooseRef(name, content + "\n");
1171 try {
1172 refdir.findRef(name);
1173 fail("read an invalid reference");
1174 } catch (IOException err) {
1175 String msg = err.getMessage();
1176 assertEquals("Not a ref: " + name + ": " + content, msg);
1177 }
1178 }
1179
1180 @Test
1181 public void testIsNameConflicting() throws IOException {
1182 writeLooseRef("refs/heads/a/b", A);
1183 writePackedRef("refs/heads/q", B);
1184
1185
1186 assertTrue(refdir.isNameConflicting("refs"));
1187 assertTrue(refdir.isNameConflicting("refs/heads"));
1188 assertTrue(refdir.isNameConflicting("refs/heads/a"));
1189
1190
1191 assertFalse(refdir.isNameConflicting("refs/heads/a/b"));
1192
1193
1194 assertFalse(refdir.isNameConflicting("refs/heads/a/d"));
1195 assertFalse(refdir.isNameConflicting("refs/heads/master"));
1196
1197
1198 assertTrue(refdir.isNameConflicting("refs/heads/a/b/c"));
1199 assertTrue(refdir.isNameConflicting("refs/heads/q/master"));
1200 }
1201
1202 @Test
1203 public void testPeelLooseTag() throws IOException {
1204 writeLooseRef("refs/tags/v1_0", v1_0);
1205 writeLooseRef("refs/tags/current", "ref: refs/tags/v1_0\n");
1206
1207 final Ref tag = refdir.findRef("refs/tags/v1_0");
1208 final Ref cur = refdir.findRef("refs/tags/current");
1209
1210 assertEquals(v1_0, tag.getObjectId());
1211 assertFalse(tag.isSymbolic());
1212 assertFalse(tag.isPeeled());
1213 assertNull(tag.getPeeledObjectId());
1214
1215 assertEquals(v1_0, cur.getObjectId());
1216 assertTrue(cur.isSymbolic());
1217 assertFalse(cur.isPeeled());
1218 assertNull(cur.getPeeledObjectId());
1219
1220 final Ref tag_p = refdir.peel(tag);
1221 final Ref cur_p = refdir.peel(cur);
1222
1223 assertNotSame(tag, tag_p);
1224 assertFalse(tag_p.isSymbolic());
1225 assertTrue(tag_p.isPeeled());
1226 assertEquals(v1_0, tag_p.getObjectId());
1227 assertEquals(v1_0.getObject(), tag_p.getPeeledObjectId());
1228 assertSame(tag_p, refdir.peel(tag_p));
1229
1230 assertNotSame(cur, cur_p);
1231 assertEquals("refs/tags/current", cur_p.getName());
1232 assertTrue(cur_p.isSymbolic());
1233 assertEquals("refs/tags/v1_0", cur_p.getTarget().getName());
1234 assertTrue(cur_p.isPeeled());
1235 assertEquals(v1_0, cur_p.getObjectId());
1236 assertEquals(v1_0.getObject(), cur_p.getPeeledObjectId());
1237
1238
1239
1240 final Ref tag_p2 = refdir.findRef("refs/tags/v1_0");
1241 assertFalse(tag_p2.isSymbolic());
1242 assertTrue(tag_p2.isPeeled());
1243 assertEquals(v1_0, tag_p2.getObjectId());
1244 assertEquals(v1_0.getObject(), tag_p2.getPeeledObjectId());
1245
1246 assertSame(tag_p2, refdir.findRef("refs/tags/v1_0"));
1247 assertSame(tag_p2, refdir.findRef("refs/tags/current").getTarget());
1248 assertSame(tag_p2, refdir.peel(tag_p2));
1249 }
1250
1251 @Test
1252 public void testPeelCommit() throws IOException {
1253 writeLooseRef("refs/heads/master", A);
1254
1255 Ref master = refdir.findRef("refs/heads/master");
1256 assertEquals(A, master.getObjectId());
1257 assertFalse(master.isPeeled());
1258 assertNull(master.getPeeledObjectId());
1259
1260 Ref master_p = refdir.peel(master);
1261 assertNotSame(master, master_p);
1262 assertEquals(A, master_p.getObjectId());
1263 assertTrue(master_p.isPeeled());
1264 assertNull(master_p.getPeeledObjectId());
1265
1266
1267
1268 Ref master_p2 = refdir.findRef("refs/heads/master");
1269 assertNotSame(master, master_p2);
1270 assertEquals(A, master_p2.getObjectId());
1271 assertTrue(master_p2.isPeeled());
1272 assertNull(master_p2.getPeeledObjectId());
1273 assertSame(master_p2, refdir.peel(master_p2));
1274 }
1275
1276 @Test
1277 public void testRefsChangedStackOverflow() throws Exception {
1278 final FileRepository newRepo = createBareRepository();
1279 final RefDatabase refDb = newRepo.getRefDatabase();
1280 File packedRefs = new File(newRepo.getDirectory(), "packed-refs");
1281 assertTrue(packedRefs.createNewFile());
1282 final AtomicReference<StackOverflowError> error = new AtomicReference<>();
1283 final AtomicReference<IOException> exception = new AtomicReference<>();
1284 final AtomicInteger changeCount = new AtomicInteger();
1285 newRepo.getListenerList()
1286 .addRefsChangedListener((RefsChangedEvent event) -> {
1287 try {
1288 refDb.getRefsByPrefix("ref");
1289 changeCount.incrementAndGet();
1290 } catch (StackOverflowError soe) {
1291 error.set(soe);
1292 } catch (IOException ioe) {
1293 exception.set(ioe);
1294 }
1295 });
1296 refDb.getRefsByPrefix("ref");
1297 refDb.getRefsByPrefix("ref");
1298 assertNull(error.get());
1299 assertNull(exception.get());
1300 assertEquals(1, changeCount.get());
1301 }
1302
1303 @Test
1304 public void testPackedRefsLockFailure() throws Exception {
1305 writeLooseRef("refs/heads/master", A);
1306 refdir.setRetrySleepMs(Arrays.asList(0, 0));
1307 LockFile myLock = refdir.lockPackedRefs();
1308 try {
1309 refdir.pack(Arrays.asList("refs/heads/master"));
1310 fail("expected LockFailedException");
1311 } catch (LockFailedException e) {
1312 assertEquals(refdir.packedRefsFile.getPath(), e.getFile().getPath());
1313 } finally {
1314 myLock.unlock();
1315 }
1316 Ref ref = refdir.findRef("refs/heads/master");
1317 assertEquals(Storage.LOOSE, ref.getStorage());
1318 }
1319
1320 private void writeLooseRef(String name, AnyObjectId id) throws IOException {
1321 writeLooseRef(name, id.name() + "\n");
1322 }
1323
1324 private void writeLooseRef(String name, String content) throws IOException {
1325 write(new File(diskRepo.getDirectory(), name), content);
1326 }
1327
1328 private void writePackedRef(String name, AnyObjectId id) throws IOException {
1329 writePackedRefs(id.name() + " " + name + "\n");
1330 }
1331
1332 private void writePackedRefs(String content) throws IOException {
1333 File pr = new File(diskRepo.getDirectory(), "packed-refs");
1334 write(pr, content);
1335 FS fs = diskRepo.getFS();
1336 fs.setLastModified(pr.toPath(), Instant.now().minusSeconds(3600));
1337 }
1338
1339 private void deleteLooseRef(String name) {
1340 File path = new File(diskRepo.getDirectory(), name);
1341 assertTrue("deleted " + name, path.delete());
1342 }
1343 }