I'm looking for a way to read a Git commit message with PHP. I suspect I need to use a Git hook, but I've never worked with them before, so I need a push in the right direction. Specifically, I'd like to implement the following process:
If at all possible, I'd like to stick with pure PHP. If there are tutorials or references that you could point out, that would be a huge help.
Display current/latest git commit message The -s flag (or --no-patch ) is optional. However, it is specified here to suppress the diff output — which is shown by the git show command by default in addition to the full commit details. The %s in --format=<format> option refers to the "subject" line of the commit output.
If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .
To view information about a merge commitA commit specifier for the destination for the merge (with the --destination-commit-specifier option). The merge option you want to use (with the --merge-option option). The name of the repository (with the --repository-name option).
By default, git-show acts against the HEAD reference. The HEAD reference always points to the last commit of the current branch. Therefore, you can use git-show to display the log message and diff output of the latest commit.
To get the commit hash, you can use
git rev-parse --verify HEAD 2> /dev/null
From within php:
exec('git rev-parse --verify HEAD 2> /dev/null', $output);
$hash = $output[0];
You can get the commit message, author and time (though - the time will simply be "now" if it's run as part of a post-commit hook) with:
exec("git show $hash", $output);
If it's not obvious, whatever you do with php is simply going to be a wrapper around the things you'd do with git on the cli - I.e. any "how can I do x with git from php" is just exec('the git answer', $output)
As far as using PHP to extract the correct commit:
There is a project called Indefero that is a PHP forge tool that has an SCM connector for git. You could easily use their git class as an API for yourself. You can just grab the git class and the SCM class.
I have, for example, pulled out two methods from the class below, which I think are the most relevant to you so you can see how they work.
getChangeLog()
/**
* Get latest changes.
*
* @param string Commit ('HEAD').
* @param int Number of changes (10).
* @return array Changes.
*/
public function getChangeLog($commit='HEAD', $n=10)
{
if ($n === null) $n = '';
else $n = ' -'.$n;
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log%s --date=iso --pretty=format:\'%s\' %s',
escapeshellarg($this->repo), $n, $this->mediumtree_fmt,
escapeshellarg($commit));
$out = array();
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
self::exec('IDF_Scm_Git::getChangeLog', $cmd, $out);
return self::parseLog($out);
}
getCommit()
/**
* Get commit details.
*
* @param string Commit
* @param bool Get commit diff (false)
* @return array Changes
*/
public function getCommit($commit, $getdiff=false)
{
if ($getdiff) {
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' show --date=iso --pretty=format:%s %s',
escapeshellarg($this->repo),
"'".$this->mediumtree_fmt."'",
escapeshellarg($commit));
} else {
$cmd = sprintf('GIT_DIR=%s '.Pluf::f('git_path', 'git').' log -1 --date=iso --pretty=format:%s %s',
escapeshellarg($this->repo),
"'".$this->mediumtree_fmt."'",
escapeshellarg($commit));
}
$out = array();
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
self::exec('IDF_Scm_Git::getCommit', $cmd, $out, $ret);
if ($ret != 0 or count($out) == 0) {
return false;
}
if ($getdiff) {
$log = array();
$change = array();
$inchange = false;
foreach ($out as $line) {
if (!$inchange and 0 === strpos($line, 'diff --git a')) {
$inchange = true;
}
if ($inchange) {
$change[] = $line;
} else {
$log[] = $line;
}
}
$out = self::parseLog($log);
$out[0]->diff = implode("\n", $change);
} else {
$out = self::parseLog($out);
$out[0]->diff = '';
}
$out[0]->branch = implode(', ', $this->inBranches($commit, null));
return $out[0];
}
There is also a library in PEAR called VersionControl_Git that would be helpful in this situation and is documented.
As @Pawel mentioned, you're going to want to work with hooks on this:
On localhost you can navigate to /.git/hooks and rename post-commit.sample to post-commit and then put inside #!/usr/bin/php There are also other hooks that may be more suitable for you.
Git will look for the post-commit
hook after you've commit and automatically run anything inside.
What you're going to want to do here depends on the task, but I'd suggest curl
ing the script - here's where things get interesting.
In order to extract the information you're looking for, you're going to want to use git log -1
- that should pull back the latest commit.
More specifically, you'll want to build your commit using the --pretty=format
toggle, which can output the latest commit with the info you need. Check out this string:
git log -1 --pretty=format:'%h - %cn (%ce) - %s (%ci)'
This would return most of the things you are looking for. Check out the git-log page to see all of the different %
variables that you can use with --pretty=format:
. Once you've made the string that you'd like, you can either POST
those via cURL to a PHP script, or run a PHP script and use shell_exec(git log -1 --pretty=format:'%h - %cn (%ce) - %s (%ci)')
to work with the commit message inline.
I was digging in the same question and found out a way to do it faster and easier.
To get just the commit message you could use
git rev-list --format=%B --max-count=1 HEAD
Obviously HEAD
may be replaced with any commit hash.
It will output something like
commit 4152601a42270440ad52680ac7c66ba87a506174
Improved migrations and models relations
Second line is what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With