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

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