更新时间:2022-05-30 21:49:48
您可以从.git/HEAD
获取当前HEAD提交哈希.然后,您可以循环所有标记引用以查找匹配的提交哈希.我们首先将阵列反转,因为您比以前的旧标签更有可能使用最新标签.
You can get the current HEAD commit hash from .git/HEAD
. You can then loop all tag refs to find a matching commit hash. We reverse the array first as you're more likely to be on a recent tag than an old one.
显然,用变量替换exit
并将其吐到页面上会带来更好的结果.
Obviously replacing the exit
s with variabled and spitting it to the page will give you a better result.
因此,如果您的php文件位于.git
文件夹下一级的public_html
或www
文件夹中...
So if your php file sits in a public_html
or www
folder one level down from the .git
folder...
<?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)
{
exit('Current tag is ' . basename($file));
}
}
exit('No matching tag');