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.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
163
164
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
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
195
196
197
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
211
212
213
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
225
226
227
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
238 }
239 }
240
241
242
243
244
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
252 }
253
254
255
256
257
258
259
260
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
270 }
271
272
273
274
275
276
277
278 @Test
279 public void testWritePack1() throws IOException {
280 config.setReuseDeltas(false);
281 writeVerifyPack1();
282 }
283
284
285
286
287
288
289
290 @Test
291 public void testWritePack1NoObjectReuse() throws IOException {
292 config.setReuseDeltas(false);
293 config.setReuseObjects(false);
294 writeVerifyPack1();
295 }
296
297
298
299
300
301
302
303 @Test
304 public void testWritePack2() throws IOException {
305 writeVerifyPack2(false);
306 }
307
308
309
310
311
312
313
314 @Test
315 public void testWritePack2DeltasReuseRefs() throws IOException {
316 writeVerifyPack2(true);
317 }
318
319
320
321
322
323
324
325 @Test
326 public void testWritePack2DeltasReuseOffsets() throws IOException {
327 config.setDeltaBaseAsOffset(true);
328 writeVerifyPack2(true);
329 }
330
331
332
333
334
335
336
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
355
356
357
358
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
387
388
389
390
391
392 @Test
393 public void testWritePack4() throws IOException {
394 writeVerifyPack4(false);
395 }
396
397
398
399
400
401
402
403 @Test
404 public void testWritePack4ThinPack() throws IOException {
405 writeVerifyPack4(true);
406 }
407
408
409
410
411
412
413
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
431
432
433
434
435
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
451
452
453
454
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
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
511 final PackIndex idx1 = PackIndex.open(indexFile);
512 assertTrue(idx1 instanceof PackIndexV2);
513 assertEquals(0x4743F1E4L, idx1.findCRC32(ObjectId
514 .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7")));
515
516
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
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
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);
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
690
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
728
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
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
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 }