从 PHP 读取 Git 提交消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9087883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a Git commit message from PHP
提问by VirtuosiMedia
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 pushin the right direction. Specifically, I'd like to implement the following process:
我正在寻找一种使用 PHP 读取 Git 提交消息的方法。我怀疑我需要使用一个Git挂钩,但我从来没有与他们合作过,所以我需要一个推在正确的方向。具体来说,我想实现以下过程:
- A PHP script is executed automatically after every commit
- The script captures the Git username, the time of the commit, and the commit content
- 每次提交后都会自动执行一个 PHP 脚本
- 脚本捕获 Git 用户名、提交时间和提交内容
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.
如果可能的话,我想坚持使用纯 PHP。如果有您可以指出的教程或参考资料,那将是一个巨大的帮助。
采纳答案by AD7six
To get the commit hash, you can use
要获取提交哈希,您可以使用
git rev-parse --verify HEAD 2> /dev/null
From within php:
从 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)
如果这不是很明显,那么无论您使用 php 做什么,都只是围绕您在 cli 上使用 git 所做的事情的包装器 - 即任何“我如何使用 php 中的 git 执行 x”只是 exec('the git answer', $output)
回答by Treffynnon
As far as using PHP to extract the correct commit:
至于使用 PHP 提取正确的提交:
Indefero
阴天
There is a project called Indeferothat is a PHP forge tool that has an SCM connector for git. You could easily use their git classas an API for yourself. You can just grab the git classand the SCM class.
有一个名为Indefero的项目,它是一个 PHP 伪造工具,具有用于 git的SCM 连接器。您可以轻松地将他们的 git 类用作自己的 API。您可以只获取git 类和SCM 类。
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()
获取变更日志列表: 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()
获取特定提交: 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_Gitfrom PEAR
来自 PEAR 的VersionControl_Git
There is also a library in PEAR called VersionControl_Gitthat would be helpful in this situation and is documented.
PEAR 中还有一个名为VersionControl_Git的库,它在这种情况下会有所帮助,并已记录在案。
回答by Nic
As @Pawel mentioned, you're going to want to work with hooks on this:
正如@Pawel 所提到的,你会想要在这方面使用钩子:
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/hooks 并将 post-commit.sample 重命名为 post-commit,然后放入 #!/usr/bin/php 还有其他一些可能更适合您的钩子。
Git will look for the post-commit
hook after you've commit and automatically run anything inside.
Git 会post-commit
在你提交后寻找钩子并自动运行里面的任何东西。
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.
你在这里想要做什么取决于任务,但我建议curl
编写脚本 - 这就是事情变得有趣的地方。
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.
为了提取您正在寻找的信息,您将要使用git log -1
- 这应该拉回最新的提交。
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:
更具体地说,您需要使用--pretty=format
切换来构建您的提交,它可以输出带有您需要的信息的最新提交。看看这个字符串:
git log -1 --pretty=format:'%h - %cn (%ce) - %s (%ci)'
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-logpage 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.
这将返回您正在寻找的大部分内容。查看git-log页面以查看%
您可以使用的所有不同变量--pretty=format:
。一旦你创建了你想要的字符串,你可以POST
通过 cURL 将这些字符串转换为 PHP 脚本,或者运行一个 PHP 脚本并用于shell_exec(git log -1 --pretty=format:'%h - %cn (%ce) - %s (%ci)')
处理内联提交消息。
回答by phaberest
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.
显然HEAD
可以用任何提交哈希替换。
It will output something like
它会输出类似的东西
commit 4152601a42270440ad52680ac7c66ba87a506174
Improved migrations and models relations
Second line is what you need.
第二行是你需要的。