ihue ihue - 1 year ago 70
PHP Question

Grab the current commit of a project and display it


I was working on a project where QAs report bugs that already fixed.
So many time, it turn out that QAs don’t have our latest code.


Now, I’m thinking to grab the current commit and display it in the
or on other page for references between us QA, and Developers.


In CLI, to get the latest commit, I just to do this

git rev-parse --verify HEAD

I’ve tried create a PHP function, and call it.

public function info(){
return str_replace('\n','', json_encode(shell_exec('git rev-parse --verify HEAD')));


It works fine on my Mac, but on my QA set up, they got


What did I do wrong ?

Is there another way to do this ?

I’m open to any suggestions at the moment. :)

Answer Source

git expects to be called from Git working directory tree as presumably it was when you executed it from the command line. PHP scripts initially set the PWD parent working directory environment variable to the directory in which the script is located and that's the same for command execution (exec, shell_exec, system, passthru). You can change directory from within PHP using chdir or change the command to

cd /path/to/git/repo && git rev-parse --verify HEAD
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download