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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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.nio.channels.Channels;
  52. import java.nio.channels.FileChannel;
  53. import java.nio.file.StandardCopyOption;
  54. import java.text.MessageFormat;
  55. import java.text.ParseException;
  56. import java.util.ArrayList;
  57. import java.util.Collection;
  58. import java.util.Collections;
  59. import java.util.Comparator;
  60. import java.util.Date;
  61. import java.util.HashMap;
  62. import java.util.HashSet;
  63. import java.util.LinkedList;
  64. import java.util.List;
  65. import java.util.Map;
  66. import java.util.Objects;
  67. import java.util.Set;
  68. import java.util.TreeMap;
  69. import org.eclipse.jgit.annotations.NonNull;
  70. import org.eclipse.jgit.dircache.DirCacheIterator;
  71. import org.eclipse.jgit.errors.CorruptObjectException;
  72. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  73. import org.eclipse.jgit.errors.MissingObjectException;
  74. import org.eclipse.jgit.errors.NoWorkTreeException;
  75. import org.eclipse.jgit.internal.JGitText;
  76. import org.eclipse.jgit.internal.storage.pack.PackExt;
  77. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  78. import org.eclipse.jgit.internal.storage.reftree.RefTreeNames;
  79. import org.eclipse.jgit.lib.ConfigConstants;
  80. import org.eclipse.jgit.lib.Constants;
  81. import org.eclipse.jgit.lib.FileMode;
  82. import org.eclipse.jgit.lib.NullProgressMonitor;
  83. import org.eclipse.jgit.lib.ObjectId;
  84. import org.eclipse.jgit.lib.ObjectIdSet;
  85. import org.eclipse.jgit.lib.ProgressMonitor;
  86. import org.eclipse.jgit.lib.Ref;
  87. import org.eclipse.jgit.lib.Ref.Storage;
  88. import org.eclipse.jgit.lib.RefDatabase;
  89. import org.eclipse.jgit.lib.ReflogEntry;
  90. import org.eclipse.jgit.lib.ReflogReader;
  91. import org.eclipse.jgit.revwalk.ObjectWalk;
  92. import org.eclipse.jgit.revwalk.RevObject;
  93. import org.eclipse.jgit.revwalk.RevWalk;
  94. import org.eclipse.jgit.storage.pack.PackConfig;
  95. import org.eclipse.jgit.treewalk.TreeWalk;
  96. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  97. import org.eclipse.jgit.util.FileUtils;
  98. import org.eclipse.jgit.util.GitDateParser;
  99. import org.eclipse.jgit.util.SystemReader;
  100. /**
  101. * A garbage collector for git {@link FileRepository}. Instances of this class
  102. * are not thread-safe. Don't use the same instance from multiple threads.
  103. *
  104. * This class started as a copy of DfsGarbageCollector from Shawn O. Pearce
  105. * adapted to FileRepositories.
  106. */
  107. public class GC {
  108. private static final String PRUNE_EXPIRE_DEFAULT = "2.weeks.ago"; //$NON-NLS-1$
  109. private static final String PRUNE_PACK_EXPIRE_DEFAULT = "1.hour.ago"; //$NON-NLS-1$
  110. private final FileRepository repo;
  111. private ProgressMonitor pm;
  112. private long expireAgeMillis = -1;
  113. private Date expire;
  114. private long packExpireAgeMillis = -1;
  115. private Date packExpire;
  116. private PackConfig pconfig = null;
  117. /**
  118. * the refs which existed during the last call to {@link #repack()}. This is
  119. * needed during {@link #prune(Set)} where we can optimize by looking at the
  120. * difference between the current refs and the refs which existed during
  121. * last {@link #repack()}.
  122. */
  123. private Collection<Ref> lastPackedRefs;
  124. /**
  125. * Holds the starting time of the last repack() execution. This is needed in
  126. * prune() to inspect only those reflog entries which have been added since
  127. * last repack().
  128. */
  129. private long lastRepackTime;
  130. /**
  131. * Creates a new garbage collector with default values. An expirationTime of
  132. * two weeks and <code>null</code> as progress monitor will be used.
  133. *
  134. * @param repo
  135. * the repo to work on
  136. */
  137. public GC(FileRepository repo) {
  138. this.repo = repo;
  139. this.pm = NullProgressMonitor.INSTANCE;
  140. }
  141. /**
  142. * Runs a garbage collector on a {@link FileRepository}. It will
  143. * <ul>
  144. * <li>pack loose references into packed-refs</li>
  145. * <li>repack all reachable objects into new pack files and delete the old
  146. * pack files</li>
  147. * <li>prune all loose objects which are now reachable by packs</li>
  148. * </ul>
  149. *
  150. * @return the collection of {@link PackFile}'s which are newly created
  151. * @throws IOException
  152. * @throws ParseException
  153. * If the configuration parameter "gc.pruneexpire" couldn't be
  154. * parsed
  155. */
  156. public Collection<PackFile> gc() throws IOException, ParseException {
  157. pm.start(6 /* tasks */);
  158. packRefs();
  159. // TODO: implement reflog_expire(pm, repo);
  160. Collection<PackFile> newPacks = repack();
  161. prune(Collections.<ObjectId> emptySet());
  162. // TODO: implement rerere_gc(pm);
  163. return newPacks;
  164. }
  165. /**
  166. * Delete old pack files. What is 'old' is defined by specifying a set of
  167. * old pack files and a set of new pack files. Each pack file contained in
  168. * old pack files but not contained in new pack files will be deleted. If an
  169. * expirationDate is set then pack files which are younger than the
  170. * expirationDate will not be deleted.
  171. *
  172. * @param oldPacks
  173. * @param newPacks
  174. * @throws ParseException
  175. * @throws IOException
  176. */
  177. private void deleteOldPacks(Collection<PackFile> oldPacks,
  178. Collection<PackFile> newPacks) throws ParseException, IOException {
  179. long packExpireDate = getPackExpireDate();
  180. oldPackLoop: for (PackFile oldPack : oldPacks) {
  181. String oldName = oldPack.getPackName();
  182. // check whether an old pack file is also among the list of new
  183. // pack files. Then we must not delete it.
  184. for (PackFile newPack : newPacks)
  185. if (oldName.equals(newPack.getPackName()))
  186. continue oldPackLoop;
  187. if (!oldPack.shouldBeKept()
  188. && repo.getFS().lastModified(
  189. oldPack.getPackFile()) < packExpireDate) {
  190. oldPack.close();
  191. prunePack(oldName);
  192. }
  193. }
  194. // close the complete object database. Thats my only chance to force
  195. // rescanning and to detect that certain pack files are now deleted.
  196. repo.getObjectDatabase().close();
  197. }
  198. /**
  199. * Delete files associated with a single pack file. First try to delete the
  200. * ".pack" file because on some platforms the ".pack" file may be locked and
  201. * can't be deleted. In such a case it is better to detect this early and
  202. * give up on deleting files for this packfile. Otherwise we may delete the
  203. * ".index" file and when failing to delete the ".pack" file we are left
  204. * with a ".pack" file without a ".index" file.
  205. *
  206. * @param packName
  207. */
  208. private void prunePack(String packName) {
  209. PackExt[] extensions = PackExt.values();
  210. try {
  211. // Delete the .pack file first and if this fails give up on deleting
  212. // the other files
  213. int deleteOptions = FileUtils.RETRY | FileUtils.SKIP_MISSING;
  214. for (PackExt ext : extensions)
  215. if (PackExt.PACK.equals(ext)) {
  216. File f = nameFor(packName, "." + ext.getExtension()); //$NON-NLS-1$
  217. FileUtils.delete(f, deleteOptions);
  218. break;
  219. }
  220. // The .pack file has been deleted. Delete as many as the other
  221. // files as you can.
  222. deleteOptions |= FileUtils.IGNORE_ERRORS;
  223. for (PackExt ext : extensions) {
  224. if (!PackExt.PACK.equals(ext)) {
  225. File f = nameFor(packName, "." + ext.getExtension()); //$NON-NLS-1$
  226. FileUtils.delete(f, deleteOptions);
  227. }
  228. }
  229. } catch (IOException e) {
  230. // Deletion of the .pack file failed. Silently return.
  231. }
  232. }
  233. /**
  234. * Like "git prune-packed" this method tries to prune all loose objects
  235. * which can be found in packs. If certain objects can't be pruned (e.g.
  236. * because the filesystem delete operation fails) this is silently ignored.
  237. *
  238. * @throws IOException
  239. */
  240. public void prunePacked() throws IOException {
  241. ObjectDirectory objdb = repo.getObjectDatabase();
  242. Collection<PackFile> packs = objdb.getPacks();
  243. File objects = repo.getObjectsDirectory();
  244. String[] fanout = objects.list();
  245. if (fanout != null && fanout.length > 0) {
  246. pm.beginTask(JGitText.get().pruneLoosePackedObjects, fanout.length);
  247. try {
  248. for (String d : fanout) {
  249. pm.update(1);
  250. if (d.length() != 2)
  251. continue;
  252. String[] entries = new File(objects, d).list();
  253. if (entries == null)
  254. continue;
  255. for (String e : entries) {
  256. if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  257. continue;
  258. ObjectId id;
  259. try {
  260. id = ObjectId.fromString(d + e);
  261. } catch (IllegalArgumentException notAnObject) {
  262. // ignoring the file that does not represent loose
  263. // object
  264. continue;
  265. }
  266. boolean found = false;
  267. for (PackFile p : packs)
  268. if (p.hasObject(id)) {
  269. found = true;
  270. break;
  271. }
  272. if (found)
  273. FileUtils.delete(objdb.fileFor(id), FileUtils.RETRY
  274. | FileUtils.SKIP_MISSING
  275. | FileUtils.IGNORE_ERRORS);
  276. }
  277. }
  278. } finally {
  279. pm.endTask();
  280. }
  281. }
  282. }
  283. /**
  284. * Like "git prune" this method tries to prune all loose objects which are
  285. * unreferenced. If certain objects can't be pruned (e.g. because the
  286. * filesystem delete operation fails) this is silently ignored.
  287. *
  288. * @param objectsToKeep
  289. * a set of objects which should explicitly not be pruned
  290. *
  291. * @throws IOException
  292. * @throws ParseException
  293. * If the configuration parameter "gc.pruneexpire" couldn't be
  294. * parsed
  295. */
  296. public void prune(Set<ObjectId> objectsToKeep) throws IOException,
  297. ParseException {
  298. long expireDate = getExpireDate();
  299. // Collect all loose objects which are old enough, not referenced from
  300. // the index and not in objectsToKeep
  301. Map<ObjectId, File> deletionCandidates = new HashMap<ObjectId, File>();
  302. Set<ObjectId> indexObjects = null;
  303. File objects = repo.getObjectsDirectory();
  304. String[] fanout = objects.list();
  305. if (fanout != null && fanout.length > 0) {
  306. pm.beginTask(JGitText.get().pruneLooseUnreferencedObjects,
  307. fanout.length);
  308. try {
  309. for (String d : fanout) {
  310. pm.update(1);
  311. if (d.length() != 2)
  312. continue;
  313. File[] entries = new File(objects, d).listFiles();
  314. if (entries == null)
  315. continue;
  316. for (File f : entries) {
  317. String fName = f.getName();
  318. if (fName.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  319. continue;
  320. if (repo.getFS().lastModified(f) >= expireDate)
  321. continue;
  322. try {
  323. ObjectId id = ObjectId.fromString(d + fName);
  324. if (objectsToKeep.contains(id))
  325. continue;
  326. if (indexObjects == null)
  327. indexObjects = listNonHEADIndexObjects();
  328. if (indexObjects.contains(id))
  329. continue;
  330. deletionCandidates.put(id, f);
  331. } catch (IllegalArgumentException notAnObject) {
  332. // ignoring the file that does not represent loose
  333. // object
  334. continue;
  335. }
  336. }
  337. }
  338. } finally {
  339. pm.endTask();
  340. }
  341. }
  342. if (deletionCandidates.isEmpty())
  343. return;
  344. // From the set of current refs remove all those which have been handled
  345. // during last repack(). Only those refs will survive which have been
  346. // added or modified since the last repack. Only these can save existing
  347. // loose refs from being pruned.
  348. Collection<Ref> newRefs;
  349. if (lastPackedRefs == null || lastPackedRefs.isEmpty())
  350. newRefs = getAllRefs();
  351. else {
  352. Map<String, Ref> last = new HashMap<>();
  353. for (Ref r : lastPackedRefs) {
  354. last.put(r.getName(), r);
  355. }
  356. newRefs = new ArrayList<>();
  357. for (Ref r : getAllRefs()) {
  358. Ref old = last.get(r.getName());
  359. if (!equals(r, old)) {
  360. newRefs.add(r);
  361. }
  362. }
  363. }
  364. if (!newRefs.isEmpty()) {
  365. // There are new/modified refs! Check which loose objects are now
  366. // referenced by these modified refs (or their reflogentries).
  367. // Remove these loose objects
  368. // from the deletionCandidates. When the last candidate is removed
  369. // leave this method.
  370. ObjectWalk w = new ObjectWalk(repo);
  371. try {
  372. for (Ref cr : newRefs)
  373. w.markStart(w.parseAny(cr.getObjectId()));
  374. if (lastPackedRefs != null)
  375. for (Ref lpr : lastPackedRefs)
  376. w.markUninteresting(w.parseAny(lpr.getObjectId()));
  377. removeReferenced(deletionCandidates, w);
  378. } finally {
  379. w.dispose();
  380. }
  381. }
  382. if (deletionCandidates.isEmpty())
  383. return;
  384. // Since we have not left the method yet there are still
  385. // deletionCandidates. Last chance for these objects not to be pruned is
  386. // that they are referenced by reflog entries. Even refs which currently
  387. // point to the same object as during last repack() may have
  388. // additional reflog entries not handled during last repack()
  389. ObjectWalk w = new ObjectWalk(repo);
  390. try {
  391. for (Ref ar : getAllRefs())
  392. for (ObjectId id : listRefLogObjects(ar, lastRepackTime))
  393. w.markStart(w.parseAny(id));
  394. if (lastPackedRefs != null)
  395. for (Ref lpr : lastPackedRefs)
  396. w.markUninteresting(w.parseAny(lpr.getObjectId()));
  397. removeReferenced(deletionCandidates, w);
  398. } finally {
  399. w.dispose();
  400. }
  401. if (deletionCandidates.isEmpty())
  402. return;
  403. // delete all candidates which have survived: these are unreferenced
  404. // loose objects
  405. for (File f : deletionCandidates.values())
  406. f.delete();
  407. repo.getObjectDatabase().close();
  408. }
  409. private long getExpireDate() throws ParseException {
  410. long expireDate = Long.MAX_VALUE;
  411. if (expire == null && expireAgeMillis == -1) {
  412. String pruneExpireStr = repo.getConfig().getString(
  413. ConfigConstants.CONFIG_GC_SECTION, null,
  414. ConfigConstants.CONFIG_KEY_PRUNEEXPIRE);
  415. if (pruneExpireStr == null)
  416. pruneExpireStr = PRUNE_EXPIRE_DEFAULT;
  417. expire = GitDateParser.parse(pruneExpireStr, null, SystemReader
  418. .getInstance().getLocale());
  419. expireAgeMillis = -1;
  420. }
  421. if (expire != null)
  422. expireDate = expire.getTime();
  423. if (expireAgeMillis != -1)
  424. expireDate = System.currentTimeMillis() - expireAgeMillis;
  425. return expireDate;
  426. }
  427. private long getPackExpireDate() throws ParseException {
  428. long packExpireDate = Long.MAX_VALUE;
  429. if (packExpire == null && packExpireAgeMillis == -1) {
  430. String prunePackExpireStr = repo.getConfig().getString(
  431. ConfigConstants.CONFIG_GC_SECTION, null,
  432. ConfigConstants.CONFIG_KEY_PRUNEPACKEXPIRE);
  433. if (prunePackExpireStr == null)
  434. prunePackExpireStr = PRUNE_PACK_EXPIRE_DEFAULT;
  435. packExpire = GitDateParser.parse(prunePackExpireStr, null,
  436. SystemReader.getInstance().getLocale());
  437. packExpireAgeMillis = -1;
  438. }
  439. if (packExpire != null)
  440. packExpireDate = packExpire.getTime();
  441. if (packExpireAgeMillis != -1)
  442. packExpireDate = System.currentTimeMillis() - packExpireAgeMillis;
  443. return packExpireDate;
  444. }
  445. /**
  446. * Remove all entries from a map which key is the id of an object referenced
  447. * by the given ObjectWalk
  448. *
  449. * @param id2File
  450. * @param w
  451. * @throws MissingObjectException
  452. * @throws IncorrectObjectTypeException
  453. * @throws IOException
  454. */
  455. private void removeReferenced(Map<ObjectId, File> id2File,
  456. ObjectWalk w) throws MissingObjectException,
  457. IncorrectObjectTypeException, IOException {
  458. RevObject ro = w.next();
  459. while (ro != null) {
  460. if (id2File.remove(ro.getId()) != null)
  461. if (id2File.isEmpty())
  462. return;
  463. ro = w.next();
  464. }
  465. ro = w.nextObject();
  466. while (ro != null) {
  467. if (id2File.remove(ro.getId()) != null)
  468. if (id2File.isEmpty())
  469. return;
  470. ro = w.nextObject();
  471. }
  472. }
  473. private static boolean equals(Ref r1, Ref r2) {
  474. if (r1 == null || r2 == null)
  475. return false;
  476. if (r1.isSymbolic()) {
  477. if (!r2.isSymbolic())
  478. return false;
  479. return r1.getTarget().getName().equals(r2.getTarget().getName());
  480. } else {
  481. if (r2.isSymbolic()) {
  482. return false;
  483. }
  484. return Objects.equals(r1.getObjectId(), r2.getObjectId());
  485. }
  486. }
  487. /**
  488. * Packs all non-symbolic, loose refs into packed-refs.
  489. *
  490. * @throws IOException
  491. */
  492. public void packRefs() throws IOException {
  493. Collection<Ref> refs = repo.getRefDatabase().getRefs(Constants.R_REFS).values();
  494. List<String> refsToBePacked = new ArrayList<String>(refs.size());
  495. pm.beginTask(JGitText.get().packRefs, refs.size());
  496. try {
  497. for (Ref ref : refs) {
  498. if (!ref.isSymbolic() && ref.getStorage().isLoose())
  499. refsToBePacked.add(ref.getName());
  500. pm.update(1);
  501. }
  502. ((RefDirectory) repo.getRefDatabase()).pack(refsToBePacked);
  503. } finally {
  504. pm.endTask();
  505. }
  506. }
  507. /**
  508. * Packs all objects which reachable from any of the heads into one pack
  509. * file. Additionally all objects which are not reachable from any head but
  510. * which are reachable from any of the other refs (e.g. tags), special refs
  511. * (e.g. FETCH_HEAD) or index are packed into a separate pack file. Objects
  512. * included in pack files which have a .keep file associated are never
  513. * repacked. All old pack files which existed before are deleted.
  514. *
  515. * @return a collection of the newly created pack files
  516. * @throws IOException
  517. * when during reading of refs, index, packfiles, objects,
  518. * reflog-entries or during writing to the packfiles
  519. * {@link IOException} occurs
  520. */
  521. public Collection<PackFile> repack() throws IOException {
  522. Collection<PackFile> toBeDeleted = repo.getObjectDatabase().getPacks();
  523. long time = System.currentTimeMillis();
  524. Collection<Ref> refsBefore = getAllRefs();
  525. Set<ObjectId> allHeads = new HashSet<ObjectId>();
  526. Set<ObjectId> nonHeads = new HashSet<ObjectId>();
  527. Set<ObjectId> txnHeads = new HashSet<ObjectId>();
  528. Set<ObjectId> tagTargets = new HashSet<ObjectId>();
  529. Set<ObjectId> indexObjects = listNonHEADIndexObjects();
  530. RefDatabase refdb = repo.getRefDatabase();
  531. for (Ref ref : refsBefore) {
  532. nonHeads.addAll(listRefLogObjects(ref, 0));
  533. if (ref.isSymbolic() || ref.getObjectId() == null)
  534. continue;
  535. if (ref.getName().startsWith(Constants.R_HEADS))
  536. allHeads.add(ref.getObjectId());
  537. else if (RefTreeNames.isRefTree(refdb, ref.getName()))
  538. txnHeads.add(ref.getObjectId());
  539. else
  540. nonHeads.add(ref.getObjectId());
  541. if (ref.getPeeledObjectId() != null)
  542. tagTargets.add(ref.getPeeledObjectId());
  543. }
  544. List<ObjectIdSet> excluded = new LinkedList<ObjectIdSet>();
  545. for (final PackFile f : repo.getObjectDatabase().getPacks())
  546. if (f.shouldBeKept())
  547. excluded.add(f.getIndex());
  548. tagTargets.addAll(allHeads);
  549. nonHeads.addAll(indexObjects);
  550. List<PackFile> ret = new ArrayList<PackFile>(2);
  551. PackFile heads = null;
  552. if (!allHeads.isEmpty()) {
  553. heads = writePack(allHeads, Collections.<ObjectId> emptySet(),
  554. tagTargets, excluded);
  555. if (heads != null) {
  556. ret.add(heads);
  557. excluded.add(0, heads.getIndex());
  558. }
  559. }
  560. if (!nonHeads.isEmpty()) {
  561. PackFile rest = writePack(nonHeads, allHeads, tagTargets, excluded);
  562. if (rest != null)
  563. ret.add(rest);
  564. }
  565. if (!txnHeads.isEmpty()) {
  566. PackFile txn = writePack(txnHeads, PackWriter.NONE, null, excluded);
  567. if (txn != null)
  568. ret.add(txn);
  569. }
  570. try {
  571. deleteOldPacks(toBeDeleted, ret);
  572. } catch (ParseException e) {
  573. // TODO: the exception has to be wrapped into an IOException because
  574. // throwing the ParseException directly would break the API, instead
  575. // we should throw a ConfigInvalidException
  576. throw new IOException(e);
  577. }
  578. prunePacked();
  579. lastPackedRefs = refsBefore;
  580. lastRepackTime = time;
  581. return ret;
  582. }
  583. /**
  584. * @param ref
  585. * the ref which log should be inspected
  586. * @param minTime only reflog entries not older then this time are processed
  587. * @return the {@link ObjectId}s contained in the reflog
  588. * @throws IOException
  589. */
  590. private Set<ObjectId> listRefLogObjects(Ref ref, long minTime) throws IOException {
  591. ReflogReader reflogReader = repo.getReflogReader(ref.getName());
  592. if (reflogReader == null) {
  593. return Collections.emptySet();
  594. }
  595. List<ReflogEntry> rlEntries = reflogReader
  596. .getReverseEntries();
  597. if (rlEntries == null || rlEntries.isEmpty())
  598. return Collections.<ObjectId> emptySet();
  599. Set<ObjectId> ret = new HashSet<ObjectId>();
  600. for (ReflogEntry e : rlEntries) {
  601. if (e.getWho().getWhen().getTime() < minTime)
  602. break;
  603. ObjectId newId = e.getNewId();
  604. if (newId != null && !ObjectId.zeroId().equals(newId))
  605. ret.add(newId);
  606. ObjectId oldId = e.getOldId();
  607. if (oldId != null && !ObjectId.zeroId().equals(oldId))
  608. ret.add(oldId);
  609. }
  610. return ret;
  611. }
  612. /**
  613. * Returns a collection of all refs and additional refs.
  614. *
  615. * Additional refs which don't start with "refs/" are not returned because
  616. * they should not save objects from being garbage collected. Examples for
  617. * such references are ORIG_HEAD, MERGE_HEAD, FETCH_HEAD and
  618. * CHERRY_PICK_HEAD.
  619. *
  620. * @return a collection of refs pointing to live objects.
  621. * @throws IOException
  622. */
  623. private Collection<Ref> getAllRefs() throws IOException {
  624. RefDatabase refdb = repo.getRefDatabase();
  625. Collection<Ref> refs = refdb.getRefs(RefDatabase.ALL).values();
  626. List<Ref> addl = refdb.getAdditionalRefs();
  627. if (!addl.isEmpty()) {
  628. List<Ref> all = new ArrayList<>(refs.size() + addl.size());
  629. all.addAll(refs);
  630. // add additional refs which start with refs/
  631. for (Ref r : addl) {
  632. if (r.getName().startsWith(Constants.R_REFS)) {
  633. all.add(r);
  634. }
  635. }
  636. return all;
  637. }
  638. return refs;
  639. }
  640. /**
  641. * Return a list of those objects in the index which differ from whats in
  642. * HEAD
  643. *
  644. * @return a set of ObjectIds of changed objects in the index
  645. * @throws IOException
  646. * @throws CorruptObjectException
  647. * @throws NoWorkTreeException
  648. */
  649. private Set<ObjectId> listNonHEADIndexObjects()
  650. throws CorruptObjectException, IOException {
  651. if (repo.isBare()) {
  652. return Collections.emptySet();
  653. }
  654. try (TreeWalk treeWalk = new TreeWalk(repo)) {
  655. treeWalk.addTree(new DirCacheIterator(repo.readDirCache()));
  656. ObjectId headID = repo.resolve(Constants.HEAD);
  657. if (headID != null) {
  658. try (RevWalk revWalk = new RevWalk(repo)) {
  659. treeWalk.addTree(revWalk.parseTree(headID));
  660. }
  661. }
  662. treeWalk.setFilter(TreeFilter.ANY_DIFF);
  663. treeWalk.setRecursive(true);
  664. Set<ObjectId> ret = new HashSet<ObjectId>();
  665. while (treeWalk.next()) {
  666. ObjectId objectId = treeWalk.getObjectId(0);
  667. switch (treeWalk.getRawMode(0) & FileMode.TYPE_MASK) {
  668. case FileMode.TYPE_MISSING:
  669. case FileMode.TYPE_GITLINK:
  670. continue;
  671. case FileMode.TYPE_TREE:
  672. case FileMode.TYPE_FILE:
  673. case FileMode.TYPE_SYMLINK:
  674. ret.add(objectId);
  675. continue;
  676. default:
  677. throw new IOException(MessageFormat.format(
  678. JGitText.get().corruptObjectInvalidMode3,
  679. String.format("%o", //$NON-NLS-1$
  680. Integer.valueOf(treeWalk.getRawMode(0))),
  681. (objectId == null) ? "null" : objectId.name(), //$NON-NLS-1$
  682. treeWalk.getPathString(), //
  683. repo.getIndexFile()));
  684. }
  685. }
  686. return ret;
  687. }
  688. }
  689. private PackFile writePack(@NonNull Set<? extends ObjectId> want,
  690. @NonNull Set<? extends ObjectId> have, Set<ObjectId> tagTargets,
  691. List<ObjectIdSet> excludeObjects) throws IOException {
  692. File tmpPack = null;
  693. Map<PackExt, File> tmpExts = new TreeMap<PackExt, File>(
  694. new Comparator<PackExt>() {
  695. public int compare(PackExt o1, PackExt o2) {
  696. // INDEX entries must be returned last, so the pack
  697. // scanner does pick up the new pack until all the
  698. // PackExt entries have been written.
  699. if (o1 == o2)
  700. return 0;
  701. if (o1 == PackExt.INDEX)
  702. return 1;
  703. if (o2 == PackExt.INDEX)
  704. return -1;
  705. return Integer.signum(o1.hashCode() - o2.hashCode());
  706. }
  707. });
  708. try (PackWriter pw = new PackWriter(
  709. (pconfig == null) ? new PackConfig(repo) : pconfig,
  710. repo.newObjectReader())) {
  711. // prepare the PackWriter
  712. pw.setDeltaBaseAsOffset(true);
  713. pw.setReuseDeltaCommits(false);
  714. if (tagTargets != null)
  715. pw.setTagTargets(tagTargets);
  716. if (excludeObjects != null)
  717. for (ObjectIdSet idx : excludeObjects)
  718. pw.excludeObjects(idx);
  719. pw.preparePack(pm, want, have);
  720. if (pw.getObjectCount() == 0)
  721. return null;
  722. // create temporary files
  723. String id = pw.computeName().getName();
  724. File packdir = new File(repo.getObjectsDirectory(), "pack"); //$NON-NLS-1$
  725. tmpPack = File.createTempFile("gc_", ".pack_tmp", packdir); //$NON-NLS-1$ //$NON-NLS-2$
  726. final String tmpBase = tmpPack.getName()
  727. .substring(0, tmpPack.getName().lastIndexOf('.'));
  728. File tmpIdx = new File(packdir, tmpBase + ".idx_tmp"); //$NON-NLS-1$
  729. tmpExts.put(INDEX, tmpIdx);
  730. if (!tmpIdx.createNewFile())
  731. throw new IOException(MessageFormat.format(
  732. JGitText.get().cannotCreateIndexfile, tmpIdx.getPath()));
  733. // write the packfile
  734. FileOutputStream fos = new FileOutputStream(tmpPack);
  735. FileChannel channel = fos.getChannel();
  736. OutputStream channelStream = Channels.newOutputStream(channel);
  737. try {
  738. pw.writePack(pm, pm, channelStream);
  739. } finally {
  740. channel.force(true);
  741. channelStream.close();
  742. fos.close();
  743. }
  744. // write the packindex
  745. fos = new FileOutputStream(tmpIdx);
  746. FileChannel idxChannel = fos.getChannel();
  747. OutputStream idxStream = Channels.newOutputStream(idxChannel);
  748. try {
  749. pw.writeIndex(idxStream);
  750. } finally {
  751. idxChannel.force(true);
  752. idxStream.close();
  753. fos.close();
  754. }
  755. if (pw.prepareBitmapIndex(pm)) {
  756. File tmpBitmapIdx = new File(packdir, tmpBase + ".bitmap_tmp"); //$NON-NLS-1$
  757. tmpExts.put(BITMAP_INDEX, tmpBitmapIdx);
  758. if (!tmpBitmapIdx.createNewFile())
  759. throw new IOException(MessageFormat.format(
  760. JGitText.get().cannotCreateIndexfile,
  761. tmpBitmapIdx.getPath()));
  762. fos = new FileOutputStream(tmpBitmapIdx);
  763. idxChannel = fos.getChannel();
  764. idxStream = Channels.newOutputStream(idxChannel);
  765. try {
  766. pw.writeBitmapIndex(idxStream);
  767. } finally {
  768. idxChannel.force(true);
  769. idxStream.close();
  770. fos.close();
  771. }
  772. }
  773. // rename the temporary files to real files
  774. File realPack = nameFor(id, ".pack"); //$NON-NLS-1$
  775. // if the packfile already exists (because we are rewriting a
  776. // packfile for the same set of objects maybe with different
  777. // PackConfig) then make sure we get rid of all handles on the file.
  778. // Windows will not allow for rename otherwise.
  779. if (realPack.exists())
  780. for (PackFile p : repo.getObjectDatabase().getPacks())
  781. if (realPack.getPath().equals(p.getPackFile().getPath())) {
  782. p.close();
  783. break;
  784. }
  785. tmpPack.setReadOnly();
  786. FileUtils.rename(tmpPack, realPack, StandardCopyOption.ATOMIC_MOVE);
  787. for (Map.Entry<PackExt, File> tmpEntry : tmpExts.entrySet()) {
  788. File tmpExt = tmpEntry.getValue();
  789. tmpExt.setReadOnly();
  790. File realExt = nameFor(id,
  791. "." + tmpEntry.getKey().getExtension()); //$NON-NLS-1$
  792. try {
  793. FileUtils.rename(tmpExt, realExt,
  794. StandardCopyOption.ATOMIC_MOVE);
  795. } catch (IOException e) {
  796. File newExt = new File(realExt.getParentFile(),
  797. realExt.getName() + ".new"); //$NON-NLS-1$
  798. try {
  799. FileUtils.rename(tmpExt, newExt,
  800. StandardCopyOption.ATOMIC_MOVE);
  801. } catch (IOException e2) {
  802. newExt = tmpExt;
  803. e = e2;
  804. }
  805. throw new IOException(MessageFormat.format(
  806. JGitText.get().panicCantRenameIndexFile, newExt,
  807. realExt), e);
  808. }
  809. }
  810. return repo.getObjectDatabase().openPack(realPack);
  811. } finally {
  812. if (tmpPack != null && tmpPack.exists())
  813. tmpPack.delete();
  814. for (File tmpExt : tmpExts.values()) {
  815. if (tmpExt.exists())
  816. tmpExt.delete();
  817. }
  818. }
  819. }
  820. private File nameFor(String name, String ext) {
  821. File packdir = new File(repo.getObjectsDirectory(), "pack"); //$NON-NLS-1$
  822. return new File(packdir, "pack-" + name + ext); //$NON-NLS-1$
  823. }
  824. /**
  825. * A class holding statistical data for a FileRepository regarding how many
  826. * objects are stored as loose or packed objects
  827. */
  828. public class RepoStatistics {
  829. /**
  830. * The number of objects stored in pack files. If the same object is
  831. * stored in multiple pack files then it is counted as often as it
  832. * occurs in pack files.
  833. */
  834. public long numberOfPackedObjects;
  835. /**
  836. * The number of pack files
  837. */
  838. public long numberOfPackFiles;
  839. /**
  840. * The number of objects stored as loose objects.
  841. */
  842. public long numberOfLooseObjects;
  843. /**
  844. * The sum of the sizes of all files used to persist loose objects.
  845. */
  846. public long sizeOfLooseObjects;
  847. /**
  848. * The sum of the sizes of all pack files.
  849. */
  850. public long sizeOfPackedObjects;
  851. /**
  852. * The number of loose refs.
  853. */
  854. public long numberOfLooseRefs;
  855. /**
  856. * The number of refs stored in pack files.
  857. */
  858. public long numberOfPackedRefs;
  859. /**
  860. * The number of bitmaps in the bitmap indices.
  861. */
  862. public long numberOfBitmaps;
  863. public String toString() {
  864. final StringBuilder b = new StringBuilder();
  865. b.append("numberOfPackedObjects=").append(numberOfPackedObjects); //$NON-NLS-1$
  866. b.append(", numberOfPackFiles=").append(numberOfPackFiles); //$NON-NLS-1$
  867. b.append(", numberOfLooseObjects=").append(numberOfLooseObjects); //$NON-NLS-1$
  868. b.append(", numberOfLooseRefs=").append(numberOfLooseRefs); //$NON-NLS-1$
  869. b.append(", numberOfPackedRefs=").append(numberOfPackedRefs); //$NON-NLS-1$
  870. b.append(", sizeOfLooseObjects=").append(sizeOfLooseObjects); //$NON-NLS-1$
  871. b.append(", sizeOfPackedObjects=").append(sizeOfPackedObjects); //$NON-NLS-1$
  872. b.append(", numberOfBitmaps=").append(numberOfBitmaps); //$NON-NLS-1$
  873. return b.toString();
  874. }
  875. }
  876. /**
  877. * Returns information about objects and pack files for a FileRepository.
  878. *
  879. * @return information about objects and pack files for a FileRepository
  880. * @throws IOException
  881. */
  882. public RepoStatistics getStatistics() throws IOException {
  883. RepoStatistics ret = new RepoStatistics();
  884. Collection<PackFile> packs = repo.getObjectDatabase().getPacks();
  885. for (PackFile f : packs) {
  886. ret.numberOfPackedObjects += f.getIndex().getObjectCount();
  887. ret.numberOfPackFiles++;
  888. ret.sizeOfPackedObjects += f.getPackFile().length();
  889. if (f.getBitmapIndex() != null)
  890. ret.numberOfBitmaps += f.getBitmapIndex().getBitmapCount();
  891. }
  892. File objDir = repo.getObjectsDirectory();
  893. String[] fanout = objDir.list();
  894. if (fanout != null && fanout.length > 0) {
  895. for (String d : fanout) {
  896. if (d.length() != 2)
  897. continue;
  898. File[] entries = new File(objDir, d).listFiles();
  899. if (entries == null)
  900. continue;
  901. for (File f : entries) {
  902. if (f.getName().length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  903. continue;
  904. ret.numberOfLooseObjects++;
  905. ret.sizeOfLooseObjects += f.length();
  906. }
  907. }
  908. }
  909. RefDatabase refDb = repo.getRefDatabase();
  910. for (Ref r : refDb.getRefs(RefDatabase.ALL).values()) {
  911. Storage storage = r.getStorage();
  912. if (storage == Storage.LOOSE || storage == Storage.LOOSE_PACKED)
  913. ret.numberOfLooseRefs++;
  914. if (storage == Storage.PACKED || storage == Storage.LOOSE_PACKED)
  915. ret.numberOfPackedRefs++;
  916. }
  917. return ret;
  918. }
  919. /**
  920. * Set the progress monitor used for garbage collection methods.
  921. *
  922. * @param pm
  923. * @return this
  924. */
  925. public GC setProgressMonitor(ProgressMonitor pm) {
  926. this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
  927. return this;
  928. }
  929. /**
  930. * During gc() or prune() each unreferenced, loose object which has been
  931. * created or modified in the last <code>expireAgeMillis</code> milliseconds
  932. * will not be pruned. Only older objects may be pruned. If set to 0 then
  933. * every object is a candidate for pruning.
  934. *
  935. * @param expireAgeMillis
  936. * minimal age of objects to be pruned in milliseconds.
  937. */
  938. public void setExpireAgeMillis(long expireAgeMillis) {
  939. this.expireAgeMillis = expireAgeMillis;
  940. expire = null;
  941. }
  942. /**
  943. * During gc() or prune() packfiles which are created or modified in the
  944. * last <code>packExpireAgeMillis</code> milliseconds will not be deleted.
  945. * Only older packfiles may be deleted. If set to 0 then every packfile is a
  946. * candidate for deletion.
  947. *
  948. * @param packExpireAgeMillis
  949. * minimal age of packfiles to be deleted in milliseconds.
  950. */
  951. public void setPackExpireAgeMillis(long packExpireAgeMillis) {
  952. this.packExpireAgeMillis = packExpireAgeMillis;
  953. expire = null;
  954. }
  955. /**
  956. * Set the PackConfig used when (re-)writing packfiles. This allows to
  957. * influence how packs are written and to implement something similar to
  958. * "git gc --aggressive"
  959. *
  960. * @since 3.6
  961. * @param pconfig
  962. * the {@link PackConfig} used when writing packs
  963. */
  964. public void setPackConfig(PackConfig pconfig) {
  965. this.pconfig = pconfig;
  966. }
  967. /**
  968. * During gc() or prune() each unreferenced, loose object which has been
  969. * created or modified after or at <code>expire</code> will not be pruned.
  970. * Only older objects may be pruned. If set to null then every object is a
  971. * candidate for pruning.
  972. *
  973. * @param expire
  974. * instant in time which defines object expiration
  975. * objects with modification time before this instant are expired
  976. * objects with modification time newer or equal to this instant
  977. * are not expired
  978. */
  979. public void setExpire(Date expire) {
  980. this.expire = expire;
  981. expireAgeMillis = -1;
  982. }
  983. /**
  984. * During gc() or prune() packfiles which are created or modified after or
  985. * at <code>packExpire</code> will not be deleted. Only older packfiles may
  986. * be deleted. If set to null then every packfile is a candidate for
  987. * deletion.
  988. *
  989. * @param packExpire
  990. * instant in time which defines packfile expiration
  991. */
  992. public void setPackExpire(Date packExpire) {
  993. this.packExpire = packExpire;
  994. packExpireAgeMillis = -1;
  995. }
  996. }