1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
47 import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
48 import static org.eclipse.jgit.lib.RefDatabase.ALL;
49
50 import java.io.File;
51 import java.io.FileOutputStream;
52 import java.io.IOException;
53 import java.io.OutputStream;
54 import java.nio.channels.Channels;
55 import java.nio.channels.FileChannel;
56 import java.text.MessageFormat;
57 import java.text.ParseException;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.Collections;
61 import java.util.Comparator;
62 import java.util.Date;
63 import java.util.HashMap;
64 import java.util.HashSet;
65 import java.util.Iterator;
66 import java.util.LinkedList;
67 import java.util.List;
68 import java.util.Map;
69 import java.util.Map.Entry;
70 import java.util.Set;
71 import java.util.TreeMap;
72
73 import org.eclipse.jgit.dircache.DirCacheIterator;
74 import org.eclipse.jgit.errors.CorruptObjectException;
75 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
76 import org.eclipse.jgit.errors.MissingObjectException;
77 import org.eclipse.jgit.errors.NoWorkTreeException;
78 import org.eclipse.jgit.internal.JGitText;
79 import org.eclipse.jgit.internal.storage.pack.PackExt;
80 import org.eclipse.jgit.internal.storage.pack.PackWriter;
81 import org.eclipse.jgit.internal.storage.pack.PackWriter.ObjectIdSet;
82 import org.eclipse.jgit.lib.AnyObjectId;
83 import org.eclipse.jgit.lib.ConfigConstants;
84 import org.eclipse.jgit.lib.Constants;
85 import org.eclipse.jgit.lib.FileMode;
86 import org.eclipse.jgit.lib.NullProgressMonitor;
87 import org.eclipse.jgit.lib.ObjectId;
88 import org.eclipse.jgit.lib.ProgressMonitor;
89 import org.eclipse.jgit.lib.Ref;
90 import org.eclipse.jgit.lib.Ref.Storage;
91 import org.eclipse.jgit.lib.RefDatabase;
92 import org.eclipse.jgit.lib.ReflogEntry;
93 import org.eclipse.jgit.revwalk.ObjectWalk;
94 import org.eclipse.jgit.revwalk.RevObject;
95 import org.eclipse.jgit.revwalk.RevWalk;
96 import org.eclipse.jgit.storage.pack.PackConfig;
97 import org.eclipse.jgit.treewalk.TreeWalk;
98 import org.eclipse.jgit.treewalk.filter.TreeFilter;
99 import org.eclipse.jgit.util.FileUtils;
100 import org.eclipse.jgit.util.GitDateParser;
101 import org.eclipse.jgit.util.SystemReader;
102
103
104
105
106
107
108
109
110 public class GC {
111 private static final String PRUNE_EXPIRE_DEFAULT = "2.weeks.ago";
112
113 private final FileRepository repo;
114
115 private ProgressMonitor pm;
116
117 private long expireAgeMillis = -1;
118
119 private Date expire;
120
121 private PackConfig pconfig = null;
122
123
124
125
126
127
128
129 private Map<String, Ref> lastPackedRefs;
130
131
132
133
134
135
136 private long lastRepackTime;
137
138
139
140
141
142
143
144
145 public GC(FileRepository repo) {
146 this.repo = repo;
147 this.pm = NullProgressMonitor.INSTANCE;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public Collection<PackFile> gc() throws IOException, ParseException {
166 pm.start(6 );
167 packRefs();
168
169 Collection<PackFile> newPacks = repack();
170 prune(Collections.<ObjectId> emptySet());
171
172 return newPacks;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186 private void deleteOldPacks(Collection<PackFile> oldPacks,
187 Collection<PackFile> newPacks) throws ParseException {
188 long expireDate = getExpireDate();
189 oldPackLoop: for (PackFile oldPack : oldPacks) {
190 String oldName = oldPack.getPackName();
191
192
193 for (PackFile newPack : newPacks)
194 if (oldName.equals(newPack.getPackName()))
195 continue oldPackLoop;
196
197 if (!oldPack.shouldBeKept()
198 && oldPack.getPackFile().lastModified() < expireDate) {
199 oldPack.close();
200 prunePack(oldName);
201 }
202 }
203
204
205 repo.getObjectDatabase().close();
206 }
207
208
209
210
211
212
213
214
215
216
217
218 private void prunePack(String packName) {
219 PackExt[] extensions = PackExt.values();
220 try {
221
222
223 int deleteOptions = FileUtils.RETRY | FileUtils.SKIP_MISSING;
224 for (PackExt ext : extensions)
225 if (PackExt.PACK.equals(ext)) {
226 File f = nameFor(packName, "." + ext.getExtension());
227 FileUtils.delete(f, deleteOptions);
228 break;
229 }
230
231
232 deleteOptions |= FileUtils.IGNORE_ERRORS;
233 for (PackExt ext : extensions) {
234 if (!PackExt.PACK.equals(ext)) {
235 File f = nameFor(packName, "." + ext.getExtension());
236 FileUtils.delete(f, deleteOptions);
237 }
238 }
239 } catch (IOException e) {
240
241 }
242 }
243
244
245
246
247
248
249
250
251 public void prunePacked() throws IOException {
252 ObjectDirectory objdb = repo.getObjectDatabase();
253 Collection<PackFile> packs = objdb.getPacks();
254 File objects = repo.getObjectsDirectory();
255 String[] fanout = objects.list();
256
257 if (fanout != null && fanout.length > 0) {
258 pm.beginTask(JGitText.get().pruneLoosePackedObjects, fanout.length);
259 try {
260 for (String d : fanout) {
261 pm.update(1);
262 if (d.length() != 2)
263 continue;
264 String[] entries = new File(objects, d).list();
265 if (entries == null)
266 continue;
267 for (String e : entries) {
268 if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
269 continue;
270 ObjectId id;
271 try {
272 id = ObjectId.fromString(d + e);
273 } catch (IllegalArgumentException notAnObject) {
274
275
276 continue;
277 }
278 boolean found = false;
279 for (PackFile p : packs)
280 if (p.hasObject(id)) {
281 found = true;
282 break;
283 }
284 if (found)
285 FileUtils.delete(objdb.fileFor(id), FileUtils.RETRY
286 | FileUtils.SKIP_MISSING
287 | FileUtils.IGNORE_ERRORS);
288 }
289 }
290 } finally {
291 pm.endTask();
292 }
293 }
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 public void prune(Set<ObjectId> objectsToKeep) throws IOException,
310 ParseException {
311 long expireDate = getExpireDate();
312
313
314
315 Map<ObjectId, File> deletionCandidates = new HashMap<ObjectId, File>();
316 Set<ObjectId> indexObjects = null;
317 File objects = repo.getObjectsDirectory();
318 String[] fanout = objects.list();
319 if (fanout != null && fanout.length > 0) {
320 pm.beginTask(JGitText.get().pruneLooseUnreferencedObjects,
321 fanout.length);
322 try {
323 for (String d : fanout) {
324 pm.update(1);
325 if (d.length() != 2)
326 continue;
327 File[] entries = new File(objects, d).listFiles();
328 if (entries == null)
329 continue;
330 for (File f : entries) {
331 String fName = f.getName();
332 if (fName.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
333 continue;
334 if (f.lastModified() >= expireDate)
335 continue;
336 try {
337 ObjectId id = ObjectId.fromString(d + fName);
338 if (objectsToKeep.contains(id))
339 continue;
340 if (indexObjects == null)
341 indexObjects = listNonHEADIndexObjects();
342 if (indexObjects.contains(id))
343 continue;
344 deletionCandidates.put(id, f);
345 } catch (IllegalArgumentException notAnObject) {
346
347
348 continue;
349 }
350 }
351 }
352 } finally {
353 pm.endTask();
354 }
355 }
356 if (deletionCandidates.isEmpty())
357 return;
358
359
360
361
362
363 Map<String, Ref> newRefs;
364 if (lastPackedRefs == null || lastPackedRefs.isEmpty())
365 newRefs = getAllRefs();
366 else {
367 newRefs = new HashMap<String, Ref>();
368 for (Iterator<Map.Entry<String, Ref>> i = getAllRefs().entrySet()
369 .iterator(); i.hasNext();) {
370 Entry<String, Ref> newEntry = i.next();
371 Ref old = lastPackedRefs.get(newEntry.getKey());
372 if (!equals(newEntry.getValue(), old))
373 newRefs.put(newEntry.getKey(), newEntry.getValue());
374 }
375 }
376
377 if (!newRefs.isEmpty()) {
378
379
380
381
382
383 ObjectWalk w = new ObjectWalk(repo);
384 try {
385 for (Ref cr : newRefs.values())
386 w.markStart(w.parseAny(cr.getObjectId()));
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 }
395
396 if (deletionCandidates.isEmpty())
397 return;
398
399
400
401
402
403
404 ObjectWalk w = new ObjectWalk(repo);
405 try {
406 for (Ref ar : getAllRefs().values())
407 for (ObjectId id : listRefLogObjects(ar, lastRepackTime))
408 w.markStart(w.parseAny(id));
409 if (lastPackedRefs != null)
410 for (Ref lpr : lastPackedRefs.values())
411 w.markUninteresting(w.parseAny(lpr.getObjectId()));
412 removeReferenced(deletionCandidates, w);
413 } finally {
414 w.dispose();
415 }
416
417 if (deletionCandidates.isEmpty())
418 return;
419
420
421
422 for (File f : deletionCandidates.values())
423 f.delete();
424
425 repo.getObjectDatabase().close();
426 }
427
428 private long getExpireDate() throws ParseException {
429 long expireDate = Long.MAX_VALUE;
430
431 if (expire == null && expireAgeMillis == -1) {
432 String pruneExpireStr = repo.getConfig().getString(
433 ConfigConstants.CONFIG_GC_SECTION, null,
434 ConfigConstants.CONFIG_KEY_PRUNEEXPIRE);
435 if (pruneExpireStr == null)
436 pruneExpireStr = PRUNE_EXPIRE_DEFAULT;
437 expire = GitDateParser.parse(pruneExpireStr, null, SystemReader
438 .getInstance().getLocale());
439 expireAgeMillis = -1;
440 }
441 if (expire != null)
442 expireDate = expire.getTime();
443 if (expireAgeMillis != -1)
444 expireDate = System.currentTimeMillis() - expireAgeMillis;
445 return expireDate;
446 }
447
448
449
450
451
452
453
454
455
456
457
458 private void removeReferenced(Map<ObjectId, File> id2File,
459 ObjectWalk w) throws MissingObjectException,
460 IncorrectObjectTypeException, IOException {
461 RevObject ro = w.next();
462 while (ro != null) {
463 if (id2File.remove(ro.getId()) != null)
464 if (id2File.isEmpty())
465 return;
466 ro = w.next();
467 }
468 ro = w.nextObject();
469 while (ro != null) {
470 if (id2File.remove(ro.getId()) != null)
471 if (id2File.isEmpty())
472 return;
473 ro = w.nextObject();
474 }
475 }
476
477 private static boolean equals(Ref r1, Ref r2) {
478 if (r1 == null || r2 == null)
479 return false;
480 if (r1.isSymbolic()) {
481 if (!r2.isSymbolic())
482 return false;
483 return r1.getTarget().getName().equals(r2.getTarget().getName());
484 } else {
485 if (r2.isSymbolic())
486 return false;
487 return r1.getObjectId().equals(r2.getObjectId());
488 }
489 }
490
491
492
493
494
495
496 public void packRefs() throws IOException {
497 Collection<Ref> refs = repo.getRefDatabase().getRefs(Constants.R_REFS).values();
498 List<String> refsToBePacked = new ArrayList<String>(refs.size());
499 pm.beginTask(JGitText.get().packRefs, refs.size());
500 try {
501 for (Ref ref : refs) {
502 if (!ref.isSymbolic() && ref.getStorage().isLoose())
503 refsToBePacked.add(ref.getName());
504 pm.update(1);
505 }
506 ((RefDirectory) repo.getRefDatabase()).pack(refsToBePacked);
507 } finally {
508 pm.endTask();
509 }
510 }
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526 public Collection<PackFile> repack() throws IOException {
527 Collection<PackFile> toBeDeleted = repo.getObjectDatabase().getPacks();
528
529 long time = System.currentTimeMillis();
530 Map<String, Ref> refsBefore = getAllRefs();
531
532 Set<ObjectId> allHeads = new HashSet<ObjectId>();
533 Set<ObjectId> nonHeads = new HashSet<ObjectId>();
534 Set<ObjectId> tagTargets = new HashSet<ObjectId>();
535 Set<ObjectId> indexObjects = listNonHEADIndexObjects();
536
537 for (Ref ref : refsBefore.values()) {
538 nonHeads.addAll(listRefLogObjects(ref, 0));
539 if (ref.isSymbolic() || ref.getObjectId() == null)
540 continue;
541 if (ref.getName().startsWith(Constants.R_HEADS))
542 allHeads.add(ref.getObjectId());
543 else
544 nonHeads.add(ref.getObjectId());
545 if (ref.getPeeledObjectId() != null)
546 tagTargets.add(ref.getPeeledObjectId());
547 }
548
549 List<ObjectIdSet> excluded = new LinkedList<ObjectIdSet>();
550 for (final PackFile f : repo.getObjectDatabase().getPacks())
551 if (f.shouldBeKept())
552 excluded.add(objectIdSet(f.getIndex()));
553
554 tagTargets.addAll(allHeads);
555 nonHeads.addAll(indexObjects);
556
557 List<PackFile> ret = new ArrayList<PackFile>(2);
558 PackFile heads = null;
559 if (!allHeads.isEmpty()) {
560 heads = writePack(allHeads, Collections.<ObjectId> emptySet(),
561 tagTargets, excluded);
562 if (heads != null) {
563 ret.add(heads);
564 excluded.add(0, objectIdSet(heads.getIndex()));
565 }
566 }
567 if (!nonHeads.isEmpty()) {
568 PackFile rest = writePack(nonHeads, allHeads, tagTargets, excluded);
569 if (rest != null)
570 ret.add(rest);
571 }
572 try {
573 deleteOldPacks(toBeDeleted, ret);
574 } catch (ParseException e) {
575
576
577
578 throw new IOException(e);
579 }
580 prunePacked();
581
582 lastPackedRefs = refsBefore;
583 lastRepackTime = time;
584 return ret;
585 }
586
587
588
589
590
591
592
593
594 private Set<ObjectId> listRefLogObjects(Ref ref, long minTime) throws IOException {
595 List<ReflogEntry> rlEntries = repo.getReflogReader(ref.getName())
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
614
615
616
617
618
619
620 private Map<String, Ref> getAllRefs() throws IOException {
621 Map<String, Ref> ret = repo.getRefDatabase().getRefs(ALL);
622 for (Ref ref : repo.getRefDatabase().getAdditionalRefs())
623 ret.put(ref.getName(), ref);
624 return ret;
625 }
626
627
628
629
630
631
632
633
634
635
636 private Set<ObjectId> listNonHEADIndexObjects()
637 throws CorruptObjectException, IOException {
638 try {
639 if (repo.getIndexFile() == null)
640 return Collections.emptySet();
641 } catch (NoWorkTreeException e) {
642 return Collections.emptySet();
643 }
644 try (TreeWalk treeWalk = new TreeWalk(repo)) {
645 treeWalk.addTree(new DirCacheIterator(repo.readDirCache()));
646 ObjectId headID = repo.resolve(Constants.HEAD);
647 if (headID != null) {
648 try (RevWalk revWalk = new RevWalk(repo)) {
649 treeWalk.addTree(revWalk.parseTree(headID));
650 }
651 }
652
653 treeWalk.setFilter(TreeFilter.ANY_DIFF);
654 treeWalk.setRecursive(true);
655 Set<ObjectId> ret = new HashSet<ObjectId>();
656
657 while (treeWalk.next()) {
658 ObjectId objectId = treeWalk.getObjectId(0);
659 switch (treeWalk.getRawMode(0) & FileMode.TYPE_MASK) {
660 case FileMode.TYPE_MISSING:
661 case FileMode.TYPE_GITLINK:
662 continue;
663 case FileMode.TYPE_TREE:
664 case FileMode.TYPE_FILE:
665 case FileMode.TYPE_SYMLINK:
666 ret.add(objectId);
667 continue;
668 default:
669 throw new IOException(MessageFormat.format(
670 JGitText.get().corruptObjectInvalidMode3,
671 String.format("%o",
672 Integer.valueOf(treeWalk.getRawMode(0))),
673 (objectId == null) ? "null" : objectId.name(),
674 treeWalk.getPathString(),
675 repo.getIndexFile()));
676 }
677 }
678 return ret;
679 }
680 }
681
682 private PackFile writePack(Set<? extends ObjectId> want,
683 Set<? extends ObjectId> have, Set<ObjectId> tagTargets,
684 List<ObjectIdSet> excludeObjects) throws IOException {
685 File tmpPack = null;
686 Map<PackExt, File> tmpExts = new TreeMap<PackExt, File>(
687 new Comparator<PackExt>() {
688 public int compare(PackExt o1, PackExt o2) {
689
690
691
692 if (o1 == o2)
693 return 0;
694 if (o1 == PackExt.INDEX)
695 return 1;
696 if (o2 == PackExt.INDEX)
697 return -1;
698 return Integer.signum(o1.hashCode() - o2.hashCode());
699 }
700
701 });
702 try (PackWriter pw = new PackWriter(
703 (pconfig == null) ? new PackConfig(repo) : pconfig,
704 repo.newObjectReader())) {
705
706 pw.setDeltaBaseAsOffset(true);
707 pw.setReuseDeltaCommits(false);
708 if (tagTargets != null)
709 pw.setTagTargets(tagTargets);
710 if (excludeObjects != null)
711 for (ObjectIdSet idx : excludeObjects)
712 pw.excludeObjects(idx);
713 pw.preparePack(pm, want, have);
714 if (pw.getObjectCount() == 0)
715 return null;
716
717
718 String id = pw.computeName().getName();
719 File packdir = new File(repo.getObjectsDirectory(), "pack");
720 tmpPack = File.createTempFile("gc_", ".pack_tmp", packdir);
721 final String tmpBase = tmpPack.getName()
722 .substring(0, tmpPack.getName().lastIndexOf('.'));
723 File tmpIdx = new File(packdir, tmpBase + ".idx_tmp");
724 tmpExts.put(INDEX, tmpIdx);
725
726 if (!tmpIdx.createNewFile())
727 throw new IOException(MessageFormat.format(
728 JGitText.get().cannotCreateIndexfile, tmpIdx.getPath()));
729
730
731 FileOutputStream fos = new FileOutputStream(tmpPack);
732 FileChannel channel = fos.getChannel();
733 OutputStream channelStream = Channels.newOutputStream(channel);
734 try {
735 pw.writePack(pm, pm, channelStream);
736 } finally {
737 channel.force(true);
738 channelStream.close();
739 fos.close();
740 }
741
742
743 fos = new FileOutputStream(tmpIdx);
744 FileChannel idxChannel = fos.getChannel();
745 OutputStream idxStream = Channels.newOutputStream(idxChannel);
746 try {
747 pw.writeIndex(idxStream);
748 } finally {
749 idxChannel.force(true);
750 idxStream.close();
751 fos.close();
752 }
753
754 if (pw.prepareBitmapIndex(pm)) {
755 File tmpBitmapIdx = new File(packdir, tmpBase + ".bitmap_tmp");
756 tmpExts.put(BITMAP_INDEX, tmpBitmapIdx);
757
758 if (!tmpBitmapIdx.createNewFile())
759 throw new IOException(MessageFormat.format(
760 JGitText.get().cannotCreateIndexfile,
761 tmpBitmapIdx.getPath()));
762
763 fos = new FileOutputStream(tmpBitmapIdx);
764 idxChannel = fos.getChannel();
765 idxStream = Channels.newOutputStream(idxChannel);
766 try {
767 pw.writeBitmapIndex(idxStream);
768 } finally {
769 idxChannel.force(true);
770 idxStream.close();
771 fos.close();
772 }
773 }
774
775
776 File realPack = nameFor(id, ".pack");
777
778
779
780
781
782 if (realPack.exists())
783 for (PackFile p : repo.getObjectDatabase().getPacks())
784 if (realPack.getPath().equals(p.getPackFile().getPath())) {
785 p.close();
786 break;
787 }
788 tmpPack.setReadOnly();
789 boolean delete = true;
790 try {
791 FileUtils.rename(tmpPack, realPack);
792 delete = false;
793 for (Map.Entry<PackExt, File> tmpEntry : tmpExts.entrySet()) {
794 File tmpExt = tmpEntry.getValue();
795 tmpExt.setReadOnly();
796
797 File realExt = nameFor(
798 id, "." + tmpEntry.getKey().getExtension());
799 try {
800 FileUtils.rename(tmpExt, realExt);
801 } catch (IOException e) {
802 File newExt = new File(realExt.getParentFile(),
803 realExt.getName() + ".new");
804 if (!tmpExt.renameTo(newExt))
805 newExt = tmpExt;
806 throw new IOException(MessageFormat.format(
807 JGitText.get().panicCantRenameIndexFile, newExt,
808 realExt));
809 }
810 }
811
812 } finally {
813 if (delete) {
814 if (tmpPack.exists())
815 tmpPack.delete();
816 for (File tmpExt : tmpExts.values()) {
817 if (tmpExt.exists())
818 tmpExt.delete();
819 }
820 }
821 }
822 return repo.getObjectDatabase().openPack(realPack);
823 } finally {
824 if (tmpPack != null && tmpPack.exists())
825 tmpPack.delete();
826 for (File tmpExt : tmpExts.values()) {
827 if (tmpExt.exists())
828 tmpExt.delete();
829 }
830 }
831 }
832
833 private File nameFor(String name, String ext) {
834 File packdir = new File(repo.getObjectsDirectory(), "pack");
835 return new File(packdir, "pack-" + name + ext);
836 }
837
838
839
840
841
842 public class RepoStatistics {
843
844
845
846
847
848 public long numberOfPackedObjects;
849
850
851
852
853 public long numberOfPackFiles;
854
855
856
857
858 public long numberOfLooseObjects;
859
860
861
862
863 public long sizeOfLooseObjects;
864
865
866
867
868 public long sizeOfPackedObjects;
869
870
871
872
873 public long numberOfLooseRefs;
874
875
876
877
878 public long numberOfPackedRefs;
879
880 public String toString() {
881 final StringBuilder b = new StringBuilder();
882 b.append("numberOfPackedObjects=").append(numberOfPackedObjects);
883 b.append(", numberOfPackFiles=").append(numberOfPackFiles);
884 b.append(", numberOfLooseObjects=").append(numberOfLooseObjects);
885 b.append(", numberOfLooseRefs=").append(numberOfLooseRefs);
886 b.append(", numberOfPackedRefs=").append(numberOfPackedRefs);
887 b.append(", sizeOfLooseObjects=").append(sizeOfLooseObjects);
888 b.append(", sizeOfPackedObjects=").append(sizeOfPackedObjects);
889 return b.toString();
890 }
891 }
892
893
894
895
896
897
898
899
900 public RepoStatistics getStatistics() throws IOException {
901 RepoStatistics ret = new RepoStatistics();
902 Collection<PackFile> packs = repo.getObjectDatabase().getPacks();
903 for (PackFile f : packs) {
904 ret.numberOfPackedObjects += f.getIndex().getObjectCount();
905 ret.numberOfPackFiles++;
906 ret.sizeOfPackedObjects += f.getPackFile().length();
907 }
908 File objDir = repo.getObjectsDirectory();
909 String[] fanout = objDir.list();
910 if (fanout != null && fanout.length > 0) {
911 for (String d : fanout) {
912 if (d.length() != 2)
913 continue;
914 File[] entries = new File(objDir, d).listFiles();
915 if (entries == null)
916 continue;
917 for (File f : entries) {
918 if (f.getName().length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
919 continue;
920 ret.numberOfLooseObjects++;
921 ret.sizeOfLooseObjects += f.length();
922 }
923 }
924 }
925
926 RefDatabase refDb = repo.getRefDatabase();
927 for (Ref r : refDb.getRefs(RefDatabase.ALL).values()) {
928 Storage storage = r.getStorage();
929 if (storage == Storage.LOOSE || storage == Storage.LOOSE_PACKED)
930 ret.numberOfLooseRefs++;
931 if (storage == Storage.PACKED || storage == Storage.LOOSE_PACKED)
932 ret.numberOfPackedRefs++;
933 }
934
935 return ret;
936 }
937
938
939
940
941
942
943
944 public GC setProgressMonitor(ProgressMonitor pm) {
945 this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
946 return this;
947 }
948
949
950
951
952
953
954
955
956
957
958 public void setExpireAgeMillis(long expireAgeMillis) {
959 this.expireAgeMillis = expireAgeMillis;
960 expire = null;
961 }
962
963
964
965
966
967
968
969
970
971
972 public void setPackConfig(PackConfig pconfig) {
973 this.pconfig = pconfig;
974 }
975
976
977
978
979
980
981
982
983
984
985
986
987
988 public void setExpire(Date expire) {
989 this.expire = expire;
990 expireAgeMillis = -1;
991 }
992
993 private static ObjectIdSet objectIdSet(final PackIndex idx) {
994 return new ObjectIdSet() {
995 public boolean contains(AnyObjectId objectId) {
996 return idx.hasObject(objectId);
997 }
998 };
999 }
1000 }