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 22KB

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