Class GC
- java.lang.Object
-
- org.eclipse.jgit.internal.storage.file.GC
-
public class GC extends Object
A garbage collector for gitFileRepository
. 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 static 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 Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Collection<Pack>>
gc()
Runs a garbage collector on aFileRepository
.GC.RepoStatistics
getStatistics()
Returns information about objects and pack files for a FileRepository.void
packRefs()
Pack ref storage.void
prune(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.Collection<Pack>
repack()
Packs all objects which reachable from any of the heads into one pack file.void
setAuto(boolean auto)
Set thegc --auto
option.static void
setExecutor(ExecutorService e)
Set the executor for running auto-gc in the background.void
setExpire(Date expire)
During gc() or prune() each unreferenced, loose object which has been created or modified after or atexpire
will not be pruned.void
setExpireAgeMillis(long expireAgeMillis)
During gc() or prune() each unreferenced, loose object which has been created or modified in the lastexpireAgeMillis
milliseconds will not be pruned.void
setPackConfig(PackConfig pconfig)
Set the PackConfig used when (re-)writing packfiles.void
setPackExpire(Date packExpire)
During gc() or prune() packfiles which are created or modified after or atpackExpire
will not be deleted.void
setPackExpireAgeMillis(long packExpireAgeMillis)
During gc() or prune() packfiles which are created or modified in the lastpackExpireAgeMillis
milliseconds will not be deleted.GC
setProgressMonitor(ProgressMonitor pm)
Set the progress monitor used for garbage collection methods.
-
-
-
Constructor Detail
-
GC
public GC(FileRepository repo)
Creates a new garbage collector with default values. An expirationTime of two weeks andnull
as progress monitor will be used.- Parameters:
repo
- the repo to work on
-
-
Method Detail
-
setExecutor
public static void setExecutor(ExecutorService e)
Set the executor for running auto-gc in the background. If no executor is set JGit's own WorkQueue will be used.- Parameters:
e
- the executor to be used for running auto-gc
-
gc
public CompletableFuture<Collection<Pack>> gc() throws IOException, ParseException
Runs a garbage collector on aFileRepository
. 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
setAuto(boolean)
was set totrue
gc
will first check whether any housekeeping is required; if not, it exits without performing any work. IfsetBackground(boolean)
was set totrue
collectGarbage
will start the gc in the background, and then return immediately. In this case, errors will not be reported except in gc.log.- Returns:
- the collection of
Pack
's which are newly created - Throws:
IOException
ParseException
- If the configuration parameter "gc.pruneexpire" couldn't be parsed
-
prunePacked
public void prunePacked() throws 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:
IOException
-
prune
public void prune(Set<ObjectId> objectsToKeep) throws IOException, 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:
IOException
ParseException
- If the configuration parameter "gc.pruneexpire" couldn't be parsed
-
packRefs
public void packRefs() throws IOException
Pack ref storage. For a RefDirectory database, this packs all non-symbolic, loose refs into packed-refs. For Reftable, all of the data is compacted into a single table.- Throws:
IOException
-
repack
public Collection<Pack> repack() throws 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:
IOException
- when during reading of refs, index, packfiles, objects, reflog-entries or during writing to the packfilesIOException
occurs
-
getStatistics
public GC.RepoStatistics getStatistics() throws IOException
Returns information about objects and pack files for a FileRepository.- Returns:
- information about objects and pack files for a FileRepository
- Throws:
IOException
-
setProgressMonitor
public GC setProgressMonitor(ProgressMonitor pm)
Set the progress monitor used for garbage collection methods.- Parameters:
pm
- aProgressMonitor
object.- Returns:
- this
-
setExpireAgeMillis
public void setExpireAgeMillis(long expireAgeMillis)
During gc() or prune() each unreferenced, loose object which has been created or modified in the lastexpireAgeMillis
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.
-
setPackExpireAgeMillis
public void setPackExpireAgeMillis(long packExpireAgeMillis)
During gc() or prune() packfiles which are created or modified in the lastpackExpireAgeMillis
milliseconds will not be deleted. Only older packfiles may be deleted. If set to 0 then every packfile is a candidate for deletion.- Parameters:
packExpireAgeMillis
- minimal age of packfiles to be deleted in milliseconds.
-
setPackConfig
public void setPackConfig(@NonNull 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
- thePackConfig
used when writing packs
-
setExpire
public void setExpire(Date expire)
During gc() or prune() each unreferenced, loose object which has been created or modified after or atexpire
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
-
setPackExpire
public void setPackExpire(Date packExpire)
During gc() or prune() packfiles which are created or modified after or atpackExpire
will not be deleted. Only older packfiles may be deleted. If set to null then every packfile is a candidate for deletion.- Parameters:
packExpire
- instant in time which defines packfile expiration
-
setAuto
public void setAuto(boolean auto)
Set thegc --auto
option. With this option, gc checks whether any housekeeping is required; if not, it exits without performing any work. Some JGit commands rungc --auto
after performing operations that could create many loose objects.Housekeeping is required if there are too many loose objects or too many packs in the repository. If the number of loose objects exceeds the value of the gc.auto option JGit GC consolidates all existing packs into a single pack (equivalent to
-A
option), whereas git-core would combine all loose objects into a single pack usingrepack -d -l
. Setting the value ofgc.auto
to 0 disables automatic packing of loose objects.If the number of packs exceeds the value of
gc.autoPackLimit
, then existing packs (except those marked with a .keep file) are consolidated into a single pack by using the-A
option of repack. Settinggc.autoPackLimit
to 0 disables automatic consolidation of packs.Like git the following jgit commands run auto gc:
- fetch
- merge
- rebase
- receive-pack
receive.autogc = false
- Parameters:
auto
- defines whether gc should do automatic housekeeping
-
-