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  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.File;
19  import java.util.Collections;
20  import java.util.Date;
21  
22  import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
23  import org.eclipse.jgit.lib.ObjectId;
24  import org.eclipse.jgit.revwalk.RevBlob;
25  import org.eclipse.jgit.revwalk.RevCommit;
26  import org.eclipse.jgit.revwalk.RevTree;
27  import org.junit.Test;
28  
29  public class GcPruneNonReferencedTest extends GcTestCase {
30  	@Test
31  	public void nonReferencedNonExpiredObject_notPruned() throws Exception {
32  		RevBlob a = tr.blob("a");
33  		gc.setExpire(new Date(lastModified(a)));
34  		gc.prune(Collections.<ObjectId> emptySet());
35  		assertTrue(repo.getObjectDatabase().has(a));
36  	}
37  
38  	@Test
39  	public void nonReferencedExpiredObject_pruned() throws Exception {
40  		RevBlob a = tr.blob("a");
41  		gc.setExpireAgeMillis(0);
42  		fsTick();
43  		gc.prune(Collections.<ObjectId> emptySet());
44  		assertFalse(repo.getObjectDatabase().has(a));
45  	}
46  
47  	@Test
48  	public void nonReferencedExpiredObjectTree_pruned() throws Exception {
49  		RevBlob a = tr.blob("a");
50  		RevTree t = tr.tree(tr.file("a", a));
51  		gc.setExpireAgeMillis(0);
52  		fsTick();
53  		gc.prune(Collections.<ObjectId> emptySet());
54  		assertFalse(repo.getObjectDatabase().has(t));
55  		assertFalse(repo.getObjectDatabase().has(a));
56  	}
57  
58  	@Test
59  	public void nonReferencedObjects_onlyExpiredPruned() throws Exception {
60  		RevBlob a = tr.blob("a");
61  		gc.setExpire(new Date(lastModified(a) + 1));
62  
63  		fsTick();
64  		RevBlob b = tr.blob("b");
65  
66  		gc.prune(Collections.<ObjectId> emptySet());
67  		assertFalse(repo.getObjectDatabase().has(a));
68  		assertTrue(repo.getObjectDatabase().has(b));
69  	}
70  
71  	@Test
72  	public void testPackCommitsAndLooseOneWithPruneNow() throws Exception {
73  		BranchBuilder bb = tr.branch("refs/heads/master");
74  		RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
75  		bb.commit().add("A", "A2").add("B", "B2").create();
76  		tr.update("refs/heads/master", first);
77  
78  		stats = gc.getStatistics();
79  		assertEquals(8, stats.numberOfLooseObjects);
80  		assertEquals(0, stats.numberOfPackedObjects);
81  		gc.setExpireAgeMillis(0);
82  		fsTick();
83  		gc.gc();
84  		stats = gc.getStatistics();
85  		assertNoEmptyFanoutDirectories();
86  		assertEquals(0, stats.numberOfLooseObjects);
87  		assertEquals(8, stats.numberOfPackedObjects);
88  		assertEquals(2, stats.numberOfPackFiles);
89  	}
90  
91  	private void assertNoEmptyFanoutDirectories() {
92  		File[] fanout = repo.getObjectsDirectory().listFiles();
93  		assertNotNull(fanout);
94  		for (File f : fanout) {
95  			if (f.isDirectory()) {
96  				String[] entries = f.list();
97  				if (entries == null || entries.length == 0) {
98  					assertFalse(
99  							"Found empty fanout directory "
100 									+ f.getAbsolutePath() + " after pruning",
101 							f.getName().length() == 2);
102 				}
103 			}
104 		}
105 	}
106 }