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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  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.internal.storage.pack;
  45. import static java.util.Objects.requireNonNull;
  46. import static org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation.PACK_DELTA;
  47. import static org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation.PACK_WHOLE;
  48. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  49. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  50. import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
  51. import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
  52. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  53. import java.io.IOException;
  54. import java.io.OutputStream;
  55. import java.lang.ref.WeakReference;
  56. import java.security.MessageDigest;
  57. import java.text.MessageFormat;
  58. import java.util.ArrayList;
  59. import java.util.Arrays;
  60. import java.util.Collection;
  61. import java.util.Collections;
  62. import java.util.Comparator;
  63. import java.util.HashSet;
  64. import java.util.Iterator;
  65. import java.util.List;
  66. import java.util.Map;
  67. import java.util.NoSuchElementException;
  68. import java.util.Set;
  69. import java.util.concurrent.ConcurrentHashMap;
  70. import java.util.concurrent.ExecutionException;
  71. import java.util.concurrent.Executor;
  72. import java.util.concurrent.ExecutorService;
  73. import java.util.concurrent.Executors;
  74. import java.util.concurrent.Future;
  75. import java.util.concurrent.TimeUnit;
  76. import java.util.zip.CRC32;
  77. import java.util.zip.CheckedOutputStream;
  78. import java.util.zip.Deflater;
  79. import java.util.zip.DeflaterOutputStream;
  80. import org.eclipse.jgit.annotations.NonNull;
  81. import org.eclipse.jgit.annotations.Nullable;
  82. import org.eclipse.jgit.errors.CorruptObjectException;
  83. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  84. import org.eclipse.jgit.errors.LargeObjectException;
  85. import org.eclipse.jgit.errors.MissingObjectException;
  86. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  87. import org.eclipse.jgit.internal.JGitText;
  88. import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder;
  89. import org.eclipse.jgit.internal.storage.file.PackBitmapIndexWriterV1;
  90. import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
  91. import org.eclipse.jgit.lib.AnyObjectId;
  92. import org.eclipse.jgit.lib.AsyncObjectSizeQueue;
  93. import org.eclipse.jgit.lib.BatchingProgressMonitor;
  94. import org.eclipse.jgit.lib.BitmapIndex;
  95. import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
  96. import org.eclipse.jgit.lib.BitmapObject;
  97. import org.eclipse.jgit.lib.Constants;
  98. import org.eclipse.jgit.lib.NullProgressMonitor;
  99. import org.eclipse.jgit.lib.ObjectId;
  100. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  101. import org.eclipse.jgit.lib.ObjectIdSet;
  102. import org.eclipse.jgit.lib.ObjectLoader;
  103. import org.eclipse.jgit.lib.ObjectReader;
  104. import org.eclipse.jgit.lib.ProgressMonitor;
  105. import org.eclipse.jgit.lib.Repository;
  106. import org.eclipse.jgit.lib.ThreadSafeProgressMonitor;
  107. import org.eclipse.jgit.revwalk.AsyncRevObjectQueue;
  108. import org.eclipse.jgit.revwalk.BitmapWalker;
  109. import org.eclipse.jgit.revwalk.DepthWalk;
  110. import org.eclipse.jgit.revwalk.ObjectWalk;
  111. import org.eclipse.jgit.revwalk.RevCommit;
  112. import org.eclipse.jgit.revwalk.RevFlag;
  113. import org.eclipse.jgit.revwalk.RevObject;
  114. import org.eclipse.jgit.revwalk.RevSort;
  115. import org.eclipse.jgit.revwalk.RevTag;
  116. import org.eclipse.jgit.revwalk.RevTree;
  117. import org.eclipse.jgit.storage.pack.PackConfig;
  118. import org.eclipse.jgit.storage.pack.PackStatistics;
  119. import org.eclipse.jgit.transport.FilterSpec;
  120. import org.eclipse.jgit.transport.ObjectCountCallback;
  121. import org.eclipse.jgit.transport.WriteAbortedException;
  122. import org.eclipse.jgit.util.BlockList;
  123. import org.eclipse.jgit.util.TemporaryBuffer;
  124. /**
  125. * <p>
  126. * PackWriter class is responsible for generating pack files from specified set
  127. * of objects from repository. This implementation produce pack files in format
  128. * version 2.
  129. * </p>
  130. * <p>
  131. * Source of objects may be specified in two ways:
  132. * <ul>
  133. * <li>(usually) by providing sets of interesting and uninteresting objects in
  134. * repository - all interesting objects and their ancestors except uninteresting
  135. * objects and their ancestors will be included in pack, or</li>
  136. * <li>by providing iterator of {@link org.eclipse.jgit.revwalk.RevObject}
  137. * specifying exact list and order of objects in pack</li>
  138. * </ul>
  139. * <p>
  140. * Typical usage consists of creating an instance, configuring options,
  141. * preparing the list of objects by calling {@link #preparePack(Iterator)} or
  142. * {@link #preparePack(ProgressMonitor, Set, Set)}, and streaming with
  143. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)}. If the
  144. * pack is being stored as a file the matching index can be written out after
  145. * writing the pack by {@link #writeIndex(OutputStream)}. An optional bitmap
  146. * index can be made by calling {@link #prepareBitmapIndex(ProgressMonitor)}
  147. * followed by {@link #writeBitmapIndex(OutputStream)}.
  148. * </p>
  149. * <p>
  150. * Class provide set of configurable options and
  151. * {@link org.eclipse.jgit.lib.ProgressMonitor} support, as operations may take
  152. * a long time for big repositories. Deltas searching algorithm is <b>NOT
  153. * IMPLEMENTED</b> yet - this implementation relies only on deltas and objects
  154. * reuse.
  155. * </p>
  156. * <p>
  157. * This class is not thread safe. It is intended to be used in one thread as a
  158. * single pass to produce one pack. Invoking methods multiple times or out of
  159. * order is not supported as internal data structures are destroyed during
  160. * certain phases to save memory when packing large repositories.
  161. * </p>
  162. */
  163. public class PackWriter implements AutoCloseable {
  164. private static final int PACK_VERSION_GENERATED = 2;
  165. /** Empty set of objects for {@code preparePack()}. */
  166. public static final Set<ObjectId> NONE = Collections.emptySet();
  167. private static final Map<WeakReference<PackWriter>, Boolean> instances =
  168. new ConcurrentHashMap<>();
  169. private static final Iterable<PackWriter> instancesIterable = new Iterable<PackWriter>() {
  170. @Override
  171. public Iterator<PackWriter> iterator() {
  172. return new Iterator<PackWriter>() {
  173. private final Iterator<WeakReference<PackWriter>> it =
  174. instances.keySet().iterator();
  175. private PackWriter next;
  176. @Override
  177. public boolean hasNext() {
  178. if (next != null)
  179. return true;
  180. while (it.hasNext()) {
  181. WeakReference<PackWriter> ref = it.next();
  182. next = ref.get();
  183. if (next != null)
  184. return true;
  185. it.remove();
  186. }
  187. return false;
  188. }
  189. @Override
  190. public PackWriter next() {
  191. if (hasNext()) {
  192. PackWriter result = next;
  193. next = null;
  194. return result;
  195. }
  196. throw new NoSuchElementException();
  197. }
  198. @Override
  199. public void remove() {
  200. throw new UnsupportedOperationException();
  201. }
  202. };
  203. }
  204. };
  205. /**
  206. * Get all allocated, non-released PackWriters instances.
  207. *
  208. * @return all allocated, non-released PackWriters instances.
  209. */
  210. public static Iterable<PackWriter> getInstances() {
  211. return instancesIterable;
  212. }
  213. @SuppressWarnings("unchecked")
  214. BlockList<ObjectToPack> objectsLists[] = new BlockList[OBJ_TAG + 1];
  215. {
  216. objectsLists[OBJ_COMMIT] = new BlockList<>();
  217. objectsLists[OBJ_TREE] = new BlockList<>();
  218. objectsLists[OBJ_BLOB] = new BlockList<>();
  219. objectsLists[OBJ_TAG] = new BlockList<>();
  220. }
  221. private ObjectIdOwnerMap<ObjectToPack> objectsMap = new ObjectIdOwnerMap<>();
  222. // edge objects for thin packs
  223. private List<ObjectToPack> edgeObjects = new BlockList<>();
  224. // Objects the client is known to have already.
  225. private BitmapBuilder haveObjects;
  226. private List<CachedPack> cachedPacks = new ArrayList<>(2);
  227. private Set<ObjectId> tagTargets = NONE;
  228. private Set<? extends ObjectId> excludeFromBitmapSelection = NONE;
  229. private ObjectIdSet[] excludeInPacks;
  230. private ObjectIdSet excludeInPackLast;
  231. private Deflater myDeflater;
  232. private final ObjectReader reader;
  233. /** {@link #reader} recast to the reuse interface, if it supports it. */
  234. private final ObjectReuseAsIs reuseSupport;
  235. final PackConfig config;
  236. private final PackStatistics.Accumulator stats;
  237. private final MutableState state;
  238. private final WeakReference<PackWriter> selfRef;
  239. private PackStatistics.ObjectType.Accumulator typeStats;
  240. private List<ObjectToPack> sortedByName;
  241. private byte packcsum[];
  242. private boolean deltaBaseAsOffset;
  243. private boolean reuseDeltas;
  244. private boolean reuseDeltaCommits;
  245. private boolean reuseValidate;
  246. private boolean thin;
  247. private boolean useCachedPacks;
  248. private boolean useBitmaps;
  249. private boolean ignoreMissingUninteresting = true;
  250. private boolean pruneCurrentObjectList;
  251. private boolean shallowPack;
  252. private boolean canBuildBitmaps;
  253. private boolean indexDisabled;
  254. private int depth;
  255. private Collection<? extends ObjectId> unshallowObjects;
  256. private PackBitmapIndexBuilder writeBitmaps;
  257. private CRC32 crc32;
  258. private ObjectCountCallback callback;
  259. private FilterSpec filterSpec = FilterSpec.NO_FILTER;
  260. /**
  261. * Create writer for specified repository.
  262. * <p>
  263. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  264. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  265. *
  266. * @param repo
  267. * repository where objects are stored.
  268. */
  269. public PackWriter(Repository repo) {
  270. this(repo, repo.newObjectReader());
  271. }
  272. /**
  273. * Create a writer to load objects from the specified reader.
  274. * <p>
  275. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  276. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  277. *
  278. * @param reader
  279. * reader to read from the repository with.
  280. */
  281. public PackWriter(ObjectReader reader) {
  282. this(new PackConfig(), reader);
  283. }
  284. /**
  285. * Create writer for specified repository.
  286. * <p>
  287. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  288. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  289. *
  290. * @param repo
  291. * repository where objects are stored.
  292. * @param reader
  293. * reader to read from the repository with.
  294. */
  295. public PackWriter(Repository repo, ObjectReader reader) {
  296. this(new PackConfig(repo), reader);
  297. }
  298. /**
  299. * Create writer with a specified configuration.
  300. * <p>
  301. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  302. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  303. *
  304. * @param config
  305. * configuration for the pack writer.
  306. * @param reader
  307. * reader to read from the repository with.
  308. */
  309. public PackWriter(PackConfig config, ObjectReader reader) {
  310. this(config, reader, null);
  311. }
  312. /**
  313. * Create writer with a specified configuration.
  314. * <p>
  315. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  316. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  317. *
  318. * @param config
  319. * configuration for the pack writer.
  320. * @param reader
  321. * reader to read from the repository with.
  322. * @param statsAccumulator
  323. * accumulator for statics
  324. */
  325. public PackWriter(PackConfig config, final ObjectReader reader,
  326. @Nullable PackStatistics.Accumulator statsAccumulator) {
  327. this.config = config;
  328. this.reader = reader;
  329. if (reader instanceof ObjectReuseAsIs)
  330. reuseSupport = ((ObjectReuseAsIs) reader);
  331. else
  332. reuseSupport = null;
  333. deltaBaseAsOffset = config.isDeltaBaseAsOffset();
  334. reuseDeltas = config.isReuseDeltas();
  335. reuseValidate = true; // be paranoid by default
  336. stats = statsAccumulator != null ? statsAccumulator
  337. : new PackStatistics.Accumulator();
  338. state = new MutableState();
  339. selfRef = new WeakReference<>(this);
  340. instances.put(selfRef, Boolean.TRUE);
  341. }
  342. /**
  343. * Set the {@code ObjectCountCallback}.
  344. * <p>
  345. * It should be set before calling
  346. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)}.
  347. *
  348. * @param callback
  349. * the callback to set
  350. * @return this object for chaining.
  351. */
  352. public PackWriter setObjectCountCallback(ObjectCountCallback callback) {
  353. this.callback = callback;
  354. return this;
  355. }
  356. /**
  357. * Records the set of shallow commits in the client.
  358. *
  359. * @param clientShallowCommits
  360. * the shallow commits in the client
  361. */
  362. public void setClientShallowCommits(Set<ObjectId> clientShallowCommits) {
  363. stats.clientShallowCommits = Collections
  364. .unmodifiableSet(new HashSet<>(clientShallowCommits));
  365. }
  366. /**
  367. * Check whether writer can store delta base as an offset (new style
  368. * reducing pack size) or should store it as an object id (legacy style,
  369. * compatible with old readers).
  370. *
  371. * Default setting: {@value PackConfig#DEFAULT_DELTA_BASE_AS_OFFSET}
  372. *
  373. * @return true if delta base is stored as an offset; false if it is stored
  374. * as an object id.
  375. */
  376. public boolean isDeltaBaseAsOffset() {
  377. return deltaBaseAsOffset;
  378. }
  379. /**
  380. * Set writer delta base format. Delta base can be written as an offset in a
  381. * pack file (new approach reducing file size) or as an object id (legacy
  382. * approach, compatible with old readers).
  383. *
  384. * Default setting: {@value PackConfig#DEFAULT_DELTA_BASE_AS_OFFSET}
  385. *
  386. * @param deltaBaseAsOffset
  387. * boolean indicating whether delta base can be stored as an
  388. * offset.
  389. */
  390. public void setDeltaBaseAsOffset(boolean deltaBaseAsOffset) {
  391. this.deltaBaseAsOffset = deltaBaseAsOffset;
  392. }
  393. /**
  394. * Check if the writer will reuse commits that are already stored as deltas.
  395. *
  396. * @return true if the writer would reuse commits stored as deltas, assuming
  397. * delta reuse is already enabled.
  398. */
  399. public boolean isReuseDeltaCommits() {
  400. return reuseDeltaCommits;
  401. }
  402. /**
  403. * Set the writer to reuse existing delta versions of commits.
  404. *
  405. * @param reuse
  406. * if true, the writer will reuse any commits stored as deltas.
  407. * By default the writer does not reuse delta commits.
  408. */
  409. public void setReuseDeltaCommits(boolean reuse) {
  410. reuseDeltaCommits = reuse;
  411. }
  412. /**
  413. * Check if the writer validates objects before copying them.
  414. *
  415. * @return true if validation is enabled; false if the reader will handle
  416. * object validation as a side-effect of it consuming the output.
  417. */
  418. public boolean isReuseValidatingObjects() {
  419. return reuseValidate;
  420. }
  421. /**
  422. * Enable (or disable) object validation during packing.
  423. *
  424. * @param validate
  425. * if true the pack writer will validate an object before it is
  426. * put into the output. This additional validation work may be
  427. * necessary to avoid propagating corruption from one local pack
  428. * file to another local pack file.
  429. */
  430. public void setReuseValidatingObjects(boolean validate) {
  431. reuseValidate = validate;
  432. }
  433. /**
  434. * Whether this writer is producing a thin pack.
  435. *
  436. * @return true if this writer is producing a thin pack.
  437. */
  438. public boolean isThin() {
  439. return thin;
  440. }
  441. /**
  442. * Whether writer may pack objects with delta base object not within set of
  443. * objects to pack
  444. *
  445. * @param packthin
  446. * a boolean indicating whether writer may pack objects with
  447. * delta base object not within set of objects to pack, but
  448. * belonging to party repository (uninteresting/boundary) as
  449. * determined by set; this kind of pack is used only for
  450. * transport; true - to produce thin pack, false - otherwise.
  451. */
  452. public void setThin(boolean packthin) {
  453. thin = packthin;
  454. }
  455. /**
  456. * Whether to reuse cached packs.
  457. *
  458. * @return {@code true} to reuse cached packs. If true index creation isn't
  459. * available.
  460. */
  461. public boolean isUseCachedPacks() {
  462. return useCachedPacks;
  463. }
  464. /**
  465. * Whether to use cached packs
  466. *
  467. * @param useCached
  468. * if set to {@code true} and a cached pack is present, it will
  469. * be appended onto the end of a thin-pack, reducing the amount
  470. * of working set space and CPU used by PackWriter. Enabling this
  471. * feature prevents PackWriter from creating an index for the
  472. * newly created pack, so its only suitable for writing to a
  473. * network client, where the client will make the index.
  474. */
  475. public void setUseCachedPacks(boolean useCached) {
  476. useCachedPacks = useCached;
  477. }
  478. /**
  479. * Whether to use bitmaps
  480. *
  481. * @return {@code true} to use bitmaps for ObjectWalks, if available.
  482. */
  483. public boolean isUseBitmaps() {
  484. return useBitmaps;
  485. }
  486. /**
  487. * Whether to use bitmaps
  488. *
  489. * @param useBitmaps
  490. * if set to true, bitmaps will be used when preparing a pack.
  491. */
  492. public void setUseBitmaps(boolean useBitmaps) {
  493. this.useBitmaps = useBitmaps;
  494. }
  495. /**
  496. * Whether the index file cannot be created by this PackWriter.
  497. *
  498. * @return {@code true} if the index file cannot be created by this
  499. * PackWriter.
  500. */
  501. public boolean isIndexDisabled() {
  502. return indexDisabled || !cachedPacks.isEmpty();
  503. }
  504. /**
  505. * Whether to disable creation of the index file.
  506. *
  507. * @param noIndex
  508. * {@code true} to disable creation of the index file.
  509. */
  510. public void setIndexDisabled(boolean noIndex) {
  511. this.indexDisabled = noIndex;
  512. }
  513. /**
  514. * Whether to ignore missing uninteresting objects
  515. *
  516. * @return {@code true} to ignore objects that are uninteresting and also
  517. * not found on local disk; false to throw a
  518. * {@link org.eclipse.jgit.errors.MissingObjectException} out of
  519. * {@link #preparePack(ProgressMonitor, Set, Set)} if an
  520. * uninteresting object is not in the source repository. By default,
  521. * true, permitting gracefully ignoring of uninteresting objects.
  522. */
  523. public boolean isIgnoreMissingUninteresting() {
  524. return ignoreMissingUninteresting;
  525. }
  526. /**
  527. * Whether writer should ignore non existing uninteresting objects
  528. *
  529. * @param ignore
  530. * {@code true} if writer should ignore non existing
  531. * uninteresting objects during construction set of objects to
  532. * pack; false otherwise - non existing uninteresting objects may
  533. * cause {@link org.eclipse.jgit.errors.MissingObjectException}
  534. */
  535. public void setIgnoreMissingUninteresting(boolean ignore) {
  536. ignoreMissingUninteresting = ignore;
  537. }
  538. /**
  539. * Set the tag targets that should be hoisted earlier during packing.
  540. * <p>
  541. * Callers may put objects into this set before invoking any of the
  542. * preparePack methods to influence where an annotated tag's target is
  543. * stored within the resulting pack. Typically these will be clustered
  544. * together, and hoisted earlier in the file even if they are ancient
  545. * revisions, allowing readers to find tag targets with better locality.
  546. *
  547. * @param objects
  548. * objects that annotated tags point at.
  549. */
  550. public void setTagTargets(Set<ObjectId> objects) {
  551. tagTargets = objects;
  552. }
  553. /**
  554. * Configure this pack for a shallow clone.
  555. *
  556. * @param depth
  557. * maximum depth of history to return. 1 means return only the
  558. * "wants".
  559. * @param unshallow
  560. * objects which used to be shallow on the client, but are being
  561. * extended as part of this fetch
  562. */
  563. public void setShallowPack(int depth,
  564. Collection<? extends ObjectId> unshallow) {
  565. this.shallowPack = true;
  566. this.depth = depth;
  567. this.unshallowObjects = unshallow;
  568. }
  569. /**
  570. * @param filter the filter which indicates what and what not this writer
  571. * should include
  572. */
  573. public void setFilterSpec(@NonNull FilterSpec filter) {
  574. filterSpec = requireNonNull(filter);
  575. }
  576. /**
  577. * Returns objects number in a pack file that was created by this writer.
  578. *
  579. * @return number of objects in pack.
  580. * @throws java.io.IOException
  581. * a cached pack cannot supply its object count.
  582. */
  583. public long getObjectCount() throws IOException {
  584. if (stats.totalObjects == 0) {
  585. long objCnt = 0;
  586. objCnt += objectsLists[OBJ_COMMIT].size();
  587. objCnt += objectsLists[OBJ_TREE].size();
  588. objCnt += objectsLists[OBJ_BLOB].size();
  589. objCnt += objectsLists[OBJ_TAG].size();
  590. for (CachedPack pack : cachedPacks)
  591. objCnt += pack.getObjectCount();
  592. return objCnt;
  593. }
  594. return stats.totalObjects;
  595. }
  596. /**
  597. * Returns the object ids in the pack file that was created by this writer.
  598. * <p>
  599. * This method can only be invoked after
  600. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)} has
  601. * been invoked and completed successfully.
  602. *
  603. * @return set of objects in pack.
  604. * @throws java.io.IOException
  605. * a cached pack cannot supply its object ids.
  606. */
  607. public ObjectIdOwnerMap<ObjectIdOwnerMap.Entry> getObjectSet()
  608. throws IOException {
  609. if (!cachedPacks.isEmpty())
  610. throw new IOException(
  611. JGitText.get().cachedPacksPreventsListingObjects);
  612. if (writeBitmaps != null) {
  613. return writeBitmaps.getObjectSet();
  614. }
  615. ObjectIdOwnerMap<ObjectIdOwnerMap.Entry> r = new ObjectIdOwnerMap<>();
  616. for (BlockList<ObjectToPack> objList : objectsLists) {
  617. if (objList != null) {
  618. for (ObjectToPack otp : objList)
  619. r.add(new ObjectIdOwnerMap.Entry(otp) {
  620. // A new entry that copies the ObjectId
  621. });
  622. }
  623. }
  624. return r;
  625. }
  626. /**
  627. * Add a pack index whose contents should be excluded from the result.
  628. *
  629. * @param idx
  630. * objects in this index will not be in the output pack.
  631. */
  632. public void excludeObjects(ObjectIdSet idx) {
  633. if (excludeInPacks == null) {
  634. excludeInPacks = new ObjectIdSet[] { idx };
  635. excludeInPackLast = idx;
  636. } else {
  637. int cnt = excludeInPacks.length;
  638. ObjectIdSet[] newList = new ObjectIdSet[cnt + 1];
  639. System.arraycopy(excludeInPacks, 0, newList, 0, cnt);
  640. newList[cnt] = idx;
  641. excludeInPacks = newList;
  642. }
  643. }
  644. /**
  645. * Prepare the list of objects to be written to the pack stream.
  646. * <p>
  647. * Iterator <b>exactly</b> determines which objects are included in a pack
  648. * and order they appear in pack (except that objects order by type is not
  649. * needed at input). This order should conform general rules of ordering
  650. * objects in git - by recency and path (type and delta-base first is
  651. * internally secured) and responsibility for guaranteeing this order is on
  652. * a caller side. Iterator must return each id of object to write exactly
  653. * once.
  654. * </p>
  655. *
  656. * @param objectsSource
  657. * iterator of object to store in a pack; order of objects within
  658. * each type is important, ordering by type is not needed;
  659. * allowed types for objects are
  660. * {@link org.eclipse.jgit.lib.Constants#OBJ_COMMIT},
  661. * {@link org.eclipse.jgit.lib.Constants#OBJ_TREE},
  662. * {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB} and
  663. * {@link org.eclipse.jgit.lib.Constants#OBJ_TAG}; objects
  664. * returned by iterator may be later reused by caller as object
  665. * id and type are internally copied in each iteration.
  666. * @throws java.io.IOException
  667. * when some I/O problem occur during reading objects.
  668. */
  669. public void preparePack(@NonNull Iterator<RevObject> objectsSource)
  670. throws IOException {
  671. while (objectsSource.hasNext()) {
  672. addObject(objectsSource.next());
  673. }
  674. }
  675. /**
  676. * Prepare the list of objects to be written to the pack stream.
  677. * <p>
  678. * Basing on these 2 sets, another set of objects to put in a pack file is
  679. * created: this set consists of all objects reachable (ancestors) from
  680. * interesting objects, except uninteresting objects and their ancestors.
  681. * This method uses class {@link org.eclipse.jgit.revwalk.ObjectWalk}
  682. * extensively to find out that appropriate set of output objects and their
  683. * optimal order in output pack. Order is consistent with general git
  684. * in-pack rules: sort by object type, recency, path and delta-base first.
  685. * </p>
  686. *
  687. * @param countingMonitor
  688. * progress during object enumeration.
  689. * @param want
  690. * collection of objects to be marked as interesting (start
  691. * points of graph traversal). Must not be {@code null}.
  692. * @param have
  693. * collection of objects to be marked as uninteresting (end
  694. * points of graph traversal). Pass {@link #NONE} if all objects
  695. * reachable from {@code want} are desired, such as when serving
  696. * a clone.
  697. * @throws java.io.IOException
  698. * when some I/O problem occur during reading objects.
  699. */
  700. public void preparePack(ProgressMonitor countingMonitor,
  701. @NonNull Set<? extends ObjectId> want,
  702. @NonNull Set<? extends ObjectId> have) throws IOException {
  703. preparePack(countingMonitor, want, have, NONE, NONE);
  704. }
  705. /**
  706. * Prepare the list of objects to be written to the pack stream.
  707. * <p>
  708. * Like {@link #preparePack(ProgressMonitor, Set, Set)} but also allows
  709. * specifying commits that should not be walked past ("shallow" commits).
  710. * The caller is responsible for filtering out commits that should not be
  711. * shallow any more ("unshallow" commits as in {@link #setShallowPack}) from
  712. * the shallow set.
  713. *
  714. * @param countingMonitor
  715. * progress during object enumeration.
  716. * @param want
  717. * objects of interest, ancestors of which will be included in
  718. * the pack. Must not be {@code null}.
  719. * @param have
  720. * objects whose ancestors (up to and including {@code shallow}
  721. * commits) do not need to be included in the pack because they
  722. * are already available from elsewhere. Must not be
  723. * {@code null}.
  724. * @param shallow
  725. * commits indicating the boundary of the history marked with
  726. * {@code have}. Shallow commits have parents but those parents
  727. * are considered not to be already available. Parents of
  728. * {@code shallow} commits and earlier generations will be
  729. * included in the pack if requested by {@code want}. Must not be
  730. * {@code null}.
  731. * @throws java.io.IOException
  732. * an I/O problem occurred while reading objects.
  733. */
  734. public void preparePack(ProgressMonitor countingMonitor,
  735. @NonNull Set<? extends ObjectId> want,
  736. @NonNull Set<? extends ObjectId> have,
  737. @NonNull Set<? extends ObjectId> shallow) throws IOException {
  738. preparePack(countingMonitor, want, have, shallow, NONE);
  739. }
  740. /**
  741. * Prepare the list of objects to be written to the pack stream.
  742. * <p>
  743. * Like {@link #preparePack(ProgressMonitor, Set, Set)} but also allows
  744. * specifying commits that should not be walked past ("shallow" commits).
  745. * The caller is responsible for filtering out commits that should not be
  746. * shallow any more ("unshallow" commits as in {@link #setShallowPack}) from
  747. * the shallow set.
  748. *
  749. * @param countingMonitor
  750. * progress during object enumeration.
  751. * @param want
  752. * objects of interest, ancestors of which will be included in
  753. * the pack. Must not be {@code null}.
  754. * @param have
  755. * objects whose ancestors (up to and including {@code shallow}
  756. * commits) do not need to be included in the pack because they
  757. * are already available from elsewhere. Must not be
  758. * {@code null}.
  759. * @param shallow
  760. * commits indicating the boundary of the history marked with
  761. * {@code have}. Shallow commits have parents but those parents
  762. * are considered not to be already available. Parents of
  763. * {@code shallow} commits and earlier generations will be
  764. * included in the pack if requested by {@code want}. Must not be
  765. * {@code null}.
  766. * @param noBitmaps
  767. * collection of objects to be excluded from bitmap commit
  768. * selection.
  769. * @throws java.io.IOException
  770. * an I/O problem occurred while reading objects.
  771. */
  772. public void preparePack(ProgressMonitor countingMonitor,
  773. @NonNull Set<? extends ObjectId> want,
  774. @NonNull Set<? extends ObjectId> have,
  775. @NonNull Set<? extends ObjectId> shallow,
  776. @NonNull Set<? extends ObjectId> noBitmaps) throws IOException {
  777. try (ObjectWalk ow = getObjectWalk()) {
  778. ow.assumeShallow(shallow);
  779. preparePack(countingMonitor, ow, want, have, noBitmaps);
  780. }
  781. }
  782. private ObjectWalk getObjectWalk() {
  783. return shallowPack ? new DepthWalk.ObjectWalk(reader, depth - 1)
  784. : new ObjectWalk(reader);
  785. }
  786. /**
  787. * Prepare the list of objects to be written to the pack stream.
  788. * <p>
  789. * Basing on these 2 sets, another set of objects to put in a pack file is
  790. * created: this set consists of all objects reachable (ancestors) from
  791. * interesting objects, except uninteresting objects and their ancestors.
  792. * This method uses class {@link org.eclipse.jgit.revwalk.ObjectWalk}
  793. * extensively to find out that appropriate set of output objects and their
  794. * optimal order in output pack. Order is consistent with general git
  795. * in-pack rules: sort by object type, recency, path and delta-base first.
  796. * </p>
  797. *
  798. * @param countingMonitor
  799. * progress during object enumeration.
  800. * @param walk
  801. * ObjectWalk to perform enumeration.
  802. * @param interestingObjects
  803. * collection of objects to be marked as interesting (start
  804. * points of graph traversal). Must not be {@code null}.
  805. * @param uninterestingObjects
  806. * collection of objects to be marked as uninteresting (end
  807. * points of graph traversal). Pass {@link #NONE} if all objects
  808. * reachable from {@code want} are desired, such as when serving
  809. * a clone.
  810. * @param noBitmaps
  811. * collection of objects to be excluded from bitmap commit
  812. * selection.
  813. * @throws java.io.IOException
  814. * when some I/O problem occur during reading objects.
  815. */
  816. public void preparePack(ProgressMonitor countingMonitor,
  817. @NonNull ObjectWalk walk,
  818. @NonNull Set<? extends ObjectId> interestingObjects,
  819. @NonNull Set<? extends ObjectId> uninterestingObjects,
  820. @NonNull Set<? extends ObjectId> noBitmaps)
  821. throws IOException {
  822. if (countingMonitor == null)
  823. countingMonitor = NullProgressMonitor.INSTANCE;
  824. if (shallowPack && !(walk instanceof DepthWalk.ObjectWalk))
  825. throw new IllegalArgumentException(
  826. JGitText.get().shallowPacksRequireDepthWalk);
  827. findObjectsToPack(countingMonitor, walk, interestingObjects,
  828. uninterestingObjects, noBitmaps);
  829. }
  830. /**
  831. * Determine if the pack file will contain the requested object.
  832. *
  833. * @param id
  834. * the object to test the existence of.
  835. * @return true if the object will appear in the output pack file.
  836. * @throws java.io.IOException
  837. * a cached pack cannot be examined.
  838. */
  839. public boolean willInclude(AnyObjectId id) throws IOException {
  840. ObjectToPack obj = objectsMap.get(id);
  841. return obj != null && !obj.isEdge();
  842. }
  843. /**
  844. * Lookup the ObjectToPack object for a given ObjectId.
  845. *
  846. * @param id
  847. * the object to find in the pack.
  848. * @return the object we are packing, or null.
  849. */
  850. public ObjectToPack get(AnyObjectId id) {
  851. ObjectToPack obj = objectsMap.get(id);
  852. return obj != null && !obj.isEdge() ? obj : null;
  853. }
  854. /**
  855. * Computes SHA-1 of lexicographically sorted objects ids written in this
  856. * pack, as used to name a pack file in repository.
  857. *
  858. * @return ObjectId representing SHA-1 name of a pack that was created.
  859. */
  860. public ObjectId computeName() {
  861. final byte[] buf = new byte[OBJECT_ID_LENGTH];
  862. final MessageDigest md = Constants.newMessageDigest();
  863. for (ObjectToPack otp : sortByName()) {
  864. otp.copyRawTo(buf, 0);
  865. md.update(buf, 0, OBJECT_ID_LENGTH);
  866. }
  867. return ObjectId.fromRaw(md.digest());
  868. }
  869. /**
  870. * Returns the index format version that will be written.
  871. * <p>
  872. * This method can only be invoked after
  873. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)} has
  874. * been invoked and completed successfully.
  875. *
  876. * @return the index format version.
  877. */
  878. public int getIndexVersion() {
  879. int indexVersion = config.getIndexVersion();
  880. if (indexVersion <= 0) {
  881. for (BlockList<ObjectToPack> objs : objectsLists)
  882. indexVersion = Math.max(indexVersion,
  883. PackIndexWriter.oldestPossibleFormat(objs));
  884. }
  885. return indexVersion;
  886. }
  887. /**
  888. * Create an index file to match the pack file just written.
  889. * <p>
  890. * Called after
  891. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)}.
  892. * <p>
  893. * Writing an index is only required for local pack storage. Packs sent on
  894. * the network do not need to create an index.
  895. *
  896. * @param indexStream
  897. * output for the index data. Caller is responsible for closing
  898. * this stream.
  899. * @throws java.io.IOException
  900. * the index data could not be written to the supplied stream.
  901. */
  902. public void writeIndex(OutputStream indexStream) throws IOException {
  903. if (isIndexDisabled())
  904. throw new IOException(JGitText.get().cachedPacksPreventsIndexCreation);
  905. long writeStart = System.currentTimeMillis();
  906. final PackIndexWriter iw = PackIndexWriter.createVersion(
  907. indexStream, getIndexVersion());
  908. iw.write(sortByName(), packcsum);
  909. stats.timeWriting += System.currentTimeMillis() - writeStart;
  910. }
  911. /**
  912. * Create a bitmap index file to match the pack file just written.
  913. * <p>
  914. * Called after {@link #prepareBitmapIndex(ProgressMonitor)}.
  915. *
  916. * @param bitmapIndexStream
  917. * output for the bitmap index data. Caller is responsible for
  918. * closing this stream.
  919. * @throws java.io.IOException
  920. * the index data could not be written to the supplied stream.
  921. */
  922. public void writeBitmapIndex(OutputStream bitmapIndexStream)
  923. throws IOException {
  924. if (writeBitmaps == null)
  925. throw new IOException(JGitText.get().bitmapsMustBePrepared);
  926. long writeStart = System.currentTimeMillis();
  927. final PackBitmapIndexWriterV1 iw = new PackBitmapIndexWriterV1(bitmapIndexStream);
  928. iw.write(writeBitmaps, packcsum);
  929. stats.timeWriting += System.currentTimeMillis() - writeStart;
  930. }
  931. private List<ObjectToPack> sortByName() {
  932. if (sortedByName == null) {
  933. int cnt = 0;
  934. cnt += objectsLists[OBJ_COMMIT].size();
  935. cnt += objectsLists[OBJ_TREE].size();
  936. cnt += objectsLists[OBJ_BLOB].size();
  937. cnt += objectsLists[OBJ_TAG].size();
  938. sortedByName = new BlockList<>(cnt);
  939. sortedByName.addAll(objectsLists[OBJ_COMMIT]);
  940. sortedByName.addAll(objectsLists[OBJ_TREE]);
  941. sortedByName.addAll(objectsLists[OBJ_BLOB]);
  942. sortedByName.addAll(objectsLists[OBJ_TAG]);
  943. Collections.sort(sortedByName);
  944. }
  945. return sortedByName;
  946. }
  947. private void beginPhase(PackingPhase phase, ProgressMonitor monitor,
  948. long cnt) {
  949. state.phase = phase;
  950. String task;
  951. switch (phase) {
  952. case COUNTING:
  953. task = JGitText.get().countingObjects;
  954. break;
  955. case GETTING_SIZES:
  956. task = JGitText.get().searchForSizes;
  957. break;
  958. case FINDING_SOURCES:
  959. task = JGitText.get().searchForReuse;
  960. break;
  961. case COMPRESSING:
  962. task = JGitText.get().compressingObjects;
  963. break;
  964. case WRITING:
  965. task = JGitText.get().writingObjects;
  966. break;
  967. case BUILDING_BITMAPS:
  968. task = JGitText.get().buildingBitmaps;
  969. break;
  970. default:
  971. throw new IllegalArgumentException(
  972. MessageFormat.format(JGitText.get().illegalPackingPhase, phase));
  973. }
  974. monitor.beginTask(task, (int) cnt);
  975. }
  976. private void endPhase(ProgressMonitor monitor) {
  977. monitor.endTask();
  978. }
  979. /**
  980. * Write the prepared pack to the supplied stream.
  981. * <p>
  982. * Called after
  983. * {@link #preparePack(ProgressMonitor, ObjectWalk, Set, Set, Set)} or
  984. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  985. * <p>
  986. * Performs delta search if enabled and writes the pack stream.
  987. * <p>
  988. * All reused objects data checksum (Adler32/CRC32) is computed and
  989. * validated against existing checksum.
  990. *
  991. * @param compressMonitor
  992. * progress monitor to report object compression work.
  993. * @param writeMonitor
  994. * progress monitor to report the number of objects written.
  995. * @param packStream
  996. * output stream of pack data. The stream should be buffered by
  997. * the caller. The caller is responsible for closing the stream.
  998. * @throws java.io.IOException
  999. * an error occurred reading a local object's data to include in
  1000. * the pack, or writing compressed object data to the output
  1001. * stream.
  1002. * @throws WriteAbortedException
  1003. * the write operation is aborted by
  1004. * {@link org.eclipse.jgit.transport.ObjectCountCallback} .
  1005. */
  1006. public void writePack(ProgressMonitor compressMonitor,
  1007. ProgressMonitor writeMonitor, OutputStream packStream)
  1008. throws IOException {
  1009. if (compressMonitor == null)
  1010. compressMonitor = NullProgressMonitor.INSTANCE;
  1011. if (writeMonitor == null)
  1012. writeMonitor = NullProgressMonitor.INSTANCE;
  1013. excludeInPacks = null;
  1014. excludeInPackLast = null;
  1015. boolean needSearchForReuse = reuseSupport != null && (
  1016. reuseDeltas
  1017. || config.isReuseObjects()
  1018. || !cachedPacks.isEmpty());
  1019. if (compressMonitor instanceof BatchingProgressMonitor) {
  1020. long delay = 1000;
  1021. if (needSearchForReuse && config.isDeltaCompress())
  1022. delay = 500;
  1023. ((BatchingProgressMonitor) compressMonitor).setDelayStart(
  1024. delay,
  1025. TimeUnit.MILLISECONDS);
  1026. }
  1027. if (needSearchForReuse)
  1028. searchForReuse(compressMonitor);
  1029. if (config.isDeltaCompress())
  1030. searchForDeltas(compressMonitor);
  1031. crc32 = new CRC32();
  1032. final PackOutputStream out = new PackOutputStream(
  1033. writeMonitor,
  1034. isIndexDisabled()
  1035. ? packStream
  1036. : new CheckedOutputStream(packStream, crc32),
  1037. this);
  1038. long objCnt = getObjectCount();
  1039. stats.totalObjects = objCnt;
  1040. if (callback != null)
  1041. callback.setObjectCount(objCnt);
  1042. beginPhase(PackingPhase.WRITING, writeMonitor, objCnt);
  1043. long writeStart = System.currentTimeMillis();
  1044. try {
  1045. out.writeFileHeader(PACK_VERSION_GENERATED, objCnt);
  1046. out.flush();
  1047. writeObjects(out);
  1048. if (!edgeObjects.isEmpty() || !cachedPacks.isEmpty()) {
  1049. for (PackStatistics.ObjectType.Accumulator typeStat : stats.objectTypes) {
  1050. if (typeStat == null)
  1051. continue;
  1052. stats.thinPackBytes += typeStat.bytes;
  1053. }
  1054. }
  1055. stats.reusedPacks = Collections.unmodifiableList(cachedPacks);
  1056. for (CachedPack pack : cachedPacks) {
  1057. long deltaCnt = pack.getDeltaCount();
  1058. stats.reusedObjects += pack.getObjectCount();
  1059. stats.reusedDeltas += deltaCnt;
  1060. stats.totalDeltas += deltaCnt;
  1061. reuseSupport.copyPackAsIs(out, pack);
  1062. }
  1063. writeChecksum(out);
  1064. out.flush();
  1065. } finally {
  1066. stats.timeWriting = System.currentTimeMillis() - writeStart;
  1067. stats.depth = depth;
  1068. for (PackStatistics.ObjectType.Accumulator typeStat : stats.objectTypes) {
  1069. if (typeStat == null)
  1070. continue;
  1071. typeStat.cntDeltas += typeStat.reusedDeltas;
  1072. stats.reusedObjects += typeStat.reusedObjects;
  1073. stats.reusedDeltas += typeStat.reusedDeltas;
  1074. stats.totalDeltas += typeStat.cntDeltas;
  1075. }
  1076. }
  1077. stats.totalBytes = out.length();
  1078. reader.close();
  1079. endPhase(writeMonitor);
  1080. }
  1081. /**
  1082. * Get statistics of what this PackWriter did in order to create the final
  1083. * pack stream.
  1084. *
  1085. * @return description of what this PackWriter did in order to create the
  1086. * final pack stream. This should only be invoked after the calls to
  1087. * create the pack/index/bitmap have completed.
  1088. */
  1089. public PackStatistics getStatistics() {
  1090. return new PackStatistics(stats);
  1091. }
  1092. /**
  1093. * Get snapshot of the current state of this PackWriter.
  1094. *
  1095. * @return snapshot of the current state of this PackWriter.
  1096. */
  1097. public State getState() {
  1098. return state.snapshot();
  1099. }
  1100. /**
  1101. * {@inheritDoc}
  1102. * <p>
  1103. * Release all resources used by this writer.
  1104. */
  1105. @Override
  1106. public void close() {
  1107. reader.close();
  1108. if (myDeflater != null) {
  1109. myDeflater.end();
  1110. myDeflater = null;
  1111. }
  1112. instances.remove(selfRef);
  1113. }
  1114. private void searchForReuse(ProgressMonitor monitor) throws IOException {
  1115. long cnt = 0;
  1116. cnt += objectsLists[OBJ_COMMIT].size();
  1117. cnt += objectsLists[OBJ_TREE].size();
  1118. cnt += objectsLists[OBJ_BLOB].size();
  1119. cnt += objectsLists[OBJ_TAG].size();
  1120. long start = System.currentTimeMillis();
  1121. beginPhase(PackingPhase.FINDING_SOURCES, monitor, cnt);
  1122. if (cnt <= 4096) {
  1123. // For small object counts, do everything as one list.
  1124. BlockList<ObjectToPack> tmp = new BlockList<>((int) cnt);
  1125. tmp.addAll(objectsLists[OBJ_TAG]);
  1126. tmp.addAll(objectsLists[OBJ_COMMIT]);
  1127. tmp.addAll(objectsLists[OBJ_TREE]);
  1128. tmp.addAll(objectsLists[OBJ_BLOB]);
  1129. searchForReuse(monitor, tmp);
  1130. if (pruneCurrentObjectList) {
  1131. // If the list was pruned, we need to re-prune the main lists.
  1132. pruneEdgesFromObjectList(objectsLists[OBJ_COMMIT]);
  1133. pruneEdgesFromObjectList(objectsLists[OBJ_TREE]);
  1134. pruneEdgesFromObjectList(objectsLists[OBJ_BLOB]);
  1135. pruneEdgesFromObjectList(objectsLists[OBJ_TAG]);
  1136. }
  1137. } else {
  1138. searchForReuse(monitor, objectsLists[OBJ_TAG]);
  1139. searchForReuse(monitor, objectsLists[OBJ_COMMIT]);
  1140. searchForReuse(monitor, objectsLists[OBJ_TREE]);
  1141. searchForReuse(monitor, objectsLists[OBJ_BLOB]);
  1142. }
  1143. endPhase(monitor);
  1144. stats.timeSearchingForReuse = System.currentTimeMillis() - start;
  1145. if (config.isReuseDeltas() && config.getCutDeltaChains()) {
  1146. cutDeltaChains(objectsLists[OBJ_TREE]);
  1147. cutDeltaChains(objectsLists[OBJ_BLOB]);
  1148. }
  1149. }
  1150. private void searchForReuse(ProgressMonitor monitor, List<ObjectToPack> list)
  1151. throws IOException, MissingObjectException {
  1152. pruneCurrentObjectList = false;
  1153. reuseSupport.selectObjectRepresentation(this, monitor, list);
  1154. if (pruneCurrentObjectList)
  1155. pruneEdgesFromObjectList(list);
  1156. }
  1157. private void cutDeltaChains(BlockList<ObjectToPack> list)
  1158. throws IOException {
  1159. int max = config.getMaxDeltaDepth();
  1160. for (int idx = list.size() - 1; idx >= 0; idx--) {
  1161. int d = 0;
  1162. ObjectToPack b = list.get(idx).getDeltaBase();
  1163. while (b != null) {
  1164. if (d < b.getChainLength())
  1165. break;
  1166. b.setChainLength(++d);
  1167. if (d >= max && b.isDeltaRepresentation()) {
  1168. reselectNonDelta(b);
  1169. break;
  1170. }
  1171. b = b.getDeltaBase();
  1172. }
  1173. }
  1174. if (config.isDeltaCompress()) {
  1175. for (ObjectToPack otp : list)
  1176. otp.clearChainLength();
  1177. }
  1178. }
  1179. private void searchForDeltas(ProgressMonitor monitor)
  1180. throws MissingObjectException, IncorrectObjectTypeException,
  1181. IOException {
  1182. // Commits and annotated tags tend to have too many differences to
  1183. // really benefit from delta compression. Consequently just don't
  1184. // bother examining those types here.
  1185. //
  1186. ObjectToPack[] list = new ObjectToPack[
  1187. objectsLists[OBJ_TREE].size()
  1188. + objectsLists[OBJ_BLOB].size()
  1189. + edgeObjects.size()];
  1190. int cnt = 0;
  1191. cnt = findObjectsNeedingDelta(list, cnt, OBJ_TREE);
  1192. cnt = findObjectsNeedingDelta(list, cnt, OBJ_BLOB);
  1193. if (cnt == 0)
  1194. return;
  1195. int nonEdgeCnt = cnt;
  1196. // Queue up any edge objects that we might delta against. We won't
  1197. // be sending these as we assume the other side has them, but we need
  1198. // them in the search phase below.
  1199. //
  1200. for (ObjectToPack eo : edgeObjects) {
  1201. eo.setWeight(0);
  1202. list[cnt++] = eo;
  1203. }
  1204. // Compute the sizes of the objects so we can do a proper sort.
  1205. // We let the reader skip missing objects if it chooses. For
  1206. // some readers this can be a huge win. We detect missing objects
  1207. // by having set the weights above to 0 and allowing the delta
  1208. // search code to discover the missing object and skip over it, or
  1209. // abort with an exception if we actually had to have it.
  1210. //
  1211. final long sizingStart = System.currentTimeMillis();
  1212. beginPhase(PackingPhase.GETTING_SIZES, monitor, cnt);
  1213. AsyncObjectSizeQueue<ObjectToPack> sizeQueue = reader.getObjectSize(
  1214. Arrays.<ObjectToPack> asList(list).subList(0, cnt), false);
  1215. try {
  1216. final long limit = Math.min(
  1217. config.getBigFileThreshold(),
  1218. Integer.MAX_VALUE);
  1219. for (;;) {
  1220. try {
  1221. if (!sizeQueue.next())
  1222. break;
  1223. } catch (MissingObjectException notFound) {
  1224. monitor.update(1);
  1225. if (ignoreMissingUninteresting) {
  1226. ObjectToPack otp = sizeQueue.getCurrent();
  1227. if (otp != null && otp.isEdge()) {
  1228. otp.setDoNotDelta();
  1229. continue;
  1230. }
  1231. otp = objectsMap.get(notFound.getObjectId());
  1232. if (otp != null && otp.isEdge()) {
  1233. otp.setDoNotDelta();
  1234. continue;
  1235. }
  1236. }
  1237. throw notFound;
  1238. }
  1239. ObjectToPack otp = sizeQueue.getCurrent();
  1240. if (otp == null)
  1241. otp = objectsMap.get(sizeQueue.getObjectId());
  1242. long sz = sizeQueue.getSize();
  1243. if (DeltaIndex.BLKSZ < sz && sz < limit)
  1244. otp.setWeight((int) sz);
  1245. else
  1246. otp.setDoNotDelta(); // too small, or too big
  1247. monitor.update(1);
  1248. }
  1249. } finally {
  1250. sizeQueue.release();
  1251. }
  1252. endPhase(monitor);
  1253. stats.timeSearchingForSizes = System.currentTimeMillis() - sizingStart;
  1254. // Sort the objects by path hash so like files are near each other,
  1255. // and then by size descending so that bigger files are first. This
  1256. // applies "Linus' Law" which states that newer files tend to be the
  1257. // bigger ones, because source files grow and hardly ever shrink.
  1258. //
  1259. Arrays.sort(list, 0, cnt, new Comparator<ObjectToPack>() {
  1260. @Override
  1261. public int compare(ObjectToPack a, ObjectToPack b) {
  1262. int cmp = (a.isDoNotDelta() ? 1 : 0)
  1263. - (b.isDoNotDelta() ? 1 : 0);
  1264. if (cmp != 0)
  1265. return cmp;
  1266. cmp = a.getType() - b.getType();
  1267. if (cmp != 0)
  1268. return cmp;
  1269. cmp = (a.getPathHash() >>> 1) - (b.getPathHash() >>> 1);
  1270. if (cmp != 0)
  1271. return cmp;
  1272. cmp = (a.getPathHash() & 1) - (b.getPathHash() & 1);
  1273. if (cmp != 0)
  1274. return cmp;
  1275. cmp = (a.isEdge() ? 0 : 1) - (b.isEdge() ? 0 : 1);
  1276. if (cmp != 0)
  1277. return cmp;
  1278. return b.getWeight() - a.getWeight();
  1279. }
  1280. });
  1281. // Above we stored the objects we cannot delta onto the end.
  1282. // Remove them from the list so we don't waste time on them.
  1283. while (0 < cnt && list[cnt - 1].isDoNotDelta()) {
  1284. if (!list[cnt - 1].isEdge())
  1285. nonEdgeCnt--;
  1286. cnt--;
  1287. }
  1288. if (cnt == 0)
  1289. return;
  1290. final long searchStart = System.currentTimeMillis();
  1291. searchForDeltas(monitor, list, cnt);
  1292. stats.deltaSearchNonEdgeObjects = nonEdgeCnt;
  1293. stats.timeCompressing = System.currentTimeMillis() - searchStart;
  1294. for (int i = 0; i < cnt; i++)
  1295. if (!list[i].isEdge() && list[i].isDeltaRepresentation())
  1296. stats.deltasFound++;
  1297. }
  1298. private int findObjectsNeedingDelta(ObjectToPack[] list, int cnt, int type) {
  1299. for (ObjectToPack otp : objectsLists[type]) {
  1300. if (otp.isDoNotDelta()) // delta is disabled for this path
  1301. continue;
  1302. if (otp.isDeltaRepresentation()) // already reusing a delta
  1303. continue;
  1304. otp.setWeight(0);
  1305. list[cnt++] = otp;
  1306. }
  1307. return cnt;
  1308. }
  1309. private void reselectNonDelta(ObjectToPack otp) throws IOException {
  1310. otp.clearDeltaBase();
  1311. otp.clearReuseAsIs();
  1312. boolean old = reuseDeltas;
  1313. reuseDeltas = false;
  1314. reuseSupport.selectObjectRepresentation(this,
  1315. NullProgressMonitor.INSTANCE,
  1316. Collections.singleton(otp));
  1317. reuseDeltas = old;
  1318. }
  1319. private void searchForDeltas(final ProgressMonitor monitor,
  1320. final ObjectToPack[] list, final int cnt)
  1321. throws MissingObjectException, IncorrectObjectTypeException,
  1322. LargeObjectException, IOException {
  1323. int threads = config.getThreads();
  1324. if (threads == 0)
  1325. threads = Runtime.getRuntime().availableProcessors();
  1326. if (threads <= 1 || cnt <= config.getDeltaSearchWindowSize())
  1327. singleThreadDeltaSearch(monitor, list, cnt);
  1328. else
  1329. parallelDeltaSearch(monitor, list, cnt, threads);
  1330. }
  1331. private void singleThreadDeltaSearch(ProgressMonitor monitor,
  1332. ObjectToPack[] list, int cnt) throws IOException {
  1333. long totalWeight = 0;
  1334. for (int i = 0; i < cnt; i++) {
  1335. ObjectToPack o = list[i];
  1336. totalWeight += DeltaTask.getAdjustedWeight(o);
  1337. }
  1338. long bytesPerUnit = 1;
  1339. while (DeltaTask.MAX_METER <= (totalWeight / bytesPerUnit))
  1340. bytesPerUnit <<= 10;
  1341. int cost = (int) (totalWeight / bytesPerUnit);
  1342. if (totalWeight % bytesPerUnit != 0)
  1343. cost++;
  1344. beginPhase(PackingPhase.COMPRESSING, monitor, cost);
  1345. new DeltaWindow(config, new DeltaCache(config), reader,
  1346. monitor, bytesPerUnit,
  1347. list, 0, cnt).search();
  1348. endPhase(monitor);
  1349. }
  1350. private void parallelDeltaSearch(ProgressMonitor monitor,
  1351. ObjectToPack[] list, int cnt, int threads) throws IOException {
  1352. DeltaCache dc = new ThreadSafeDeltaCache(config);
  1353. ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(monitor);
  1354. DeltaTask.Block taskBlock = new DeltaTask.Block(threads, config,
  1355. reader, dc, pm,
  1356. list, 0, cnt);
  1357. taskBlock.partitionTasks();
  1358. beginPhase(PackingPhase.COMPRESSING, monitor, taskBlock.cost());
  1359. pm.startWorkers(taskBlock.tasks.size());
  1360. Executor executor = config.getExecutor();
  1361. final List<Throwable> errors =
  1362. Collections.synchronizedList(new ArrayList<>(threads));
  1363. if (executor instanceof ExecutorService) {
  1364. // Caller supplied us a service, use it directly.
  1365. runTasks((ExecutorService) executor, pm, taskBlock, errors);
  1366. } else if (executor == null) {
  1367. // Caller didn't give us a way to run the tasks, spawn up a
  1368. // temporary thread pool and make sure it tears down cleanly.
  1369. ExecutorService pool = Executors.newFixedThreadPool(threads);
  1370. try {
  1371. runTasks(pool, pm, taskBlock, errors);
  1372. } finally {
  1373. pool.shutdown();
  1374. for (;;) {
  1375. try {
  1376. if (pool.awaitTermination(60, TimeUnit.SECONDS))
  1377. break;
  1378. } catch (InterruptedException e) {
  1379. throw new IOException(
  1380. JGitText.get().packingCancelledDuringObjectsWriting);
  1381. }
  1382. }
  1383. }
  1384. } else {
  1385. // The caller gave us an executor, but it might not do
  1386. // asynchronous execution. Wrap everything and hope it
  1387. // can schedule these for us.
  1388. for (DeltaTask task : taskBlock.tasks) {
  1389. executor.execute(new Runnable() {
  1390. @Override
  1391. public void run() {
  1392. try {
  1393. task.call();
  1394. } catch (Throwable failure) {
  1395. errors.add(failure);
  1396. }
  1397. }
  1398. });
  1399. }
  1400. try {
  1401. pm.waitForCompletion();
  1402. } catch (InterruptedException ie) {
  1403. // We can't abort the other tasks as we have no handle.
  1404. // Cross our fingers and just break out anyway.
  1405. //
  1406. throw new IOException(
  1407. JGitText.get().packingCancelledDuringObjectsWriting);
  1408. }
  1409. }
  1410. // If any task threw an error, try to report it back as
  1411. // though we weren't using a threaded search algorithm.
  1412. //
  1413. if (!errors.isEmpty()) {
  1414. Throwable err = errors.get(0);
  1415. if (err instanceof Error)
  1416. throw (Error) err;
  1417. if (err instanceof RuntimeException)
  1418. throw (RuntimeException) err;
  1419. if (err instanceof IOException)
  1420. throw (IOException) err;
  1421. throw new IOException(err.getMessage(), err);
  1422. }
  1423. endPhase(monitor);
  1424. }
  1425. private static void runTasks(ExecutorService pool,
  1426. ThreadSafeProgressMonitor pm,
  1427. DeltaTask.Block tb, List<Throwable> errors) throws IOException {
  1428. List<Future<?>> futures = new ArrayList<>(tb.tasks.size());
  1429. for (DeltaTask task : tb.tasks)
  1430. futures.add(pool.submit(task));
  1431. try {
  1432. pm.waitForCompletion();
  1433. for (Future<?> f : futures) {
  1434. try {
  1435. f.get();
  1436. } catch (ExecutionException failed) {
  1437. errors.add(failed.getCause());
  1438. }
  1439. }
  1440. } catch (InterruptedException ie) {
  1441. for (Future<?> f : futures)
  1442. f.cancel(true);
  1443. throw new IOException(
  1444. JGitText.get().packingCancelledDuringObjectsWriting);
  1445. }
  1446. }
  1447. private void writeObjects(PackOutputStream out) throws IOException {
  1448. writeObjects(out, objectsLists[OBJ_COMMIT]);
  1449. writeObjects(out, objectsLists[OBJ_TAG]);
  1450. writeObjects(out, objectsLists[OBJ_TREE]);
  1451. writeObjects(out, objectsLists[OBJ_BLOB]);
  1452. }
  1453. private void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  1454. throws IOException {
  1455. if (list.isEmpty())
  1456. return;
  1457. typeStats = stats.objectTypes[list.get(0).getType()];
  1458. long beginOffset = out.length();
  1459. if (reuseSupport != null) {
  1460. reuseSupport.writeObjects(out, list);
  1461. } else {
  1462. for (ObjectToPack otp : list)
  1463. out.writeObject(otp);
  1464. }
  1465. typeStats.bytes += out.length() - beginOffset;
  1466. typeStats.cntObjects = list.size();
  1467. }
  1468. void writeObject(PackOutputStream out, ObjectToPack otp) throws IOException {
  1469. if (!otp.isWritten())
  1470. writeObjectImpl(out, otp);
  1471. }
  1472. private void writeObjectImpl(PackOutputStream out, ObjectToPack otp)
  1473. throws IOException {
  1474. if (otp.wantWrite()) {
  1475. // A cycle exists in this delta chain. This should only occur if a
  1476. // selected object representation disappeared during writing
  1477. // (for example due to a concurrent repack) and a different base
  1478. // was chosen, forcing a cycle. Select something other than a
  1479. // delta, and write this object.
  1480. reselectNonDelta(otp);
  1481. }
  1482. otp.markWantWrite();
  1483. while (otp.isReuseAsIs()) {
  1484. writeBase(out, otp.getDeltaBase());
  1485. if (otp.isWritten())
  1486. return; // Delta chain cycle caused this to write already.
  1487. crc32.reset();
  1488. otp.setOffset(out.length());
  1489. try {
  1490. reuseSupport.copyObjectAsIs(out, otp, reuseValidate);
  1491. out.endObject();
  1492. otp.setCRC((int) crc32.getValue());
  1493. typeStats.reusedObjects++;
  1494. if (otp.isDeltaRepresentation()) {
  1495. typeStats.reusedDeltas++;
  1496. typeStats.deltaBytes += out.length() - otp.getOffset();
  1497. }
  1498. return;
  1499. } catch (StoredObjectRepresentationNotAvailableException gone) {
  1500. if (otp.getOffset() == out.length()) {
  1501. otp.setOffset(0);
  1502. otp.clearDeltaBase();
  1503. otp.clearReuseAsIs();
  1504. reuseSupport.selectObjectRepresentation(this,
  1505. NullProgressMonitor.INSTANCE,
  1506. Collections.singleton(otp));
  1507. continue;
  1508. } else {
  1509. // Object writing already started, we cannot recover.
  1510. //
  1511. CorruptObjectException coe;
  1512. coe = new CorruptObjectException(otp, ""); //$NON-NLS-1$
  1513. coe.initCause(gone);
  1514. throw coe;
  1515. }
  1516. }
  1517. }
  1518. // If we reached here, reuse wasn't possible.
  1519. //
  1520. if (otp.isDeltaRepresentation())
  1521. writeDeltaObjectDeflate(out, otp);
  1522. else
  1523. writeWholeObjectDeflate(out, otp);
  1524. out.endObject();
  1525. otp.setCRC((int) crc32.getValue());
  1526. }
  1527. private void writeBase(PackOutputStream out, ObjectToPack base)
  1528. throws IOException {
  1529. if (base != null && !base.isWritten() && !base.isEdge())
  1530. writeObjectImpl(out, base);
  1531. }
  1532. private void writeWholeObjectDeflate(PackOutputStream out,
  1533. final ObjectToPack otp) throws IOException {
  1534. final Deflater deflater = deflater();
  1535. final ObjectLoader ldr = reader.open(otp, otp.getType());
  1536. crc32.reset();
  1537. otp.setOffset(out.length());
  1538. out.writeHeader(otp, ldr.getSize());
  1539. deflater.reset();
  1540. DeflaterOutputStream dst = new DeflaterOutputStream(out, deflater);
  1541. ldr.copyTo(dst);
  1542. dst.finish();
  1543. }
  1544. private void writeDeltaObjectDeflate(PackOutputStream out,
  1545. final ObjectToPack otp) throws IOException {
  1546. writeBase(out, otp.getDeltaBase());
  1547. crc32.reset();
  1548. otp.setOffset(out.length());
  1549. DeltaCache.Ref ref = otp.popCachedDelta();
  1550. if (ref != null) {
  1551. byte[] zbuf = ref.get();
  1552. if (zbuf != null) {
  1553. out.writeHeader(otp, otp.getCachedSize());
  1554. out.write(zbuf);
  1555. typeStats.cntDeltas++;
  1556. typeStats.deltaBytes += out.length() - otp.getOffset();
  1557. return;
  1558. }
  1559. }
  1560. try (TemporaryBuffer.Heap delta = delta(otp)) {
  1561. out.writeHeader(otp, delta.length());
  1562. Deflater deflater = deflater();
  1563. deflater.reset();
  1564. DeflaterOutputStream dst = new DeflaterOutputStream(out, deflater);
  1565. delta.writeTo(dst, null);
  1566. dst.finish();
  1567. }
  1568. typeStats.cntDeltas++;
  1569. typeStats.deltaBytes += out.length() - otp.getOffset();
  1570. }
  1571. private TemporaryBuffer.Heap delta(ObjectToPack otp)
  1572. throws IOException {
  1573. DeltaIndex index = new DeltaIndex(buffer(otp.getDeltaBaseId()));
  1574. byte[] res = buffer(otp);
  1575. // We never would have proposed this pair if the delta would be
  1576. // larger than the unpacked version of the object. So using it
  1577. // as our buffer limit is valid: we will never reach it.
  1578. //
  1579. TemporaryBuffer.Heap delta = new TemporaryBuffer.Heap(res.length);
  1580. index.encode(delta, res);
  1581. return delta;
  1582. }
  1583. private byte[] buffer(AnyObjectId objId) throws IOException {
  1584. return buffer(config, reader, objId);
  1585. }
  1586. static byte[] buffer(PackConfig config, ObjectReader or, AnyObjectId objId)
  1587. throws IOException {
  1588. // PackWriter should have already pruned objects that
  1589. // are above the big file threshold, so our chances of
  1590. // the object being below it are very good. We really
  1591. // shouldn't be here, unless the implementation is odd.
  1592. return or.open(objId).getCachedBytes(config.getBigFileThreshold());
  1593. }
  1594. private Deflater deflater() {
  1595. if (myDeflater == null)
  1596. myDeflater = new Deflater(config.getCompressionLevel());
  1597. return myDeflater;
  1598. }
  1599. private void writeChecksum(PackOutputStream out) throws IOException {
  1600. packcsum = out.getDigest();
  1601. out.write(packcsum);
  1602. }
  1603. private void findObjectsToPack(@NonNull ProgressMonitor countingMonitor,
  1604. @NonNull ObjectWalk walker, @NonNull Set<? extends ObjectId> want,
  1605. @NonNull Set<? extends ObjectId> have,
  1606. @NonNull Set<? extends ObjectId> noBitmaps) throws IOException {
  1607. final long countingStart = System.currentTimeMillis();
  1608. beginPhase(PackingPhase.COUNTING, countingMonitor, ProgressMonitor.UNKNOWN);
  1609. stats.interestingObjects = Collections.unmodifiableSet(new HashSet<ObjectId>(want));
  1610. stats.uninterestingObjects = Collections.unmodifiableSet(new HashSet<ObjectId>(have));
  1611. excludeFromBitmapSelection = noBitmaps;
  1612. canBuildBitmaps = config.isBuildBitmaps()
  1613. && !shallowPack
  1614. && have.isEmpty()
  1615. && (excludeInPacks == null || excludeInPacks.length == 0);
  1616. if (!shallowPack && useBitmaps) {
  1617. BitmapIndex bitmapIndex = reader.getBitmapIndex();
  1618. if (bitmapIndex != null) {
  1619. BitmapWalker bitmapWalker = new BitmapWalker(
  1620. walker, bitmapIndex, countingMonitor);
  1621. findObjectsToPackUsingBitmaps(bitmapWalker, want, have);
  1622. endPhase(countingMonitor);
  1623. stats.timeCounting = System.currentTimeMillis() - countingStart;
  1624. stats.bitmapIndexMisses = bitmapWalker.getCountOfBitmapIndexMisses();
  1625. return;
  1626. }
  1627. }
  1628. List<ObjectId> all = new ArrayList<>(want.size() + have.size());
  1629. all.addAll(want);
  1630. all.addAll(have);
  1631. final RevFlag include = walker.newFlag("include"); //$NON-NLS-1$
  1632. final RevFlag added = walker.newFlag("added"); //$NON-NLS-1$
  1633. walker.carry(include);
  1634. int haveEst = have.size();
  1635. if (have.isEmpty()) {
  1636. walker.sort(RevSort.COMMIT_TIME_DESC);
  1637. } else {
  1638. walker.sort(RevSort.TOPO);
  1639. if (thin)
  1640. walker.sort(RevSort.BOUNDARY, true);
  1641. }
  1642. List<RevObject> wantObjs = new ArrayList<>(want.size());
  1643. List<RevObject> haveObjs = new ArrayList<>(haveEst);
  1644. List<RevTag> wantTags = new ArrayList<>(want.size());
  1645. // Retrieve the RevWalk's versions of "want" and "have" objects to
  1646. // maintain any state previously set in the RevWalk.
  1647. AsyncRevObjectQueue q = walker.parseAny(all, true);
  1648. try {
  1649. for (;;) {
  1650. try {
  1651. RevObject o = q.next();
  1652. if (o == null)
  1653. break;
  1654. if (have.contains(o))
  1655. haveObjs.add(o);
  1656. if (want.contains(o)) {
  1657. o.add(include);
  1658. wantObjs.add(o);
  1659. if (o instanceof RevTag)
  1660. wantTags.add((RevTag) o);
  1661. }
  1662. } catch (MissingObjectException e) {
  1663. if (ignoreMissingUninteresting
  1664. && have.contains(e.getObjectId()))
  1665. continue;
  1666. throw e;
  1667. }
  1668. }
  1669. } finally {
  1670. q.release();
  1671. }
  1672. if (!wantTags.isEmpty()) {
  1673. all = new ArrayList<>(wantTags.size());
  1674. for (RevTag tag : wantTags)
  1675. all.add(tag.getObject());
  1676. q = walker.parseAny(all, true);
  1677. try {
  1678. while (q.next() != null) {
  1679. // Just need to pop the queue item to parse the object.
  1680. }
  1681. } finally {
  1682. q.release();
  1683. }
  1684. }
  1685. if (walker instanceof DepthWalk.ObjectWalk) {
  1686. DepthWalk.ObjectWalk depthWalk = (DepthWalk.ObjectWalk) walker;
  1687. for (RevObject obj : wantObjs) {
  1688. depthWalk.markRoot(obj);
  1689. }
  1690. // Mark the tree objects associated with "have" commits as
  1691. // uninteresting to avoid writing redundant blobs. A normal RevWalk
  1692. // lazily propagates the "uninteresting" state from a commit to its
  1693. // tree during the walk, but DepthWalks can terminate early so
  1694. // preemptively propagate that state here.
  1695. for (RevObject obj : haveObjs) {
  1696. if (obj instanceof RevCommit) {
  1697. RevTree t = ((RevCommit) obj).getTree();
  1698. depthWalk.markUninteresting(t);
  1699. }
  1700. }
  1701. if (unshallowObjects != null) {
  1702. for (ObjectId id : unshallowObjects) {
  1703. depthWalk.markUnshallow(walker.parseAny(id));
  1704. }
  1705. }
  1706. } else {
  1707. for (RevObject obj : wantObjs)
  1708. walker.markStart(obj);
  1709. }
  1710. for (RevObject obj : haveObjs)
  1711. walker.markUninteresting(obj);
  1712. final int maxBases = config.getDeltaSearchWindowSize();
  1713. Set<RevTree> baseTrees = new HashSet<>();
  1714. BlockList<RevCommit> commits = new BlockList<>();
  1715. Set<ObjectId> roots = new HashSet<>();
  1716. RevCommit c;
  1717. while ((c = walker.next()) != null) {
  1718. if (exclude(c))
  1719. continue;
  1720. if (c.has(RevFlag.UNINTERESTING)) {
  1721. if (baseTrees.size() <= maxBases)
  1722. baseTrees.add(c.getTree());
  1723. continue;
  1724. }
  1725. commits.add(c);
  1726. if (c.getParentCount() == 0) {
  1727. roots.add(c.copy());
  1728. }
  1729. countingMonitor.update(1);
  1730. }
  1731. stats.rootCommits = Collections.unmodifiableSet(roots);
  1732. if (shallowPack) {
  1733. for (RevCommit cmit : commits) {
  1734. addObject(cmit, 0);
  1735. }
  1736. } else {
  1737. int commitCnt = 0;
  1738. boolean putTagTargets = false;
  1739. for (RevCommit cmit : commits) {
  1740. if (!cmit.has(added)) {
  1741. cmit.add(added);
  1742. addObject(cmit, 0);
  1743. commitCnt++;
  1744. }
  1745. for (int i = 0; i < cmit.getParentCount(); i++) {
  1746. RevCommit p = cmit.getParent(i);
  1747. if (!p.has(added) && !p.has(RevFlag.UNINTERESTING)
  1748. && !exclude(p)) {
  1749. p.add(added);
  1750. addObject(p, 0);
  1751. commitCnt++;
  1752. }
  1753. }
  1754. if (!putTagTargets && 4096 < commitCnt) {
  1755. for (ObjectId id : tagTargets) {
  1756. RevObject obj = walker.lookupOrNull(id);
  1757. if (obj instanceof RevCommit
  1758. && obj.has(include)
  1759. && !obj.has(RevFlag.UNINTERESTING)
  1760. && !obj.has(added)) {
  1761. obj.add(added);
  1762. addObject(obj, 0);
  1763. }
  1764. }
  1765. putTagTargets = true;
  1766. }
  1767. }
  1768. }
  1769. commits = null;
  1770. if (thin && !baseTrees.isEmpty()) {
  1771. BaseSearch bases = new BaseSearch(countingMonitor, baseTrees, //
  1772. objectsMap, edgeObjects, reader);
  1773. RevObject o;
  1774. while ((o = walker.nextObject()) != null) {
  1775. if (o.has(RevFlag.UNINTERESTING))
  1776. continue;
  1777. if (exclude(o))
  1778. continue;
  1779. int pathHash = walker.getPathHashCode();
  1780. byte[] pathBuf = walker.getPathBuffer();
  1781. int pathLen = walker.getPathLength();
  1782. bases.addBase(o.getType(), pathBuf, pathLen, pathHash);
  1783. filterAndAddObject(o, o.getType(), pathHash, want);
  1784. countingMonitor.update(1);
  1785. }
  1786. } else {
  1787. RevObject o;
  1788. while ((o = walker.nextObject()) != null) {
  1789. if (o.has(RevFlag.UNINTERESTING))
  1790. continue;
  1791. if (exclude(o))
  1792. continue;
  1793. filterAndAddObject(o, o.getType(), walker.getPathHashCode(), want);
  1794. countingMonitor.update(1);
  1795. }
  1796. }
  1797. for (CachedPack pack : cachedPacks)
  1798. countingMonitor.update((int) pack.getObjectCount());
  1799. endPhase(countingMonitor);
  1800. stats.timeCounting = System.currentTimeMillis() - countingStart;
  1801. stats.bitmapIndexMisses = -1;
  1802. }
  1803. private void findObjectsToPackUsingBitmaps(
  1804. BitmapWalker bitmapWalker, Set<? extends ObjectId> want,
  1805. Set<? extends ObjectId> have)
  1806. throws MissingObjectException, IncorrectObjectTypeException,
  1807. IOException {
  1808. BitmapBuilder haveBitmap = bitmapWalker.findObjects(have, null, true);
  1809. BitmapBuilder wantBitmap = bitmapWalker.findObjects(want, haveBitmap,
  1810. false);
  1811. BitmapBuilder needBitmap = wantBitmap.andNot(haveBitmap);
  1812. if (useCachedPacks && reuseSupport != null && !reuseValidate
  1813. && (excludeInPacks == null || excludeInPacks.length == 0))
  1814. cachedPacks.addAll(
  1815. reuseSupport.getCachedPacksAndUpdate(needBitmap));
  1816. for (BitmapObject obj : needBitmap) {
  1817. ObjectId objectId = obj.getObjectId();
  1818. if (exclude(objectId)) {
  1819. needBitmap.remove(objectId);
  1820. continue;
  1821. }
  1822. filterAndAddObject(objectId, obj.getType(), 0, want);
  1823. }
  1824. if (thin)
  1825. haveObjects = haveBitmap;
  1826. }
  1827. private static void pruneEdgesFromObjectList(List<ObjectToPack> list) {
  1828. final int size = list.size();
  1829. int src = 0;
  1830. int dst = 0;
  1831. for (; src < size; src++) {
  1832. ObjectToPack obj = list.get(src);
  1833. if (obj.isEdge())
  1834. continue;
  1835. if (dst != src)
  1836. list.set(dst, obj);
  1837. dst++;
  1838. }
  1839. while (dst < list.size())
  1840. list.remove(list.size() - 1);
  1841. }
  1842. /**
  1843. * Include one object to the output file.
  1844. * <p>
  1845. * Objects are written in the order they are added. If the same object is
  1846. * added twice, it may be written twice, creating a larger than necessary
  1847. * file.
  1848. *
  1849. * @param object
  1850. * the object to add.
  1851. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  1852. * the object is an unsupported type.
  1853. */
  1854. public void addObject(RevObject object)
  1855. throws IncorrectObjectTypeException {
  1856. if (!exclude(object))
  1857. addObject(object, 0);
  1858. }
  1859. private void addObject(RevObject object, int pathHashCode) {
  1860. addObject(object, object.getType(), pathHashCode);
  1861. }
  1862. private void addObject(
  1863. final AnyObjectId src, final int type, final int pathHashCode) {
  1864. final ObjectToPack otp;
  1865. if (reuseSupport != null)
  1866. otp = reuseSupport.newObjectToPack(src, type);
  1867. else
  1868. otp = new ObjectToPack(src, type);
  1869. otp.setPathHash(pathHashCode);
  1870. objectsLists[type].add(otp);
  1871. objectsMap.add(otp);
  1872. }
  1873. // Adds the given object as an object to be packed, first performing
  1874. // filtering on blobs at or exceeding a given size.
  1875. private void filterAndAddObject(@NonNull AnyObjectId src, int type,
  1876. int pathHashCode, @NonNull Set<? extends AnyObjectId> want)
  1877. throws IOException {
  1878. // Check if this object needs to be rejected, doing the cheaper
  1879. // checks first.
  1880. boolean reject = filterSpec.getBlobLimit() >= 0 &&
  1881. type == OBJ_BLOB &&
  1882. !want.contains(src) &&
  1883. reader.getObjectSize(src, OBJ_BLOB) > filterSpec.getBlobLimit();
  1884. if (!reject) {
  1885. addObject(src, type, pathHashCode);
  1886. }
  1887. }
  1888. private boolean exclude(AnyObjectId objectId) {
  1889. if (excludeInPacks == null)
  1890. return false;
  1891. if (excludeInPackLast.contains(objectId))
  1892. return true;
  1893. for (ObjectIdSet idx : excludeInPacks) {
  1894. if (idx.contains(objectId)) {
  1895. excludeInPackLast = idx;
  1896. return true;
  1897. }
  1898. }
  1899. return false;
  1900. }
  1901. /**
  1902. * Select an object representation for this writer.
  1903. * <p>
  1904. * An {@link org.eclipse.jgit.lib.ObjectReader} implementation should invoke
  1905. * this method once for each representation available for an object, to
  1906. * allow the writer to find the most suitable one for the output.
  1907. *
  1908. * @param otp
  1909. * the object being packed.
  1910. * @param next
  1911. * the next available representation from the repository.
  1912. */
  1913. public void select(ObjectToPack otp, StoredObjectRepresentation next) {
  1914. int nFmt = next.getFormat();
  1915. if (!cachedPacks.isEmpty()) {
  1916. if (otp.isEdge())
  1917. return;
  1918. if ((nFmt == PACK_WHOLE) | (nFmt == PACK_DELTA)) {
  1919. for (CachedPack pack : cachedPacks) {
  1920. if (pack.hasObject(otp, next)) {
  1921. otp.setEdge();
  1922. otp.clearDeltaBase();
  1923. otp.clearReuseAsIs();
  1924. pruneCurrentObjectList = true;
  1925. return;
  1926. }
  1927. }
  1928. }
  1929. }
  1930. if (nFmt == PACK_DELTA && reuseDeltas && reuseDeltaFor(otp)) {
  1931. ObjectId baseId = next.getDeltaBase();
  1932. ObjectToPack ptr = objectsMap.get(baseId);
  1933. if (ptr != null && !ptr.isEdge()) {
  1934. otp.setDeltaBase(ptr);
  1935. otp.setReuseAsIs();
  1936. } else if (thin && have(ptr, baseId)) {
  1937. otp.setDeltaBase(baseId);
  1938. otp.setReuseAsIs();
  1939. } else {
  1940. otp.clearDeltaBase();
  1941. otp.clearReuseAsIs();
  1942. }
  1943. } else if (nFmt == PACK_WHOLE && config.isReuseObjects()) {
  1944. int nWeight = next.getWeight();
  1945. if (otp.isReuseAsIs() && !otp.isDeltaRepresentation()) {
  1946. // We've chosen another PACK_WHOLE format for this object,
  1947. // choose the one that has the smaller compressed size.
  1948. //
  1949. if (otp.getWeight() <= nWeight)
  1950. return;
  1951. }
  1952. otp.clearDeltaBase();
  1953. otp.setReuseAsIs();
  1954. otp.setWeight(nWeight);
  1955. } else {
  1956. otp.clearDeltaBase();
  1957. otp.clearReuseAsIs();
  1958. }
  1959. otp.setDeltaAttempted(reuseDeltas & next.wasDeltaAttempted());
  1960. otp.select(next);
  1961. }
  1962. private final boolean have(ObjectToPack ptr, AnyObjectId objectId) {
  1963. return (ptr != null && ptr.isEdge())
  1964. || (haveObjects != null && haveObjects.contains(objectId));
  1965. }
  1966. /**
  1967. * Prepares the bitmaps to be written to the bitmap index file.
  1968. * <p>
  1969. * Bitmaps can be used to speed up fetches and clones by storing the entire
  1970. * object graph at selected commits. Writing a bitmap index is an optional
  1971. * feature that not all pack users may require.
  1972. * <p>
  1973. * Called after {@link #writeIndex(OutputStream)}.
  1974. * <p>
  1975. * To reduce memory internal state is cleared during this method, rendering
  1976. * the PackWriter instance useless for anything further than a call to write
  1977. * out the new bitmaps with {@link #writeBitmapIndex(OutputStream)}.
  1978. *
  1979. * @param pm
  1980. * progress monitor to report bitmap building work.
  1981. * @return whether a bitmap index may be written.
  1982. * @throws java.io.IOException
  1983. * when some I/O problem occur during reading objects.
  1984. */
  1985. public boolean prepareBitmapIndex(ProgressMonitor pm) throws IOException {
  1986. if (!canBuildBitmaps || getObjectCount() > Integer.MAX_VALUE
  1987. || !cachedPacks.isEmpty())
  1988. return false;
  1989. if (pm == null)
  1990. pm = NullProgressMonitor.INSTANCE;
  1991. int numCommits = objectsLists[OBJ_COMMIT].size();
  1992. List<ObjectToPack> byName = sortByName();
  1993. sortedByName = null;
  1994. objectsLists = null;
  1995. objectsMap = null;
  1996. writeBitmaps = new PackBitmapIndexBuilder(byName);
  1997. byName = null;
  1998. PackWriterBitmapPreparer bitmapPreparer = new PackWriterBitmapPreparer(
  1999. reader, writeBitmaps, pm, stats.interestingObjects, config);
  2000. Collection<PackWriterBitmapPreparer.BitmapCommit> selectedCommits = bitmapPreparer
  2001. .selectCommits(numCommits, excludeFromBitmapSelection);
  2002. beginPhase(PackingPhase.BUILDING_BITMAPS, pm, selectedCommits.size());
  2003. BitmapWalker walker = bitmapPreparer.newBitmapWalker();
  2004. AnyObjectId last = null;
  2005. for (PackWriterBitmapPreparer.BitmapCommit cmit : selectedCommits) {
  2006. if (!cmit.isReuseWalker()) {
  2007. walker = bitmapPreparer.newBitmapWalker();
  2008. }
  2009. BitmapBuilder bitmap = walker.findObjects(
  2010. Collections.singleton(cmit), null, false);
  2011. if (last != null && cmit.isReuseWalker() && !bitmap.contains(last))
  2012. throw new IllegalStateException(MessageFormat.format(
  2013. JGitText.get().bitmapMissingObject, cmit.name(),
  2014. last.name()));
  2015. last = cmit;
  2016. writeBitmaps.addBitmap(cmit, bitmap.build(), cmit.getFlags());
  2017. pm.update(1);
  2018. }
  2019. endPhase(pm);
  2020. return true;
  2021. }
  2022. private boolean reuseDeltaFor(ObjectToPack otp) {
  2023. int type = otp.getType();
  2024. if ((type & 2) != 0) // OBJ_TREE(2) or OBJ_BLOB(3)
  2025. return true;
  2026. if (type == OBJ_COMMIT)
  2027. return reuseDeltaCommits;
  2028. if (type == OBJ_TAG)
  2029. return false;
  2030. return true;
  2031. }
  2032. private class MutableState {
  2033. /** Estimated size of a single ObjectToPack instance. */
  2034. // Assume 64-bit pointers, since this is just an estimate.
  2035. private static final long OBJECT_TO_PACK_SIZE =
  2036. (2 * 8) // Object header
  2037. + (2 * 8) + (2 * 8) // ObjectToPack fields
  2038. + (8 + 8) // PackedObjectInfo fields
  2039. + 8 // ObjectIdOwnerMap fields
  2040. + 40 // AnyObjectId fields
  2041. + 8; // Reference in BlockList
  2042. private final long totalDeltaSearchBytes;
  2043. private volatile PackingPhase phase;
  2044. MutableState() {
  2045. phase = PackingPhase.COUNTING;
  2046. if (config.isDeltaCompress()) {
  2047. int threads = config.getThreads();
  2048. if (threads <= 0)
  2049. threads = Runtime.getRuntime().availableProcessors();
  2050. totalDeltaSearchBytes = (threads * config.getDeltaSearchMemoryLimit())
  2051. + config.getBigFileThreshold();
  2052. } else
  2053. totalDeltaSearchBytes = 0;
  2054. }
  2055. State snapshot() {
  2056. long objCnt = 0;
  2057. BlockList<ObjectToPack>[] lists = objectsLists;
  2058. if (lists != null) {
  2059. objCnt += lists[OBJ_COMMIT].size();
  2060. objCnt += lists[OBJ_TREE].size();
  2061. objCnt += lists[OBJ_BLOB].size();
  2062. objCnt += lists[OBJ_TAG].size();
  2063. // Exclude CachedPacks.
  2064. }
  2065. long bytesUsed = OBJECT_TO_PACK_SIZE * objCnt;
  2066. PackingPhase curr = phase;
  2067. if (curr == PackingPhase.COMPRESSING)
  2068. bytesUsed += totalDeltaSearchBytes;
  2069. return new State(curr, bytesUsed);
  2070. }
  2071. }
  2072. /** Possible states that a PackWriter can be in. */
  2073. public static enum PackingPhase {
  2074. /** Counting objects phase. */
  2075. COUNTING,
  2076. /** Getting sizes phase. */
  2077. GETTING_SIZES,
  2078. /** Finding sources phase. */
  2079. FINDING_SOURCES,
  2080. /** Compressing objects phase. */
  2081. COMPRESSING,
  2082. /** Writing objects phase. */
  2083. WRITING,
  2084. /** Building bitmaps phase. */
  2085. BUILDING_BITMAPS;
  2086. }
  2087. /** Summary of the current state of a PackWriter. */
  2088. public class State {
  2089. private final PackingPhase phase;
  2090. private final long bytesUsed;
  2091. State(PackingPhase phase, long bytesUsed) {
  2092. this.phase = phase;
  2093. this.bytesUsed = bytesUsed;
  2094. }
  2095. /** @return the PackConfig used to build the writer. */
  2096. public PackConfig getConfig() {
  2097. return config;
  2098. }
  2099. /** @return the current phase of the writer. */
  2100. public PackingPhase getPhase() {
  2101. return phase;
  2102. }
  2103. /** @return an estimate of the total memory used by the writer. */
  2104. public long estimateBytesUsed() {
  2105. return bytesUsed;
  2106. }
  2107. @SuppressWarnings("nls")
  2108. @Override
  2109. public String toString() {
  2110. return "PackWriter.State[" + phase + ", memory=" + bytesUsed + "]";
  2111. }
  2112. }
  2113. }