git push errors “insufficient permission for adding an object to repository” and “unpack failed: unpack-objects abnormal exit”

We occasionally have this error when pushing to a git repository:

$ git push
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.14 KiB, done.
Total 4 (delta 3), reused 0 (delta 0)
error: insufficient permission for adding an object to repository database ./objects

fatal: failed to write object
error: unpack failed: unpack-objects abnormal exit
To zzzzzz@xxxxx.libpf.com:/var/git/cases.git
 ! [remote rejected] test_qqqqq -> test_qqqqq (n/a (unpacker error))
error: failed to push some refs to
'zzzzzz@xxxxx.libpf.com:/var/git/cases.git'

This hints to some server-side permission problem. This is how we troubleshooted it.

First we scan the repository premissions:

find /var/git -type f | xargs ls -l  | awk '{print $1}' | sort | uniq

We had set the setgid for all directories in /var/git (“Setting the setgid permission on a directory (chmod g+s) causes new files and subdirectories created within it to inherit its group ID, rather than the primary group ID of the user who created the file (the owner ID is never affected, only the group ID). Newly created subdirectories inherit the setgid bit.“)  so we had expected all files to be “-rwxrwx—“, but this what we got:

-r--r--r--
-rw-r--r--
-rwxrwx---

Somebody had been writing files to the repositories with the wrong permissions ! But who ? This is how we found out:

find /var/git -type f | xargs ls -l  | grep 'r--'

A long list of files came out with the wrong permissions. The other users are not allowed to overwrite these files, potentially causing similar errors in the future.

The problem can be brute-force fixed with this command:

cd /var/git
chmod -R 770 *

But this will not prevent it from happening again. The true solution is to set the git config variable “core.sharedRepository” for all repositories:

cd /var/git/aaaaaa.git
git config core.sharedrepository true

etc. ect.

Hope it helps !