ihue ihue - 2 months ago 11
PHP Question

Grab the current commit of a project and display it

Issue



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.




Solution



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




Tried



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')));
}





Result



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




Questions



What did I do wrong ?

Is there another way to do this ?

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

Answer

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
Comments