MrO*_*MrO 18 php versioning git github
我想在我的网站上显示git版本号.
我发现它是一个提交哈希,这不适合非技术用户参考.
我创建了这个类来显示这个脚本当前git本地的版本'number'.
Jen*_*och 45
首先,一些git获取版本信息的命令:
git log --pretty="%H" -n1 HEAD git log --pretty="%h" -n1 HEAD git log --pretty="%ci" -n1 HEADgit describe --tags --abbrev=0git describe --tags其次,只需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)
Run Code Online (Sandbox Code Playgroud)
MrO*_*MrO 14
要点: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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果你不想这样做exec()而且你正在使用git标记:
您可以从.git/HEAD或获取当前的HEAD提交哈希.git/refs/heads/master.然后我们循环找到匹配.首先反转数组以获得速度,因为您更有可能使用更高的最新标记.
因此,如果当前的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';
Run Code Online (Sandbox Code Playgroud)