You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PackWriterTest.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. package org.eclipse.jgit.internal.storage.file;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.File;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import java.text.ParseException;
  55. import java.util.ArrayList;
  56. import java.util.Arrays;
  57. import java.util.Collections;
  58. import java.util.Comparator;
  59. import java.util.HashSet;
  60. import java.util.List;
  61. import java.util.Set;
  62. import org.eclipse.jgit.errors.MissingObjectException;
  63. import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
  64. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  65. import org.eclipse.jgit.internal.storage.pack.PackWriter.ObjectIdSet;
  66. import org.eclipse.jgit.junit.JGitTestUtil;
  67. import org.eclipse.jgit.junit.TestRepository;
  68. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  69. import org.eclipse.jgit.lib.AnyObjectId;
  70. import org.eclipse.jgit.lib.NullProgressMonitor;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectInserter;
  73. import org.eclipse.jgit.revwalk.RevBlob;
  74. import org.eclipse.jgit.revwalk.RevCommit;
  75. import org.eclipse.jgit.revwalk.RevObject;
  76. import org.eclipse.jgit.revwalk.RevWalk;
  77. import org.eclipse.jgit.storage.pack.PackConfig;
  78. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  79. import org.eclipse.jgit.transport.PackParser;
  80. import org.junit.After;
  81. import org.junit.Before;
  82. import org.junit.Test;
  83. public class PackWriterTest extends SampleDataRepositoryTestCase {
  84. private static final Set<ObjectId> EMPTY_SET_OBJECT = Collections
  85. .<ObjectId> emptySet();
  86. private static final List<RevObject> EMPTY_LIST_REVS = Collections
  87. .<RevObject> emptyList();
  88. private PackConfig config;
  89. private PackWriter writer;
  90. private ByteArrayOutputStream os;
  91. private PackFile pack;
  92. private ObjectInserter inserter;
  93. private FileRepository dst;
  94. @Before
  95. public void setUp() throws Exception {
  96. super.setUp();
  97. os = new ByteArrayOutputStream();
  98. config = new PackConfig(db);
  99. dst = createBareRepository();
  100. File alt = new File(dst.getObjectDatabase().getDirectory(), "info/alternates");
  101. alt.getParentFile().mkdirs();
  102. write(alt, db.getObjectDatabase().getDirectory().getAbsolutePath() + "\n");
  103. }
  104. @After
  105. public void tearDown() throws Exception {
  106. if (writer != null) {
  107. writer.release();
  108. writer = null;
  109. }
  110. if (inserter != null) {
  111. inserter.release();
  112. inserter = null;
  113. }
  114. super.tearDown();
  115. }
  116. /**
  117. * Test constructor for exceptions, default settings, initialization.
  118. *
  119. * @throws IOException
  120. */
  121. @Test
  122. public void testContructor() throws IOException {
  123. writer = new PackWriter(config, db.newObjectReader());
  124. assertFalse(writer.isDeltaBaseAsOffset());
  125. assertTrue(config.isReuseDeltas());
  126. assertTrue(config.isReuseObjects());
  127. assertEquals(0, writer.getObjectCount());
  128. }
  129. /**
  130. * Change default settings and verify them.
  131. */
  132. @Test
  133. public void testModifySettings() {
  134. config.setReuseDeltas(false);
  135. config.setReuseObjects(false);
  136. config.setDeltaBaseAsOffset(false);
  137. assertFalse(config.isReuseDeltas());
  138. assertFalse(config.isReuseObjects());
  139. assertFalse(config.isDeltaBaseAsOffset());
  140. writer = new PackWriter(config, db.newObjectReader());
  141. writer.setDeltaBaseAsOffset(true);
  142. assertTrue(writer.isDeltaBaseAsOffset());
  143. assertFalse(config.isDeltaBaseAsOffset());
  144. }
  145. /**
  146. * Write empty pack by providing empty sets of interesting/uninteresting
  147. * objects and check for correct format.
  148. *
  149. * @throws IOException
  150. */
  151. @Test
  152. public void testWriteEmptyPack1() throws IOException {
  153. createVerifyOpenPack(EMPTY_SET_OBJECT, EMPTY_SET_OBJECT, false, false);
  154. assertEquals(0, writer.getObjectCount());
  155. assertEquals(0, pack.getObjectCount());
  156. assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", writer
  157. .computeName().name());
  158. }
  159. /**
  160. * Write empty pack by providing empty iterator of objects to write and
  161. * check for correct format.
  162. *
  163. * @throws IOException
  164. */
  165. @Test
  166. public void testWriteEmptyPack2() throws IOException {
  167. createVerifyOpenPack(EMPTY_LIST_REVS);
  168. assertEquals(0, writer.getObjectCount());
  169. assertEquals(0, pack.getObjectCount());
  170. }
  171. /**
  172. * Try to pass non-existing object as uninteresting, with non-ignoring
  173. * setting.
  174. *
  175. * @throws IOException
  176. */
  177. @Test
  178. public void testNotIgnoreNonExistingObjects() throws IOException {
  179. final ObjectId nonExisting = ObjectId
  180. .fromString("0000000000000000000000000000000000000001");
  181. try {
  182. createVerifyOpenPack(EMPTY_SET_OBJECT, Collections.singleton(
  183. nonExisting), false, false);
  184. fail("Should have thrown MissingObjectException");
  185. } catch (MissingObjectException x) {
  186. // expected
  187. }
  188. }
  189. /**
  190. * Try to pass non-existing object as uninteresting, with ignoring setting.
  191. *
  192. * @throws IOException
  193. */
  194. @Test
  195. public void testIgnoreNonExistingObjects() throws IOException {
  196. final ObjectId nonExisting = ObjectId
  197. .fromString("0000000000000000000000000000000000000001");
  198. createVerifyOpenPack(EMPTY_SET_OBJECT, Collections.singleton(
  199. nonExisting), false, true);
  200. // shouldn't throw anything
  201. }
  202. /**
  203. * Try to pass non-existing object as uninteresting, with ignoring setting.
  204. * Use a repo with bitmap indexes because then PackWriter will use
  205. * PackWriterBitmapWalker which had problems with this situation.
  206. *
  207. * @throws IOException
  208. * @throws ParseException
  209. */
  210. @Test
  211. public void testIgnoreNonExistingObjectsWithBitmaps() throws IOException,
  212. ParseException {
  213. final ObjectId nonExisting = ObjectId
  214. .fromString("0000000000000000000000000000000000000001");
  215. new GC(db).gc();
  216. createVerifyOpenPack(EMPTY_SET_OBJECT,
  217. Collections.singleton(nonExisting), false, true, true);
  218. // shouldn't throw anything
  219. }
  220. /**
  221. * Create pack basing on only interesting objects, then precisely verify
  222. * content. No delta reuse here.
  223. *
  224. * @throws IOException
  225. */
  226. @Test
  227. public void testWritePack1() throws IOException {
  228. config.setReuseDeltas(false);
  229. writeVerifyPack1();
  230. }
  231. /**
  232. * Test writing pack without object reuse. Pack content/preparation as in
  233. * {@link #testWritePack1()}.
  234. *
  235. * @throws IOException
  236. */
  237. @Test
  238. public void testWritePack1NoObjectReuse() throws IOException {
  239. config.setReuseDeltas(false);
  240. config.setReuseObjects(false);
  241. writeVerifyPack1();
  242. }
  243. /**
  244. * Create pack basing on both interesting and uninteresting objects, then
  245. * precisely verify content. No delta reuse here.
  246. *
  247. * @throws IOException
  248. */
  249. @Test
  250. public void testWritePack2() throws IOException {
  251. writeVerifyPack2(false);
  252. }
  253. /**
  254. * Test pack writing with deltas reuse, delta-base first rule. Pack
  255. * content/preparation as in {@link #testWritePack2()}.
  256. *
  257. * @throws IOException
  258. */
  259. @Test
  260. public void testWritePack2DeltasReuseRefs() throws IOException {
  261. writeVerifyPack2(true);
  262. }
  263. /**
  264. * Test pack writing with delta reuse. Delta bases referred as offsets. Pack
  265. * configuration as in {@link #testWritePack2DeltasReuseRefs()}.
  266. *
  267. * @throws IOException
  268. */
  269. @Test
  270. public void testWritePack2DeltasReuseOffsets() throws IOException {
  271. config.setDeltaBaseAsOffset(true);
  272. writeVerifyPack2(true);
  273. }
  274. /**
  275. * Test pack writing with delta reuse. Raw-data copy (reuse) is made on a
  276. * pack with CRC32 index. Pack configuration as in
  277. * {@link #testWritePack2DeltasReuseRefs()}.
  278. *
  279. * @throws IOException
  280. */
  281. @Test
  282. public void testWritePack2DeltasCRC32Copy() throws IOException {
  283. final File packDir = new File(db.getObjectDatabase().getDirectory(), "pack");
  284. final File crc32Pack = new File(packDir,
  285. "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack");
  286. final File crc32Idx = new File(packDir,
  287. "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.idx");
  288. copyFile(JGitTestUtil.getTestResourceFile(
  289. "pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.idxV2"),
  290. crc32Idx);
  291. db.openPack(crc32Pack);
  292. writeVerifyPack2(true);
  293. }
  294. /**
  295. * Create pack basing on fixed objects list, then precisely verify content.
  296. * No delta reuse here.
  297. *
  298. * @throws IOException
  299. * @throws MissingObjectException
  300. *
  301. */
  302. @Test
  303. public void testWritePack3() throws MissingObjectException, IOException {
  304. config.setReuseDeltas(false);
  305. final ObjectId forcedOrder[] = new ObjectId[] {
  306. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  307. ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
  308. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  309. ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
  310. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"),
  311. ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") };
  312. final RevWalk parser = new RevWalk(db);
  313. final RevObject forcedOrderRevs[] = new RevObject[forcedOrder.length];
  314. for (int i = 0; i < forcedOrder.length; i++)
  315. forcedOrderRevs[i] = parser.parseAny(forcedOrder[i]);
  316. createVerifyOpenPack(Arrays.asList(forcedOrderRevs));
  317. assertEquals(forcedOrder.length, writer.getObjectCount());
  318. verifyObjectsOrder(forcedOrder);
  319. assertEquals("ed3f96b8327c7c66b0f8f70056129f0769323d86", writer
  320. .computeName().name());
  321. }
  322. /**
  323. * Another pack creation: basing on both interesting and uninteresting
  324. * objects. No delta reuse possible here, as this is a specific case when we
  325. * write only 1 commit, associated with 1 tree, 1 blob.
  326. *
  327. * @throws IOException
  328. */
  329. @Test
  330. public void testWritePack4() throws IOException {
  331. writeVerifyPack4(false);
  332. }
  333. /**
  334. * Test thin pack writing: 1 blob delta base is on objects edge. Pack
  335. * configuration as in {@link #testWritePack4()}.
  336. *
  337. * @throws IOException
  338. */
  339. @Test
  340. public void testWritePack4ThinPack() throws IOException {
  341. writeVerifyPack4(true);
  342. }
  343. /**
  344. * Compare sizes of packs created using {@link #testWritePack2()} and
  345. * {@link #testWritePack2DeltasReuseRefs()}. The pack using deltas should
  346. * be smaller.
  347. *
  348. * @throws Exception
  349. */
  350. @Test
  351. public void testWritePack2SizeDeltasVsNoDeltas() throws Exception {
  352. testWritePack2();
  353. final long sizePack2NoDeltas = os.size();
  354. tearDown();
  355. setUp();
  356. testWritePack2DeltasReuseRefs();
  357. final long sizePack2DeltasRefs = os.size();
  358. assertTrue(sizePack2NoDeltas > sizePack2DeltasRefs);
  359. }
  360. /**
  361. * Compare sizes of packs created using
  362. * {@link #testWritePack2DeltasReuseRefs()} and
  363. * {@link #testWritePack2DeltasReuseOffsets()}. The pack with delta bases
  364. * written as offsets should be smaller.
  365. *
  366. * @throws Exception
  367. */
  368. @Test
  369. public void testWritePack2SizeOffsetsVsRefs() throws Exception {
  370. testWritePack2DeltasReuseRefs();
  371. final long sizePack2DeltasRefs = os.size();
  372. tearDown();
  373. setUp();
  374. testWritePack2DeltasReuseOffsets();
  375. final long sizePack2DeltasOffsets = os.size();
  376. assertTrue(sizePack2DeltasRefs > sizePack2DeltasOffsets);
  377. }
  378. /**
  379. * Compare sizes of packs created using {@link #testWritePack4()} and
  380. * {@link #testWritePack4ThinPack()}. Obviously, the thin pack should be
  381. * smaller.
  382. *
  383. * @throws Exception
  384. */
  385. @Test
  386. public void testWritePack4SizeThinVsNoThin() throws Exception {
  387. testWritePack4();
  388. final long sizePack4 = os.size();
  389. tearDown();
  390. setUp();
  391. testWritePack4ThinPack();
  392. final long sizePack4Thin = os.size();
  393. assertTrue(sizePack4 > sizePack4Thin);
  394. }
  395. @Test
  396. public void testWriteIndex() throws Exception {
  397. config.setIndexVersion(2);
  398. writeVerifyPack4(false);
  399. File packFile = pack.getPackFile();
  400. String name = packFile.getName();
  401. String base = name.substring(0, name.lastIndexOf('.'));
  402. File indexFile = new File(packFile.getParentFile(), base + ".idx");
  403. // Validate that IndexPack came up with the right CRC32 value.
  404. final PackIndex idx1 = PackIndex.open(indexFile);
  405. assertTrue(idx1 instanceof PackIndexV2);
  406. assertEquals(0x4743F1E4L, idx1.findCRC32(ObjectId
  407. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7")));
  408. // Validate that an index written by PackWriter is the same.
  409. final File idx2File = new File(indexFile.getAbsolutePath() + ".2");
  410. final FileOutputStream is = new FileOutputStream(idx2File);
  411. try {
  412. writer.writeIndex(is);
  413. } finally {
  414. is.close();
  415. }
  416. final PackIndex idx2 = PackIndex.open(idx2File);
  417. assertTrue(idx2 instanceof PackIndexV2);
  418. assertEquals(idx1.getObjectCount(), idx2.getObjectCount());
  419. assertEquals(idx1.getOffset64Count(), idx2.getOffset64Count());
  420. for (int i = 0; i < idx1.getObjectCount(); i++) {
  421. final ObjectId id = idx1.getObjectId(i);
  422. assertEquals(id, idx2.getObjectId(i));
  423. assertEquals(idx1.findOffset(id), idx2.findOffset(id));
  424. assertEquals(idx1.findCRC32(id), idx2.findCRC32(id));
  425. }
  426. }
  427. @Test
  428. public void testExclude() throws Exception {
  429. FileRepository repo = createBareRepository();
  430. TestRepository<FileRepository> testRepo = new TestRepository<FileRepository>(
  431. repo);
  432. BranchBuilder bb = testRepo.branch("refs/heads/master");
  433. RevBlob contentA = testRepo.blob("A");
  434. RevCommit c1 = bb.commit().add("f", contentA).create();
  435. testRepo.getRevWalk().parseHeaders(c1);
  436. PackIndex pf1 = writePack(repo, Collections.singleton(c1),
  437. Collections.<ObjectIdSet> emptySet());
  438. assertContent(
  439. pf1,
  440. Arrays.asList(c1.getId(), c1.getTree().getId(),
  441. contentA.getId()));
  442. RevBlob contentB = testRepo.blob("B");
  443. RevCommit c2 = bb.commit().add("f", contentB).create();
  444. testRepo.getRevWalk().parseHeaders(c2);
  445. PackIndex pf2 = writePack(repo, Collections.singleton(c2),
  446. Collections.singleton(objectIdSet(pf1)));
  447. assertContent(
  448. pf2,
  449. Arrays.asList(c2.getId(), c2.getTree().getId(),
  450. contentB.getId()));
  451. }
  452. private static void assertContent(PackIndex pi, List<ObjectId> expected) {
  453. assertEquals("Pack index has wrong size.", expected.size(),
  454. pi.getObjectCount());
  455. for (int i = 0; i < pi.getObjectCount(); i++)
  456. assertTrue(
  457. "Pack index didn't contain the expected id "
  458. + pi.getObjectId(i),
  459. expected.contains(pi.getObjectId(i)));
  460. }
  461. private static PackIndex writePack(FileRepository repo,
  462. Set<? extends ObjectId> want, Set<ObjectIdSet> excludeObjects)
  463. throws IOException {
  464. PackWriter pw = new PackWriter(repo);
  465. pw.setDeltaBaseAsOffset(true);
  466. pw.setReuseDeltaCommits(false);
  467. for (ObjectIdSet idx : excludeObjects)
  468. pw.excludeObjects(idx);
  469. pw.preparePack(NullProgressMonitor.INSTANCE, want,
  470. Collections.<ObjectId> emptySet());
  471. String id = pw.computeName().getName();
  472. File packdir = new File(repo.getObjectsDirectory(), "pack");
  473. File packFile = new File(packdir, "pack-" + id + ".pack");
  474. FileOutputStream packOS = new FileOutputStream(packFile);
  475. pw.writePack(NullProgressMonitor.INSTANCE,
  476. NullProgressMonitor.INSTANCE, packOS);
  477. packOS.close();
  478. File idxFile = new File(packdir, "pack-" + id + ".idx");
  479. FileOutputStream idxOS = new FileOutputStream(idxFile);
  480. pw.writeIndex(idxOS);
  481. idxOS.close();
  482. pw.release();
  483. return PackIndex.open(idxFile);
  484. }
  485. // TODO: testWritePackDeltasCycle()
  486. // TODO: testWritePackDeltasDepth()
  487. private void writeVerifyPack1() throws IOException {
  488. final HashSet<ObjectId> interestings = new HashSet<ObjectId>();
  489. interestings.add(ObjectId
  490. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
  491. createVerifyOpenPack(interestings, EMPTY_SET_OBJECT, false, false);
  492. final ObjectId expectedOrder[] = new ObjectId[] {
  493. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  494. ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
  495. ObjectId.fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"),
  496. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  497. ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
  498. ObjectId.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904"),
  499. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"),
  500. ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") };
  501. assertEquals(expectedOrder.length, writer.getObjectCount());
  502. verifyObjectsOrder(expectedOrder);
  503. assertEquals("34be9032ac282b11fa9babdc2b2a93ca996c9c2f", writer
  504. .computeName().name());
  505. }
  506. private void writeVerifyPack2(boolean deltaReuse) throws IOException {
  507. config.setReuseDeltas(deltaReuse);
  508. final HashSet<ObjectId> interestings = new HashSet<ObjectId>();
  509. interestings.add(ObjectId
  510. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
  511. final HashSet<ObjectId> uninterestings = new HashSet<ObjectId>();
  512. uninterestings.add(ObjectId
  513. .fromString("540a36d136cf413e4b064c2b0e0a4db60f77feab"));
  514. createVerifyOpenPack(interestings, uninterestings, false, false);
  515. final ObjectId expectedOrder[] = new ObjectId[] {
  516. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  517. ObjectId.fromString("c59759f143fb1fe21c197981df75a7ee00290799"),
  518. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  519. ObjectId.fromString("902d5476fa249b7abc9d84c611577a81381f0327"),
  520. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259"),
  521. ObjectId.fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3") };
  522. if (deltaReuse) {
  523. // objects order influenced (swapped) by delta-base first rule
  524. ObjectId temp = expectedOrder[4];
  525. expectedOrder[4] = expectedOrder[5];
  526. expectedOrder[5] = temp;
  527. }
  528. assertEquals(expectedOrder.length, writer.getObjectCount());
  529. verifyObjectsOrder(expectedOrder);
  530. assertEquals("ed3f96b8327c7c66b0f8f70056129f0769323d86", writer
  531. .computeName().name());
  532. }
  533. private void writeVerifyPack4(final boolean thin) throws IOException {
  534. final HashSet<ObjectId> interestings = new HashSet<ObjectId>();
  535. interestings.add(ObjectId
  536. .fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"));
  537. final HashSet<ObjectId> uninterestings = new HashSet<ObjectId>();
  538. uninterestings.add(ObjectId
  539. .fromString("c59759f143fb1fe21c197981df75a7ee00290799"));
  540. createVerifyOpenPack(interestings, uninterestings, thin, false);
  541. final ObjectId writtenObjects[] = new ObjectId[] {
  542. ObjectId.fromString("82c6b885ff600be425b4ea96dee75dca255b69e7"),
  543. ObjectId.fromString("aabf2ffaec9b497f0950352b3e582d73035c2035"),
  544. ObjectId.fromString("5b6e7c66c276e7610d4a73c70ec1a1f7c1003259") };
  545. assertEquals(writtenObjects.length, writer.getObjectCount());
  546. ObjectId expectedObjects[];
  547. if (thin) {
  548. expectedObjects = new ObjectId[4];
  549. System.arraycopy(writtenObjects, 0, expectedObjects, 0,
  550. writtenObjects.length);
  551. expectedObjects[3] = ObjectId
  552. .fromString("6ff87c4664981e4397625791c8ea3bbb5f2279a3");
  553. } else {
  554. expectedObjects = writtenObjects;
  555. }
  556. verifyObjectsOrder(expectedObjects);
  557. assertEquals("cded4b74176b4456afa456768b2b5aafb41c44fc", writer
  558. .computeName().name());
  559. }
  560. private void createVerifyOpenPack(final Set<ObjectId> interestings,
  561. final Set<ObjectId> uninterestings, final boolean thin,
  562. final boolean ignoreMissingUninteresting)
  563. throws MissingObjectException, IOException {
  564. createVerifyOpenPack(interestings, uninterestings, thin,
  565. ignoreMissingUninteresting, false);
  566. }
  567. private void createVerifyOpenPack(final Set<ObjectId> interestings,
  568. final Set<ObjectId> uninterestings, final boolean thin,
  569. final boolean ignoreMissingUninteresting, boolean useBitmaps)
  570. throws MissingObjectException, IOException {
  571. NullProgressMonitor m = NullProgressMonitor.INSTANCE;
  572. writer = new PackWriter(config, db.newObjectReader());
  573. writer.setUseBitmaps(useBitmaps);
  574. writer.setThin(thin);
  575. writer.setIgnoreMissingUninteresting(ignoreMissingUninteresting);
  576. writer.preparePack(m, interestings, uninterestings);
  577. writer.writePack(m, m, os);
  578. writer.release();
  579. verifyOpenPack(thin);
  580. }
  581. private void createVerifyOpenPack(final List<RevObject> objectSource)
  582. throws MissingObjectException, IOException {
  583. NullProgressMonitor m = NullProgressMonitor.INSTANCE;
  584. writer = new PackWriter(config, db.newObjectReader());
  585. writer.preparePack(objectSource.iterator());
  586. assertEquals(objectSource.size(), writer.getObjectCount());
  587. writer.writePack(m, m, os);
  588. writer.release();
  589. verifyOpenPack(false);
  590. }
  591. private void verifyOpenPack(final boolean thin) throws IOException {
  592. final byte[] packData = os.toByteArray();
  593. if (thin) {
  594. PackParser p = index(packData);
  595. try {
  596. p.parse(NullProgressMonitor.INSTANCE);
  597. fail("indexer should grumble about missing object");
  598. } catch (IOException x) {
  599. // expected
  600. }
  601. }
  602. ObjectDirectoryPackParser p = (ObjectDirectoryPackParser) index(packData);
  603. p.setKeepEmpty(true);
  604. p.setAllowThin(thin);
  605. p.setIndexVersion(2);
  606. p.parse(NullProgressMonitor.INSTANCE);
  607. pack = p.getPackFile();
  608. assertNotNull("have PackFile after parsing", pack);
  609. }
  610. private PackParser index(final byte[] packData) throws IOException {
  611. if (inserter == null)
  612. inserter = dst.newObjectInserter();
  613. return inserter.newPackParser(new ByteArrayInputStream(packData));
  614. }
  615. private void verifyObjectsOrder(final ObjectId objectsOrder[]) {
  616. final List<PackIndex.MutableEntry> entries = new ArrayList<PackIndex.MutableEntry>();
  617. for (MutableEntry me : pack) {
  618. entries.add(me.cloneEntry());
  619. }
  620. Collections.sort(entries, new Comparator<PackIndex.MutableEntry>() {
  621. public int compare(MutableEntry o1, MutableEntry o2) {
  622. return Long.signum(o1.getOffset() - o2.getOffset());
  623. }
  624. });
  625. int i = 0;
  626. for (MutableEntry me : entries) {
  627. assertEquals(objectsOrder[i++].toObjectId(), me.toObjectId());
  628. }
  629. }
  630. private static ObjectIdSet objectIdSet(final PackIndex idx) {
  631. return new ObjectIdSet() {
  632. public boolean contains(AnyObjectId objectId) {
  633. return idx.hasObject(objectId);
  634. }
  635. };
  636. }
  637. }