[英]Display the current Git 'version' in PHP
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 的语义版本号,网站的非技术用户在提出问题时可以轻松参考?
Firstly, some git
commands to fetch version information:首先,一些git
命令来获取版本信息:
git log --pretty="%H" -n1 HEAD
git log --pretty="%h" -n1 HEAD
git log --pretty="%ci" -n1 HEAD
git describe --tags --abbrev=0
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)
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;
}
}
If you'd like to do it without exec()
and you're using git lightweight (see comments below) tagging:如果您想在没有exec()
情况下exec()
并且您正在使用 git 轻量级(请参阅下面的评论)标记:
You can get the current HEAD commit hash from .git/HEAD
or .git/refs/heads/master
.您可以从.git/HEAD
或.git/refs/heads/master
获取当前的 HEAD 提交哈希。 We then loop to find matching.然后我们循环查找匹配项。 Reversing the array first for speed because you're more likely to at a higher recent tag.首先反转数组以提高速度,因为您更有可能使用更高的最近标签。
So if the current php file sits in a public_html
or www
folder one level down from the .git
folder...因此,如果当前的 php 文件位于.git
文件夹下一级的public_html
或www
文件夹中......
<?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';
Simple way:简单的方法:
$rev = exec('git rev-parse --short HEAD');
短哈希: $rev = exec('git rev-parse --short HEAD');
$rev = exec('git rev-parse HEAD');
完整哈希: $rev = exec('git rev-parse HEAD');
I did it just as:我是这样做的:
substr(file_get_contents(GIT_DIR.'/refs/heads/master'),0,7)
resource friendly and the same as i have under eclipse shown资源友好,与我在 eclipse 下显示的相同
Run git tag
in terminal to preview your tags and say you got ie:在终端中运行git tag
来预览你的标签并说你得到了 ie:
v1.0.0
v1.1.0
v1.2.4
here's how to get the latest version v1.2.4
以下是获取最新版本v1.2.4
function getVersion() {
$hash = exec("git rev-list --tags --max-count=1");
return exec("git describe --tags $hash");
}
echo getVersion(); // "v1.2.4"
Coincidentally (if your tags are ordered), since exec
returns only the last row we could just do:巧合的是(如果你的标签是有序的),因为exec
只返回最后一行,我们可以这样做:
function getVersion() {
return exec("git tag");
}
echo getVersion(); // "v1.2.4"
To get all the rows string use shell_exec
:要获取所有行字符串,请使用shell_exec
:
function getVersions() {
return shell_exec("git tag");
}
echo getVersions(); // "v1.0.0
// v1.1.0
// v1.2.4"
To get an Array:获取数组:
$tagsArray = explode(PHP_EOL, shell_exec("git tag"));
To sort tags by date:按日期对标签进行排序:
git tag --sort=committerdate
Docs: git-for-each-ref#_field_names文档:git-for-each-ref#_field_names
For sorting purposes, fields with numeric values sort in numeric order (objectsize, authordate, committerdate, creatordate, taggerdate).出于排序目的,具有数值的字段按数字顺序排序(对象大小、作者日期、提交者日期、创建者日期、标记日期)。 All other fields are used to sort in their byte-value order.所有其他字段都用于按其字节值顺序进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.