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.

PackWriter.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.storage.pack;
  45. import static org.eclipse.jgit.storage.pack.StoredObjectRepresentation.PACK_DELTA;
  46. import static org.eclipse.jgit.storage.pack.StoredObjectRepresentation.PACK_WHOLE;
  47. import java.io.IOException;
  48. import java.io.OutputStream;
  49. import java.security.MessageDigest;
  50. import java.util.ArrayList;
  51. import java.util.Collection;
  52. import java.util.Collections;
  53. import java.util.Iterator;
  54. import java.util.List;
  55. import java.util.zip.Deflater;
  56. import org.eclipse.jgit.JGitText;
  57. import org.eclipse.jgit.errors.CorruptObjectException;
  58. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  59. import org.eclipse.jgit.errors.MissingObjectException;
  60. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  61. import org.eclipse.jgit.lib.AnyObjectId;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.CoreConfig;
  64. import org.eclipse.jgit.lib.NullProgressMonitor;
  65. import org.eclipse.jgit.lib.ObjectId;
  66. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  67. import org.eclipse.jgit.lib.ObjectLoader;
  68. import org.eclipse.jgit.lib.ObjectReader;
  69. import org.eclipse.jgit.lib.ProgressMonitor;
  70. import org.eclipse.jgit.lib.Repository;
  71. import org.eclipse.jgit.revwalk.ObjectWalk;
  72. import org.eclipse.jgit.revwalk.RevFlag;
  73. import org.eclipse.jgit.revwalk.RevObject;
  74. import org.eclipse.jgit.revwalk.RevSort;
  75. import org.eclipse.jgit.storage.file.PackIndexWriter;
  76. /**
  77. * <p>
  78. * PackWriter class is responsible for generating pack files from specified set
  79. * of objects from repository. This implementation produce pack files in format
  80. * version 2.
  81. * </p>
  82. * <p>
  83. * Source of objects may be specified in two ways:
  84. * <ul>
  85. * <li>(usually) by providing sets of interesting and uninteresting objects in
  86. * repository - all interesting objects and their ancestors except uninteresting
  87. * objects and their ancestors will be included in pack, or</li>
  88. * <li>by providing iterator of {@link RevObject} specifying exact list and
  89. * order of objects in pack</li>
  90. * </ul>
  91. * Typical usage consists of creating instance intended for some pack,
  92. * configuring options, preparing the list of objects by calling
  93. * {@link #preparePack(Iterator)} or
  94. * {@link #preparePack(Collection, Collection)}, and finally
  95. * producing the stream with {@link #writePack(OutputStream)}.
  96. * </p>
  97. * <p>
  98. * Class provide set of configurable options and {@link ProgressMonitor}
  99. * support, as operations may take a long time for big repositories. Deltas
  100. * searching algorithm is <b>NOT IMPLEMENTED</b> yet - this implementation
  101. * relies only on deltas and objects reuse.
  102. * </p>
  103. * <p>
  104. * This class is not thread safe, it is intended to be used in one thread, with
  105. * one instance per created pack. Subsequent calls to writePack result in
  106. * undefined behavior.
  107. * </p>
  108. */
  109. public class PackWriter {
  110. /**
  111. * Title of {@link ProgressMonitor} task used during counting objects to
  112. * pack.
  113. *
  114. * @see #preparePack(Collection, Collection)
  115. */
  116. public static final String COUNTING_OBJECTS_PROGRESS = JGitText.get().countingObjects;
  117. /**
  118. * Title of {@link ProgressMonitor} task used during searching for objects
  119. * reuse or delta reuse.
  120. *
  121. * @see #writePack(OutputStream)
  122. */
  123. public static final String SEARCHING_REUSE_PROGRESS = JGitText.get().compressingObjects;
  124. /**
  125. * Title of {@link ProgressMonitor} task used during writing out pack
  126. * (objects)
  127. *
  128. * @see #writePack(OutputStream)
  129. */
  130. public static final String WRITING_OBJECTS_PROGRESS = JGitText.get().writingObjects;
  131. /**
  132. * Default value of deltas reuse option.
  133. *
  134. * @see #setReuseDeltas(boolean)
  135. */
  136. public static final boolean DEFAULT_REUSE_DELTAS = true;
  137. /**
  138. * Default value of objects reuse option.
  139. *
  140. * @see #setReuseObjects(boolean)
  141. */
  142. public static final boolean DEFAULT_REUSE_OBJECTS = true;
  143. /**
  144. * Default value of delta base as offset option.
  145. *
  146. * @see #setDeltaBaseAsOffset(boolean)
  147. */
  148. public static final boolean DEFAULT_DELTA_BASE_AS_OFFSET = false;
  149. /**
  150. * Default value of maximum delta chain depth.
  151. *
  152. * @see #setMaxDeltaDepth(int)
  153. */
  154. public static final int DEFAULT_MAX_DELTA_DEPTH = 50;
  155. private static final int PACK_VERSION_GENERATED = 2;
  156. @SuppressWarnings("unchecked")
  157. private final List<ObjectToPack> objectsLists[] = new List[Constants.OBJ_TAG + 1];
  158. {
  159. objectsLists[0] = Collections.<ObjectToPack> emptyList();
  160. objectsLists[Constants.OBJ_COMMIT] = new ArrayList<ObjectToPack>();
  161. objectsLists[Constants.OBJ_TREE] = new ArrayList<ObjectToPack>();
  162. objectsLists[Constants.OBJ_BLOB] = new ArrayList<ObjectToPack>();
  163. objectsLists[Constants.OBJ_TAG] = new ArrayList<ObjectToPack>();
  164. }
  165. private final ObjectIdSubclassMap<ObjectToPack> objectsMap = new ObjectIdSubclassMap<ObjectToPack>();
  166. // edge objects for thin packs
  167. private final ObjectIdSubclassMap<ObjectId> edgeObjects = new ObjectIdSubclassMap<ObjectId>();
  168. private final Repository db;
  169. private PackOutputStream out;
  170. private final Deflater deflater;
  171. private ProgressMonitor initMonitor;
  172. private ProgressMonitor writeMonitor;
  173. private final ObjectReader reader;
  174. /** {@link #reader} recast to the reuse interface, if it supports it. */
  175. private final ObjectReuseAsIs reuseSupport;
  176. private List<ObjectToPack> sortedByName;
  177. private byte packcsum[];
  178. private boolean reuseDeltas = DEFAULT_REUSE_DELTAS;
  179. private boolean reuseObjects = DEFAULT_REUSE_OBJECTS;
  180. private boolean deltaBaseAsOffset = DEFAULT_DELTA_BASE_AS_OFFSET;
  181. private int maxDeltaDepth = DEFAULT_MAX_DELTA_DEPTH;
  182. private int outputVersion;
  183. private boolean thin;
  184. private boolean ignoreMissingUninteresting = true;
  185. /**
  186. * Create writer for specified repository.
  187. * <p>
  188. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  189. * {@link #preparePack(Collection, Collection)}.
  190. *
  191. * @param repo
  192. * repository where objects are stored.
  193. * @param monitor
  194. * operations progress monitor, used within
  195. * {@link #preparePack(Iterator)},
  196. * {@link #preparePack(Collection, Collection)}
  197. * , or {@link #writePack(OutputStream)}.
  198. */
  199. public PackWriter(final Repository repo, final ProgressMonitor monitor) {
  200. this(repo, monitor, monitor);
  201. }
  202. /**
  203. * Create writer for specified repository.
  204. * <p>
  205. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  206. * {@link #preparePack(Collection, Collection)}.
  207. *
  208. * @param repo
  209. * repository where objects are stored.
  210. * @param imonitor
  211. * operations progress monitor, used within
  212. * {@link #preparePack(Iterator)},
  213. * {@link #preparePack(Collection, Collection)}
  214. * @param wmonitor
  215. * operations progress monitor, used within
  216. * {@link #writePack(OutputStream)}.
  217. */
  218. public PackWriter(final Repository repo, final ProgressMonitor imonitor,
  219. final ProgressMonitor wmonitor) {
  220. this.db = repo;
  221. reader = db.newObjectReader();
  222. if (reader instanceof ObjectReuseAsIs)
  223. reuseSupport = ((ObjectReuseAsIs) reader);
  224. else
  225. reuseSupport = null;
  226. initMonitor = imonitor == null ? NullProgressMonitor.INSTANCE : imonitor;
  227. writeMonitor = wmonitor == null ? NullProgressMonitor.INSTANCE : wmonitor;
  228. final CoreConfig coreConfig = db.getConfig().get(CoreConfig.KEY);
  229. this.deflater = new Deflater(coreConfig.getCompression());
  230. outputVersion = coreConfig.getPackIndexVersion();
  231. }
  232. /**
  233. * Check whether object is configured to reuse deltas existing in
  234. * repository.
  235. * <p>
  236. * Default setting: {@value #DEFAULT_REUSE_DELTAS}
  237. * </p>
  238. *
  239. * @return true if object is configured to reuse deltas; false otherwise.
  240. */
  241. public boolean isReuseDeltas() {
  242. return reuseDeltas;
  243. }
  244. /**
  245. * Set reuse deltas configuration option for this writer. When enabled,
  246. * writer will search for delta representation of object in repository and
  247. * use it if possible. Normally, only deltas with base to another object
  248. * existing in set of objects to pack will be used. Exception is however
  249. * thin-pack (see
  250. * {@link #preparePack(Collection, Collection)} and
  251. * {@link #preparePack(Iterator)}) where base object must exist on other
  252. * side machine.
  253. * <p>
  254. * When raw delta data is directly copied from a pack file, checksum is
  255. * computed to verify data.
  256. * </p>
  257. * <p>
  258. * Default setting: {@value #DEFAULT_REUSE_DELTAS}
  259. * </p>
  260. *
  261. * @param reuseDeltas
  262. * boolean indicating whether or not try to reuse deltas.
  263. */
  264. public void setReuseDeltas(boolean reuseDeltas) {
  265. this.reuseDeltas = reuseDeltas;
  266. }
  267. /**
  268. * Checks whether object is configured to reuse existing objects
  269. * representation in repository.
  270. * <p>
  271. * Default setting: {@value #DEFAULT_REUSE_OBJECTS}
  272. * </p>
  273. *
  274. * @return true if writer is configured to reuse objects representation from
  275. * pack; false otherwise.
  276. */
  277. public boolean isReuseObjects() {
  278. return reuseObjects;
  279. }
  280. /**
  281. * Set reuse objects configuration option for this writer. If enabled,
  282. * writer searches for representation in a pack file. If possible,
  283. * compressed data is directly copied from such a pack file. Data checksum
  284. * is verified.
  285. * <p>
  286. * Default setting: {@value #DEFAULT_REUSE_OBJECTS}
  287. * </p>
  288. *
  289. * @param reuseObjects
  290. * boolean indicating whether or not writer should reuse existing
  291. * objects representation.
  292. */
  293. public void setReuseObjects(boolean reuseObjects) {
  294. this.reuseObjects = reuseObjects;
  295. }
  296. /**
  297. * Check whether writer can store delta base as an offset (new style
  298. * reducing pack size) or should store it as an object id (legacy style,
  299. * compatible with old readers).
  300. * <p>
  301. * Default setting: {@value #DEFAULT_DELTA_BASE_AS_OFFSET}
  302. * </p>
  303. *
  304. * @return true if delta base is stored as an offset; false if it is stored
  305. * as an object id.
  306. */
  307. public boolean isDeltaBaseAsOffset() {
  308. return deltaBaseAsOffset;
  309. }
  310. /**
  311. * Set writer delta base format. Delta base can be written as an offset in a
  312. * pack file (new approach reducing file size) or as an object id (legacy
  313. * approach, compatible with old readers).
  314. * <p>
  315. * Default setting: {@value #DEFAULT_DELTA_BASE_AS_OFFSET}
  316. * </p>
  317. *
  318. * @param deltaBaseAsOffset
  319. * boolean indicating whether delta base can be stored as an
  320. * offset.
  321. */
  322. public void setDeltaBaseAsOffset(boolean deltaBaseAsOffset) {
  323. this.deltaBaseAsOffset = deltaBaseAsOffset;
  324. }
  325. /**
  326. * Get maximum depth of delta chain set up for this writer. Generated chains
  327. * are not longer than this value.
  328. * <p>
  329. * Default setting: {@value #DEFAULT_MAX_DELTA_DEPTH}
  330. * </p>
  331. *
  332. * @return maximum delta chain depth.
  333. */
  334. public int getMaxDeltaDepth() {
  335. return maxDeltaDepth;
  336. }
  337. /**
  338. * Set up maximum depth of delta chain for this writer. Generated chains are
  339. * not longer than this value. Too low value causes low compression level,
  340. * while too big makes unpacking (reading) longer.
  341. * <p>
  342. * Default setting: {@value #DEFAULT_MAX_DELTA_DEPTH}
  343. * </p>
  344. *
  345. * @param maxDeltaDepth
  346. * maximum delta chain depth.
  347. */
  348. public void setMaxDeltaDepth(int maxDeltaDepth) {
  349. this.maxDeltaDepth = maxDeltaDepth;
  350. }
  351. /** @return true if this writer is producing a thin pack. */
  352. public boolean isThin() {
  353. return thin;
  354. }
  355. /**
  356. * @param packthin
  357. * a boolean indicating whether writer may pack objects with
  358. * delta base object not within set of objects to pack, but
  359. * belonging to party repository (uninteresting/boundary) as
  360. * determined by set; this kind of pack is used only for
  361. * transport; true - to produce thin pack, false - otherwise.
  362. */
  363. public void setThin(final boolean packthin) {
  364. thin = packthin;
  365. }
  366. /**
  367. * @return true to ignore objects that are uninteresting and also not found
  368. * on local disk; false to throw a {@link MissingObjectException}
  369. * out of {@link #preparePack(Collection, Collection)} if an
  370. * uninteresting object is not in the source repository. By default,
  371. * true, permitting gracefully ignoring of uninteresting objects.
  372. */
  373. public boolean isIgnoreMissingUninteresting() {
  374. return ignoreMissingUninteresting;
  375. }
  376. /**
  377. * @param ignore
  378. * true if writer should ignore non existing uninteresting
  379. * objects during construction set of objects to pack; false
  380. * otherwise - non existing uninteresting objects may cause
  381. * {@link MissingObjectException}
  382. */
  383. public void setIgnoreMissingUninteresting(final boolean ignore) {
  384. ignoreMissingUninteresting = ignore;
  385. }
  386. /**
  387. * Set the pack index file format version this instance will create.
  388. *
  389. * @param version
  390. * the version to write. The special version 0 designates the
  391. * oldest (most compatible) format available for the objects.
  392. * @see PackIndexWriter
  393. */
  394. public void setIndexVersion(final int version) {
  395. outputVersion = version;
  396. }
  397. /**
  398. * Returns objects number in a pack file that was created by this writer.
  399. *
  400. * @return number of objects in pack.
  401. */
  402. public int getObjectsNumber() {
  403. return objectsMap.size();
  404. }
  405. /**
  406. * Prepare the list of objects to be written to the pack stream.
  407. * <p>
  408. * Iterator <b>exactly</b> determines which objects are included in a pack
  409. * and order they appear in pack (except that objects order by type is not
  410. * needed at input). This order should conform general rules of ordering
  411. * objects in git - by recency and path (type and delta-base first is
  412. * internally secured) and responsibility for guaranteeing this order is on
  413. * a caller side. Iterator must return each id of object to write exactly
  414. * once.
  415. * </p>
  416. * <p>
  417. * When iterator returns object that has {@link RevFlag#UNINTERESTING} flag,
  418. * this object won't be included in an output pack. Instead, it is recorded
  419. * as edge-object (known to remote repository) for thin-pack. In such a case
  420. * writer may pack objects with delta base object not within set of objects
  421. * to pack, but belonging to party repository - those marked with
  422. * {@link RevFlag#UNINTERESTING} flag. This type of pack is used only for
  423. * transport.
  424. * </p>
  425. *
  426. * @param objectsSource
  427. * iterator of object to store in a pack; order of objects within
  428. * each type is important, ordering by type is not needed;
  429. * allowed types for objects are {@link Constants#OBJ_COMMIT},
  430. * {@link Constants#OBJ_TREE}, {@link Constants#OBJ_BLOB} and
  431. * {@link Constants#OBJ_TAG}; objects returned by iterator may
  432. * be later reused by caller as object id and type are internally
  433. * copied in each iteration; if object returned by iterator has
  434. * {@link RevFlag#UNINTERESTING} flag set, it won't be included
  435. * in a pack, but is considered as edge-object for thin-pack.
  436. * @throws IOException
  437. * when some I/O problem occur during reading objects.
  438. */
  439. public void preparePack(final Iterator<RevObject> objectsSource)
  440. throws IOException {
  441. while (objectsSource.hasNext()) {
  442. addObject(objectsSource.next());
  443. }
  444. }
  445. /**
  446. * Prepare the list of objects to be written to the pack stream.
  447. * <p>
  448. * Basing on these 2 sets, another set of objects to put in a pack file is
  449. * created: this set consists of all objects reachable (ancestors) from
  450. * interesting objects, except uninteresting objects and their ancestors.
  451. * This method uses class {@link ObjectWalk} extensively to find out that
  452. * appropriate set of output objects and their optimal order in output pack.
  453. * Order is consistent with general git in-pack rules: sort by object type,
  454. * recency, path and delta-base first.
  455. * </p>
  456. *
  457. * @param interestingObjects
  458. * collection of objects to be marked as interesting (start
  459. * points of graph traversal).
  460. * @param uninterestingObjects
  461. * collection of objects to be marked as uninteresting (end
  462. * points of graph traversal).
  463. * @throws IOException
  464. * when some I/O problem occur during reading objects.
  465. */
  466. public void preparePack(
  467. final Collection<? extends ObjectId> interestingObjects,
  468. final Collection<? extends ObjectId> uninterestingObjects)
  469. throws IOException {
  470. ObjectWalk walker = setUpWalker(interestingObjects,
  471. uninterestingObjects);
  472. findObjectsToPack(walker);
  473. }
  474. /**
  475. * Determine if the pack file will contain the requested object.
  476. *
  477. * @param id
  478. * the object to test the existence of.
  479. * @return true if the object will appear in the output pack file.
  480. */
  481. public boolean willInclude(final AnyObjectId id) {
  482. return objectsMap.get(id) != null;
  483. }
  484. /**
  485. * Computes SHA-1 of lexicographically sorted objects ids written in this
  486. * pack, as used to name a pack file in repository.
  487. *
  488. * @return ObjectId representing SHA-1 name of a pack that was created.
  489. */
  490. public ObjectId computeName() {
  491. final byte[] buf = new byte[Constants.OBJECT_ID_LENGTH];
  492. final MessageDigest md = Constants.newMessageDigest();
  493. for (ObjectToPack otp : sortByName()) {
  494. otp.copyRawTo(buf, 0);
  495. md.update(buf, 0, Constants.OBJECT_ID_LENGTH);
  496. }
  497. return ObjectId.fromRaw(md.digest());
  498. }
  499. /**
  500. * Create an index file to match the pack file just written.
  501. * <p>
  502. * This method can only be invoked after {@link #preparePack(Iterator)} or
  503. * {@link #preparePack(Collection, Collection)} has been
  504. * invoked and completed successfully. Writing a corresponding index is an
  505. * optional feature that not all pack users may require.
  506. *
  507. * @param indexStream
  508. * output for the index data. Caller is responsible for closing
  509. * this stream.
  510. * @throws IOException
  511. * the index data could not be written to the supplied stream.
  512. */
  513. public void writeIndex(final OutputStream indexStream) throws IOException {
  514. final List<ObjectToPack> list = sortByName();
  515. final PackIndexWriter iw;
  516. if (outputVersion <= 0)
  517. iw = PackIndexWriter.createOldestPossible(indexStream, list);
  518. else
  519. iw = PackIndexWriter.createVersion(indexStream, outputVersion);
  520. iw.write(list, packcsum);
  521. }
  522. private List<ObjectToPack> sortByName() {
  523. if (sortedByName == null) {
  524. sortedByName = new ArrayList<ObjectToPack>(objectsMap.size());
  525. for (List<ObjectToPack> list : objectsLists) {
  526. for (ObjectToPack otp : list)
  527. sortedByName.add(otp);
  528. }
  529. Collections.sort(sortedByName);
  530. }
  531. return sortedByName;
  532. }
  533. /**
  534. * Write the prepared pack to the supplied stream.
  535. * <p>
  536. * At first, this method collects and sorts objects to pack, then deltas
  537. * search is performed if set up accordingly, finally pack stream is
  538. * written. {@link ProgressMonitor} tasks {@value #SEARCHING_REUSE_PROGRESS}
  539. * (only if reuseDeltas or reuseObjects is enabled) and
  540. * {@value #WRITING_OBJECTS_PROGRESS} are updated during packing.
  541. * </p>
  542. * <p>
  543. * All reused objects data checksum (Adler32/CRC32) is computed and
  544. * validated against existing checksum.
  545. * </p>
  546. *
  547. * @param packStream
  548. * output stream of pack data. The stream should be buffered by
  549. * the caller. The caller is responsible for closing the stream.
  550. * @throws IOException
  551. * an error occurred reading a local object's data to include in
  552. * the pack, or writing compressed object data to the output
  553. * stream.
  554. */
  555. public void writePack(OutputStream packStream) throws IOException {
  556. if ((reuseDeltas || reuseObjects) && reuseSupport != null)
  557. searchForReuse();
  558. out = new PackOutputStream(packStream, isDeltaBaseAsOffset());
  559. writeMonitor.beginTask(WRITING_OBJECTS_PROGRESS, getObjectsNumber());
  560. out.writeFileHeader(PACK_VERSION_GENERATED, getObjectsNumber());
  561. writeObjects();
  562. writeChecksum();
  563. out = null;
  564. reader.release();
  565. writeMonitor.endTask();
  566. }
  567. private void searchForReuse() throws IOException {
  568. initMonitor.beginTask(SEARCHING_REUSE_PROGRESS, getObjectsNumber());
  569. for (List<ObjectToPack> list : objectsLists) {
  570. for (ObjectToPack otp : list) {
  571. if (initMonitor.isCancelled())
  572. throw new IOException(
  573. JGitText.get().packingCancelledDuringObjectsWriting);
  574. reuseSupport.selectObjectRepresentation(this, otp);
  575. initMonitor.update(1);
  576. }
  577. }
  578. initMonitor.endTask();
  579. }
  580. private void writeObjects() throws IOException {
  581. for (List<ObjectToPack> list : objectsLists) {
  582. for (ObjectToPack otp : list) {
  583. if (writeMonitor.isCancelled())
  584. throw new IOException(
  585. JGitText.get().packingCancelledDuringObjectsWriting);
  586. if (!otp.isWritten())
  587. writeObject(otp);
  588. }
  589. }
  590. }
  591. private void writeObject(final ObjectToPack otp) throws IOException {
  592. if (otp.isWritten())
  593. return; // We shouldn't be here.
  594. otp.markWantWrite();
  595. if (otp.isDeltaRepresentation())
  596. writeBaseFirst(otp);
  597. out.resetCRC32();
  598. otp.setOffset(out.length());
  599. while (otp.isReuseAsIs()) {
  600. try {
  601. reuseSupport.copyObjectAsIs(out, otp);
  602. otp.setCRC(out.getCRC32());
  603. writeMonitor.update(1);
  604. return;
  605. } catch (StoredObjectRepresentationNotAvailableException gone) {
  606. if (otp.getOffset() == out.length()) {
  607. redoSearchForReuse(otp);
  608. continue;
  609. } else {
  610. // Object writing already started, we cannot recover.
  611. //
  612. CorruptObjectException coe;
  613. coe = new CorruptObjectException(otp, "");
  614. coe.initCause(gone);
  615. throw coe;
  616. }
  617. }
  618. }
  619. // If we reached here, reuse wasn't possible.
  620. //
  621. writeWholeObjectDeflate(otp);
  622. otp.setCRC(out.getCRC32());
  623. writeMonitor.update(1);
  624. }
  625. private void writeBaseFirst(final ObjectToPack otp) throws IOException {
  626. ObjectToPack baseInPack = otp.getDeltaBase();
  627. if (baseInPack != null) {
  628. if (!baseInPack.isWritten()) {
  629. if (baseInPack.wantWrite()) {
  630. // There is a cycle. Our caller is trying to write the
  631. // object we want as a base, and called us. Turn off
  632. // delta reuse so we can find another form.
  633. //
  634. reuseDeltas = false;
  635. redoSearchForReuse(otp);
  636. reuseDeltas = true;
  637. } else {
  638. writeObject(baseInPack);
  639. }
  640. }
  641. } else if (!thin) {
  642. // This should never occur, the base isn't in the pack and
  643. // the pack isn't allowed to reference base outside objects.
  644. // Write the object as a whole form, even if that is slow.
  645. //
  646. otp.clearDeltaBase();
  647. otp.clearReuseAsIs();
  648. }
  649. }
  650. private void redoSearchForReuse(final ObjectToPack otp) throws IOException,
  651. MissingObjectException {
  652. otp.clearDeltaBase();
  653. otp.clearReuseAsIs();
  654. reuseSupport.selectObjectRepresentation(this, otp);
  655. }
  656. private void writeWholeObjectDeflate(final ObjectToPack otp)
  657. throws IOException {
  658. final ObjectLoader loader = db.openObject(reader, otp);
  659. final byte[] data = loader.getCachedBytes();
  660. out.writeHeader(otp, data.length);
  661. deflater.reset();
  662. deflater.setInput(data, 0, data.length);
  663. deflater.finish();
  664. byte[] buf = out.getCopyBuffer();
  665. do {
  666. final int n = deflater.deflate(buf, 0, buf.length);
  667. if (n > 0)
  668. out.write(buf, 0, n);
  669. } while (!deflater.finished());
  670. }
  671. private void writeChecksum() throws IOException {
  672. packcsum = out.getDigest();
  673. out.write(packcsum);
  674. }
  675. private ObjectWalk setUpWalker(
  676. final Collection<? extends ObjectId> interestingObjects,
  677. final Collection<? extends ObjectId> uninterestingObjects)
  678. throws MissingObjectException, IOException,
  679. IncorrectObjectTypeException {
  680. final ObjectWalk walker = new ObjectWalk(db);
  681. walker.setRetainBody(false);
  682. walker.sort(RevSort.COMMIT_TIME_DESC);
  683. if (thin)
  684. walker.sort(RevSort.BOUNDARY, true);
  685. for (ObjectId id : interestingObjects) {
  686. RevObject o = walker.parseAny(id);
  687. walker.markStart(o);
  688. }
  689. if (uninterestingObjects != null) {
  690. for (ObjectId id : uninterestingObjects) {
  691. final RevObject o;
  692. try {
  693. o = walker.parseAny(id);
  694. } catch (MissingObjectException x) {
  695. if (ignoreMissingUninteresting)
  696. continue;
  697. throw x;
  698. }
  699. walker.markUninteresting(o);
  700. }
  701. }
  702. return walker;
  703. }
  704. private void findObjectsToPack(final ObjectWalk walker)
  705. throws MissingObjectException, IncorrectObjectTypeException,
  706. IOException {
  707. initMonitor.beginTask(COUNTING_OBJECTS_PROGRESS,
  708. ProgressMonitor.UNKNOWN);
  709. RevObject o;
  710. while ((o = walker.next()) != null) {
  711. addObject(o);
  712. initMonitor.update(1);
  713. }
  714. while ((o = walker.nextObject()) != null) {
  715. addObject(o);
  716. initMonitor.update(1);
  717. }
  718. initMonitor.endTask();
  719. }
  720. /**
  721. * Include one object to the output file.
  722. * <p>
  723. * Objects are written in the order they are added. If the same object is
  724. * added twice, it may be written twice, creating a larger than necessary
  725. * file.
  726. *
  727. * @param object
  728. * the object to add.
  729. * @throws IncorrectObjectTypeException
  730. * the object is an unsupported type.
  731. */
  732. public void addObject(final RevObject object)
  733. throws IncorrectObjectTypeException {
  734. if (object.has(RevFlag.UNINTERESTING)) {
  735. edgeObjects.add(object);
  736. thin = true;
  737. return;
  738. }
  739. final ObjectToPack otp;
  740. if (reuseSupport != null)
  741. otp = reuseSupport.newObjectToPack(object);
  742. else
  743. otp = new ObjectToPack(object);
  744. try {
  745. objectsLists[object.getType()].add(otp);
  746. } catch (ArrayIndexOutOfBoundsException x) {
  747. throw new IncorrectObjectTypeException(object,
  748. JGitText.get().incorrectObjectType_COMMITnorTREEnorBLOBnorTAG);
  749. } catch (UnsupportedOperationException x) {
  750. // index pointing to "dummy" empty list
  751. throw new IncorrectObjectTypeException(object,
  752. JGitText.get().incorrectObjectType_COMMITnorTREEnorBLOBnorTAG);
  753. }
  754. objectsMap.add(otp);
  755. }
  756. /**
  757. * Select an object representation for this writer.
  758. * <p>
  759. * An {@link ObjectReader} implementation should invoke this method once for
  760. * each representation available for an object, to allow the writer to find
  761. * the most suitable one for the output.
  762. *
  763. * @param otp
  764. * the object being packed.
  765. * @param next
  766. * the next available representation from the repository.
  767. */
  768. public void select(ObjectToPack otp, StoredObjectRepresentation next) {
  769. int nFmt = next.getFormat();
  770. int nWeight;
  771. if (otp.isReuseAsIs()) {
  772. // We've already chosen to reuse a packed form, if next
  773. // cannot beat that break out early.
  774. //
  775. if (PACK_WHOLE < nFmt)
  776. return; // next isn't packed
  777. else if (PACK_DELTA < nFmt && otp.isDeltaRepresentation())
  778. return; // next isn't a delta, but we are
  779. nWeight = next.getWeight();
  780. if (otp.getWeight() <= nWeight)
  781. return; // next would be bigger
  782. } else
  783. nWeight = next.getWeight();
  784. if (nFmt == PACK_DELTA && reuseDeltas) {
  785. ObjectId baseId = next.getDeltaBase();
  786. ObjectToPack ptr = objectsMap.get(baseId);
  787. if (ptr != null) {
  788. otp.setDeltaBase(ptr);
  789. otp.setReuseAsIs();
  790. otp.setWeight(nWeight);
  791. } else if (thin && edgeObjects.contains(baseId)) {
  792. otp.setDeltaBase(baseId);
  793. otp.setReuseAsIs();
  794. otp.setWeight(nWeight);
  795. } else {
  796. otp.clearDeltaBase();
  797. otp.clearReuseAsIs();
  798. }
  799. } else if (nFmt == PACK_WHOLE && reuseObjects) {
  800. otp.clearDeltaBase();
  801. otp.setReuseAsIs();
  802. otp.setWeight(nWeight);
  803. } else {
  804. otp.clearDeltaBase();
  805. otp.clearReuseAsIs();
  806. }
  807. otp.select(next);
  808. }
  809. }