My website reports on the Git status (current commit and branch, mostly, and also git ls-files --modified to show any files which have been modified directly on the server) so I can check at a glance whether it's up to date. This used to work, but the latest Git does not allow these things to be read by a user which does not own the repo.

The command git config --global --add safe.directory {$dir} works in principle, but does not work for www-data, because it has no $HOME, so there's nowhere for the .gitconfig file to be created. The response is fatal: $HOME not set.

Is there any straightforward way to allow Apache to read a few facts about the Git repo? This is all PHP code using exec() to run commands such as git branch --show-current and git log -1 --pretty=%H, among others.

1 Answer 1


You can set environment variables. So, setting GIT_CONFIG_GLOBAL to point to your modified configuration should solve the "no-$home problem".

Using -c key=value, you can set configuration options for every git call separately, which should allow you to work like you want to, as well.

However, hm. You're running your PHP as a user which has read privileges on private files, it seems! Either, you're running a shared repository as a dedicated user, in which case it'd seem more appropriate to run your PHP interpreter as that user. Or, you give your apache-run PHP interpreter read access to your private files. That's basically not OK for anything public – having a wrapper script that really only does one job (namely, calling git with exactly the right options and maybe a branch name passed as argument) that you add to your /etc/sudoers so that your PHP can sudo that (and only that) as the repo-owning user, but not otherwise read data, does seem wiser.

I'd like to add that, if you're exposing this publicly,

This is all PHP code using exec() to run commands such as git branch --show-current and git log -1 --pretty=%H

does sound like a lot of potential for remote code execution. You'd probably be better off using a library that does that for you instead!

  • The commands are hard-coded, not user input. And that section of code is never run unless you're (a) on an unusual page on the site, and (b) logged in as an administrator. I know that exec() is not recommended, but I think this is safe enough.
    – TRiG
    Commented May 16, 2022 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .