I'm writing a pre-receive hook for GitLab, based on this question ( https://stackoverflow.com/questions/47791509/how-to-prevent-a-git-repository-growing-above-a-maximum-size?noredirect =1&lq=1 ), which blocks repository updates if they exceed a size limit.
The problem is that the command they use there to know the size of the repository ( git count-objects -v
), does not return the actual size of the repository.
When comparing the size returned by that command, with the one shown by the GitLab API, I see that the GitLab API shows a different value (usually higher) than the one shown by this command.
The size shown by git-count
in the pre-receive is this (see error line)
While GitLab shows this other size
One way to get it would be by calling the API itself. But, for that, I would need to know the name and owner of the project I am in. And it doesn't seem trivial
how could i get the actual size of the project?
Probably the way to calculate the size in GitLab is something specific or proprietary, it will depend on factors that only GitLab knows, however, a reliable way to know how much the project occupies in gitLab is using its own REST API:
The output, among other data, should contain an object like this:
You have more information in the projects API doc: https://docs.gitlab.com/ee/api/projects.html#list-all-projects
Finally, I have discovered the problem:
The size that GitLab shows is a measure that is calculated in the statistics, but it is not updated on each push, but is recalculated every certain time interval. Therefore, the size that GitLab shows does not always correspond to the real size of the repository.
Doing a
du -sb
in the directory where the repository is located on the server, I can get the real size of it (which corresponds to the one that GitLab shows after its tracking, after some time). The pre-receive is executed on top of that same directory, so it's simply a matter of running the command in the script.But, when calculating it in the pre-receive, this size also includes the new object that we want to upload.
Still, for the time being, I've stuck with calculating the size using
du -sb