Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jgit-dev] Cannot garbage collect some objects

I am working on a JGit server that stores bare repositories on disk. I'd like to limit the size of these repositories by rejecting pushes that bring a repository over some threshold.

I've written a JGit PreRecevieHook that calculates the size of a bare repository on disk. If a receiving repository becomes too large, the push is rejected. This mostly works great. I've tested by checking in a very large file to a local clone and then attempting to push this file to my server. I see the push is rejected with my helpful error message.

There is one problem. Let's say after my push is rejected, I decide to abandon the recent commit with the new, big file. In theory, I could do this by resetting HEAD to a commit without the file. To remove the file from git entirely, I could run the following commands locally:

git reflog expire --expire-unreachable=now --all

git gc --prune=now


I can confirm the large file is gone because the size of my repository on my local disk is now small.

If I push again, my git server indicates my repository is still too big. I can see that the problem is that, in the bare repository on the server, the large file object is still in git. This file should be unreachable via a ref though, so I should be able to remove it by running garbage collection. Indeed, running git gc --prune=now on the server removes the blob and brings the repository back to a small size. I am then able to push again.

I tried translating this git gc command into a JGit command:

try (Git git = new Git(repository)) {
    GarbageCollectCommand command = git.gc();
    command.setExpire(null);
    try {
        command.call();
    } catch (GitAPIException e) {
        e.printStackTrace();
    }
}

Unfortunately, this did not remove the large file like the CLI version did.

I am on the latest release of JGit. Am I using GarbageCollectCommand incorrectly? Is there a good way to debug why JGit does not think this object can be garbage collected?

Thank you for your time and expertise!

- Jordan Place

Back to the top