在 PHP 中显示当前的 Git 'version'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16334310/
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
Display the current Git 'version' in PHP
提问by lukeocodes
I want to display the Git version on my site.
我想在我的网站上显示 Git 版本。
How can I display a semantic version number from Git, that non-technical users of a site can easily reference when raising issues?
如何显示来自 Git 的语义版本号,网站的非技术用户在提出问题时可以轻松参考?
回答by Jens A. Koch
Firstly, some git
commands to fetch version information:
首先是一些git
获取版本信息的命令:
- commit hash long
git log --pretty="%H" -n1 HEAD
- commit hash short
git log --pretty="%h" -n1 HEAD
- commit date
git log --pretty="%ci" -n1 HEAD
- tag
git describe --tags --abbrev=0
- tag long with hash
git describe --tags
- 提交哈希长
git log --pretty="%H" -n1 HEAD
- 提交哈希短
git log --pretty="%h" -n1 HEAD
- 提交日期
git log --pretty="%ci" -n1 HEAD
- 标签
git describe --tags --abbrev=0
- 用哈希标记 long
git describe --tags
Secondly, use exec()
combined with the git commands of your choice from above to build the version identifier:
其次,exec()
结合您从上面选择的 git 命令来构建版本标识符:
class ApplicationVersion
{
const MAJOR = 1;
const MINOR = 2;
const PATCH = 3;
public static function get()
{
$commitHash = trim(exec('git log --pretty="%h" -n1 HEAD'));
$commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD')));
$commitDate->setTimezone(new \DateTimeZone('UTC'));
return sprintf('v%s.%s.%s-dev.%s (%s)', self::MAJOR, self::MINOR, self::PATCH, $commitHash, $commitDate->format('Y-m-d H:i:s'));
}
}
// Usage: echo 'MyApplication ' . ApplicationVersion::get();
// MyApplication v1.2.3-dev.b576fd7 (2016-11-02 14:11:22)
回答by lukeocodes
Gist: https://gist.github.com/lukeoliff/5501074
要点:https: //gist.github.com/lukeoliff/5501074
<?php
class QuickGit {
public static function version() {
exec('git describe --always',$version_mini_hash);
exec('git rev-list HEAD | wc -l',$version_number);
exec('git log -1',$line);
$version['short'] = "v1.".trim($version_number[0]).".".$version_mini_hash[0];
$version['full'] = "v1.".trim($version_number[0]).".$version_mini_hash[0] (".str_replace('commit ','',$line[0]).")";
return $version;
}
}
回答by Harry B
If you'd like to do it without exec()
and you're using git lightweight (see comments below) tagging:
如果您不想这样做exec()
并且您正在使用 git 轻量级(请参阅下面的评论)标记:
You can get the current HEAD commit hash from .git/HEAD
or .git/refs/heads/master
. We then loop to find matching. Reversing the array first for speed because you're more likely to at a higher recent tag.
您可以从.git/HEAD
或获取当前的 HEAD 提交哈希.git/refs/heads/master
。然后我们循环查找匹配项。首先反转数组以提高速度,因为您更有可能使用更高的最近标签。
So if the current php file sits in a public_html
or www
folder one level down from the .git
folder...
因此,如果当前的 php 文件位于文件夹下一级的public_html
或www
文件.git
夹中...
<?php
$HEAD_hash = file_get_contents('../.git/refs/heads/master'); // or branch x
$files = glob('../.git/refs/tags/*');
foreach(array_reverse($files) as $file) {
$contents = file_get_contents($file);
if($HEAD_hash === $contents)
{
print 'Current tag is ' . basename($file);
exit;
}
}
print 'No matching tag';
回答by Sergio Rodrigues
Simple way:
简单的方法:
- Short hash:
$rev = exec('git rev-parse --short HEAD');
- Full hash:
$rev = exec('git rev-parse HEAD');
- 短哈希:
$rev = exec('git rev-parse --short HEAD');
- 完整哈希:
$rev = exec('git rev-parse HEAD');
回答by Virtimus
I did it just as:
我是这样做的:
substr(file_get_contents(GIT_DIR.'/refs/heads/master'),0,7)
substr(file_get_contents(GIT_DIR.'/refs/heads/master'),0,7)
resource friendly and the same as i have under eclipse shown
资源友好,与我在 eclipse 下显示的相同