View Javadoc
1   /*
2    * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import java.io.File;
16  import java.util.Collections;
17  
18  import org.eclipse.jgit.api.Git;
19  import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
20  import org.eclipse.jgit.lib.Constants;
21  import org.eclipse.jgit.lib.ObjectId;
22  import org.eclipse.jgit.lib.RefUpdate;
23  import org.eclipse.jgit.revwalk.RevCommit;
24  import org.eclipse.jgit.util.FileUtils;
25  import org.junit.Test;
26  
27  public class GcReflogTest extends GcTestCase {
28  	@Test
29  	public void testPruneNone() throws Exception {
30  		BranchBuilder bb = tr.branch("refs/heads/master");
31  		bb.commit().add("A", "A").add("B", "B").create();
32  		bb.commit().add("A", "A2").add("B", "B2").create();
33  		new File(repo.getDirectory(), Constants.LOGS + "/refs/heads/master")
34  				.delete();
35  		stats = gc.getStatistics();
36  		assertEquals(8, stats.numberOfLooseObjects);
37  		gc.setExpireAgeMillis(0);
38  		fsTick();
39  		gc.prune(Collections.<ObjectId> emptySet());
40  		stats = gc.getStatistics();
41  		assertEquals(8, stats.numberOfLooseObjects);
42  		tr.blob("x");
43  		stats = gc.getStatistics();
44  		assertEquals(9, stats.numberOfLooseObjects);
45  		fsTick();
46  		gc.prune(Collections.<ObjectId> emptySet());
47  		stats = gc.getStatistics();
48  		assertEquals(8, stats.numberOfLooseObjects);
49  	}
50  
51  	@Test
52  	public void testPackRepoWithCorruptReflog() throws Exception {
53  		// create a reflog entry "0000... 0000... foobar" by doing an initial
54  		// refupdate for HEAD which points to a non-existing ref. The
55  		// All-Projects repo of gerrit instances had such entries
56  		RefUpdate ru = repo.updateRef(Constants.HEAD);
57  		ru.link("refs/to/garbage");
58  		tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
59  				.create();
60  		// make sure HEAD exists
61  		Git.wrap(repo).checkout().setName("refs/heads/master").call();
62  		gc.gc().get();
63  	}
64  
65  	@Test
66  	public void testPackCommitsAndLooseOneNoReflog() throws Exception {
67  		BranchBuilder bb = tr.branch("refs/heads/master");
68  		RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
69  		bb.commit().add("A", "A2").add("B", "B2").create();
70  		tr.update("refs/heads/master", first);
71  
72  		stats = gc.getStatistics();
73  		assertEquals(8, stats.numberOfLooseObjects);
74  		assertEquals(0, stats.numberOfPackedObjects);
75  
76  		FileUtils.delete(new File(repo.getDirectory(), "logs/HEAD"),
77  				FileUtils.RETRY | FileUtils.SKIP_MISSING);
78  		FileUtils.delete(
79  				new File(repo.getDirectory(), "logs/refs/heads/master"),
80  				FileUtils.RETRY | FileUtils.SKIP_MISSING);
81  		gc.gc().get();
82  
83  		stats = gc.getStatistics();
84  		assertEquals(4, stats.numberOfLooseObjects);
85  		assertEquals(4, stats.numberOfPackedObjects);
86  		assertEquals(1, stats.numberOfPackFiles);
87  	}
88  
89  	@Test
90  	public void testPackCommitsAndLooseOneWithPruneNowNoReflog()
91  			throws Exception {
92  		BranchBuilder bb = tr.branch("refs/heads/master");
93  		RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
94  		bb.commit().add("A", "A2").add("B", "B2").create();
95  		tr.update("refs/heads/master", first);
96  
97  		stats = gc.getStatistics();
98  		assertEquals(8, stats.numberOfLooseObjects);
99  		assertEquals(0, stats.numberOfPackedObjects);
100 
101 		FileUtils.delete(new File(repo.getDirectory(), "logs/HEAD"),
102 				FileUtils.RETRY | FileUtils.SKIP_MISSING);
103 		FileUtils.delete(
104 				new File(repo.getDirectory(), "logs/refs/heads/master"),
105 				FileUtils.RETRY | FileUtils.SKIP_MISSING);
106 		gc.setExpireAgeMillis(0);
107 		gc.gc().get();
108 
109 		stats = gc.getStatistics();
110 		assertEquals(0, stats.numberOfLooseObjects);
111 		assertEquals(4, stats.numberOfPackedObjects);
112 		assertEquals(1, stats.numberOfPackFiles);
113 	}
114 }