View Javadoc
1   /*
2    * Copyright (C) 2010, 2013 Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.internal.storage.file;
45  
46  import static org.eclipse.jgit.lib.Constants.HEAD;
47  import static org.eclipse.jgit.lib.Constants.R_HEADS;
48  import static org.eclipse.jgit.lib.Constants.R_TAGS;
49  import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
50  import static org.eclipse.jgit.lib.Ref.Storage.NEW;
51  import static org.junit.Assert.assertEquals;
52  import static org.junit.Assert.assertFalse;
53  import static org.junit.Assert.assertNotNull;
54  import static org.junit.Assert.assertNotSame;
55  import static org.junit.Assert.assertNull;
56  import static org.junit.Assert.assertSame;
57  import static org.junit.Assert.assertTrue;
58  import static org.junit.Assert.fail;
59  
60  import java.io.File;
61  import java.io.IOException;
62  import java.util.ArrayList;
63  import java.util.Arrays;
64  import java.util.List;
65  import java.util.Map;
66  import java.util.concurrent.atomic.AtomicInteger;
67  import java.util.concurrent.atomic.AtomicReference;
68  
69  import org.eclipse.jgit.events.ListenerHandle;
70  import org.eclipse.jgit.events.RefsChangedEvent;
71  import org.eclipse.jgit.events.RefsChangedListener;
72  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
73  import org.eclipse.jgit.junit.TestRepository;
74  import org.eclipse.jgit.lib.AnyObjectId;
75  import org.eclipse.jgit.lib.BatchRefUpdate;
76  import org.eclipse.jgit.lib.ProgressMonitor;
77  import org.eclipse.jgit.lib.NullProgressMonitor;
78  import org.eclipse.jgit.lib.Ref;
79  import org.eclipse.jgit.lib.Ref.Storage;
80  import org.eclipse.jgit.lib.RefDatabase;
81  import org.eclipse.jgit.lib.Repository;
82  import org.eclipse.jgit.revwalk.RevCommit;
83  import org.eclipse.jgit.revwalk.RevTag;
84  import org.eclipse.jgit.revwalk.RevWalk;
85  import org.eclipse.jgit.transport.ReceiveCommand;
86  import org.eclipse.jgit.transport.ReceiveCommand.Type;
87  import org.junit.Before;
88  import org.junit.Test;
89  
90  public class RefDirectoryTest extends LocalDiskRepositoryTestCase {
91  	private Repository diskRepo;
92  
93  	private TestRepository<Repository> repo;
94  
95  	private RefDirectory refdir;
96  
97  	private RevCommit A;
98  
99  	private RevCommit B;
100 
101 	private RevTag v1_0;
102 
103 	@Before
104 	public void setUp() throws Exception {
105 		super.setUp();
106 
107 		diskRepo = createBareRepository();
108 		refdir = (RefDirectory) diskRepo.getRefDatabase();
109 
110 		repo = new TestRepository<Repository>(diskRepo);
111 		A = repo.commit().create();
112 		B = repo.commit(repo.getRevWalk().parseCommit(A));
113 		v1_0 = repo.tag("v1_0", B);
114 		repo.getRevWalk().parseBody(v1_0);
115 	}
116 
117 	@Test
118 	public void testCreate() throws IOException {
119 		// setUp above created the directory. We just have to test it.
120 		File d = diskRepo.getDirectory();
121 		assertSame(diskRepo, refdir.getRepository());
122 
123 		assertTrue(new File(d, "refs").isDirectory());
124 		assertTrue(new File(d, "logs").isDirectory());
125 		assertTrue(new File(d, "logs/refs").isDirectory());
126 		assertFalse(new File(d, "packed-refs").exists());
127 
128 		assertTrue(new File(d, "refs/heads").isDirectory());
129 		assertTrue(new File(d, "refs/tags").isDirectory());
130 		assertEquals(2, new File(d, "refs").list().length);
131 		assertEquals(0, new File(d, "refs/heads").list().length);
132 		assertEquals(0, new File(d, "refs/tags").list().length);
133 
134 		assertTrue(new File(d, "logs/refs/heads").isDirectory());
135 		assertFalse(new File(d, "logs/HEAD").exists());
136 		assertEquals(0, new File(d, "logs/refs/heads").list().length);
137 
138 		assertEquals("ref: refs/heads/master\n", read(new File(d, HEAD)));
139 	}
140 
141 	@Test
142 	public void testGetRefs_EmptyDatabase() throws IOException {
143 		Map<String, Ref> all;
144 
145 		all = refdir.getRefs(RefDatabase.ALL);
146 		assertTrue("no references", all.isEmpty());
147 
148 		all = refdir.getRefs(R_HEADS);
149 		assertTrue("no references", all.isEmpty());
150 
151 		all = refdir.getRefs(R_TAGS);
152 		assertTrue("no references", all.isEmpty());
153 	}
154 
155 	@Test
156 	public void testGetRefs_HeadOnOneBranch() throws IOException {
157 		Map<String, Ref> all;
158 		Ref head, master;
159 
160 		writeLooseRef("refs/heads/master", A);
161 
162 		all = refdir.getRefs(RefDatabase.ALL);
163 		assertEquals(2, all.size());
164 		assertTrue("has HEAD", all.containsKey(HEAD));
165 		assertTrue("has master", all.containsKey("refs/heads/master"));
166 
167 		head = all.get(HEAD);
168 		master = all.get("refs/heads/master");
169 
170 		assertEquals(HEAD, head.getName());
171 		assertTrue(head.isSymbolic());
172 		assertSame(LOOSE, head.getStorage());
173 		assertSame("uses same ref as target", master, head.getTarget());
174 
175 		assertEquals("refs/heads/master", master.getName());
176 		assertFalse(master.isSymbolic());
177 		assertSame(LOOSE, master.getStorage());
178 		assertEquals(A, master.getObjectId());
179 	}
180 
181 	@Test
182 	public void testGetRefs_DeatchedHead1() throws IOException {
183 		Map<String, Ref> all;
184 		Ref head;
185 
186 		writeLooseRef(HEAD, A);
187 
188 		all = refdir.getRefs(RefDatabase.ALL);
189 		assertEquals(1, all.size());
190 		assertTrue("has HEAD", all.containsKey(HEAD));
191 
192 		head = all.get(HEAD);
193 
194 		assertEquals(HEAD, head.getName());
195 		assertFalse(head.isSymbolic());
196 		assertSame(LOOSE, head.getStorage());
197 		assertEquals(A, head.getObjectId());
198 	}
199 
200 	@Test
201 	public void testGetRefs_DeatchedHead2() throws IOException {
202 		Map<String, Ref> all;
203 		Ref head, master;
204 
205 		writeLooseRef(HEAD, A);
206 		writeLooseRef("refs/heads/master", B);
207 
208 		all = refdir.getRefs(RefDatabase.ALL);
209 		assertEquals(2, all.size());
210 
211 		head = all.get(HEAD);
212 		master = all.get("refs/heads/master");
213 
214 		assertEquals(HEAD, head.getName());
215 		assertFalse(head.isSymbolic());
216 		assertSame(LOOSE, head.getStorage());
217 		assertEquals(A, head.getObjectId());
218 
219 		assertEquals("refs/heads/master", master.getName());
220 		assertFalse(master.isSymbolic());
221 		assertSame(LOOSE, master.getStorage());
222 		assertEquals(B, master.getObjectId());
223 	}
224 
225 	@Test
226 	public void testGetRefs_DeeplyNestedBranch() throws IOException {
227 		String name = "refs/heads/a/b/c/d/e/f/g/h/i/j/k";
228 		Map<String, Ref> all;
229 		Ref r;
230 
231 		writeLooseRef(name, A);
232 
233 		all = refdir.getRefs(RefDatabase.ALL);
234 		assertEquals(1, all.size());
235 
236 		r = all.get(name);
237 		assertEquals(name, r.getName());
238 		assertFalse(r.isSymbolic());
239 		assertSame(LOOSE, r.getStorage());
240 		assertEquals(A, r.getObjectId());
241 	}
242 
243 	@Test
244 	public void testGetRefs_HeadBranchNotBorn() throws IOException {
245 		Map<String, Ref> all;
246 		Ref a, b;
247 
248 		writeLooseRef("refs/heads/A", A);
249 		writeLooseRef("refs/heads/B", B);
250 
251 		all = refdir.getRefs(RefDatabase.ALL);
252 		assertEquals(2, all.size());
253 		assertFalse("no HEAD", all.containsKey(HEAD));
254 
255 		a = all.get("refs/heads/A");
256 		b = all.get("refs/heads/B");
257 
258 		assertEquals(A, a.getObjectId());
259 		assertEquals(B, b.getObjectId());
260 
261 		assertEquals("refs/heads/A", a.getName());
262 		assertEquals("refs/heads/B", b.getName());
263 	}
264 
265 	@Test
266 	public void testGetRefs_LooseOverridesPacked() throws IOException {
267 		Map<String, Ref> heads;
268 		Ref a;
269 
270 		writeLooseRef("refs/heads/master", B);
271 		writePackedRef("refs/heads/master", A);
272 
273 		heads = refdir.getRefs(R_HEADS);
274 		assertEquals(1, heads.size());
275 
276 		a = heads.get("master");
277 		assertEquals("refs/heads/master", a.getName());
278 		assertEquals(B, a.getObjectId());
279 	}
280 
281 	@Test
282 	public void testGetRefs_IgnoresGarbageRef1() throws IOException {
283 		Map<String, Ref> heads;
284 		Ref a;
285 
286 		writeLooseRef("refs/heads/A", A);
287 		write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
288 
289 		heads = refdir.getRefs(RefDatabase.ALL);
290 		assertEquals(1, heads.size());
291 
292 		a = heads.get("refs/heads/A");
293 		assertEquals("refs/heads/A", a.getName());
294 		assertEquals(A, a.getObjectId());
295 	}
296 
297 	@Test
298 	public void testGetRefs_IgnoresGarbageRef2() throws IOException {
299 		Map<String, Ref> heads;
300 		Ref a;
301 
302 		writeLooseRef("refs/heads/A", A);
303 		write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "");
304 
305 		heads = refdir.getRefs(RefDatabase.ALL);
306 		assertEquals(1, heads.size());
307 
308 		a = heads.get("refs/heads/A");
309 		assertEquals("refs/heads/A", a.getName());
310 		assertEquals(A, a.getObjectId());
311 	}
312 
313 	@Test
314 	public void testGetRefs_IgnoresGarbageRef3() throws IOException {
315 		Map<String, Ref> heads;
316 		Ref a;
317 
318 		writeLooseRef("refs/heads/A", A);
319 		write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "\n");
320 
321 		heads = refdir.getRefs(RefDatabase.ALL);
322 		assertEquals(1, heads.size());
323 
324 		a = heads.get("refs/heads/A");
325 		assertEquals("refs/heads/A", a.getName());
326 		assertEquals(A, a.getObjectId());
327 	}
328 
329 	@Test
330 	public void testGetRefs_IgnoresGarbageRef4() throws IOException {
331 		Map<String, Ref> heads;
332 		Ref a, b, c;
333 
334 		writeLooseRef("refs/heads/A", A);
335 		writeLooseRef("refs/heads/B", B);
336 		writeLooseRef("refs/heads/C", A);
337 		heads = refdir.getRefs(RefDatabase.ALL);
338 		assertEquals(3, heads.size());
339 		assertTrue(heads.containsKey("refs/heads/A"));
340 		assertTrue(heads.containsKey("refs/heads/B"));
341 		assertTrue(heads.containsKey("refs/heads/C"));
342 
343 		writeLooseRef("refs/heads/B", "FAIL\n");
344 
345 		heads = refdir.getRefs(RefDatabase.ALL);
346 		assertEquals(2, heads.size());
347 
348 		a = heads.get("refs/heads/A");
349 		b = heads.get("refs/heads/B");
350 		c = heads.get("refs/heads/C");
351 
352 		assertEquals("refs/heads/A", a.getName());
353 		assertEquals(A, a.getObjectId());
354 
355 		assertNull("no refs/heads/B", b);
356 
357 		assertEquals("refs/heads/C", c.getName());
358 		assertEquals(A, c.getObjectId());
359 	}
360 
361 	@Test
362 	public void testFirstExactRef_IgnoresGarbageRef() throws IOException {
363 		writeLooseRef("refs/heads/A", A);
364 		write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
365 
366 		Ref a = refdir.firstExactRef("refs/heads/bad", "refs/heads/A");
367 		assertEquals("refs/heads/A", a.getName());
368 		assertEquals(A, a.getObjectId());
369 	}
370 
371 	@Test
372 	public void testExactRef_IgnoresGarbageRef() throws IOException {
373 		writeLooseRef("refs/heads/A", A);
374 		write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
375 
376 		Map<String, Ref> refs =
377 				refdir.exactRef("refs/heads/bad", "refs/heads/A");
378 
379 		assertNull("no refs/heads/bad", refs.get("refs/heads/bad"));
380 
381 		Ref a = refs.get("refs/heads/A");
382 		assertEquals("refs/heads/A", a.getName());
383 		assertEquals(A, a.getObjectId());
384 
385 		assertEquals(1, refs.size());
386 	}
387 
388 	@Test
389 	public void testGetRefs_InvalidName() throws IOException {
390 		writeLooseRef("refs/heads/A", A);
391 
392 		assertTrue("empty refs/heads", refdir.getRefs("refs/heads").isEmpty());
393 		assertTrue("empty objects", refdir.getRefs("objects").isEmpty());
394 		assertTrue("empty objects/", refdir.getRefs("objects/").isEmpty());
395 	}
396 
397 	@Test
398 	public void testReadNotExistingBranchConfig() throws IOException {
399 		assertNull("find branch config", refdir.getRef("config"));
400 		assertNull("find branch config", refdir.getRef("refs/heads/config"));
401 	}
402 
403 	@Test
404 	public void testReadBranchConfig() throws IOException {
405 		writeLooseRef("refs/heads/config", A);
406 
407 		assertNotNull("find branch config", refdir.getRef("config"));
408 	}
409 
410 	@Test
411 	public void testGetRefs_HeadsOnly_AllLoose() throws IOException {
412 		Map<String, Ref> heads;
413 		Ref a, b;
414 
415 		writeLooseRef("refs/heads/A", A);
416 		writeLooseRef("refs/heads/B", B);
417 		writeLooseRef("refs/tags/v1.0", v1_0);
418 
419 		heads = refdir.getRefs(R_HEADS);
420 		assertEquals(2, heads.size());
421 
422 		a = heads.get("A");
423 		b = heads.get("B");
424 
425 		assertEquals("refs/heads/A", a.getName());
426 		assertEquals("refs/heads/B", b.getName());
427 
428 		assertEquals(A, a.getObjectId());
429 		assertEquals(B, b.getObjectId());
430 	}
431 
432 	@Test
433 	public void testGetRefs_HeadsOnly_AllPacked1() throws IOException {
434 		Map<String, Ref> heads;
435 		Ref a;
436 
437 		deleteLooseRef(HEAD);
438 		writePackedRef("refs/heads/A", A);
439 
440 		heads = refdir.getRefs(R_HEADS);
441 		assertEquals(1, heads.size());
442 
443 		a = heads.get("A");
444 
445 		assertEquals("refs/heads/A", a.getName());
446 		assertEquals(A, a.getObjectId());
447 	}
448 
449 	@Test
450 	public void testGetRefs_HeadsOnly_SymrefToPacked() throws IOException {
451 		Map<String, Ref> heads;
452 		Ref master, other;
453 
454 		writeLooseRef("refs/heads/other", "ref: refs/heads/master\n");
455 		writePackedRef("refs/heads/master", A);
456 
457 		heads = refdir.getRefs(R_HEADS);
458 		assertEquals(2, heads.size());
459 
460 		master = heads.get("master");
461 		other = heads.get("other");
462 
463 		assertEquals("refs/heads/master", master.getName());
464 		assertEquals(A, master.getObjectId());
465 
466 		assertEquals("refs/heads/other", other.getName());
467 		assertEquals(A, other.getObjectId());
468 		assertSame(master, other.getTarget());
469 	}
470 
471 	@Test
472 	public void testGetRefs_HeadsOnly_Mixed() throws IOException {
473 		Map<String, Ref> heads;
474 		Ref a, b;
475 
476 		writeLooseRef("refs/heads/A", A);
477 		writeLooseRef("refs/heads/B", B);
478 		writePackedRef("refs/tags/v1.0", v1_0);
479 
480 		heads = refdir.getRefs(R_HEADS);
481 		assertEquals(2, heads.size());
482 
483 		a = heads.get("A");
484 		b = heads.get("B");
485 
486 		assertEquals("refs/heads/A", a.getName());
487 		assertEquals("refs/heads/B", b.getName());
488 
489 		assertEquals(A, a.getObjectId());
490 		assertEquals(B, b.getObjectId());
491 	}
492 
493 	@Test
494 	public void testFirstExactRef_Mixed() throws IOException {
495 		writeLooseRef("refs/heads/A", A);
496 		writePackedRef("refs/tags/v1.0", v1_0);
497 
498 		Ref a = refdir.firstExactRef("refs/heads/A", "refs/tags/v1.0");
499 		Ref one = refdir.firstExactRef("refs/tags/v1.0", "refs/heads/A");
500 
501 		assertEquals("refs/heads/A", a.getName());
502 		assertEquals("refs/tags/v1.0", one.getName());
503 
504 		assertEquals(A, a.getObjectId());
505 		assertEquals(v1_0, one.getObjectId());
506 	}
507 
508 	@Test
509 	public void testGetRefs_TagsOnly_AllLoose() throws IOException {
510 		Map<String, Ref> tags;
511 		Ref a;
512 
513 		writeLooseRef("refs/heads/A", A);
514 		writeLooseRef("refs/tags/v1.0", v1_0);
515 
516 		tags = refdir.getRefs(R_TAGS);
517 		assertEquals(1, tags.size());
518 
519 		a = tags.get("v1.0");
520 
521 		assertEquals("refs/tags/v1.0", a.getName());
522 		assertEquals(v1_0, a.getObjectId());
523 	}
524 
525 	@Test
526 	public void testGetRefs_LooseSortedCorrectly() throws IOException {
527 		Map<String, Ref> refs;
528 
529 		writeLooseRef("refs/heads/project1/A", A);
530 		writeLooseRef("refs/heads/project1-B", B);
531 
532 		refs = refdir.getRefs(RefDatabase.ALL);
533 		assertEquals(2, refs.size());
534 		assertEquals(A, refs.get("refs/heads/project1/A").getObjectId());
535 		assertEquals(B, refs.get("refs/heads/project1-B").getObjectId());
536 	}
537 
538 	@Test
539 	public void testGetRefs_LooseSorting_Bug_348834() throws IOException {
540 		Map<String, Ref> refs;
541 
542 		writeLooseRef("refs/heads/my/a+b", A);
543 		writeLooseRef("refs/heads/my/a/b/c", B);
544 
545 		final int[] count = new int[1];
546 
547 		ListenerHandle listener = Repository.getGlobalListenerList()
548 				.addRefsChangedListener(new RefsChangedListener() {
549 
550 					public void onRefsChanged(RefsChangedEvent event) {
551 						count[0]++;
552 					}
553 				});
554 
555 		refs = refdir.getRefs(RefDatabase.ALL);
556 		refs = refdir.getRefs(RefDatabase.ALL);
557 		listener.remove();
558 		assertEquals(1, count[0]); // Bug 348834 multiple RefsChangedEvents
559 		assertEquals(2, refs.size());
560 		assertEquals(A, refs.get("refs/heads/my/a+b").getObjectId());
561 		assertEquals(B, refs.get("refs/heads/my/a/b/c").getObjectId());
562 
563 	}
564 
565 	@Test
566 	public void testGetRefs_TagsOnly_AllPacked() throws IOException {
567 		Map<String, Ref> tags;
568 		Ref a;
569 
570 		deleteLooseRef(HEAD);
571 		writePackedRef("refs/tags/v1.0", v1_0);
572 
573 		tags = refdir.getRefs(R_TAGS);
574 		assertEquals(1, tags.size());
575 
576 		a = tags.get("v1.0");
577 
578 		assertEquals("refs/tags/v1.0", a.getName());
579 		assertEquals(v1_0, a.getObjectId());
580 	}
581 
582 	@Test
583 	public void testGetRefs_DiscoversNewLoose1() throws IOException {
584 		Map<String, Ref> orig, next;
585 		Ref orig_r, next_r;
586 
587 		writeLooseRef("refs/heads/master", A);
588 		orig = refdir.getRefs(RefDatabase.ALL);
589 
590 		writeLooseRef("refs/heads/next", B);
591 		next = refdir.getRefs(RefDatabase.ALL);
592 
593 		assertEquals(2, orig.size());
594 		assertEquals(3, next.size());
595 
596 		assertFalse(orig.containsKey("refs/heads/next"));
597 		assertTrue(next.containsKey("refs/heads/next"));
598 
599 		orig_r = orig.get("refs/heads/master");
600 		next_r = next.get("refs/heads/master");
601 		assertEquals(A, orig_r.getObjectId());
602 		assertSame("uses cached instance", orig_r, next_r);
603 		assertSame("same HEAD", orig_r, orig.get(HEAD).getTarget());
604 		assertSame("same HEAD", orig_r, next.get(HEAD).getTarget());
605 
606 		next_r = next.get("refs/heads/next");
607 		assertSame(LOOSE, next_r.getStorage());
608 		assertEquals(B, next_r.getObjectId());
609 	}
610 
611 	@Test
612 	public void testGetRefs_DiscoversNewLoose2() throws IOException {
613 		Map<String, Ref> orig, next, news;
614 
615 		writeLooseRef("refs/heads/pu", A);
616 		orig = refdir.getRefs(RefDatabase.ALL);
617 
618 		writeLooseRef("refs/heads/new/B", B);
619 		news = refdir.getRefs("refs/heads/new/");
620 		next = refdir.getRefs(RefDatabase.ALL);
621 
622 		assertEquals(1, orig.size());
623 		assertEquals(2, next.size());
624 		assertEquals(1, news.size());
625 
626 		assertTrue(orig.containsKey("refs/heads/pu"));
627 		assertTrue(next.containsKey("refs/heads/pu"));
628 		assertFalse(news.containsKey("refs/heads/pu"));
629 
630 		assertFalse(orig.containsKey("refs/heads/new/B"));
631 		assertTrue(next.containsKey("refs/heads/new/B"));
632 		assertTrue(news.containsKey("B"));
633 	}
634 
635 	@Test
636 	public void testGetRefs_DiscoversModifiedLoose() throws IOException {
637 		Map<String, Ref> all;
638 
639 		writeLooseRef("refs/heads/master", A);
640 		all = refdir.getRefs(RefDatabase.ALL);
641 		assertEquals(A, all.get(HEAD).getObjectId());
642 
643 		writeLooseRef("refs/heads/master", B);
644 		all = refdir.getRefs(RefDatabase.ALL);
645 		assertEquals(B, all.get(HEAD).getObjectId());
646 	}
647 
648 	@Test
649 	public void testGetRef_DiscoversModifiedLoose() throws IOException {
650 		Map<String, Ref> all;
651 
652 		writeLooseRef("refs/heads/master", A);
653 		all = refdir.getRefs(RefDatabase.ALL);
654 		assertEquals(A, all.get(HEAD).getObjectId());
655 
656 		writeLooseRef("refs/heads/master", B);
657 
658 		Ref master = refdir.getRef("refs/heads/master");
659 		assertEquals(B, master.getObjectId());
660 	}
661 
662 	@Test
663 	public void testGetRefs_DiscoversDeletedLoose1() throws IOException {
664 		Map<String, Ref> orig, next;
665 		Ref orig_r, next_r;
666 
667 		writeLooseRef("refs/heads/B", B);
668 		writeLooseRef("refs/heads/master", A);
669 		orig = refdir.getRefs(RefDatabase.ALL);
670 
671 		deleteLooseRef("refs/heads/B");
672 		next = refdir.getRefs(RefDatabase.ALL);
673 
674 		assertEquals(3, orig.size());
675 		assertEquals(2, next.size());
676 
677 		assertTrue(orig.containsKey("refs/heads/B"));
678 		assertFalse(next.containsKey("refs/heads/B"));
679 
680 		orig_r = orig.get("refs/heads/master");
681 		next_r = next.get("refs/heads/master");
682 		assertEquals(A, orig_r.getObjectId());
683 		assertSame("uses cached instance", orig_r, next_r);
684 		assertSame("same HEAD", orig_r, orig.get(HEAD).getTarget());
685 		assertSame("same HEAD", orig_r, next.get(HEAD).getTarget());
686 
687 		orig_r = orig.get("refs/heads/B");
688 		assertSame(LOOSE, orig_r.getStorage());
689 		assertEquals(B, orig_r.getObjectId());
690 	}
691 
692 	@Test
693 	public void testGetRef_DiscoversDeletedLoose() throws IOException {
694 		Map<String, Ref> all;
695 
696 		writeLooseRef("refs/heads/master", A);
697 		all = refdir.getRefs(RefDatabase.ALL);
698 		assertEquals(A, all.get(HEAD).getObjectId());
699 
700 		deleteLooseRef("refs/heads/master");
701 		assertNull(refdir.getRef("refs/heads/master"));
702 		assertTrue(refdir.getRefs(RefDatabase.ALL).isEmpty());
703 	}
704 
705 	@Test
706 	public void testGetRefs_DiscoversDeletedLoose2() throws IOException {
707 		Map<String, Ref> orig, next;
708 
709 		writeLooseRef("refs/heads/master", A);
710 		writeLooseRef("refs/heads/pu", B);
711 		orig = refdir.getRefs(RefDatabase.ALL);
712 
713 		deleteLooseRef("refs/heads/pu");
714 		next = refdir.getRefs(RefDatabase.ALL);
715 
716 		assertEquals(3, orig.size());
717 		assertEquals(2, next.size());
718 
719 		assertTrue(orig.containsKey("refs/heads/pu"));
720 		assertFalse(next.containsKey("refs/heads/pu"));
721 	}
722 
723 	@Test
724 	public void testGetRefs_DiscoversDeletedLoose3() throws IOException {
725 		Map<String, Ref> orig, next;
726 
727 		writeLooseRef("refs/heads/master", A);
728 		writeLooseRef("refs/heads/next", B);
729 		writeLooseRef("refs/heads/pu", B);
730 		writeLooseRef("refs/tags/v1.0", v1_0);
731 		orig = refdir.getRefs(RefDatabase.ALL);
732 
733 		deleteLooseRef("refs/heads/pu");
734 		deleteLooseRef("refs/heads/next");
735 		next = refdir.getRefs(RefDatabase.ALL);
736 
737 		assertEquals(5, orig.size());
738 		assertEquals(3, next.size());
739 
740 		assertTrue(orig.containsKey("refs/heads/pu"));
741 		assertTrue(orig.containsKey("refs/heads/next"));
742 		assertFalse(next.containsKey("refs/heads/pu"));
743 		assertFalse(next.containsKey("refs/heads/next"));
744 	}
745 
746 	@Test
747 	public void testGetRefs_DiscoversDeletedLoose4() throws IOException {
748 		Map<String, Ref> orig, next;
749 		Ref orig_r, next_r;
750 
751 		writeLooseRef("refs/heads/B", B);
752 		writeLooseRef("refs/heads/master", A);
753 		orig = refdir.getRefs(RefDatabase.ALL);
754 
755 		deleteLooseRef("refs/heads/master");
756 		next = refdir.getRefs("refs/heads/");
757 
758 		assertEquals(3, orig.size());
759 		assertEquals(1, next.size());
760 
761 		assertTrue(orig.containsKey("refs/heads/B"));
762 		assertTrue(orig.containsKey("refs/heads/master"));
763 		assertTrue(next.containsKey("B"));
764 		assertFalse(next.containsKey("master"));
765 
766 		orig_r = orig.get("refs/heads/B");
767 		next_r = next.get("B");
768 		assertEquals(B, orig_r.getObjectId());
769 		assertSame("uses cached instance", orig_r, next_r);
770 	}
771 
772 	@Test
773 	public void testGetRefs_DiscoversDeletedLoose5() throws IOException {
774 		Map<String, Ref> orig, next;
775 
776 		writeLooseRef("refs/heads/master", A);
777 		writeLooseRef("refs/heads/pu", B);
778 		orig = refdir.getRefs(RefDatabase.ALL);
779 
780 		deleteLooseRef("refs/heads/pu");
781 		writeLooseRef("refs/tags/v1.0", v1_0);
782 		next = refdir.getRefs(RefDatabase.ALL);
783 
784 		assertEquals(3, orig.size());
785 		assertEquals(3, next.size());
786 
787 		assertTrue(orig.containsKey("refs/heads/pu"));
788 		assertFalse(orig.containsKey("refs/tags/v1.0"));
789 		assertFalse(next.containsKey("refs/heads/pu"));
790 		assertTrue(next.containsKey("refs/tags/v1.0"));
791 	}
792 
793 	@Test
794 	public void testGetRefs_SkipsLockFiles() throws IOException {
795 		Map<String, Ref> all;
796 
797 		writeLooseRef("refs/heads/master", A);
798 		writeLooseRef("refs/heads/pu.lock", B);
799 		all = refdir.getRefs(RefDatabase.ALL);
800 
801 		assertEquals(2, all.size());
802 
803 		assertTrue(all.containsKey(HEAD));
804 		assertTrue(all.containsKey("refs/heads/master"));
805 		assertFalse(all.containsKey("refs/heads/pu.lock"));
806 	}
807 
808 	@Test
809 	public void testGetRefs_CycleInSymbolicRef() throws IOException {
810 		Map<String, Ref> all;
811 		Ref r;
812 
813 		writeLooseRef("refs/1", "ref: refs/2\n");
814 		writeLooseRef("refs/2", "ref: refs/3\n");
815 		writeLooseRef("refs/3", "ref: refs/4\n");
816 		writeLooseRef("refs/4", "ref: refs/5\n");
817 		writeLooseRef("refs/5", "ref: refs/end\n");
818 		writeLooseRef("refs/end", A);
819 
820 		all = refdir.getRefs(RefDatabase.ALL);
821 		r = all.get("refs/1");
822 		assertNotNull("has 1", r);
823 
824 		assertEquals("refs/1", r.getName());
825 		assertEquals(A, r.getObjectId());
826 		assertTrue(r.isSymbolic());
827 
828 		r = r.getTarget();
829 		assertEquals("refs/2", r.getName());
830 		assertEquals(A, r.getObjectId());
831 		assertTrue(r.isSymbolic());
832 
833 		r = r.getTarget();
834 		assertEquals("refs/3", r.getName());
835 		assertEquals(A, r.getObjectId());
836 		assertTrue(r.isSymbolic());
837 
838 		r = r.getTarget();
839 		assertEquals("refs/4", r.getName());
840 		assertEquals(A, r.getObjectId());
841 		assertTrue(r.isSymbolic());
842 
843 		r = r.getTarget();
844 		assertEquals("refs/5", r.getName());
845 		assertEquals(A, r.getObjectId());
846 		assertTrue(r.isSymbolic());
847 
848 		r = r.getTarget();
849 		assertEquals("refs/end", r.getName());
850 		assertEquals(A, r.getObjectId());
851 		assertFalse(r.isSymbolic());
852 
853 		writeLooseRef("refs/5", "ref: refs/6\n");
854 		writeLooseRef("refs/6", "ref: refs/end\n");
855 		all = refdir.getRefs(RefDatabase.ALL);
856 		r = all.get("refs/1");
857 		assertNull("mising 1 due to cycle", r);
858 	}
859 
860 	@Test
861 	public void testGetRefs_PackedNotPeeled_Sorted() throws IOException {
862 		Map<String, Ref> all;
863 
864 		writePackedRefs("" + //
865 				A.name() + " refs/heads/master\n" + //
866 				B.name() + " refs/heads/other\n" + //
867 				v1_0.name() + " refs/tags/v1.0\n");
868 		all = refdir.getRefs(RefDatabase.ALL);
869 
870 		assertEquals(4, all.size());
871 		final Ref head = all.get(HEAD);
872 		final Ref master = all.get("refs/heads/master");
873 		final Ref other = all.get("refs/heads/other");
874 		final Ref tag = all.get("refs/tags/v1.0");
875 
876 		assertEquals(A, master.getObjectId());
877 		assertFalse(master.isPeeled());
878 		assertNull(master.getPeeledObjectId());
879 
880 		assertEquals(B, other.getObjectId());
881 		assertFalse(other.isPeeled());
882 		assertNull(other.getPeeledObjectId());
883 
884 		assertSame(master, head.getTarget());
885 		assertEquals(A, head.getObjectId());
886 		assertFalse(head.isPeeled());
887 		assertNull(head.getPeeledObjectId());
888 
889 		assertEquals(v1_0, tag.getObjectId());
890 		assertFalse(tag.isPeeled());
891 		assertNull(tag.getPeeledObjectId());
892 	}
893 
894 	@Test
895 	public void testGetRef_PackedNotPeeled_WrongSort() throws IOException {
896 		writePackedRefs("" + //
897 				v1_0.name() + " refs/tags/v1.0\n" + //
898 				B.name() + " refs/heads/other\n" + //
899 				A.name() + " refs/heads/master\n");
900 
901 		final Ref head = refdir.getRef(HEAD);
902 		final Ref master = refdir.getRef("refs/heads/master");
903 		final Ref other = refdir.getRef("refs/heads/other");
904 		final Ref tag = refdir.getRef("refs/tags/v1.0");
905 
906 		assertEquals(A, master.getObjectId());
907 		assertFalse(master.isPeeled());
908 		assertNull(master.getPeeledObjectId());
909 
910 		assertEquals(B, other.getObjectId());
911 		assertFalse(other.isPeeled());
912 		assertNull(other.getPeeledObjectId());
913 
914 		assertSame(master, head.getTarget());
915 		assertEquals(A, head.getObjectId());
916 		assertFalse(head.isPeeled());
917 		assertNull(head.getPeeledObjectId());
918 
919 		assertEquals(v1_0, tag.getObjectId());
920 		assertFalse(tag.isPeeled());
921 		assertNull(tag.getPeeledObjectId());
922 	}
923 
924 	@Test
925 	public void testGetRefs_PackedWithPeeled() throws IOException {
926 		Map<String, Ref> all;
927 
928 		writePackedRefs("# pack-refs with: peeled \n" + //
929 				A.name() + " refs/heads/master\n" + //
930 				B.name() + " refs/heads/other\n" + //
931 				v1_0.name() + " refs/tags/v1.0\n" + //
932 				"^" + v1_0.getObject().name() + "\n");
933 		all = refdir.getRefs(RefDatabase.ALL);
934 
935 		assertEquals(4, all.size());
936 		final Ref head = all.get(HEAD);
937 		final Ref master = all.get("refs/heads/master");
938 		final Ref other = all.get("refs/heads/other");
939 		final Ref tag = all.get("refs/tags/v1.0");
940 
941 		assertEquals(A, master.getObjectId());
942 		assertTrue(master.isPeeled());
943 		assertNull(master.getPeeledObjectId());
944 
945 		assertEquals(B, other.getObjectId());
946 		assertTrue(other.isPeeled());
947 		assertNull(other.getPeeledObjectId());
948 
949 		assertSame(master, head.getTarget());
950 		assertEquals(A, head.getObjectId());
951 		assertTrue(head.isPeeled());
952 		assertNull(head.getPeeledObjectId());
953 
954 		assertEquals(v1_0, tag.getObjectId());
955 		assertTrue(tag.isPeeled());
956 		assertEquals(v1_0.getObject(), tag.getPeeledObjectId());
957 	}
958 
959 	@Test
960 	public void test_repack() throws Exception {
961 		Map<String, Ref> all;
962 
963 		writePackedRefs("# pack-refs with: peeled \n" + //
964 				A.name() + " refs/heads/master\n" + //
965 				B.name() + " refs/heads/other\n" + //
966 				v1_0.name() + " refs/tags/v1.0\n" + //
967 				"^" + v1_0.getObject().name() + "\n");
968 		all = refdir.getRefs(RefDatabase.ALL);
969 
970 		assertEquals(4, all.size());
971 		assertEquals(Storage.LOOSE, all.get(HEAD).getStorage());
972 		assertEquals(Storage.PACKED, all.get("refs/heads/master").getStorage());
973 		assertEquals(A.getId(), all.get("refs/heads/master").getObjectId());
974 		assertEquals(Storage.PACKED, all.get("refs/heads/other").getStorage());
975 		assertEquals(Storage.PACKED, all.get("refs/tags/v1.0").getStorage());
976 
977 		repo.update("refs/heads/master", B.getId());
978 		RevTag v0_1 = repo.tag("v0.1", A);
979 		repo.update("refs/tags/v0.1", v0_1);
980 
981 		all = refdir.getRefs(RefDatabase.ALL);
982 		assertEquals(5, all.size());
983 		assertEquals(Storage.LOOSE, all.get(HEAD).getStorage());
984 		// Why isn't the next ref LOOSE_PACKED?
985 		assertEquals(Storage.LOOSE, all.get("refs/heads/master")
986 				.getStorage());
987 		assertEquals(B.getId(), all.get("refs/heads/master").getObjectId());
988 		assertEquals(Storage.PACKED, all.get("refs/heads/other").getStorage());
989 		assertEquals(Storage.PACKED, all.get("refs/tags/v1.0").getStorage());
990 		assertEquals(Storage.LOOSE, all.get("refs/tags/v0.1").getStorage());
991 		assertEquals(v0_1.getId(), all.get("refs/tags/v0.1").getObjectId());
992 
993 		all = refdir.getRefs(RefDatabase.ALL);
994 		refdir.pack(new ArrayList<String>(all.keySet()));
995 
996 		all = refdir.getRefs(RefDatabase.ALL);
997 		assertEquals(5, all.size());
998 		assertEquals(Storage.LOOSE, all.get(HEAD).getStorage());
999 		// Why isn't the next ref LOOSE_PACKED?
1000 		assertEquals(Storage.PACKED, all.get("refs/heads/master").getStorage());
1001 		assertEquals(B.getId(), all.get("refs/heads/master").getObjectId());
1002 		assertEquals(Storage.PACKED, all.get("refs/heads/other").getStorage());
1003 		assertEquals(Storage.PACKED, all.get("refs/tags/v1.0").getStorage());
1004 		assertEquals(Storage.PACKED, all.get("refs/tags/v0.1").getStorage());
1005 		assertEquals(v0_1.getId(), all.get("refs/tags/v0.1").getObjectId());
1006 	}
1007 
1008 	@Test
1009 	public void testGetRef_EmptyDatabase() throws IOException {
1010 		Ref r;
1011 
1012 		r = refdir.getRef(HEAD);
1013 		assertTrue(r.isSymbolic());
1014 		assertSame(LOOSE, r.getStorage());
1015 		assertEquals("refs/heads/master", r.getTarget().getName());
1016 		assertSame(NEW, r.getTarget().getStorage());
1017 		assertNull(r.getTarget().getObjectId());
1018 
1019 		assertNull(refdir.getRef("refs/heads/master"));
1020 		assertNull(refdir.getRef("refs/tags/v1.0"));
1021 		assertNull(refdir.getRef("FETCH_HEAD"));
1022 		assertNull(refdir.getRef("NOT.A.REF.NAME"));
1023 		assertNull(refdir.getRef("master"));
1024 		assertNull(refdir.getRef("v1.0"));
1025 	}
1026 
1027 	@Test
1028 	public void testExactRef_EmptyDatabase() throws IOException {
1029 		Ref r;
1030 
1031 		r = refdir.exactRef(HEAD);
1032 		assertTrue(r.isSymbolic());
1033 		assertSame(LOOSE, r.getStorage());
1034 		assertEquals("refs/heads/master", r.getTarget().getName());
1035 		assertSame(NEW, r.getTarget().getStorage());
1036 		assertNull(r.getTarget().getObjectId());
1037 
1038 		assertNull(refdir.exactRef("refs/heads/master"));
1039 		assertNull(refdir.exactRef("refs/tags/v1.0"));
1040 		assertNull(refdir.exactRef("FETCH_HEAD"));
1041 		assertNull(refdir.exactRef("NOT.A.REF.NAME"));
1042 		assertNull(refdir.exactRef("master"));
1043 		assertNull(refdir.exactRef("v1.0"));
1044 	}
1045 
1046 	@Test
1047 	public void testGetRef_FetchHead() throws IOException {
1048 		// This is an odd special case where we need to make sure we read
1049 		// exactly the first 40 bytes of the file and nothing further on
1050 		// that line, or the remainder of the file.
1051 		write(new File(diskRepo.getDirectory(), "FETCH_HEAD"), A.name()
1052 				+ "\tnot-for-merge"
1053 				+ "\tbranch 'master' of git://egit.eclipse.org/jgit\n");
1054 
1055 		Ref r = refdir.getRef("FETCH_HEAD");
1056 		assertFalse(r.isSymbolic());
1057 		assertEquals(A, r.getObjectId());
1058 		assertEquals("FETCH_HEAD", r.getName());
1059 		assertFalse(r.isPeeled());
1060 		assertNull(r.getPeeledObjectId());
1061 	}
1062 
1063 	@Test
1064 	public void testExactRef_FetchHead() throws IOException {
1065 		// This is an odd special case where we need to make sure we read
1066 		// exactly the first 40 bytes of the file and nothing further on
1067 		// that line, or the remainder of the file.
1068 		write(new File(diskRepo.getDirectory(), "FETCH_HEAD"), A.name()
1069 				+ "\tnot-for-merge"
1070 				+ "\tbranch 'master' of git://egit.eclipse.org/jgit\n");
1071 
1072 		Ref r = refdir.exactRef("FETCH_HEAD");
1073 		assertFalse(r.isSymbolic());
1074 		assertEquals(A, r.getObjectId());
1075 		assertEquals("FETCH_HEAD", r.getName());
1076 		assertFalse(r.isPeeled());
1077 		assertNull(r.getPeeledObjectId());
1078 	}
1079 
1080 	@Test
1081 	public void testGetRef_AnyHeadWithGarbage() throws IOException {
1082 		write(new File(diskRepo.getDirectory(), "refs/heads/A"), A.name()
1083 				+ "012345 . this is not a standard reference\n"
1084 				+ "#and even more junk\n");
1085 
1086 		Ref r = refdir.getRef("refs/heads/A");
1087 		assertFalse(r.isSymbolic());
1088 		assertEquals(A, r.getObjectId());
1089 		assertEquals("refs/heads/A", r.getName());
1090 		assertFalse(r.isPeeled());
1091 		assertNull(r.getPeeledObjectId());
1092 	}
1093 
1094 	@Test
1095 	public void testGetRefs_CorruptSymbolicReference() throws IOException {
1096 		String name = "refs/heads/A";
1097 		writeLooseRef(name, "ref: \n");
1098 		assertTrue(refdir.getRefs(RefDatabase.ALL).isEmpty());
1099 	}
1100 
1101 	@Test
1102 	public void testGetRef_CorruptSymbolicReference() throws IOException {
1103 		String name = "refs/heads/A";
1104 		writeLooseRef(name, "ref: \n");
1105 		try {
1106 			refdir.getRef(name);
1107 			fail("read an invalid reference");
1108 		} catch (IOException err) {
1109 			String msg = err.getMessage();
1110 			assertEquals("Not a ref: " + name + ": ref:", msg);
1111 		}
1112 	}
1113 
1114 	@Test
1115 	public void testGetRefs_CorruptObjectIdReference() throws IOException {
1116 		String name = "refs/heads/A";
1117 		String content = "zoo" + A.name();
1118 		writeLooseRef(name, content + "\n");
1119 		assertTrue(refdir.getRefs(RefDatabase.ALL).isEmpty());
1120 	}
1121 
1122 	@Test
1123 	public void testGetRef_CorruptObjectIdReference() throws IOException {
1124 		String name = "refs/heads/A";
1125 		String content = "zoo" + A.name();
1126 		writeLooseRef(name, content + "\n");
1127 		try {
1128 			refdir.getRef(name);
1129 			fail("read an invalid reference");
1130 		} catch (IOException err) {
1131 			String msg = err.getMessage();
1132 			assertEquals("Not a ref: " + name + ": " + content, msg);
1133 		}
1134 	}
1135 
1136 	@Test
1137 	public void testIsNameConflicting() throws IOException {
1138 		writeLooseRef("refs/heads/a/b", A);
1139 		writePackedRef("refs/heads/q", B);
1140 
1141 		// new references cannot replace an existing container
1142 		assertTrue(refdir.isNameConflicting("refs"));
1143 		assertTrue(refdir.isNameConflicting("refs/heads"));
1144 		assertTrue(refdir.isNameConflicting("refs/heads/a"));
1145 
1146 		// existing reference is not conflicting
1147 		assertFalse(refdir.isNameConflicting("refs/heads/a/b"));
1148 
1149 		// new references are not conflicting
1150 		assertFalse(refdir.isNameConflicting("refs/heads/a/d"));
1151 		assertFalse(refdir.isNameConflicting("refs/heads/master"));
1152 
1153 		// existing reference must not be used as a container
1154 		assertTrue(refdir.isNameConflicting("refs/heads/a/b/c"));
1155 		assertTrue(refdir.isNameConflicting("refs/heads/q/master"));
1156 	}
1157 
1158 	@Test
1159 	public void testPeelLooseTag() throws IOException {
1160 		writeLooseRef("refs/tags/v1_0", v1_0);
1161 		writeLooseRef("refs/tags/current", "ref: refs/tags/v1_0\n");
1162 
1163 		final Ref tag = refdir.getRef("refs/tags/v1_0");
1164 		final Ref cur = refdir.getRef("refs/tags/current");
1165 
1166 		assertEquals(v1_0, tag.getObjectId());
1167 		assertFalse(tag.isSymbolic());
1168 		assertFalse(tag.isPeeled());
1169 		assertNull(tag.getPeeledObjectId());
1170 
1171 		assertEquals(v1_0, cur.getObjectId());
1172 		assertTrue(cur.isSymbolic());
1173 		assertFalse(cur.isPeeled());
1174 		assertNull(cur.getPeeledObjectId());
1175 
1176 		final Ref tag_p = refdir.peel(tag);
1177 		final Ref cur_p = refdir.peel(cur);
1178 
1179 		assertNotSame(tag, tag_p);
1180 		assertFalse(tag_p.isSymbolic());
1181 		assertTrue(tag_p.isPeeled());
1182 		assertEquals(v1_0, tag_p.getObjectId());
1183 		assertEquals(v1_0.getObject(), tag_p.getPeeledObjectId());
1184 		assertSame(tag_p, refdir.peel(tag_p));
1185 
1186 		assertNotSame(cur, cur_p);
1187 		assertEquals("refs/tags/current", cur_p.getName());
1188 		assertTrue(cur_p.isSymbolic());
1189 		assertEquals("refs/tags/v1_0", cur_p.getTarget().getName());
1190 		assertTrue(cur_p.isPeeled());
1191 		assertEquals(v1_0, cur_p.getObjectId());
1192 		assertEquals(v1_0.getObject(), cur_p.getPeeledObjectId());
1193 
1194 		// reuses cached peeling later, but not immediately due to
1195 		// the implementation so we have to fetch it once.
1196 		final Ref tag_p2 = refdir.getRef("refs/tags/v1_0");
1197 		assertFalse(tag_p2.isSymbolic());
1198 		assertTrue(tag_p2.isPeeled());
1199 		assertEquals(v1_0, tag_p2.getObjectId());
1200 		assertEquals(v1_0.getObject(), tag_p2.getPeeledObjectId());
1201 
1202 		assertSame(tag_p2, refdir.getRef("refs/tags/v1_0"));
1203 		assertSame(tag_p2, refdir.getRef("refs/tags/current").getTarget());
1204 		assertSame(tag_p2, refdir.peel(tag_p2));
1205 	}
1206 
1207 	@Test
1208 	public void testPeelCommit() throws IOException {
1209 		writeLooseRef("refs/heads/master", A);
1210 
1211 		Ref master = refdir.getRef("refs/heads/master");
1212 		assertEquals(A, master.getObjectId());
1213 		assertFalse(master.isPeeled());
1214 		assertNull(master.getPeeledObjectId());
1215 
1216 		Ref master_p = refdir.peel(master);
1217 		assertNotSame(master, master_p);
1218 		assertEquals(A, master_p.getObjectId());
1219 		assertTrue(master_p.isPeeled());
1220 		assertNull(master_p.getPeeledObjectId());
1221 
1222 		// reuses cached peeling later, but not immediately due to
1223 		// the implementation so we have to fetch it once.
1224 		Ref master_p2 = refdir.getRef("refs/heads/master");
1225 		assertNotSame(master, master_p2);
1226 		assertEquals(A, master_p2.getObjectId());
1227 		assertTrue(master_p2.isPeeled());
1228 		assertNull(master_p2.getPeeledObjectId());
1229 		assertSame(master_p2, refdir.peel(master_p2));
1230 	}
1231 
1232 	@Test
1233 	public void testRefsChangedStackOverflow() throws Exception {
1234 		final FileRepository newRepo = createBareRepository();
1235 		final RefDatabase refDb = newRepo.getRefDatabase();
1236 		File packedRefs = new File(newRepo.getDirectory(), "packed-refs");
1237 		assertTrue(packedRefs.createNewFile());
1238 		final AtomicReference<StackOverflowError> error = new AtomicReference<StackOverflowError>();
1239 		final AtomicReference<IOException> exception = new AtomicReference<IOException>();
1240 		final AtomicInteger changeCount = new AtomicInteger();
1241 		newRepo.getListenerList().addRefsChangedListener(
1242 				new RefsChangedListener() {
1243 
1244 					public void onRefsChanged(RefsChangedEvent event) {
1245 						try {
1246 							refDb.getRefs("ref");
1247 							changeCount.incrementAndGet();
1248 						} catch (StackOverflowError soe) {
1249 							error.set(soe);
1250 						} catch (IOException ioe) {
1251 							exception.set(ioe);
1252 						}
1253 					}
1254 				});
1255 		refDb.getRefs("ref");
1256 		refDb.getRefs("ref");
1257 		assertNull(error.get());
1258 		assertNull(exception.get());
1259 		assertEquals(1, changeCount.get());
1260 	}
1261 
1262 	@Test
1263 	public void testBatchRefUpdateSimpleNoForce() throws IOException {
1264 		writeLooseRef("refs/heads/master", A);
1265 		writeLooseRef("refs/heads/masters", B);
1266 		List<ReceiveCommand> commands = Arrays.asList(
1267 				newCommand(A, B, "refs/heads/master",
1268 						ReceiveCommand.Type.UPDATE),
1269 				newCommand(B, A, "refs/heads/masters",
1270 						ReceiveCommand.Type.UPDATE_NONFASTFORWARD));
1271 		BatchRefUpdate batchUpdate = refdir.newBatchUpdate();
1272 		batchUpdate.addCommand(commands);
1273 		batchUpdate.execute(new RevWalk(diskRepo), new StrictWorkMonitor());
1274 		Map<String, Ref> refs = refdir.getRefs(RefDatabase.ALL);
1275 		assertEquals(ReceiveCommand.Result.OK, commands.get(0).getResult());
1276 		assertEquals(ReceiveCommand.Result.REJECTED_NONFASTFORWARD, commands
1277 				.get(1).getResult());
1278 		assertEquals("[HEAD, refs/heads/master, refs/heads/masters]", refs
1279 				.keySet().toString());
1280 		assertEquals(B.getId(), refs.get("refs/heads/master").getObjectId());
1281 		assertEquals(B.getId(), refs.get("refs/heads/masters").getObjectId());
1282 	}
1283 
1284 	@Test
1285 	public void testBatchRefUpdateSimpleForce() throws IOException {
1286 		writeLooseRef("refs/heads/master", A);
1287 		writeLooseRef("refs/heads/masters", B);
1288 		List<ReceiveCommand> commands = Arrays.asList(
1289 				newCommand(A, B, "refs/heads/master",
1290 						ReceiveCommand.Type.UPDATE),
1291 				newCommand(B, A, "refs/heads/masters",
1292 						ReceiveCommand.Type.UPDATE_NONFASTFORWARD));
1293 		BatchRefUpdate batchUpdate = refdir.newBatchUpdate();
1294 		batchUpdate.setAllowNonFastForwards(true);
1295 		batchUpdate.addCommand(commands);
1296 		batchUpdate.execute(new RevWalk(diskRepo), new StrictWorkMonitor());
1297 		Map<String, Ref> refs = refdir.getRefs(RefDatabase.ALL);
1298 		assertEquals(ReceiveCommand.Result.OK, commands.get(0).getResult());
1299 		assertEquals(ReceiveCommand.Result.OK, commands.get(1).getResult());
1300 		assertEquals("[HEAD, refs/heads/master, refs/heads/masters]", refs
1301 				.keySet().toString());
1302 		assertEquals(B.getId(), refs.get("refs/heads/master").getObjectId());
1303 		assertEquals(A.getId(), refs.get("refs/heads/masters").getObjectId());
1304 	}
1305 
1306 	@Test
1307 	public void testBatchRefUpdateNonFastForwardDoesNotDoExpensiveMergeCheck()
1308 			throws IOException {
1309 		writeLooseRef("refs/heads/master", B);
1310 		List<ReceiveCommand> commands = Arrays.asList(
1311 				newCommand(B, A, "refs/heads/master",
1312 						ReceiveCommand.Type.UPDATE_NONFASTFORWARD));
1313 		BatchRefUpdate batchUpdate = refdir.newBatchUpdate();
1314 		batchUpdate.setAllowNonFastForwards(true);
1315 		batchUpdate.addCommand(commands);
1316 		batchUpdate.execute(new RevWalk(diskRepo) {
1317 			@Override
1318 			public boolean isMergedInto(RevCommit base, RevCommit tip) {
1319 				throw new AssertionError("isMergedInto() should not be called");
1320 			}
1321 		}, new StrictWorkMonitor());
1322 		Map<String, Ref> refs = refdir.getRefs(RefDatabase.ALL);
1323 		assertEquals(ReceiveCommand.Result.OK, commands.get(0).getResult());
1324 		assertEquals(A.getId(), refs.get("refs/heads/master").getObjectId());
1325 	}
1326 
1327 	@Test
1328 	public void testBatchRefUpdateConflict() throws IOException {
1329 		writeLooseRef("refs/heads/master", A);
1330 		writeLooseRef("refs/heads/masters", B);
1331 		List<ReceiveCommand> commands = Arrays.asList(
1332 				newCommand(A, B, "refs/heads/master",
1333 						ReceiveCommand.Type.UPDATE),
1334 				newCommand(null, A, "refs/heads/master/x",
1335 						ReceiveCommand.Type.CREATE),
1336 				newCommand(null, A, "refs/heads", ReceiveCommand.Type.CREATE));
1337 		BatchRefUpdate batchUpdate = refdir.newBatchUpdate();
1338 		batchUpdate.setAllowNonFastForwards(true);
1339 		batchUpdate.addCommand(commands);
1340 		batchUpdate
1341 				.execute(new RevWalk(diskRepo), NullProgressMonitor.INSTANCE);
1342 		Map<String, Ref> refs = refdir.getRefs(RefDatabase.ALL);
1343 		assertEquals(ReceiveCommand.Result.OK, commands.get(0).getResult());
1344 		assertEquals(ReceiveCommand.Result.LOCK_FAILURE, commands.get(1)
1345 				.getResult());
1346 		assertEquals(ReceiveCommand.Result.LOCK_FAILURE, commands.get(2)
1347 				.getResult());
1348 		assertEquals("[HEAD, refs/heads/master, refs/heads/masters]", refs
1349 				.keySet().toString());
1350 		assertEquals(B.getId(), refs.get("refs/heads/master").getObjectId());
1351 		assertEquals(B.getId(), refs.get("refs/heads/masters").getObjectId());
1352 	}
1353 
1354 	@Test
1355 	public void testBatchRefUpdateConflictThanksToDelete() throws IOException {
1356 		writeLooseRef("refs/heads/master", A);
1357 		writeLooseRef("refs/heads/masters", B);
1358 		List<ReceiveCommand> commands = Arrays.asList(
1359 				newCommand(A, B, "refs/heads/master",
1360 						ReceiveCommand.Type.UPDATE),
1361 				newCommand(null, A, "refs/heads/masters/x",
1362 						ReceiveCommand.Type.CREATE),
1363 				newCommand(B, null, "refs/heads/masters",
1364 						ReceiveCommand.Type.DELETE));
1365 		BatchRefUpdate batchUpdate = refdir.newBatchUpdate();
1366 		batchUpdate.setAllowNonFastForwards(true);
1367 		batchUpdate.addCommand(commands);
1368 		batchUpdate.execute(new RevWalk(diskRepo), new StrictWorkMonitor());
1369 		Map<String, Ref> refs = refdir.getRefs(RefDatabase.ALL);
1370 		assertEquals(ReceiveCommand.Result.OK, commands.get(0).getResult());
1371 		assertEquals(ReceiveCommand.Result.OK, commands.get(1).getResult());
1372 		assertEquals(ReceiveCommand.Result.OK, commands.get(2).getResult());
1373 		assertEquals("[HEAD, refs/heads/master, refs/heads/masters/x]", refs
1374 				.keySet().toString());
1375 		assertEquals(A.getId(), refs.get("refs/heads/masters/x").getObjectId());
1376 	}
1377 
1378 	private static ReceiveCommand newCommand(RevCommit a, RevCommit b,
1379 			String string, Type update) {
1380 		return new ReceiveCommand(a != null ? a.getId() : null,
1381 				b != null ? b.getId() : null, string, update);
1382 	}
1383 
1384 	private void writeLooseRef(String name, AnyObjectId id) throws IOException {
1385 		writeLooseRef(name, id.name() + "\n");
1386 	}
1387 
1388 	private void writeLooseRef(String name, String content) throws IOException {
1389 		write(new File(diskRepo.getDirectory(), name), content);
1390 	}
1391 
1392 	private void writePackedRef(String name, AnyObjectId id) throws IOException {
1393 		writePackedRefs(id.name() + " " + name + "\n");
1394 	}
1395 
1396 	private void writePackedRefs(String content) throws IOException {
1397 		File pr = new File(diskRepo.getDirectory(), "packed-refs");
1398 		write(pr, content);
1399 
1400 		final long now = System.currentTimeMillis();
1401 		final int oneHourAgo = 3600 * 1000;
1402 		pr.setLastModified(now - oneHourAgo);
1403 	}
1404 
1405 	private void deleteLooseRef(String name) {
1406 		File path = new File(diskRepo.getDirectory(), name);
1407 		assertTrue("deleted " + name, path.delete());
1408 	}
1409 
1410 	private static final class StrictWorkMonitor implements ProgressMonitor {
1411 		private int lastWork, totalWork;
1412 
1413 		public void start(int totalTasks) {
1414 			// empty
1415 		}
1416 
1417 		public void beginTask(String title, int totalWork) {
1418 			this.totalWork = totalWork;
1419 			lastWork = 0;
1420 		}
1421 
1422 		public void update(int completed) {
1423 			lastWork += completed;
1424 		}
1425 
1426 		public void endTask() {
1427 			assertEquals("Units of work recorded", totalWork, lastWork);
1428 		}
1429 
1430 		public boolean isCancelled() {
1431 			return false;
1432 		}
1433 	}
1434 }