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