View Javadoc
1   /*
2    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.internal.storage.file;
45  
46  import static org.eclipse.jgit.internal.storage.pack.PackWriter.NONE;
47  import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
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.assertTrue;
52  import static org.junit.Assert.fail;
53  
54  import java.io.ByteArrayInputStream;
55  import java.io.ByteArrayOutputStream;
56  import java.io.File;
57  import java.io.FileOutputStream;
58  import java.io.IOException;
59  import java.text.ParseException;
60  import java.util.ArrayList;
61  import java.util.Arrays;
62  import java.util.Collections;
63  import java.util.Comparator;
64  import java.util.HashSet;
65  import java.util.List;
66  import java.util.Set;
67  
68  import org.eclipse.jgit.errors.MissingObjectException;
69  import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
70  import org.eclipse.jgit.internal.storage.pack.PackWriter;
71  import org.eclipse.jgit.junit.JGitTestUtil;
72  import org.eclipse.jgit.junit.TestRepository;
73  import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
74  import org.eclipse.jgit.lib.NullProgressMonitor;
75  import org.eclipse.jgit.lib.ObjectId;
76  import org.eclipse.jgit.lib.ObjectIdSet;
77  import org.eclipse.jgit.lib.ObjectInserter;
78  import org.eclipse.jgit.lib.Repository;
79  import org.eclipse.jgit.lib.Sets;
80  import org.eclipse.jgit.revwalk.DepthWalk;
81  import org.eclipse.jgit.revwalk.ObjectWalk;
82  import org.eclipse.jgit.revwalk.RevBlob;
83  import org.eclipse.jgit.revwalk.RevCommit;
84  import org.eclipse.jgit.revwalk.RevObject;
85  import org.eclipse.jgit.revwalk.RevWalk;
86  import org.eclipse.jgit.storage.pack.PackConfig;
87  import org.eclipse.jgit.storage.pack.PackStatistics;
88  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
89  import org.eclipse.jgit.transport.PackParser;
90  import org.junit.After;
91  import org.junit.Before;
92  import org.junit.Test;
93  
94  public class PackWriterTest extends SampleDataRepositoryTestCase {
95  
96  	private static final List<RevObject> EMPTY_LIST_REVS = Collections
97  			.<RevObject> emptyList();
98  
99  	private static final Set<ObjectIdSet> EMPTY_ID_SET = Collections
100 			.<ObjectIdSet> emptySet();
101 
102 	private PackConfig config;
103 
104 	private PackWriter writer;
105 
106 	private ByteArrayOutputStream os;
107 
108 	private PackFile pack;
109 
110 	private ObjectInserter inserter;
111 
112 	private FileRepository dst;
113 
114 	private RevBlob contentA;
115 
116 	private RevBlob contentB;
117 
118 	private RevBlob contentC;
119 
120 	private RevBlob contentD;
121 
122 	private RevBlob contentE;
123 
124 	private RevCommit c1;
125 
126 	private RevCommit c2;
127 
128 	private RevCommit c3;
129 
130 	private RevCommit c4;
131 
132 	private RevCommit c5;
133 
134 	@Override
135 	@Before
136 	public void setUp() throws Exception {
137 		super.setUp();
138 		os = new ByteArrayOutputStream();
139 		config = new PackConfig(db);
140 
141 		dst = createBareRepository();
142 		File alt = new File(dst.getObjectDatabase().getDirectory(), "info/alternates");
143 		alt.getParentFile().mkdirs();
144 		write(alt, db.getObjectDatabase().getDirectory().getAbsolutePath() + "\n");
145 	}
146 
147 	@Override
148 	@After
149 	public void tearDown() throws Exception {
150 		if (writer != null) {
151 			writer.close();
152 			writer = null;
153 		}
154 		if (inserter != null) {
155 			inserter.close();
156 			inserter = null;
157 		}
158 		super.tearDown();
159 	}
160 
161 	/**
162 	 * Test constructor for exceptions, default settings, initialization.
163 	 *
164 	 * @throws IOException
165 	 */
166 	@Test
167 	public void testContructor() throws IOException {
168 		writer = new PackWriter(config, db.newObjectReader());
169 		assertFalse(writer.isDeltaBaseAsOffset());
170 		assertTrue(config.isReuseDeltas());
171 		assertTrue(config.isReuseObjects());
172 		assertEquals(0, writer.getObjectCount());
173 	}
174 
175 	/**
176 	 * Change default settings and verify them.
177 	 */
178 	@Test
179 	public void testModifySettings() {
180 		config.setReuseDeltas(false);
181 		config.setReuseObjects(false);
182 		config.setDeltaBaseAsOffset(false);
183 		assertFalse(config.isReuseDeltas());
184 		assertFalse(config.isReuseObjects());
185 		assertFalse(config.isDeltaBaseAsOffset());
186 
187 		writer = new PackWriter(config, db.newObjectReader());
188 		writer.setDeltaBaseAsOffset(true);
189 		assertTrue(writer.isDeltaBaseAsOffset());
190 		assertFalse(config.isDeltaBaseAsOffset());
191 	}
192 
193 	/**
194 	 * Write empty pack by providing empty sets of interesting/uninteresting
195 	 * objects and check for correct format.
196 	 *
197 	 * @throws IOException
198 	 */
199 	@Test
200 	public void testWriteEmptyPack1() throws IOException {
201 		createVerifyOpenPack(NONE, NONE, false, false);
202 
203 		assertEquals(0, writer.getObjectCount());
204 		assertEquals(0, pack.getObjectCount());
205 		assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", writer
206 				.computeName().name());
207 	}
208 
209 	/**
210 	 * Write empty pack by providing empty iterator of objects to write and
211 	 * check for correct format.
212 	 *
213 	 * @throws IOException
214 	 */
215 	@Test
216 	public void testWriteEmptyPack2() throws IOException {
217 		createVerifyOpenPack(EMPTY_LIST_REVS);
218 
219 		assertEquals(0, writer.getObjectCount());
220 		assertEquals(0, pack.getObjectCount());
221 	}
222 
223 	/**
224 	 * Try to pass non-existing object as uninteresting, with non-ignoring
225 	 * setting.
226 	 *
227 	 * @throws IOException
228 	 */
229 	@Test
230 	public void testNotIgnoreNonExistingObjects() throws IOException {
231 		final ObjectId nonExisting = ObjectId
232 				.fromString("0000000000000000000000000000000000000001");
233 		try {
234 			createVerifyOpenPack(NONE, haves(nonExisting), false, false);
235 			fail("Should have thrown MissingObjectException");
236 		} catch (MissingObjectException x) {
237 			// expected
238 		}
239 	}
240 
241 	/**
242 	 * Try to pass non-existing object as uninteresting, with ignoring setting.
243 	 *
244 	 * @throws IOException
245 	 */
246 	@Test
247 	public void testIgnoreNonExistingObjects() throws IOException {
248 		final ObjectId nonExisting = ObjectId
249 				.fromString("0000000000000000000000000000000000000001");
250 		createVerifyOpenPack(NONE, haves(nonExisting), false, true);
251 		// shouldn't throw anything
252 	}
253 
254 	/**
255 	 * Try to pass non-existing object as uninteresting, with ignoring setting.
256 	 * Use a repo with bitmap indexes because then PackWriter will use
257 	 * PackWriterBitmapWalker which had problems with this situation.
258 	 *
259 	 * @throws IOException
260 	 * @throws ParseException
261 	 */
262 	@Test
263 	public void testIgnoreNonExistingObjectsWithBitmaps() throws IOException,
264 			ParseException {
265 		final ObjectId nonExisting = ObjectId
266 				.fromString("0000000000000000000000000000000000000001");
267 		new GC(db).gc();
268 		createVerifyOpenPack(NONE, haves(nonExisting), false, true, true);
269 		// shouldn't throw anything
270 	}
271 
272 	/**
273 	 * Create pack basing on only interesting objects, then precisely verify
274 	 * content. No delta reuse here.
275 	 *
276 	 * @throws IOException
277 	 */
278 	@Test
279 	public void testWritePack1() throws IOException {
280 		config.setReuseDeltas(false);
281 		writeVerifyPack1();
282 	}
283 
284 	/**
285 	 * Test writing pack without object reuse. Pack content/preparation as in
286 	 * {@link #testWritePack1()}.
287 	 *
288 	 * @throws IOException
289 	 */
290 	@Test
291 	public void testWritePack1NoObjectReuse() throws IOException {
292 		config.setReuseDeltas(false);
293 		config.setReuseObjects(false);
294 		writeVerifyPack1();
295 	}
296 
297 	/**
298 	 * Create pack basing on both interesting and uninteresting objects, then
299 	 * precisely verify content. No delta reuse here.
300 	 *
301 	 * @throws IOException
302 	 */
303 	@Test
304 	public void testWritePack2() throws IOException {
305 		writeVerifyPack2(false);
306 	}
307 
308 	/**
309 	 * Test pack writing with deltas reuse, delta-base first rule. Pack
310 	 * content/preparation as in {@link #testWritePack2()}.
311 	 *
312 	 * @throws IOException
313 	 */
314 	@Test
315 	public void testWritePack2DeltasReuseRefs() throws IOException {
316 		writeVerifyPack2(true);
317 	}
318 
319 	/**
320 	 * Test pack writing with delta reuse. Delta bases referred as offsets. Pack
321 	 * configuration as in {@link #testWritePack2DeltasReuseRefs()}.
322 	 *
323 	 * @throws IOException
324 	 */
325 	@Test
326 	public void testWritePack2DeltasReuseOffsets() throws IOException {
327 		config.setDeltaBaseAsOffset(true);
328 		writeVerifyPack2(true);
329 	}
330 
331 	/**
332 	 * Test pack writing with delta reuse. Raw-data copy (reuse) is made on a
333 	 * pack with CRC32 index. Pack configuration as in
334 	 * {@link #testWritePack2DeltasReuseRefs()}.
335 	 *
336 	 * @throws IOException
337 	 */
338 	@Test
339 	public void testWritePack2DeltasCRC32Copy() throws IOException {
340 		final File packDir = db.getObjectDatabase().getPackDirectory();
341 		final File crc32Pack = new File(packDir,
342 				"pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack");
343 		final File crc32Idx = new File(packDir,
344 				"pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.idx");
345 		copyFile(JGitTestUtil.getTestResourceFile(
346 				"pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.idxV2"),
347 				crc32Idx);
348 		db.openPack(crc32Pack);
349 
350 		writeVerifyPack2(true);
351 	}
352 
353 	/**
354 	 * Create pack basing on fixed objects list, then precisely verify content.
355 	 * No delta reuse here.
356 	 *
357 	 * @throws IOException
358 	 * @throws MissingObjectException
359 	 *
360 	 */
361 	@Test
362 	public void testWritePack3() throws MissingObjectException, IOException {
363 		config.setReuseDeltas(false);
364 		final ObjectId forcedOrder[] = new ObjectId[] {
365 				ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
366 				ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
367 				ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
368 				ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
369 				ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") ,
370 				ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259") };
371 		try (RevWalk parser = new RevWalk(db)) {
372 			final RevObject forcedOrderRevs[] = new RevObject[forcedOrder.length];
373 			for (int i = 0; i < forcedOrder.length; i++)
374 				forcedOrderRevs[i] = parser.parseAny(forcedOrder[i]);
375 
376 			createVerifyOpenPack(Arrays.asList(forcedOrderRevs));
377 		}
378 
379 		assertEquals(forcedOrder.length, writer.getObjectCount());
380 		verifyObjectsOrder(forcedOrder);
381 		assertEquals("ed3f96b8327c7c66b0f8f70056129f0769323d86", writer
382 				.computeName().name());
383 	}
384 
385 	/**
386 	 * Another pack creation: basing on both interesting and uninteresting
387 	 * objects. No delta reuse possible here, as this is a specific case when we
388 	 * write only 1 commit, associated with 1 tree, 1 blob.
389 	 *
390 	 * @throws IOException
391 	 */
392 	@Test
393 	public void testWritePack4() throws IOException {
394 		writeVerifyPack4(false);
395 	}
396 
397 	/**
398 	 * Test thin pack writing: 1 blob delta base is on objects edge. Pack
399 	 * configuration as in {@link #testWritePack4()}.
400 	 *
401 	 * @throws IOException
402 	 */
403 	@Test
404 	public void testWritePack4ThinPack() throws IOException {
405 		writeVerifyPack4(true);
406 	}
407 
408 	/**
409 	 * Compare sizes of packs created using {@link #testWritePack2()} and
410 	 * {@link #testWritePack2DeltasReuseRefs()}. The pack using deltas should
411 	 * be smaller.
412 	 *
413 	 * @throws Exception
414 	 */
415 	@Test
416 	public void testWritePack2SizeDeltasVsNoDeltas() throws Exception {
417 		config.setReuseDeltas(false);
418 		config.setDeltaCompress(false);
419 		testWritePack2();
420 		final long sizePack2NoDeltas = os.size();
421 		tearDown();
422 		setUp();
423 		testWritePack2DeltasReuseRefs();
424 		final long sizePack2DeltasRefs = os.size();
425 
426 		assertTrue(sizePack2NoDeltas > sizePack2DeltasRefs);
427 	}
428 
429 	/**
430 	 * Compare sizes of packs created using
431 	 * {@link #testWritePack2DeltasReuseRefs()} and
432 	 * {@link #testWritePack2DeltasReuseOffsets()}. The pack with delta bases
433 	 * written as offsets should be smaller.
434 	 *
435 	 * @throws Exception
436 	 */
437 	@Test
438 	public void testWritePack2SizeOffsetsVsRefs() throws Exception {
439 		testWritePack2DeltasReuseRefs();
440 		final long sizePack2DeltasRefs = os.size();
441 		tearDown();
442 		setUp();
443 		testWritePack2DeltasReuseOffsets();
444 		final long sizePack2DeltasOffsets = os.size();
445 
446 		assertTrue(sizePack2DeltasRefs > sizePack2DeltasOffsets);
447 	}
448 
449 	/**
450 	 * Compare sizes of packs created using {@link #testWritePack4()} and
451 	 * {@link #testWritePack4ThinPack()}. Obviously, the thin pack should be
452 	 * smaller.
453 	 *
454 	 * @throws Exception
455 	 */
456 	@Test
457 	public void testWritePack4SizeThinVsNoThin() throws Exception {
458 		testWritePack4();
459 		final long sizePack4 = os.size();
460 		tearDown();
461 		setUp();
462 		testWritePack4ThinPack();
463 		final long sizePack4Thin = os.size();
464 
465 		assertTrue(sizePack4 > sizePack4Thin);
466 	}
467 
468 	@Test
469 	public void testDeltaStatistics() throws Exception {
470 		config.setDeltaCompress(true);
471 		FileRepository repo = createBareRepository();
472 		TestRepository<FileRepository> testRepo = new TestRepository<>(repo);
473 		ArrayList<RevObject> blobs = new ArrayList<>();
474 		blobs.add(testRepo.blob(genDeltableData(1000)));
475 		blobs.add(testRepo.blob(genDeltableData(1005)));
476 
477 		try (PackWriter pw = new PackWriter(repo)) {
478 			NullProgressMonitor m = NullProgressMonitor.INSTANCE;
479 			pw.preparePack(blobs.iterator());
480 			pw.writePack(m, m, os);
481 			PackStatistics stats = pw.getStatistics();
482 			assertEquals(1, stats.getTotalDeltas());
483 			assertTrue("Delta bytes not set.",
484 					stats.byObjectType(OBJ_BLOB).getDeltaBytes() > 0);
485 		}
486 	}
487 
488 	// Generate consistent junk data for building files that delta well
489 	private String genDeltableData(int length) {
490 		assertTrue("Generated data must have a length > 0", length > 0);
491 		char[] data = {'a', 'b', 'c', '\n'};
492 		StringBuilder builder = new StringBuilder(length);
493 		for (int i = 0; i < length; i++) {
494 			builder.append(data[i % 4]);
495 		}
496 		return builder.toString();
497 	}
498 
499 
500 	@Test
501 	public void testWriteIndex() throws Exception {
502 		config.setIndexVersion(2);
503 		writeVerifyPack4(false);
504 
505 		File packFile = pack.getPackFile();
506 		String name = packFile.getName();
507 		String base = name.substring(0, name.lastIndexOf('.'));
508 		File indexFile = new File(packFile.getParentFile(), base + ".idx");
509 
510 		// Validate that IndexPack came up with the right CRC32 value.
511 		final PackIndex idx1 = PackIndex.open(indexFile);
512 		assertTrue(idx1 instanceof PackIndexV2);
513 		assertEquals(0x4743F1E4L, idx1.findCRC32(ObjectId
514 				.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7")));
515 
516 		// Validate that an index written by PackWriter is the same.
517 		final File idx2File = new File(indexFile.getAbsolutePath() + ".2");
518 		try (FileOutputStream is = new FileOutputStream(idx2File)) {
519 			writer.writeIndex(is);
520 		}
521 		final PackIndex idx2 = PackIndex.open(idx2File);
522 		assertTrue(idx2 instanceof PackIndexV2);
523 		assertEquals(idx1.getObjectCount(), idx2.getObjectCount());
524 		assertEquals(idx1.getOffset64Count(), idx2.getOffset64Count());
525 
526 		for (int i = 0; i < idx1.getObjectCount(); i++) {
527 			final ObjectId id = idx1.getObjectId(i);
528 			assertEquals(id, idx2.getObjectId(i));
529 			assertEquals(idx1.findOffset(id), idx2.findOffset(id));
530 			assertEquals(idx1.findCRC32(id), idx2.findCRC32(id));
531 		}
532 	}
533 
534 	@Test
535 	public void testExclude() throws Exception {
536 		FileRepository repo = createBareRepository();
537 
538 		TestRepository<FileRepository> testRepo = new TestRepository<>(
539 				repo);
540 		BranchBuilder bb = testRepo.branch("refs/heads/master");
541 		contentA = testRepo.blob("A");
542 		c1 = bb.commit().add("f", contentA).create();
543 		testRepo.getRevWalk().parseHeaders(c1);
544 		PackIndex pf1 = writePack(repo, wants(c1), EMPTY_ID_SET);
545 		assertContent(
546 				pf1,
547 				Arrays.asList(c1.getId(), c1.getTree().getId(),
548 						contentA.getId()));
549 		contentB = testRepo.blob("B");
550 		c2 = bb.commit().add("f", contentB).create();
551 		testRepo.getRevWalk().parseHeaders(c2);
552 		PackIndex pf2 = writePack(repo, wants(c2), Sets.of((ObjectIdSet) pf1));
553 		assertContent(
554 				pf2,
555 				Arrays.asList(c2.getId(), c2.getTree().getId(),
556 						contentB.getId()));
557 	}
558 
559 	private static void assertContent(PackIndex pi, List<ObjectId> expected) {
560 		assertEquals("Pack index has wrong size.", expected.size(),
561 				pi.getObjectCount());
562 		for (int i = 0; i < pi.getObjectCount(); i++)
563 			assertTrue(
564 					"Pack index didn't contain the expected id "
565 							+ pi.getObjectId(i),
566 					expected.contains(pi.getObjectId(i)));
567 	}
568 
569 	@Test
570 	public void testShallowIsMinimalDepth1() throws Exception {
571 		FileRepository repo = setupRepoForShallowFetch();
572 
573 		PackIndex idx = writeShallowPack(repo, 1, wants(c2), NONE, NONE);
574 		assertContent(idx, Arrays.asList(c2.getId(), c2.getTree().getId(),
575 				contentA.getId(), contentB.getId()));
576 
577 		// Client already has blobs A and B, verify those are not packed.
578 		idx = writeShallowPack(repo, 1, wants(c5), haves(c2), shallows(c2));
579 		assertContent(idx, Arrays.asList(c5.getId(), c5.getTree().getId(),
580 				contentC.getId(), contentD.getId(), contentE.getId()));
581 	}
582 
583 	@Test
584 	public void testShallowIsMinimalDepth2() throws Exception {
585 		FileRepository repo = setupRepoForShallowFetch();
586 
587 		PackIndex idx = writeShallowPack(repo, 2, wants(c2), NONE, NONE);
588 		assertContent(idx,
589 				Arrays.asList(c1.getId(), c2.getId(), c1.getTree().getId(),
590 						c2.getTree().getId(), contentA.getId(),
591 						contentB.getId()));
592 
593 		// Client already has blobs A and B, verify those are not packed.
594 		idx = writeShallowPack(repo, 2, wants(c5), haves(c1, c2), shallows(c1));
595 		assertContent(idx,
596 				Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(),
597 						c5.getTree().getId(), contentC.getId(),
598 						contentD.getId(), contentE.getId()));
599 	}
600 
601 	@Test
602 	public void testShallowFetchShallowParentDepth1() throws Exception {
603 		FileRepository repo = setupRepoForShallowFetch();
604 
605 		PackIndex idx = writeShallowPack(repo, 1, wants(c5), NONE, NONE);
606 		assertContent(idx,
607 				Arrays.asList(c5.getId(), c5.getTree().getId(),
608 						contentA.getId(), contentB.getId(), contentC.getId(),
609 						contentD.getId(), contentE.getId()));
610 
611 		idx = writeShallowPack(repo, 1, wants(c4), haves(c5), shallows(c5));
612 		assertContent(idx, Arrays.asList(c4.getId(), c4.getTree().getId()));
613 	}
614 
615 	@Test
616 	public void testShallowFetchShallowParentDepth2() throws Exception {
617 		FileRepository repo = setupRepoForShallowFetch();
618 
619 		PackIndex idx = writeShallowPack(repo, 2, wants(c5), NONE, NONE);
620 		assertContent(idx,
621 				Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(),
622 						c5.getTree().getId(), contentA.getId(),
623 						contentB.getId(), contentC.getId(), contentD.getId(),
624 						contentE.getId()));
625 
626 		idx = writeShallowPack(repo, 2, wants(c3), haves(c4, c5), shallows(c4));
627 		assertContent(idx, Arrays.asList(c2.getId(), c3.getId(),
628 				c2.getTree().getId(), c3.getTree().getId()));
629 	}
630 
631 	@Test
632 	public void testShallowFetchShallowAncestorDepth1() throws Exception {
633 		FileRepository repo = setupRepoForShallowFetch();
634 
635 		PackIndex idx = writeShallowPack(repo, 1, wants(c5), NONE, NONE);
636 		assertContent(idx,
637 				Arrays.asList(c5.getId(), c5.getTree().getId(),
638 						contentA.getId(), contentB.getId(), contentC.getId(),
639 						contentD.getId(), contentE.getId()));
640 
641 		idx = writeShallowPack(repo, 1, wants(c3), haves(c5), shallows(c5));
642 		assertContent(idx, Arrays.asList(c3.getId(), c3.getTree().getId()));
643 	}
644 
645 	@Test
646 	public void testShallowFetchShallowAncestorDepth2() throws Exception {
647 		FileRepository repo = setupRepoForShallowFetch();
648 
649 		PackIndex idx = writeShallowPack(repo, 2, wants(c5), NONE, NONE);
650 		assertContent(idx,
651 				Arrays.asList(c4.getId(), c5.getId(), c4.getTree().getId(),
652 						c5.getTree().getId(), contentA.getId(),
653 						contentB.getId(), contentC.getId(), contentD.getId(),
654 						contentE.getId()));
655 
656 		idx = writeShallowPack(repo, 2, wants(c2), haves(c4, c5), shallows(c4));
657 		assertContent(idx, Arrays.asList(c1.getId(), c2.getId(),
658 				c1.getTree().getId(), c2.getTree().getId()));
659 	}
660 
661 	private FileRepository setupRepoForShallowFetch() throws Exception {
662 		FileRepository repo = createBareRepository();
663 		TestRepository<Repository> r = new TestRepository<>(repo);
664 		BranchBuilder bb = r.branch("refs/heads/master");
665 		contentA = r.blob("A");
666 		contentB = r.blob("B");
667 		contentC = r.blob("C");
668 		contentD = r.blob("D");
669 		contentE = r.blob("E");
670 		c1 = bb.commit().add("a", contentA).create();
671 		c2 = bb.commit().add("b", contentB).create();
672 		c3 = bb.commit().add("c", contentC).create();
673 		c4 = bb.commit().add("d", contentD).create();
674 		c5 = bb.commit().add("e", contentE).create();
675 		r.getRevWalk().parseHeaders(c5); // fully initialize the tip RevCommit
676 		return repo;
677 	}
678 
679 	private static PackIndex writePack(FileRepository repo,
680 			Set<? extends ObjectId> want, Set<ObjectIdSet> excludeObjects)
681 					throws IOException {
682 		RevWalk walk = new RevWalk(repo);
683 		return writePack(repo, walk, 0, want, NONE, excludeObjects);
684 	}
685 
686 	private static PackIndex writeShallowPack(FileRepository repo, int depth,
687 			Set<? extends ObjectId> want, Set<? extends ObjectId> have,
688 			Set<? extends ObjectId> shallow) throws IOException {
689 		// During negotiation, UploadPack would have set up a DepthWalk and
690 		// marked the client's "shallow" commits. Emulate that here.
691 		DepthWalk.RevWalk walk = new DepthWalk.RevWalk(repo, depth - 1);
692 		walk.assumeShallow(shallow);
693 		return writePack(repo, walk, depth, want, have, EMPTY_ID_SET);
694 	}
695 
696 	private static PackIndex writePack(FileRepository repo, RevWalk walk,
697 			int depth, Set<? extends ObjectId> want,
698 			Set<? extends ObjectId> have, Set<ObjectIdSet> excludeObjects)
699 					throws IOException {
700 		try (PackWriter pw = new PackWriter(repo)) {
701 			pw.setDeltaBaseAsOffset(true);
702 			pw.setReuseDeltaCommits(false);
703 			for (ObjectIdSet idx : excludeObjects) {
704 				pw.excludeObjects(idx);
705 			}
706 			if (depth > 0) {
707 				pw.setShallowPack(depth, null);
708 			}
709 			ObjectWalk ow = walk.toObjectWalkWithSameObjects();
710 
711 			pw.preparePack(NullProgressMonitor.INSTANCE, ow, want, have, NONE);
712 			String id = pw.computeName().getName();
713 			File packdir = repo.getObjectDatabase().getPackDirectory();
714 			File packFile = new File(packdir, "pack-" + id + ".pack");
715 			try (FileOutputStream packOS = new FileOutputStream(packFile)) {
716 				pw.writePack(NullProgressMonitor.INSTANCE,
717 						NullProgressMonitor.INSTANCE, packOS);
718 			}
719 			File idxFile = new File(packdir, "pack-" + id + ".idx");
720 			try (FileOutputStream idxOS = new FileOutputStream(idxFile)) {
721 				pw.writeIndex(idxOS);
722 			}
723 			return PackIndex.open(idxFile);
724 		}
725 	}
726 
727 	// TODO: testWritePackDeltasCycle()
728 	// TODO: testWritePackDeltasDepth()
729 
730 	private void writeVerifyPack1() throws IOException {
731 		final HashSet<ObjectId> interestings = new HashSet<>();
732 		interestings.add(ObjectId
733 				.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
734 		createVerifyOpenPack(interestings, NONE, false, false);
735 
736 		final ObjectId expectedOrder[] = new ObjectId[] {
737 				ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
738 				ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
739 				ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"),
740 				ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
741 				ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
742 				ObjectId.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904"),
743 				ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3"),
744 				ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259") };
745 
746 		assertEquals(expectedOrder.length, writer.getObjectCount());
747 		verifyObjectsOrder(expectedOrder);
748 		assertEquals("34be9032ac282b11fa9babdc2b2a93ca996c9c2f", writer
749 				.computeName().name());
750 	}
751 
752 	private void writeVerifyPack2(boolean deltaReuse) throws IOException {
753 		config.setReuseDeltas(deltaReuse);
754 		final HashSet<ObjectId> interestings = new HashSet<>();
755 		interestings.add(ObjectId
756 				.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
757 		final HashSet<ObjectId> uninterestings = new HashSet<>();
758 		uninterestings.add(ObjectId
759 				.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"));
760 		createVerifyOpenPack(interestings, uninterestings, false, false);
761 
762 		final ObjectId expectedOrder[] = new ObjectId[] {
763 				ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
764 				ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
765 				ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
766 				ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
767 				ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") ,
768 				ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259") };
769 		if (!config.isReuseDeltas() && !config.isDeltaCompress()) {
770 			// If no deltas are in the file the final two entries swap places.
771 			swap(expectedOrder, 4, 5);
772 		}
773 		assertEquals(expectedOrder.length, writer.getObjectCount());
774 		verifyObjectsOrder(expectedOrder);
775 		assertEquals("ed3f96b8327c7c66b0f8f70056129f0769323d86", writer
776 				.computeName().name());
777 	}
778 
779 	private static void swap(ObjectId[] arr, int a, int b) {
780 		ObjectId tmp = arr[a];
781 		arr[a] = arr[b];
782 		arr[b] = tmp;
783 	}
784 
785 	private void writeVerifyPack4(final boolean thin) throws IOException {
786 		final HashSet<ObjectId> interestings = new HashSet<>();
787 		interestings.add(ObjectId
788 				.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
789 		final HashSet<ObjectId> uninterestings = new HashSet<>();
790 		uninterestings.add(ObjectId
791 				.fromString("c59759f143fb1fe21c197981df75a7ee00290799"));
792 		createVerifyOpenPack(interestings, uninterestings, thin, false);
793 
794 		final ObjectId writtenObjects[] = new ObjectId[] {
795 				ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
796 				ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
797 				ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259") };
798 		assertEquals(writtenObjects.length, writer.getObjectCount());
799 		ObjectId expectedObjects[];
800 		if (thin) {
801 			expectedObjects = new ObjectId[4];
802 			System.arraycopy(writtenObjects, 0, expectedObjects, 0,
803 					writtenObjects.length);
804 			expectedObjects[3] = ObjectId
805 					.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3");
806 
807 		} else {
808 			expectedObjects = writtenObjects;
809 		}
810 		verifyObjectsOrder(expectedObjects);
811 		assertEquals("cded4b74176b4456afa456768b2b5aafb41c44fc", writer
812 				.computeName().name());
813 	}
814 
815 	private void createVerifyOpenPack(final Set<ObjectId> interestings,
816 			final Set<ObjectId> uninterestings, final boolean thin,
817 			final boolean ignoreMissingUninteresting)
818 			throws MissingObjectException, IOException {
819 		createVerifyOpenPack(interestings, uninterestings, thin,
820 				ignoreMissingUninteresting, false);
821 	}
822 
823 	private void createVerifyOpenPack(final Set<ObjectId> interestings,
824 			final Set<ObjectId> uninterestings, final boolean thin,
825 			final boolean ignoreMissingUninteresting, boolean useBitmaps)
826 			throws MissingObjectException, IOException {
827 		NullProgressMonitor m = NullProgressMonitor.INSTANCE;
828 		writer = new PackWriter(config, db.newObjectReader());
829 		writer.setUseBitmaps(useBitmaps);
830 		writer.setThin(thin);
831 		writer.setIgnoreMissingUninteresting(ignoreMissingUninteresting);
832 		writer.preparePack(m, interestings, uninterestings);
833 		writer.writePack(m, m, os);
834 		writer.close();
835 		verifyOpenPack(thin);
836 	}
837 
838 	private void createVerifyOpenPack(List<RevObject> objectSource)
839 			throws MissingObjectException, IOException {
840 		NullProgressMonitor m = NullProgressMonitor.INSTANCE;
841 		writer = new PackWriter(config, db.newObjectReader());
842 		writer.preparePack(objectSource.iterator());
843 		assertEquals(objectSource.size(), writer.getObjectCount());
844 		writer.writePack(m, m, os);
845 		writer.close();
846 		verifyOpenPack(false);
847 	}
848 
849 	private void verifyOpenPack(boolean thin) throws IOException {
850 		final byte[] packData = os.toByteArray();
851 
852 		if (thin) {
853 			PackParser p = index(packData);
854 			try {
855 				p.parse(NullProgressMonitor.INSTANCE);
856 				fail("indexer should grumble about missing object");
857 			} catch (IOException x) {
858 				// expected
859 			}
860 		}
861 
862 		ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(packData);
863 		p.setKeepEmpty(true);
864 		p.setAllowThin(thin);
865 		p.setIndexVersion(2);
866 		p.parse(NullProgressMonitor.INSTANCE);
867 		pack = p.getPackFile();
868 		assertNotNull("have PackFile after parsing", pack);
869 	}
870 
871 	private PackParser index(byte[] packData) throws IOException {
872 		if (inserter == null)
873 			inserter = dst.newObjectInserter();
874 		return inserter.newPackParser(new ByteArrayInputStream(packData));
875 	}
876 
877 	private void verifyObjectsOrder(ObjectId objectsOrder[]) {
878 		final List<PackIndex.MutableEntry> entries = new ArrayList<>();
879 
880 		for (MutableEntry me : pack) {
881 			entries.add(me.cloneEntry());
882 		}
883 		Collections.sort(entries, new Comparator<PackIndex.MutableEntry>() {
884 			@Override
885 			public int compare(MutableEntry o1, MutableEntry o2) {
886 				return Long.signum(o1.getOffset() - o2.getOffset());
887 			}
888 		});
889 
890 		int i = 0;
891 		for (MutableEntry me : entries) {
892 			assertEquals(objectsOrder[i++].toObjectId(), me.toObjectId());
893 		}
894 	}
895 
896 	private static Set<ObjectId> haves(ObjectId... objects) {
897 		return Sets.of(objects);
898 	}
899 
900 	private static Set<ObjectId> wants(ObjectId... objects) {
901 		return Sets.of(objects);
902 	}
903 
904 	private static Set<ObjectId> shallows(ObjectId... objects) {
905 		return Sets.of(objects);
906 	}
907 }