Reading a Git commit message from PHP Reading a Git commit message from PHP php php

Reading a Git commit message from PHP


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:

Indefero

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.

Get a changelog list: 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);}

Get a particular commit: 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];}

VersionControl_Git from PEAR

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 curling 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.