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
45 package org.eclipse.jgit.internal.storage.file;
46
47 import static org.junit.Assert.assertArrayEquals;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertFalse;
50 import static org.junit.Assert.assertNotNull;
51 import static org.junit.Assert.assertNotSame;
52 import static org.junit.Assert.fail;
53
54 import java.io.BufferedOutputStream;
55 import java.io.File;
56 import java.io.FileOutputStream;
57 import java.io.IOException;
58 import java.io.OutputStream;
59
60 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
61 import org.eclipse.jgit.errors.MissingObjectException;
62 import org.eclipse.jgit.internal.storage.pack.PackWriter;
63 import org.eclipse.jgit.junit.RepositoryTestCase;
64 import org.eclipse.jgit.lib.AnyObjectId;
65 import org.eclipse.jgit.lib.Constants;
66 import org.eclipse.jgit.lib.NullProgressMonitor;
67 import org.eclipse.jgit.lib.ObjectId;
68 import org.eclipse.jgit.lib.ObjectInserter;
69 import org.eclipse.jgit.lib.ObjectLoader;
70 import org.eclipse.jgit.lib.Repository;
71 import org.eclipse.jgit.revwalk.RevObject;
72 import org.eclipse.jgit.revwalk.RevWalk;
73 import org.eclipse.jgit.storage.file.WindowCacheConfig;
74 import org.eclipse.jgit.util.FileUtils;
75 import org.junit.After;
76 import org.junit.Before;
77 import org.junit.Test;
78
79 public class ConcurrentRepackTest extends RepositoryTestCase {
80 @Override
81 @Before
82 public void setUp() throws Exception {
83 WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
84 windowCacheConfig.setPackedGitOpenFiles(1);
85 windowCacheConfig.install();
86 super.setUp();
87 }
88
89 @Override
90 @After
91 public void tearDown() throws Exception {
92 super.tearDown();
93 new WindowCacheConfig().install();
94 }
95
96 @Test
97 public void testObjectInNewPack() throws IncorrectObjectTypeException,
98 IOException {
99
100
101 final Repository eden = createBareRepository();
102 final RevObject o1 = writeBlob(eden, "o1");
103 pack(eden, o1);
104 assertEquals(o1.name(), parse(o1).name());
105 }
106
107 @Test
108 public void testObjectMovedToNewPack1()
109 throws IncorrectObjectTypeException, IOException {
110
111
112
113
114 final Repository eden = createBareRepository();
115 final RevObject o1 = writeBlob(eden, "o1");
116 final File[] out1 = pack(eden, o1);
117 assertEquals(o1.name(), parse(o1).name());
118
119 final RevObject o2 = writeBlob(eden, "o2");
120 pack(eden, o2, o1);
121
122
123
124 whackCache();
125 delete(out1);
126
127
128
129
130 assertEquals(o2.name(), parse(o2).name());
131 assertEquals(o1.name(), parse(o1).name());
132 }
133
134 @Test
135 public void testObjectMovedWithinPack()
136 throws IncorrectObjectTypeException, IOException {
137
138
139 final Repository eden = createBareRepository();
140 final RevObject o1 = writeBlob(eden, "o1");
141 final File[] out1 = pack(eden, o1);
142 assertEquals(o1.name(), parse(o1).name());
143
144
145
146 whackCache();
147
148
149
150
151
152
153 final RevObject o2 = writeBlob(eden, "o2");
154 try (PackWriter pw = new PackWriter(eden)) {
155 pw.addObject(o2);
156 pw.addObject(o1);
157 write(out1, pw);
158 }
159
160
161
162
163 assertEquals(o1.name(), parse(o1).name());
164 assertEquals(o2.name(), parse(o2).name());
165 }
166
167 @Test
168 public void testObjectMovedToNewPack2()
169 throws IncorrectObjectTypeException, IOException {
170
171
172
173
174 final Repository eden = createBareRepository();
175 final RevObject o1 = writeBlob(eden, "o1");
176 final File[] out1 = pack(eden, o1);
177 assertEquals(o1.name(), parse(o1).name());
178
179 final ObjectLoader load1 = db.open(o1, Constants.OBJ_BLOB);
180 assertNotNull(load1);
181
182 final RevObject o2 = writeBlob(eden, "o2");
183 pack(eden, o2, o1);
184
185
186
187 whackCache();
188 delete(out1);
189
190
191
192
193
194 final ObjectLoader load2 = db.open(o1, Constants.OBJ_BLOB);
195 assertNotNull(load2);
196 assertNotSame(load1, load2);
197
198 final byte[] data2 = load2.getCachedBytes();
199 final byte[] data1 = load1.getCachedBytes();
200 assertNotNull(data2);
201 assertNotNull(data1);
202 assertNotSame(data1, data2);
203 assertArrayEquals(data1, data2);
204 assertEquals(load2.getType(), load1.getType());
205 }
206
207 private static void whackCache() {
208 final WindowCacheConfig config = new WindowCacheConfig();
209 config.setPackedGitOpenFiles(1);
210 config.install();
211 }
212
213 private RevObject parse(AnyObjectId id)
214 throws MissingObjectException, IOException {
215 try (RevWalk rw = new RevWalk(db)) {
216 return rw.parseAny(id);
217 }
218 }
219
220 private File[] pack(Repository src, RevObject... list)
221 throws IOException {
222 try (PackWriter pw = new PackWriter(src)) {
223 for (RevObject o : list) {
224 pw.addObject(o);
225 }
226
227 final ObjectId name = pw.computeName();
228 final File packFile = fullPackFileName(name, ".pack");
229 final File idxFile = fullPackFileName(name, ".idx");
230 final File[] files = new File[] { packFile, idxFile };
231 write(files, pw);
232 return files;
233 }
234 }
235
236 private static void write(File[] files, PackWriter pw)
237 throws IOException {
238 final long begin = files[0].getParentFile().lastModified();
239 NullProgressMonitor m = NullProgressMonitor.INSTANCE;
240
241 try (OutputStream out = new BufferedOutputStream(
242 new FileOutputStream(files[0]))) {
243 pw.writePack(m, m, out);
244 }
245
246 try (OutputStream out = new BufferedOutputStream(
247 new FileOutputStream(files[1]))) {
248 pw.writeIndex(out);
249 }
250
251 touch(begin, files[0].getParentFile());
252 }
253
254 private static void delete(File[] list) throws IOException {
255 final long begin = list[0].getParentFile().lastModified();
256 for (File f : list) {
257 FileUtils.delete(f);
258 assertFalse(f + " was removed", f.exists());
259 }
260 touch(begin, list[0].getParentFile());
261 }
262
263 private static void touch(long begin, File dir) {
264 while (begin >= dir.lastModified()) {
265 try {
266 Thread.sleep(25);
267 } catch (InterruptedException ie) {
268
269 }
270 dir.setLastModified(System.currentTimeMillis());
271 }
272 }
273
274 private File fullPackFileName(ObjectId name, String suffix) {
275 final File packdir = db.getObjectDatabase().getPackDirectory();
276 return new File(packdir, "pack-" + name.name() + suffix);
277 }
278
279 private RevObject writeBlob(Repository repo, String data)
280 throws IOException {
281 final byte[] bytes = Constants.encode(data);
282 final ObjectId id;
283 try (ObjectInserter inserter = repo.newObjectInserter()) {
284 id = inserter.insert(Constants.OBJ_BLOB, bytes);
285 inserter.flush();
286 }
287 try {
288 parse(id);
289 fail("Object " + id.name() + " should not exist in test repository");
290 } catch (MissingObjectException e) {
291
292 }
293 try (RevWalk revWalk = new RevWalk(repo)) {
294 return revWalk.lookupBlob(id);
295 }
296 }
297 }