Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PackWriter.java 28KB

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