Class GC


  • public class GC
    extends java.lang.Object
    A garbage collector for git FileRepository. Instances of this class are not thread-safe. Don't use the same instance from multiple threads. This class started as a copy of DfsGarbageCollector from Shawn O. Pearce adapted to FileRepositories.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  GC.RepoStatistics
      A class holding statistical data for a FileRepository regarding how many objects are stored as loose or packed objects
    • Constructor Summary

      Constructors 
      Constructor Description
      GC​(FileRepository repo)
      Creates a new garbage collector with default values.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Collection<PackFile> gc()
      Runs a garbage collector on a FileRepository.
      GC.RepoStatistics getStatistics()
      Returns the number of objects stored in pack files.
      void packRefs()
      Packs all non-symbolic, loose refs into packed-refs.
      void prune​(java.util.Set<ObjectId> objectsToKeep)
      Like "git prune" this method tries to prune all loose objects which are unreferenced.
      void prunePacked()
      Like "git prune-packed" this method tries to prune all loose objects which can be found in packs.
      java.util.Collection<PackFile> repack()
      Packs all objects which reachable from any of the heads into one pack file.
      void setExpire​(java.util.Date expire)
      During gc() or prune() each unreferenced, loose object which has been created or modified after or at expire will not be pruned.
      void setExpireAgeMillis​(long expireAgeMillis)
      During gc() or prune() each unreferenced, loose object which has been created or modified in the last expireAgeMillis milliseconds will not be pruned.
      void setPackConfig​(PackConfig pconfig)
      Set the PackConfig used when (re-)writing packfiles.
      GC setProgressMonitor​(ProgressMonitor pm)
      Set the progress monitor used for garbage collection methods.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GC

        public GC​(FileRepository repo)
        Creates a new garbage collector with default values. An expirationTime of two weeks and null as progress monitor will be used.
        Parameters:
        repo - the repo to work on
    • Method Detail

      • gc

        public java.util.Collection<PackFile> gc()
                                          throws java.io.IOException,
                                                 java.text.ParseException
        Runs a garbage collector on a FileRepository. It will
        • pack loose references into packed-refs
        • repack all reachable objects into new pack files and delete the old pack files
        • prune all loose objects which are now reachable by packs
        Returns:
        the collection of PackFile's which are newly created
        Throws:
        java.io.IOException
        java.text.ParseException - If the configuration parameter "gc.pruneexpire" couldn't be parsed
      • prunePacked

        public void prunePacked()
                         throws java.io.IOException
        Like "git prune-packed" this method tries to prune all loose objects which can be found in packs. If certain objects can't be pruned (e.g. because the filesystem delete operation fails) this is silently ignored.
        Throws:
        java.io.IOException
      • prune

        public void prune​(java.util.Set<ObjectId> objectsToKeep)
                   throws java.io.IOException,
                          java.text.ParseException
        Like "git prune" this method tries to prune all loose objects which are unreferenced. If certain objects can't be pruned (e.g. because the filesystem delete operation fails) this is silently ignored.
        Parameters:
        objectsToKeep - a set of objects which should explicitly not be pruned
        Throws:
        java.io.IOException
        java.text.ParseException - If the configuration parameter "gc.pruneexpire" couldn't be parsed
      • packRefs

        public void packRefs()
                      throws java.io.IOException
        Packs all non-symbolic, loose refs into packed-refs.
        Throws:
        java.io.IOException
      • repack

        public java.util.Collection<PackFile> repack()
                                              throws java.io.IOException
        Packs all objects which reachable from any of the heads into one pack file. Additionally all objects which are not reachable from any head but which are reachable from any of the other refs (e.g. tags), special refs (e.g. FETCH_HEAD) or index are packed into a separate pack file. Objects included in pack files which have a .keep file associated are never repacked. All old pack files which existed before are deleted.
        Returns:
        a collection of the newly created pack files
        Throws:
        java.io.IOException - when during reading of refs, index, packfiles, objects, reflog-entries or during writing to the packfiles IOException occurs
      • getStatistics

        public GC.RepoStatistics getStatistics()
                                        throws java.io.IOException
        Returns the number of objects stored in pack files. If an object is contained in multiple pack files it is counted as often as it occurs.
        Returns:
        the number of objects stored in pack files
        Throws:
        java.io.IOException
      • setProgressMonitor

        public GC setProgressMonitor​(ProgressMonitor pm)
        Set the progress monitor used for garbage collection methods.
        Parameters:
        pm -
        Returns:
        this
      • setExpireAgeMillis

        public void setExpireAgeMillis​(long expireAgeMillis)
        During gc() or prune() each unreferenced, loose object which has been created or modified in the last expireAgeMillis milliseconds will not be pruned. Only older objects may be pruned. If set to 0 then every object is a candidate for pruning.
        Parameters:
        expireAgeMillis - minimal age of objects to be pruned in milliseconds.
      • setPackConfig

        public void setPackConfig​(PackConfig pconfig)
        Set the PackConfig used when (re-)writing packfiles. This allows to influence how packs are written and to implement something similar to "git gc --aggressive"
        Parameters:
        pconfig - the PackConfig used when writing packs
        Since:
        3.6
      • setExpire

        public void setExpire​(java.util.Date expire)
        During gc() or prune() each unreferenced, loose object which has been created or modified after or at expire will not be pruned. Only older objects may be pruned. If set to null then every object is a candidate for pruning.
        Parameters:
        expire - instant in time which defines object expiration objects with modification time before this instant are expired objects with modification time newer or equal to this instant are not expired