1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import static org.junit.Assert.assertEquals;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.util.Collection;
51 import java.util.Date;
52 import java.util.Iterator;
53
54 import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
55 import org.eclipse.jgit.revwalk.RevCommit;
56 import org.eclipse.jgit.storage.pack.PackConfig;
57 import org.junit.Test;
58 import org.junit.experimental.theories.DataPoints;
59 import org.junit.experimental.theories.Theories;
60 import org.junit.experimental.theories.Theory;
61 import org.junit.runner.RunWith;
62
63 @RunWith(Theories.class)
64 public class GcBasicPackingTest extends GcTestCase {
65 @DataPoints
66 public static boolean[] aggressiveValues = { true, false };
67
68 @Theory
69 public void repackEmptyRepo_noPackCreated(boolean aggressive)
70 throws IOException {
71 configureGc(gc, aggressive);
72 gc.repack();
73 assertEquals(0, repo.getObjectDatabase().getPacks().size());
74 }
75
76 @Theory
77 public void testPackRepoWithNoRefs(boolean aggressive) throws Exception {
78 tr.commit().add("A", "A").add("B", "B").create();
79 stats = gc.getStatistics();
80 assertEquals(4, stats.numberOfLooseObjects);
81 assertEquals(0, stats.numberOfPackedObjects);
82 configureGc(gc, aggressive);
83 gc.gc();
84 stats = gc.getStatistics();
85 assertEquals(4, stats.numberOfLooseObjects);
86 assertEquals(0, stats.numberOfPackedObjects);
87 assertEquals(0, stats.numberOfPackFiles);
88 }
89
90 @Theory
91 public void testPack2Commits(boolean aggressive) throws Exception {
92 BranchBuilder bb = tr.branch("refs/heads/master");
93 bb.commit().add("A", "A").add("B", "B").create();
94 bb.commit().add("A", "A2").add("B", "B2").create();
95
96 stats = gc.getStatistics();
97 assertEquals(8, stats.numberOfLooseObjects);
98 assertEquals(0, stats.numberOfPackedObjects);
99 configureGc(gc, aggressive);
100 gc.gc();
101 stats = gc.getStatistics();
102 assertEquals(0, stats.numberOfLooseObjects);
103 assertEquals(8, stats.numberOfPackedObjects);
104 assertEquals(1, stats.numberOfPackFiles);
105 }
106
107 @Theory
108 public void testPackAllObjectsInOnePack(boolean aggressive)
109 throws Exception {
110 tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
111 .create();
112 stats = gc.getStatistics();
113 assertEquals(4, stats.numberOfLooseObjects);
114 assertEquals(0, stats.numberOfPackedObjects);
115 configureGc(gc, aggressive);
116 gc.gc();
117 stats = gc.getStatistics();
118 assertEquals(0, stats.numberOfLooseObjects);
119 assertEquals(4, stats.numberOfPackedObjects);
120 assertEquals(1, stats.numberOfPackFiles);
121
122
123 gc.gc();
124 stats = gc.getStatistics();
125 assertEquals(0, stats.numberOfLooseObjects);
126 assertEquals(4, stats.numberOfPackedObjects);
127 assertEquals(1, stats.numberOfPackFiles);
128 }
129
130 @Theory
131 public void testPackCommitsAndLooseOne(boolean aggressive) throws Exception {
132 BranchBuilder bb = tr.branch("refs/heads/master");
133 RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
134 bb.commit().add("A", "A2").add("B", "B2").create();
135 tr.update("refs/heads/master", first);
136
137 stats = gc.getStatistics();
138 assertEquals(8, stats.numberOfLooseObjects);
139 assertEquals(0, stats.numberOfPackedObjects);
140 configureGc(gc, aggressive);
141 gc.gc();
142 stats = gc.getStatistics();
143 assertEquals(0, stats.numberOfLooseObjects);
144 assertEquals(8, stats.numberOfPackedObjects);
145 assertEquals(2, stats.numberOfPackFiles);
146 }
147
148 @Theory
149 public void testNotPackTwice(boolean aggressive) throws Exception {
150 BranchBuilder bb = tr.branch("refs/heads/master");
151 RevCommit first = bb.commit().message("M").add("M", "M").create();
152 bb.commit().message("B").add("B", "Q").create();
153 bb.commit().message("A").add("A", "A").create();
154 RevCommit second = tr.commit().parent(first).message("R").add("R", "Q")
155 .create();
156 tr.update("refs/tags/t1", second);
157
158 Collection<PackFile> oldPacks = tr.getRepository().getObjectDatabase()
159 .getPacks();
160 assertEquals(0, oldPacks.size());
161 stats = gc.getStatistics();
162 assertEquals(11, stats.numberOfLooseObjects);
163 assertEquals(0, stats.numberOfPackedObjects);
164
165 gc.setExpireAgeMillis(0);
166 fsTick();
167 configureGc(gc, aggressive);
168 gc.gc();
169 stats = gc.getStatistics();
170 assertEquals(0, stats.numberOfLooseObjects);
171
172 Iterator<PackFile> pIt = repo.getObjectDatabase().getPacks().iterator();
173 long c = pIt.next().getObjectCount();
174 if (c == 9)
175 assertEquals(2, pIt.next().getObjectCount());
176 else {
177 assertEquals(2, c);
178 assertEquals(9, pIt.next().getObjectCount());
179 }
180 }
181
182 @Test
183 public void testDonePruneTooYoungPacks() throws Exception {
184 BranchBuilder bb = tr.branch("refs/heads/master");
185 bb.commit().message("M").add("M", "M").create();
186
187 gc.setExpireAgeMillis(0);
188 gc.gc();
189 stats = gc.getStatistics();
190 assertEquals(0, stats.numberOfLooseObjects);
191 assertEquals(3, stats.numberOfPackedObjects);
192 assertEquals(1, stats.numberOfPackFiles);
193 File oldPackfile = tr.getRepository().getObjectDatabase().getPacks()
194 .iterator().next().getPackFile();
195
196 fsTick();
197 bb.commit().message("B").add("B", "Q").create();
198
199
200
201 gc.setExpire(new Date(oldPackfile.lastModified() - 1));
202 gc.gc();
203 stats = gc.getStatistics();
204 assertEquals(0, stats.numberOfLooseObjects);
205
206
207 assertEquals(9, stats.numberOfPackedObjects);
208 assertEquals(2, stats.numberOfPackFiles);
209
210
211
212 gc.setExpireAgeMillis(0);
213 gc.gc();
214 stats = gc.getStatistics();
215 assertEquals(0, stats.numberOfLooseObjects);
216
217
218 assertEquals(6, stats.numberOfPackedObjects);
219 assertEquals(1, stats.numberOfPackFiles);
220
221 }
222
223 private void configureGc(GC myGc, boolean aggressive) {
224 PackConfig pconfig = new PackConfig(repo);
225 if (aggressive) {
226 pconfig.setDeltaSearchWindowSize(250);
227 pconfig.setMaxDeltaDepth(250);
228 pconfig.setReuseObjects(false);
229 } else
230 pconfig = new PackConfig(repo);
231 myGc.setPackConfig(pconfig);
232 }
233 }