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.util.Iterator;
18  
19  import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
20  import org.eclipse.jgit.internal.storage.pack.PackExt;
21  import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
22  import org.junit.Test;
23  
24  public class GcKeepFilesTest extends GcTestCase {
25  	@Test
26  	public void testKeepFiles() throws Exception {
27  		BranchBuilder bb = tr.branch("refs/heads/master");
28  		bb.commit().add("A", "A").add("B", "B").create();
29  		stats = gc.getStatistics();
30  		assertEquals(4, stats.numberOfLooseObjects);
31  		assertEquals(0, stats.numberOfPackedObjects);
32  		assertEquals(0, stats.numberOfPackFiles);
33  		gc.gc().get();
34  		stats = gc.getStatistics();
35  		assertEquals(0, stats.numberOfLooseObjects);
36  		assertEquals(4, stats.numberOfPackedObjects);
37  		assertEquals(1, stats.numberOfPackFiles);
38  
39  		Iterator<Pack> packIt = repo.getObjectDatabase().getPacks()
40  				.iterator();
41  		Pack singlePack = packIt.next();
42  		assertFalse(packIt.hasNext());
43  		PackFile keepFile = singlePack.getPackFile().create(PackExt.KEEP);
44  		assertFalse(keepFile.exists());
45  		assertTrue(keepFile.createNewFile());
46  		bb.commit().add("A", "A2").add("B", "B2").create();
47  		stats = gc.getStatistics();
48  		assertEquals(4, stats.numberOfLooseObjects);
49  		assertEquals(4, stats.numberOfPackedObjects);
50  		assertEquals(1, stats.numberOfPackFiles);
51  		gc.gc().get();
52  		stats = gc.getStatistics();
53  		assertEquals(0, stats.numberOfLooseObjects);
54  		assertEquals(8, stats.numberOfPackedObjects);
55  		assertEquals(2, stats.numberOfPackFiles);
56  
57  		// check that no object is packed twice
58  		Iterator<Pack> packs = repo.getObjectDatabase().getPacks()
59  				.iterator();
60  		PackIndex ind1 = packs.next().getIndex();
61  		assertEquals(4, ind1.getObjectCount());
62  		PackIndex ind2 = packs.next().getIndex();
63  		assertEquals(4, ind2.getObjectCount());
64  		for (MutableEntry e: ind1)
65  			if (ind2.hasObject(e.toObjectId()))
66  				assertFalse(
67  						"the following object is in both packfiles: "
68  								+ e.toObjectId(),
69  						ind2.hasObject(e.toObjectId()));
70  	}
71  }