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.

GC.java 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2011, Shawn O. Pearce <spearce@spearce.org>
  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.file;
  45. import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
  46. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  47. import java.io.File;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.io.OutputStream;
  51. import java.io.PrintWriter;
  52. import java.io.StringWriter;
  53. import java.nio.channels.Channels;
  54. import java.nio.channels.FileChannel;
  55. import java.nio.file.DirectoryStream;
  56. import java.nio.file.Files;
  57. import java.nio.file.Path;
  58. import java.nio.file.Paths;
  59. import java.nio.file.StandardCopyOption;
  60. import java.text.MessageFormat;
  61. import java.text.ParseException;
  62. import java.time.Instant;
  63. import java.time.temporal.ChronoUnit;
  64. import java.util.ArrayList;
  65. import java.util.Collection;
  66. import java.util.Collections;
  67. import java.util.Comparator;
  68. import java.util.Date;
  69. import java.util.HashMap;
  70. import java.util.HashSet;
  71. import java.util.Iterator;
  72. import java.util.LinkedList;
  73. import java.util.List;
  74. import java.util.Map;
  75. import java.util.Objects;
  76. import java.util.Set;
  77. import java.util.TreeMap;
  78. import java.util.concurrent.Callable;
  79. import java.util.concurrent.ExecutorService;
  80. import java.util.concurrent.Executors;
  81. import java.util.regex.Pattern;
  82. import java.util.stream.Collectors;
  83. import java.util.stream.Stream;
  84. import org.eclipse.jgit.annotations.NonNull;
  85. import org.eclipse.jgit.dircache.DirCacheIterator;
  86. import org.eclipse.jgit.errors.CancelledException;
  87. import org.eclipse.jgit.errors.CorruptObjectException;
  88. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  89. import org.eclipse.jgit.errors.MissingObjectException;
  90. import org.eclipse.jgit.errors.NoWorkTreeException;
  91. import org.eclipse.jgit.internal.JGitText;
  92. import org.eclipse.jgit.internal.storage.pack.PackExt;
  93. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  94. import org.eclipse.jgit.internal.storage.reftree.RefTreeNames;
  95. import org.eclipse.jgit.lib.ConfigConstants;
  96. import org.eclipse.jgit.lib.Constants;
  97. import org.eclipse.jgit.lib.FileMode;
  98. import org.eclipse.jgit.lib.NullProgressMonitor;
  99. import org.eclipse.jgit.lib.ObjectId;
  100. import org.eclipse.jgit.lib.ObjectIdSet;
  101. import org.eclipse.jgit.lib.ObjectLoader;
  102. import org.eclipse.jgit.lib.ObjectReader;
  103. import org.eclipse.jgit.lib.ProgressMonitor;
  104. import org.eclipse.jgit.lib.Ref;
  105. import org.eclipse.jgit.lib.Ref.Storage;
  106. import org.eclipse.jgit.lib.RefDatabase;
  107. import org.eclipse.jgit.lib.ReflogEntry;
  108. import org.eclipse.jgit.lib.ReflogReader;
  109. import org.eclipse.jgit.revwalk.ObjectWalk;
  110. import org.eclipse.jgit.revwalk.RevObject;
  111. import org.eclipse.jgit.revwalk.RevWalk;
  112. import org.eclipse.jgit.storage.pack.PackConfig;
  113. import org.eclipse.jgit.treewalk.TreeWalk;
  114. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  115. import org.eclipse.jgit.util.FileUtils;
  116. import org.eclipse.jgit.util.GitDateParser;
  117. import org.eclipse.jgit.util.SystemReader;
  118. import org.slf4j.Logger;
  119. import org.slf4j.LoggerFactory;
  120. /**
  121. * A garbage collector for git {@link FileRepository}. Instances of this class
  122. * are not thread-safe. Don't use the same instance from multiple threads.
  123. *
  124. * This class started as a copy of DfsGarbageCollector from Shawn O. Pearce
  125. * adapted to FileRepositories.
  126. */
  127. public class GC {
  128. private final static Logger LOG = LoggerFactory
  129. .getLogger(GC.class);
  130. private static final String PRUNE_EXPIRE_DEFAULT = "2.weeks.ago"; //$NON-NLS-1$
  131. private static final String PRUNE_PACK_EXPIRE_DEFAULT = "1.hour.ago"; //$NON-NLS-1$
  132. private static final Pattern PATTERN_LOOSE_OBJECT = Pattern
  133. .compile("[0-9a-fA-F]{38}"); //$NON-NLS-1$
  134. private static final String PACK_EXT = "." + PackExt.PACK.getExtension();//$NON-NLS-1$
  135. private static final String BITMAP_EXT = "." //$NON-NLS-1$
  136. + PackExt.BITMAP_INDEX.getExtension();
  137. private static final String INDEX_EXT = "." + PackExt.INDEX.getExtension(); //$NON-NLS-1$
  138. private static final int DEFAULT_AUTOPACKLIMIT = 50;
  139. private static final int DEFAULT_AUTOLIMIT = 6700;
  140. private static ExecutorService executor = Executors.newFixedThreadPool(1);
  141. private final FileRepository repo;
  142. private ProgressMonitor pm;
  143. private long expireAgeMillis = -1;
  144. private Date expire;
  145. private long packExpireAgeMillis = -1;
  146. private Date packExpire;
  147. private PackConfig pconfig = null;
  148. /**
  149. * the refs which existed during the last call to {@link #repack()}. This is
  150. * needed during {@link #prune(Set)} where we can optimize by looking at the
  151. * difference between the current refs and the refs which existed during
  152. * last {@link #repack()}.
  153. */
  154. private Collection<Ref> lastPackedRefs;
  155. /**
  156. * Holds the starting time of the last repack() execution. This is needed in
  157. * prune() to inspect only those reflog entries which have been added since
  158. * last repack().
  159. */
  160. private long lastRepackTime;
  161. /**
  162. * Whether gc should do automatic housekeeping
  163. */
  164. private boolean automatic;
  165. /**
  166. * Whether to run gc in a background thread
  167. */
  168. private boolean background;
  169. /**
  170. * Creates a new garbage collector with default values. An expirationTime of
  171. * two weeks and <code>null</code> as progress monitor will be used.
  172. *
  173. * @param repo
  174. * the repo to work on
  175. */
  176. public GC(FileRepository repo) {
  177. this.repo = repo;
  178. this.pm = NullProgressMonitor.INSTANCE;
  179. }
  180. /**
  181. * Runs a garbage collector on a {@link FileRepository}. It will
  182. * <ul>
  183. * <li>pack loose references into packed-refs</li>
  184. * <li>repack all reachable objects into new pack files and delete the old
  185. * pack files</li>
  186. * <li>prune all loose objects which are now reachable by packs</li>
  187. * </ul>
  188. *
  189. * If {@link #setAuto(boolean)} was set to {@code true} {@code gc} will
  190. * first check whether any housekeeping is required; if not, it exits
  191. * without performing any work.
  192. *
  193. * If {@link #setBackground(boolean)} was set to {@code true}
  194. * {@code collectGarbage} will start the gc in the background, and then
  195. * return immediately. In this case, errors will not be reported except in
  196. * gc.log.
  197. *
  198. * @return the collection of {@link PackFile}'s which are newly created
  199. * @throws IOException
  200. * @throws ParseException
  201. * If the configuration parameter "gc.pruneexpire" couldn't be
  202. * parsed
  203. */
  204. // TODO(ms): in 5.0 change signature and return Future<Collection<PackFile>>
  205. public Collection<PackFile> gc() throws IOException, ParseException {
  206. if (!background) {
  207. return doGc();
  208. }
  209. final GcLog gcLog = new GcLog(repo);
  210. if (!gcLog.lock()) {
  211. // there is already a background gc running
  212. return Collections.emptyList();
  213. }
  214. Callable<Collection<PackFile>> gcTask = () -> {
  215. try {
  216. Collection<PackFile> newPacks = doGc();
  217. if (automatic && tooManyLooseObjects()) {
  218. String message = JGitText.get().gcTooManyUnpruned;
  219. gcLog.write(message);
  220. gcLog.commit();
  221. }
  222. return newPacks;
  223. } catch (IOException | ParseException e) {
  224. try {
  225. gcLog.write(e.getMessage());
  226. StringWriter sw = new StringWriter();
  227. e.printStackTrace(new PrintWriter(sw));
  228. gcLog.write(sw.toString());
  229. gcLog.commit();
  230. } catch (IOException e2) {
  231. e2.addSuppressed(e);
  232. LOG.error(e2.getMessage(), e2);
  233. }
  234. } finally {
  235. gcLog.unlock();
  236. }
  237. return Collections.emptyList();
  238. };
  239. // TODO(ms): in 5.0 change signature and return the Future
  240. executor.submit(gcTask);
  241. return Collections.emptyList();
  242. }
  243. private Collection<PackFile> doGc() throws IOException, ParseException {
  244. if (automatic && !needGc()) {
  245. return Collections.emptyList();
  246. }
  247. pm.start(6 /* tasks */);
  248. packRefs();
  249. // TODO: implement reflog_expire(pm, repo);
  250. Collection<PackFile> newPacks = repack();
  251. prune(Collections.<ObjectId> emptySet());
  252. // TODO: implement rerere_gc(pm);
  253. return newPacks;
  254. }
  255. /**
  256. * Loosen objects in a pack file which are not also in the newly-created
  257. * pack files.
  258. *
  259. * @param inserter
  260. * @param reader
  261. * @param pack
  262. * @param existing
  263. * @throws IOException
  264. */
  265. private void loosen(ObjectDirectoryInserter inserter, ObjectReader reader, PackFile pack, HashSet<ObjectId> existing)
  266. throws IOException {
  267. for (PackIndex.MutableEntry entry : pack) {
  268. ObjectId oid = entry.toObjectId();
  269. if (existing.contains(oid)) {
  270. continue;
  271. }
  272. existing.add(oid);
  273. ObjectLoader loader = reader.open(oid);
  274. inserter.insert(loader.getType(),
  275. loader.getSize(),
  276. loader.openStream(),
  277. true /* create this object even though it's a duplicate */);
  278. }
  279. }
  280. /**
  281. * Delete old pack files. What is 'old' is defined by specifying a set of
  282. * old pack files and a set of new pack files. Each pack file contained in
  283. * old pack files but not contained in new pack files will be deleted. If
  284. * preserveOldPacks is set, keep a copy of the pack file in the preserve
  285. * directory. If an expirationDate is set then pack files which are younger
  286. * than the expirationDate will not be deleted nor preserved.
  287. * <p>
  288. * If we're not immediately expiring loose objects, loosen any objects
  289. * in the old pack files which aren't in the new pack files.
  290. *
  291. * @param oldPacks
  292. * @param newPacks
  293. * @throws ParseException
  294. * @throws IOException
  295. */
  296. private void deleteOldPacks(Collection<PackFile> oldPacks,
  297. Collection<PackFile> newPacks) throws ParseException, IOException {
  298. HashSet<ObjectId> ids = new HashSet<>();
  299. for (PackFile pack : newPacks) {
  300. for (PackIndex.MutableEntry entry : pack) {
  301. ids.add(entry.toObjectId());
  302. }
  303. }
  304. ObjectReader reader = repo.newObjectReader();
  305. ObjectDirectory dir = repo.getObjectDatabase();
  306. ObjectDirectoryInserter inserter = dir.newInserter();
  307. boolean shouldLoosen = !"now".equals(getPruneExpireStr()) && //$NON-NLS-1$
  308. getExpireDate() < Long.MAX_VALUE;
  309. prunePreserved();
  310. long packExpireDate = getPackExpireDate();
  311. oldPackLoop: for (PackFile oldPack : oldPacks) {
  312. checkCancelled();
  313. String oldName = oldPack.getPackName();
  314. // check whether an old pack file is also among the list of new
  315. // pack files. Then we must not delete it.
  316. for (PackFile newPack : newPacks)
  317. if (oldName.equals(newPack.getPackName()))
  318. continue oldPackLoop;
  319. if (!oldPack.shouldBeKept()
  320. && repo.getFS().lastModified(
  321. oldPack.getPackFile()) < packExpireDate) {
  322. oldPack.close();
  323. if (shouldLoosen) {
  324. loosen(inserter, reader, oldPack, ids);
  325. }
  326. prunePack(oldName);
  327. }
  328. }
  329. // close the complete object database. That's my only chance to force
  330. // rescanning and to detect that certain pack files are now deleted.
  331. repo.getObjectDatabase().close();
  332. }
  333. /**
  334. * Deletes old pack file, unless 'preserve-oldpacks' is set, in which case it
  335. * moves the pack file to the preserved directory
  336. *
  337. * @param packFile
  338. * @param packName
  339. * @param ext
  340. * @param deleteOptions
  341. * @throws IOException
  342. */
  343. private void removeOldPack(File packFile, String packName, PackExt ext,
  344. int deleteOptions) throws IOException {
  345. if (pconfig != null && pconfig.isPreserveOldPacks()) {
  346. File oldPackDir = repo.getObjectDatabase().getPreservedDirectory();
  347. FileUtils.mkdir(oldPackDir, true);
  348. String oldPackName = "pack-" + packName + ".old-" + ext.getExtension(); //$NON-NLS-1$ //$NON-NLS-2$
  349. File oldPackFile = new File(oldPackDir, oldPackName);
  350. FileUtils.rename(packFile, oldPackFile);
  351. } else {
  352. FileUtils.delete(packFile, deleteOptions);
  353. }
  354. }
  355. /**
  356. * Delete the preserved directory including all pack files within
  357. */
  358. private void prunePreserved() {
  359. if (pconfig != null && pconfig.isPrunePreserved()) {
  360. try {
  361. FileUtils.delete(repo.getObjectDatabase().getPreservedDirectory(),
  362. FileUtils.RECURSIVE | FileUtils.RETRY | FileUtils.SKIP_MISSING);
  363. } catch (IOException e) {
  364. // Deletion of the preserved pack files failed. Silently return.
  365. }
  366. }
  367. }
  368. /**
  369. * Delete files associated with a single pack file. First try to delete the
  370. * ".pack" file because on some platforms the ".pack" file may be locked and
  371. * can't be deleted. In such a case it is better to detect this early and
  372. * give up on deleting files for this packfile. Otherwise we may delete the
  373. * ".index" file and when failing to delete the ".pack" file we are left
  374. * with a ".pack" file without a ".index" file.
  375. *
  376. * @param packName
  377. */
  378. private void prunePack(String packName) {
  379. PackExt[] extensions = PackExt.values();
  380. try {
  381. // Delete the .pack file first and if this fails give up on deleting
  382. // the other files
  383. int deleteOptions = FileUtils.RETRY | FileUtils.SKIP_MISSING;
  384. for (PackExt ext : extensions)
  385. if (PackExt.PACK.equals(ext)) {
  386. File f = nameFor(packName, "." + ext.getExtension()); //$NON-NLS-1$
  387. removeOldPack(f, packName, ext, deleteOptions);
  388. break;
  389. }
  390. // The .pack file has been deleted. Delete as many as the other
  391. // files as you can.
  392. deleteOptions |= FileUtils.IGNORE_ERRORS;
  393. for (PackExt ext : extensions) {
  394. if (!PackExt.PACK.equals(ext)) {
  395. File f = nameFor(packName, "." + ext.getExtension()); //$NON-NLS-1$
  396. removeOldPack(f, packName, ext, deleteOptions);
  397. }
  398. }
  399. } catch (IOException e) {
  400. // Deletion of the .pack file failed. Silently return.
  401. }
  402. }
  403. /**
  404. * Like "git prune-packed" this method tries to prune all loose objects
  405. * which can be found in packs. If certain objects can't be pruned (e.g.
  406. * because the filesystem delete operation fails) this is silently ignored.
  407. *
  408. * @throws IOException
  409. */
  410. public void prunePacked() throws IOException {
  411. ObjectDirectory objdb = repo.getObjectDatabase();
  412. Collection<PackFile> packs = objdb.getPacks();
  413. File objects = repo.getObjectsDirectory();
  414. String[] fanout = objects.list();
  415. if (fanout != null && fanout.length > 0) {
  416. pm.beginTask(JGitText.get().pruneLoosePackedObjects, fanout.length);
  417. try {
  418. for (String d : fanout) {
  419. checkCancelled();
  420. pm.update(1);
  421. if (d.length() != 2)
  422. continue;
  423. String[] entries = new File(objects, d).list();
  424. if (entries == null)
  425. continue;
  426. for (String e : entries) {
  427. checkCancelled();
  428. if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  429. continue;
  430. ObjectId id;
  431. try {
  432. id = ObjectId.fromString(d + e);
  433. } catch (IllegalArgumentException notAnObject) {
  434. // ignoring the file that does not represent loose
  435. // object
  436. continue;
  437. }
  438. boolean found = false;
  439. for (PackFile p : packs) {
  440. checkCancelled();
  441. if (p.hasObject(id)) {
  442. found = true;
  443. break;
  444. }
  445. }
  446. if (found)
  447. FileUtils.delete(objdb.fileFor(id), FileUtils.RETRY
  448. | FileUtils.SKIP_MISSING
  449. | FileUtils.IGNORE_ERRORS);
  450. }
  451. }
  452. } finally {
  453. pm.endTask();
  454. }
  455. }
  456. }
  457. /**
  458. * Like "git prune" this method tries to prune all loose objects which are
  459. * unreferenced. If certain objects can't be pruned (e.g. because the
  460. * filesystem delete operation fails) this is silently ignored.
  461. *
  462. * @param objectsToKeep
  463. * a set of objects which should explicitly not be pruned
  464. *
  465. * @throws IOException
  466. * @throws ParseException
  467. * If the configuration parameter "gc.pruneexpire" couldn't be
  468. * parsed
  469. */
  470. public void prune(Set<ObjectId> objectsToKeep) throws IOException,
  471. ParseException {
  472. long expireDate = getExpireDate();
  473. // Collect all loose objects which are old enough, not referenced from
  474. // the index and not in objectsToKeep
  475. Map<ObjectId, File> deletionCandidates = new HashMap<>();
  476. Set<ObjectId> indexObjects = null;
  477. File objects = repo.getObjectsDirectory();
  478. String[] fanout = objects.list();
  479. if (fanout == null || fanout.length == 0) {
  480. return;
  481. }
  482. pm.beginTask(JGitText.get().pruneLooseUnreferencedObjects,
  483. fanout.length);
  484. try {
  485. for (String d : fanout) {
  486. checkCancelled();
  487. pm.update(1);
  488. if (d.length() != 2)
  489. continue;
  490. File[] entries = new File(objects, d).listFiles();
  491. if (entries == null)
  492. continue;
  493. for (File f : entries) {
  494. checkCancelled();
  495. String fName = f.getName();
  496. if (fName.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  497. continue;
  498. if (repo.getFS().lastModified(f) >= expireDate)
  499. continue;
  500. try {
  501. ObjectId id = ObjectId.fromString(d + fName);
  502. if (objectsToKeep.contains(id))
  503. continue;
  504. if (indexObjects == null)
  505. indexObjects = listNonHEADIndexObjects();
  506. if (indexObjects.contains(id))
  507. continue;
  508. deletionCandidates.put(id, f);
  509. } catch (IllegalArgumentException notAnObject) {
  510. // ignoring the file that does not represent loose
  511. // object
  512. continue;
  513. }
  514. }
  515. }
  516. } finally {
  517. pm.endTask();
  518. }
  519. if (deletionCandidates.isEmpty()) {
  520. return;
  521. }
  522. checkCancelled();
  523. // From the set of current refs remove all those which have been handled
  524. // during last repack(). Only those refs will survive which have been
  525. // added or modified since the last repack. Only these can save existing
  526. // loose refs from being pruned.
  527. Collection<Ref> newRefs;
  528. if (lastPackedRefs == null || lastPackedRefs.isEmpty())
  529. newRefs = getAllRefs();
  530. else {
  531. Map<String, Ref> last = new HashMap<>();
  532. for (Ref r : lastPackedRefs) {
  533. last.put(r.getName(), r);
  534. }
  535. newRefs = new ArrayList<>();
  536. for (Ref r : getAllRefs()) {
  537. Ref old = last.get(r.getName());
  538. if (!equals(r, old)) {
  539. newRefs.add(r);
  540. }
  541. }
  542. }
  543. if (!newRefs.isEmpty()) {
  544. // There are new/modified refs! Check which loose objects are now
  545. // referenced by these modified refs (or their reflogentries).
  546. // Remove these loose objects
  547. // from the deletionCandidates. When the last candidate is removed
  548. // leave this method.
  549. ObjectWalk w = new ObjectWalk(repo);
  550. try {
  551. for (Ref cr : newRefs) {
  552. checkCancelled();
  553. w.markStart(w.parseAny(cr.getObjectId()));
  554. }
  555. if (lastPackedRefs != null)
  556. for (Ref lpr : lastPackedRefs) {
  557. w.markUninteresting(w.parseAny(lpr.getObjectId()));
  558. }
  559. removeReferenced(deletionCandidates, w);
  560. } finally {
  561. w.dispose();
  562. }
  563. }
  564. if (deletionCandidates.isEmpty())
  565. return;
  566. // Since we have not left the method yet there are still
  567. // deletionCandidates. Last chance for these objects not to be pruned is
  568. // that they are referenced by reflog entries. Even refs which currently
  569. // point to the same object as during last repack() may have
  570. // additional reflog entries not handled during last repack()
  571. ObjectWalk w = new ObjectWalk(repo);
  572. try {
  573. for (Ref ar : getAllRefs())
  574. for (ObjectId id : listRefLogObjects(ar, lastRepackTime)) {
  575. checkCancelled();
  576. w.markStart(w.parseAny(id));
  577. }
  578. if (lastPackedRefs != null)
  579. for (Ref lpr : lastPackedRefs) {
  580. checkCancelled();
  581. w.markUninteresting(w.parseAny(lpr.getObjectId()));
  582. }
  583. removeReferenced(deletionCandidates, w);
  584. } finally {
  585. w.dispose();
  586. }
  587. if (deletionCandidates.isEmpty())
  588. return;
  589. checkCancelled();
  590. // delete all candidates which have survived: these are unreferenced
  591. // loose objects. Make a last check, though, to avoid deleting objects
  592. // that could have been referenced while the candidates list was being
  593. // built (by an incoming push, for example).
  594. Set<File> touchedFanout = new HashSet<>();
  595. for (File f : deletionCandidates.values()) {
  596. if (f.lastModified() < expireDate) {
  597. f.delete();
  598. touchedFanout.add(f.getParentFile());
  599. }
  600. }
  601. for (File f : touchedFanout) {
  602. FileUtils.delete(f,
  603. FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.IGNORE_ERRORS);
  604. }
  605. repo.getObjectDatabase().close();
  606. }
  607. private long getExpireDate() throws ParseException {
  608. long expireDate = Long.MAX_VALUE;
  609. if (expire == null && expireAgeMillis == -1) {
  610. String pruneExpireStr = getPruneExpireStr();
  611. if (pruneExpireStr == null)
  612. pruneExpireStr = PRUNE_EXPIRE_DEFAULT;
  613. expire = GitDateParser.parse(pruneExpireStr, null, SystemReader
  614. .getInstance().getLocale());
  615. expireAgeMillis = -1;
  616. }
  617. if (expire != null)
  618. expireDate = expire.getTime();
  619. if (expireAgeMillis != -1)
  620. expireDate = System.currentTimeMillis() - expireAgeMillis;
  621. return expireDate;
  622. }
  623. private String getPruneExpireStr() {
  624. return repo.getConfig().getString(
  625. ConfigConstants.CONFIG_GC_SECTION, null,
  626. ConfigConstants.CONFIG_KEY_PRUNEEXPIRE);
  627. }
  628. private long getPackExpireDate() throws ParseException {
  629. long packExpireDate = Long.MAX_VALUE;
  630. if (packExpire == null && packExpireAgeMillis == -1) {
  631. String prunePackExpireStr = repo.getConfig().getString(
  632. ConfigConstants.CONFIG_GC_SECTION, null,
  633. ConfigConstants.CONFIG_KEY_PRUNEPACKEXPIRE);
  634. if (prunePackExpireStr == null)
  635. prunePackExpireStr = PRUNE_PACK_EXPIRE_DEFAULT;
  636. packExpire = GitDateParser.parse(prunePackExpireStr, null,
  637. SystemReader.getInstance().getLocale());
  638. packExpireAgeMillis = -1;
  639. }
  640. if (packExpire != null)
  641. packExpireDate = packExpire.getTime();
  642. if (packExpireAgeMillis != -1)
  643. packExpireDate = System.currentTimeMillis() - packExpireAgeMillis;
  644. return packExpireDate;
  645. }
  646. /**
  647. * Remove all entries from a map which key is the id of an object referenced
  648. * by the given ObjectWalk
  649. *
  650. * @param id2File
  651. * @param w
  652. * @throws MissingObjectException
  653. * @throws IncorrectObjectTypeException
  654. * @throws IOException
  655. */
  656. private void removeReferenced(Map<ObjectId, File> id2File,
  657. ObjectWalk w) throws MissingObjectException,
  658. IncorrectObjectTypeException, IOException {
  659. RevObject ro = w.next();
  660. while (ro != null) {
  661. checkCancelled();
  662. if (id2File.remove(ro.getId()) != null)
  663. if (id2File.isEmpty())
  664. return;
  665. ro = w.next();
  666. }
  667. ro = w.nextObject();
  668. while (ro != null) {
  669. checkCancelled();
  670. if (id2File.remove(ro.getId()) != null)
  671. if (id2File.isEmpty())
  672. return;
  673. ro = w.nextObject();
  674. }
  675. }
  676. private static boolean equals(Ref r1, Ref r2) {
  677. if (r1 == null || r2 == null)
  678. return false;
  679. if (r1.isSymbolic()) {
  680. if (!r2.isSymbolic())
  681. return false;
  682. return r1.getTarget().getName().equals(r2.getTarget().getName());
  683. } else {
  684. if (r2.isSymbolic()) {
  685. return false;
  686. }
  687. return Objects.equals(r1.getObjectId(), r2.getObjectId());
  688. }
  689. }
  690. /**
  691. * Packs all non-symbolic, loose refs into packed-refs.
  692. *
  693. * @throws IOException
  694. */
  695. public void packRefs() throws IOException {
  696. Collection<Ref> refs = repo.getRefDatabase().getRefs(Constants.R_REFS).values();
  697. List<String> refsToBePacked = new ArrayList<>(refs.size());
  698. pm.beginTask(JGitText.get().packRefs, refs.size());
  699. try {
  700. for (Ref ref : refs) {
  701. checkCancelled();
  702. if (!ref.isSymbolic() && ref.getStorage().isLoose())
  703. refsToBePacked.add(ref.getName());
  704. pm.update(1);
  705. }
  706. ((RefDirectory) repo.getRefDatabase()).pack(refsToBePacked);
  707. } finally {
  708. pm.endTask();
  709. }
  710. }
  711. /**
  712. * Packs all objects which reachable from any of the heads into one pack
  713. * file. Additionally all objects which are not reachable from any head but
  714. * which are reachable from any of the other refs (e.g. tags), special refs
  715. * (e.g. FETCH_HEAD) or index are packed into a separate pack file. Objects
  716. * included in pack files which have a .keep file associated are never
  717. * repacked. All old pack files which existed before are deleted.
  718. *
  719. * @return a collection of the newly created pack files
  720. * @throws IOException
  721. * when during reading of refs, index, packfiles, objects,
  722. * reflog-entries or during writing to the packfiles
  723. * {@link IOException} occurs
  724. */
  725. public Collection<PackFile> repack() throws IOException {
  726. Collection<PackFile> toBeDeleted = repo.getObjectDatabase().getPacks();
  727. long time = System.currentTimeMillis();
  728. Collection<Ref> refsBefore = getAllRefs();
  729. Set<ObjectId> allHeads = new HashSet<>();
  730. Set<ObjectId> nonHeads = new HashSet<>();
  731. Set<ObjectId> txnHeads = new HashSet<>();
  732. Set<ObjectId> tagTargets = new HashSet<>();
  733. Set<ObjectId> indexObjects = listNonHEADIndexObjects();
  734. RefDatabase refdb = repo.getRefDatabase();
  735. for (Ref ref : refsBefore) {
  736. checkCancelled();
  737. nonHeads.addAll(listRefLogObjects(ref, 0));
  738. if (ref.isSymbolic() || ref.getObjectId() == null)
  739. continue;
  740. if (isHead(ref) || isTag(ref))
  741. allHeads.add(ref.getObjectId());
  742. else if (RefTreeNames.isRefTree(refdb, ref.getName()))
  743. txnHeads.add(ref.getObjectId());
  744. else
  745. nonHeads.add(ref.getObjectId());
  746. if (ref.getPeeledObjectId() != null)
  747. tagTargets.add(ref.getPeeledObjectId());
  748. }
  749. List<ObjectIdSet> excluded = new LinkedList<>();
  750. for (final PackFile f : repo.getObjectDatabase().getPacks()) {
  751. checkCancelled();
  752. if (f.shouldBeKept())
  753. excluded.add(f.getIndex());
  754. }
  755. tagTargets.addAll(allHeads);
  756. nonHeads.addAll(indexObjects);
  757. List<PackFile> ret = new ArrayList<>(2);
  758. PackFile heads = null;
  759. if (!allHeads.isEmpty()) {
  760. heads = writePack(allHeads, Collections.<ObjectId> emptySet(),
  761. tagTargets, excluded);
  762. if (heads != null) {
  763. ret.add(heads);
  764. excluded.add(0, heads.getIndex());
  765. }
  766. }
  767. if (!nonHeads.isEmpty()) {
  768. PackFile rest = writePack(nonHeads, allHeads, tagTargets, excluded);
  769. if (rest != null)
  770. ret.add(rest);
  771. }
  772. if (!txnHeads.isEmpty()) {
  773. PackFile txn = writePack(txnHeads, PackWriter.NONE, null, excluded);
  774. if (txn != null)
  775. ret.add(txn);
  776. }
  777. try {
  778. deleteOldPacks(toBeDeleted, ret);
  779. } catch (ParseException e) {
  780. // TODO: the exception has to be wrapped into an IOException because
  781. // throwing the ParseException directly would break the API, instead
  782. // we should throw a ConfigInvalidException
  783. throw new IOException(e);
  784. }
  785. prunePacked();
  786. deleteEmptyRefsFolders();
  787. deleteOrphans();
  788. lastPackedRefs = refsBefore;
  789. lastRepackTime = time;
  790. return ret;
  791. }
  792. private static boolean isHead(Ref ref) {
  793. return ref.getName().startsWith(Constants.R_HEADS);
  794. }
  795. private static boolean isTag(Ref ref) {
  796. return ref.getName().startsWith(Constants.R_TAGS);
  797. }
  798. private void deleteEmptyRefsFolders() throws IOException {
  799. Path refs = repo.getDirectory().toPath().resolve(Constants.R_REFS);
  800. // Avoid deleting a folder that was created after the threshold so that concurrent
  801. // operations trying to create a reference are not impacted
  802. Instant threshold = Instant.now().minus(30, ChronoUnit.SECONDS);
  803. try (Stream<Path> entries = Files.list(refs)) {
  804. Iterator<Path> iterator = entries.iterator();
  805. while (iterator.hasNext()) {
  806. try (Stream<Path> s = Files.list(iterator.next())) {
  807. s.filter(path -> canBeSafelyDeleted(path, threshold)).forEach(this::deleteDir);
  808. }
  809. }
  810. }
  811. }
  812. private boolean canBeSafelyDeleted(Path path, Instant threshold) {
  813. try {
  814. return Files.getLastModifiedTime(path).toInstant().isBefore(threshold);
  815. }
  816. catch (IOException e) {
  817. LOG.warn(MessageFormat.format(
  818. JGitText.get().cannotAccessLastModifiedForSafeDeletion,
  819. path), e);
  820. return false;
  821. }
  822. }
  823. private void deleteDir(Path dir) {
  824. try (Stream<Path> dirs = Files.walk(dir)) {
  825. dirs.filter(this::isDirectory).sorted(Comparator.reverseOrder())
  826. .forEach(this::delete);
  827. } catch (IOException e) {
  828. LOG.error(e.getMessage(), e);
  829. }
  830. }
  831. private boolean isDirectory(Path p) {
  832. return p.toFile().isDirectory();
  833. }
  834. private void delete(Path d) {
  835. try {
  836. Files.delete(d);
  837. } catch (IOException e) {
  838. LOG.error(MessageFormat.format(JGitText.get().cannotDeleteFile, d),
  839. e);
  840. }
  841. }
  842. /**
  843. * Deletes orphans
  844. * <p>
  845. * A file is considered an orphan if it is either a "bitmap" or an index
  846. * file, and its corresponding pack file is missing in the list.
  847. * </p>
  848. */
  849. private void deleteOrphans() {
  850. Path packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(),
  851. "pack"); //$NON-NLS-1$
  852. List<String> fileNames = null;
  853. try (Stream<Path> files = Files.list(packDir)) {
  854. fileNames = files.map(path -> path.getFileName().toString())
  855. .filter(name -> {
  856. return (name.endsWith(PACK_EXT)
  857. || name.endsWith(BITMAP_EXT)
  858. || name.endsWith(INDEX_EXT));
  859. }).sorted(Collections.reverseOrder())
  860. .collect(Collectors.toList());
  861. } catch (IOException e1) {
  862. // ignore
  863. }
  864. if (fileNames == null) {
  865. return;
  866. }
  867. String base = null;
  868. for (String n : fileNames) {
  869. if (n.endsWith(PACK_EXT)) {
  870. base = n.substring(0, n.lastIndexOf('.'));
  871. } else {
  872. if (base == null || !n.startsWith(base)) {
  873. try {
  874. Files.delete(new File(packDir.toFile(), n).toPath());
  875. } catch (IOException e) {
  876. LOG.error(e.getMessage(), e);
  877. }
  878. }
  879. }
  880. }
  881. }
  882. /**
  883. * @param ref
  884. * the ref which log should be inspected
  885. * @param minTime only reflog entries not older then this time are processed
  886. * @return the {@link ObjectId}s contained in the reflog
  887. * @throws IOException
  888. */
  889. private Set<ObjectId> listRefLogObjects(Ref ref, long minTime) throws IOException {
  890. ReflogReader reflogReader = repo.getReflogReader(ref.getName());
  891. if (reflogReader == null) {
  892. return Collections.emptySet();
  893. }
  894. List<ReflogEntry> rlEntries = reflogReader
  895. .getReverseEntries();
  896. if (rlEntries == null || rlEntries.isEmpty())
  897. return Collections.<ObjectId> emptySet();
  898. Set<ObjectId> ret = new HashSet<>();
  899. for (ReflogEntry e : rlEntries) {
  900. if (e.getWho().getWhen().getTime() < minTime)
  901. break;
  902. ObjectId newId = e.getNewId();
  903. if (newId != null && !ObjectId.zeroId().equals(newId))
  904. ret.add(newId);
  905. ObjectId oldId = e.getOldId();
  906. if (oldId != null && !ObjectId.zeroId().equals(oldId))
  907. ret.add(oldId);
  908. }
  909. return ret;
  910. }
  911. /**
  912. * Returns a collection of all refs and additional refs.
  913. *
  914. * Additional refs which don't start with "refs/" are not returned because
  915. * they should not save objects from being garbage collected. Examples for
  916. * such references are ORIG_HEAD, MERGE_HEAD, FETCH_HEAD and
  917. * CHERRY_PICK_HEAD.
  918. *
  919. * @return a collection of refs pointing to live objects.
  920. * @throws IOException
  921. */
  922. private Collection<Ref> getAllRefs() throws IOException {
  923. RefDatabase refdb = repo.getRefDatabase();
  924. Collection<Ref> refs = refdb.getRefs(RefDatabase.ALL).values();
  925. List<Ref> addl = refdb.getAdditionalRefs();
  926. if (!addl.isEmpty()) {
  927. List<Ref> all = new ArrayList<>(refs.size() + addl.size());
  928. all.addAll(refs);
  929. // add additional refs which start with refs/
  930. for (Ref r : addl) {
  931. checkCancelled();
  932. if (r.getName().startsWith(Constants.R_REFS)) {
  933. all.add(r);
  934. }
  935. }
  936. return all;
  937. }
  938. return refs;
  939. }
  940. /**
  941. * Return a list of those objects in the index which differ from whats in
  942. * HEAD
  943. *
  944. * @return a set of ObjectIds of changed objects in the index
  945. * @throws IOException
  946. * @throws CorruptObjectException
  947. * @throws NoWorkTreeException
  948. */
  949. private Set<ObjectId> listNonHEADIndexObjects()
  950. throws CorruptObjectException, IOException {
  951. if (repo.isBare()) {
  952. return Collections.emptySet();
  953. }
  954. try (TreeWalk treeWalk = new TreeWalk(repo)) {
  955. treeWalk.addTree(new DirCacheIterator(repo.readDirCache()));
  956. ObjectId headID = repo.resolve(Constants.HEAD);
  957. if (headID != null) {
  958. try (RevWalk revWalk = new RevWalk(repo)) {
  959. treeWalk.addTree(revWalk.parseTree(headID));
  960. }
  961. }
  962. treeWalk.setFilter(TreeFilter.ANY_DIFF);
  963. treeWalk.setRecursive(true);
  964. Set<ObjectId> ret = new HashSet<>();
  965. while (treeWalk.next()) {
  966. checkCancelled();
  967. ObjectId objectId = treeWalk.getObjectId(0);
  968. switch (treeWalk.getRawMode(0) & FileMode.TYPE_MASK) {
  969. case FileMode.TYPE_MISSING:
  970. case FileMode.TYPE_GITLINK:
  971. continue;
  972. case FileMode.TYPE_TREE:
  973. case FileMode.TYPE_FILE:
  974. case FileMode.TYPE_SYMLINK:
  975. ret.add(objectId);
  976. continue;
  977. default:
  978. throw new IOException(MessageFormat.format(
  979. JGitText.get().corruptObjectInvalidMode3,
  980. String.format("%o", //$NON-NLS-1$
  981. Integer.valueOf(treeWalk.getRawMode(0))),
  982. (objectId == null) ? "null" : objectId.name(), //$NON-NLS-1$
  983. treeWalk.getPathString(), //
  984. repo.getIndexFile()));
  985. }
  986. }
  987. return ret;
  988. }
  989. }
  990. private PackFile writePack(@NonNull Set<? extends ObjectId> want,
  991. @NonNull Set<? extends ObjectId> have, Set<ObjectId> tagTargets,
  992. List<ObjectIdSet> excludeObjects) throws IOException {
  993. checkCancelled();
  994. File tmpPack = null;
  995. Map<PackExt, File> tmpExts = new TreeMap<>(
  996. new Comparator<PackExt>() {
  997. @Override
  998. public int compare(PackExt o1, PackExt o2) {
  999. // INDEX entries must be returned last, so the pack
  1000. // scanner does pick up the new pack until all the
  1001. // PackExt entries have been written.
  1002. if (o1 == o2)
  1003. return 0;
  1004. if (o1 == PackExt.INDEX)
  1005. return 1;
  1006. if (o2 == PackExt.INDEX)
  1007. return -1;
  1008. return Integer.signum(o1.hashCode() - o2.hashCode());
  1009. }
  1010. });
  1011. try (PackWriter pw = new PackWriter(
  1012. (pconfig == null) ? new PackConfig(repo) : pconfig,
  1013. repo.newObjectReader())) {
  1014. // prepare the PackWriter
  1015. pw.setDeltaBaseAsOffset(true);
  1016. pw.setReuseDeltaCommits(false);
  1017. if (tagTargets != null)
  1018. pw.setTagTargets(tagTargets);
  1019. if (excludeObjects != null)
  1020. for (ObjectIdSet idx : excludeObjects)
  1021. pw.excludeObjects(idx);
  1022. pw.preparePack(pm, want, have);
  1023. if (pw.getObjectCount() == 0)
  1024. return null;
  1025. checkCancelled();
  1026. // create temporary files
  1027. String id = pw.computeName().getName();
  1028. File packdir = new File(repo.getObjectsDirectory(), "pack"); //$NON-NLS-1$
  1029. tmpPack = File.createTempFile("gc_", ".pack_tmp", packdir); //$NON-NLS-1$ //$NON-NLS-2$
  1030. final String tmpBase = tmpPack.getName()
  1031. .substring(0, tmpPack.getName().lastIndexOf('.'));
  1032. File tmpIdx = new File(packdir, tmpBase + ".idx_tmp"); //$NON-NLS-1$
  1033. tmpExts.put(INDEX, tmpIdx);
  1034. if (!tmpIdx.createNewFile())
  1035. throw new IOException(MessageFormat.format(
  1036. JGitText.get().cannotCreateIndexfile, tmpIdx.getPath()));
  1037. // write the packfile
  1038. FileOutputStream fos = new FileOutputStream(tmpPack);
  1039. FileChannel channel = fos.getChannel();
  1040. OutputStream channelStream = Channels.newOutputStream(channel);
  1041. try {
  1042. pw.writePack(pm, pm, channelStream);
  1043. } finally {
  1044. channel.force(true);
  1045. channelStream.close();
  1046. fos.close();
  1047. }
  1048. // write the packindex
  1049. fos = new FileOutputStream(tmpIdx);
  1050. FileChannel idxChannel = fos.getChannel();
  1051. OutputStream idxStream = Channels.newOutputStream(idxChannel);
  1052. try {
  1053. pw.writeIndex(idxStream);
  1054. } finally {
  1055. idxChannel.force(true);
  1056. idxStream.close();
  1057. fos.close();
  1058. }
  1059. if (pw.prepareBitmapIndex(pm)) {
  1060. File tmpBitmapIdx = new File(packdir, tmpBase + ".bitmap_tmp"); //$NON-NLS-1$
  1061. tmpExts.put(BITMAP_INDEX, tmpBitmapIdx);
  1062. if (!tmpBitmapIdx.createNewFile())
  1063. throw new IOException(MessageFormat.format(
  1064. JGitText.get().cannotCreateIndexfile,
  1065. tmpBitmapIdx.getPath()));
  1066. fos = new FileOutputStream(tmpBitmapIdx);
  1067. idxChannel = fos.getChannel();
  1068. idxStream = Channels.newOutputStream(idxChannel);
  1069. try {
  1070. pw.writeBitmapIndex(idxStream);
  1071. } finally {
  1072. idxChannel.force(true);
  1073. idxStream.close();
  1074. fos.close();
  1075. }
  1076. }
  1077. // rename the temporary files to real files
  1078. File realPack = nameFor(id, ".pack"); //$NON-NLS-1$
  1079. // if the packfile already exists (because we are rewriting a
  1080. // packfile for the same set of objects maybe with different
  1081. // PackConfig) then make sure we get rid of all handles on the file.
  1082. // Windows will not allow for rename otherwise.
  1083. if (realPack.exists())
  1084. for (PackFile p : repo.getObjectDatabase().getPacks())
  1085. if (realPack.getPath().equals(p.getPackFile().getPath())) {
  1086. p.close();
  1087. break;
  1088. }
  1089. tmpPack.setReadOnly();
  1090. FileUtils.rename(tmpPack, realPack, StandardCopyOption.ATOMIC_MOVE);
  1091. for (Map.Entry<PackExt, File> tmpEntry : tmpExts.entrySet()) {
  1092. File tmpExt = tmpEntry.getValue();
  1093. tmpExt.setReadOnly();
  1094. File realExt = nameFor(id,
  1095. "." + tmpEntry.getKey().getExtension()); //$NON-NLS-1$
  1096. try {
  1097. FileUtils.rename(tmpExt, realExt,
  1098. StandardCopyOption.ATOMIC_MOVE);
  1099. } catch (IOException e) {
  1100. File newExt = new File(realExt.getParentFile(),
  1101. realExt.getName() + ".new"); //$NON-NLS-1$
  1102. try {
  1103. FileUtils.rename(tmpExt, newExt,
  1104. StandardCopyOption.ATOMIC_MOVE);
  1105. } catch (IOException e2) {
  1106. newExt = tmpExt;
  1107. e = e2;
  1108. }
  1109. throw new IOException(MessageFormat.format(
  1110. JGitText.get().panicCantRenameIndexFile, newExt,
  1111. realExt), e);
  1112. }
  1113. }
  1114. return repo.getObjectDatabase().openPack(realPack);
  1115. } finally {
  1116. if (tmpPack != null && tmpPack.exists())
  1117. tmpPack.delete();
  1118. for (File tmpExt : tmpExts.values()) {
  1119. if (tmpExt.exists())
  1120. tmpExt.delete();
  1121. }
  1122. }
  1123. }
  1124. private File nameFor(String name, String ext) {
  1125. File packdir = new File(repo.getObjectsDirectory(), "pack"); //$NON-NLS-1$
  1126. return new File(packdir, "pack-" + name + ext); //$NON-NLS-1$
  1127. }
  1128. private void checkCancelled() throws CancelledException {
  1129. if (pm.isCancelled()) {
  1130. throw new CancelledException(JGitText.get().operationCanceled);
  1131. }
  1132. }
  1133. /**
  1134. * A class holding statistical data for a FileRepository regarding how many
  1135. * objects are stored as loose or packed objects
  1136. */
  1137. public static class RepoStatistics {
  1138. /**
  1139. * The number of objects stored in pack files. If the same object is
  1140. * stored in multiple pack files then it is counted as often as it
  1141. * occurs in pack files.
  1142. */
  1143. public long numberOfPackedObjects;
  1144. /**
  1145. * The number of pack files
  1146. */
  1147. public long numberOfPackFiles;
  1148. /**
  1149. * The number of objects stored as loose objects.
  1150. */
  1151. public long numberOfLooseObjects;
  1152. /**
  1153. * The sum of the sizes of all files used to persist loose objects.
  1154. */
  1155. public long sizeOfLooseObjects;
  1156. /**
  1157. * The sum of the sizes of all pack files.
  1158. */
  1159. public long sizeOfPackedObjects;
  1160. /**
  1161. * The number of loose refs.
  1162. */
  1163. public long numberOfLooseRefs;
  1164. /**
  1165. * The number of refs stored in pack files.
  1166. */
  1167. public long numberOfPackedRefs;
  1168. /**
  1169. * The number of bitmaps in the bitmap indices.
  1170. */
  1171. public long numberOfBitmaps;
  1172. @Override
  1173. public String toString() {
  1174. final StringBuilder b = new StringBuilder();
  1175. b.append("numberOfPackedObjects=").append(numberOfPackedObjects); //$NON-NLS-1$
  1176. b.append(", numberOfPackFiles=").append(numberOfPackFiles); //$NON-NLS-1$
  1177. b.append(", numberOfLooseObjects=").append(numberOfLooseObjects); //$NON-NLS-1$
  1178. b.append(", numberOfLooseRefs=").append(numberOfLooseRefs); //$NON-NLS-1$
  1179. b.append(", numberOfPackedRefs=").append(numberOfPackedRefs); //$NON-NLS-1$
  1180. b.append(", sizeOfLooseObjects=").append(sizeOfLooseObjects); //$NON-NLS-1$
  1181. b.append(", sizeOfPackedObjects=").append(sizeOfPackedObjects); //$NON-NLS-1$
  1182. b.append(", numberOfBitmaps=").append(numberOfBitmaps); //$NON-NLS-1$
  1183. return b.toString();
  1184. }
  1185. }
  1186. /**
  1187. * Returns information about objects and pack files for a FileRepository.
  1188. *
  1189. * @return information about objects and pack files for a FileRepository
  1190. * @throws IOException
  1191. */
  1192. public RepoStatistics getStatistics() throws IOException {
  1193. RepoStatistics ret = new RepoStatistics();
  1194. Collection<PackFile> packs = repo.getObjectDatabase().getPacks();
  1195. for (PackFile f : packs) {
  1196. ret.numberOfPackedObjects += f.getIndex().getObjectCount();
  1197. ret.numberOfPackFiles++;
  1198. ret.sizeOfPackedObjects += f.getPackFile().length();
  1199. if (f.getBitmapIndex() != null)
  1200. ret.numberOfBitmaps += f.getBitmapIndex().getBitmapCount();
  1201. }
  1202. File objDir = repo.getObjectsDirectory();
  1203. String[] fanout = objDir.list();
  1204. if (fanout != null && fanout.length > 0) {
  1205. for (String d : fanout) {
  1206. if (d.length() != 2)
  1207. continue;
  1208. File[] entries = new File(objDir, d).listFiles();
  1209. if (entries == null)
  1210. continue;
  1211. for (File f : entries) {
  1212. if (f.getName().length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  1213. continue;
  1214. ret.numberOfLooseObjects++;
  1215. ret.sizeOfLooseObjects += f.length();
  1216. }
  1217. }
  1218. }
  1219. RefDatabase refDb = repo.getRefDatabase();
  1220. for (Ref r : refDb.getRefs(RefDatabase.ALL).values()) {
  1221. Storage storage = r.getStorage();
  1222. if (storage == Storage.LOOSE || storage == Storage.LOOSE_PACKED)
  1223. ret.numberOfLooseRefs++;
  1224. if (storage == Storage.PACKED || storage == Storage.LOOSE_PACKED)
  1225. ret.numberOfPackedRefs++;
  1226. }
  1227. return ret;
  1228. }
  1229. /**
  1230. * Set the progress monitor used for garbage collection methods.
  1231. *
  1232. * @param pm
  1233. * @return this
  1234. */
  1235. public GC setProgressMonitor(ProgressMonitor pm) {
  1236. this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
  1237. return this;
  1238. }
  1239. /**
  1240. * During gc() or prune() each unreferenced, loose object which has been
  1241. * created or modified in the last <code>expireAgeMillis</code> milliseconds
  1242. * will not be pruned. Only older objects may be pruned. If set to 0 then
  1243. * every object is a candidate for pruning.
  1244. *
  1245. * @param expireAgeMillis
  1246. * minimal age of objects to be pruned in milliseconds.
  1247. */
  1248. public void setExpireAgeMillis(long expireAgeMillis) {
  1249. this.expireAgeMillis = expireAgeMillis;
  1250. expire = null;
  1251. }
  1252. /**
  1253. * During gc() or prune() packfiles which are created or modified in the
  1254. * last <code>packExpireAgeMillis</code> milliseconds will not be deleted.
  1255. * Only older packfiles may be deleted. If set to 0 then every packfile is a
  1256. * candidate for deletion.
  1257. *
  1258. * @param packExpireAgeMillis
  1259. * minimal age of packfiles to be deleted in milliseconds.
  1260. */
  1261. public void setPackExpireAgeMillis(long packExpireAgeMillis) {
  1262. this.packExpireAgeMillis = packExpireAgeMillis;
  1263. expire = null;
  1264. }
  1265. /**
  1266. * Set the PackConfig used when (re-)writing packfiles. This allows to
  1267. * influence how packs are written and to implement something similar to
  1268. * "git gc --aggressive"
  1269. *
  1270. * @param pconfig
  1271. * the {@link PackConfig} used when writing packs
  1272. */
  1273. public void setPackConfig(PackConfig pconfig) {
  1274. this.pconfig = pconfig;
  1275. }
  1276. /**
  1277. * During gc() or prune() each unreferenced, loose object which has been
  1278. * created or modified after or at <code>expire</code> will not be pruned.
  1279. * Only older objects may be pruned. If set to null then every object is a
  1280. * candidate for pruning.
  1281. *
  1282. * @param expire
  1283. * instant in time which defines object expiration
  1284. * objects with modification time before this instant are expired
  1285. * objects with modification time newer or equal to this instant
  1286. * are not expired
  1287. */
  1288. public void setExpire(Date expire) {
  1289. this.expire = expire;
  1290. expireAgeMillis = -1;
  1291. }
  1292. /**
  1293. * During gc() or prune() packfiles which are created or modified after or
  1294. * at <code>packExpire</code> will not be deleted. Only older packfiles may
  1295. * be deleted. If set to null then every packfile is a candidate for
  1296. * deletion.
  1297. *
  1298. * @param packExpire
  1299. * instant in time which defines packfile expiration
  1300. */
  1301. public void setPackExpire(Date packExpire) {
  1302. this.packExpire = packExpire;
  1303. packExpireAgeMillis = -1;
  1304. }
  1305. /**
  1306. * Set the {@code gc --auto} option.
  1307. *
  1308. * With this option, gc checks whether any housekeeping is required; if not,
  1309. * it exits without performing any work. Some JGit commands run
  1310. * {@code gc --auto} after performing operations that could create many
  1311. * loose objects.
  1312. * <p/>
  1313. * Housekeeping is required if there are too many loose objects or too many
  1314. * packs in the repository. If the number of loose objects exceeds the value
  1315. * of the gc.auto option JGit GC consolidates all existing packs into a
  1316. * single pack (equivalent to {@code -A} option), whereas git-core would
  1317. * combine all loose objects into a single pack using {@code repack -d -l}.
  1318. * Setting the value of {@code gc.auto} to 0 disables automatic packing of
  1319. * loose objects.
  1320. * <p/>
  1321. * If the number of packs exceeds the value of {@code gc.autoPackLimit},
  1322. * then existing packs (except those marked with a .keep file) are
  1323. * consolidated into a single pack by using the {@code -A} option of repack.
  1324. * Setting {@code gc.autoPackLimit} to 0 disables automatic consolidation of
  1325. * packs.
  1326. * <p/>
  1327. * Like git the following jgit commands run auto gc:
  1328. * <ul>
  1329. * <li>fetch</li>
  1330. * <li>merge</li>
  1331. * <li>rebase</li>
  1332. * <li>receive-pack</li>
  1333. * </ul>
  1334. * The auto gc for receive-pack can be suppressed by setting the config
  1335. * option {@code receive.autogc = false}
  1336. *
  1337. * @param auto
  1338. * defines whether gc should do automatic housekeeping
  1339. */
  1340. public void setAuto(boolean auto) {
  1341. this.automatic = auto;
  1342. }
  1343. /**
  1344. * @param background
  1345. * whether to run the gc in a background thread.
  1346. */
  1347. void setBackground(boolean background) {
  1348. this.background = background;
  1349. }
  1350. private boolean needGc() {
  1351. if (tooManyPacks()) {
  1352. addRepackAllOption();
  1353. } else if (!tooManyLooseObjects()) {
  1354. return false;
  1355. }
  1356. // TODO run pre-auto-gc hook, if it fails return false
  1357. return true;
  1358. }
  1359. private void addRepackAllOption() {
  1360. // TODO: if JGit GC is enhanced to support repack's option -l this
  1361. // method needs to be implemented
  1362. }
  1363. /**
  1364. * @return {@code true} if number of packs > gc.autopacklimit (default 50)
  1365. */
  1366. boolean tooManyPacks() {
  1367. int autopacklimit = repo.getConfig().getInt(
  1368. ConfigConstants.CONFIG_GC_SECTION,
  1369. ConfigConstants.CONFIG_KEY_AUTOPACKLIMIT,
  1370. DEFAULT_AUTOPACKLIMIT);
  1371. if (autopacklimit <= 0) {
  1372. return false;
  1373. }
  1374. // JGit always creates two packfiles, one for the objects reachable from
  1375. // branches, and another one for the rest
  1376. return repo.getObjectDatabase().getPacks().size() > (autopacklimit + 1);
  1377. }
  1378. /**
  1379. * Quickly estimate number of loose objects, SHA1 is distributed evenly so
  1380. * counting objects in one directory (bucket 17) is sufficient
  1381. *
  1382. * @return {@code true} if number of loose objects > gc.auto (default 6700)
  1383. */
  1384. boolean tooManyLooseObjects() {
  1385. int auto = getLooseObjectLimit();
  1386. if (auto <= 0) {
  1387. return false;
  1388. }
  1389. int n = 0;
  1390. int threshold = (auto + 255) / 256;
  1391. Path dir = repo.getObjectsDirectory().toPath().resolve("17"); //$NON-NLS-1$
  1392. if (!Files.exists(dir)) {
  1393. return false;
  1394. }
  1395. try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,
  1396. new DirectoryStream.Filter<Path>() {
  1397. @Override
  1398. public boolean accept(Path file) throws IOException {
  1399. Path fileName = file.getFileName();
  1400. return Files.isRegularFile(file) && fileName != null
  1401. && PATTERN_LOOSE_OBJECT
  1402. .matcher(fileName.toString()).matches();
  1403. }
  1404. })) {
  1405. for (Iterator<Path> iter = stream.iterator(); iter.hasNext();
  1406. iter.next()) {
  1407. if (++n > threshold) {
  1408. return true;
  1409. }
  1410. }
  1411. } catch (IOException e) {
  1412. LOG.error(e.getMessage(), e);
  1413. }
  1414. return false;
  1415. }
  1416. private int getLooseObjectLimit() {
  1417. return repo.getConfig().getInt(ConfigConstants.CONFIG_GC_SECTION,
  1418. ConfigConstants.CONFIG_KEY_AUTO, DEFAULT_AUTOLIMIT);
  1419. }
  1420. }