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

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