假设当前签出了一个标记,并且.git文件夹位于站点代码库的根目录中,我想将该标记包含在使用php的页面呈现的html中。
我已经看到了使用shell_exec/shell的解决方案,但假设我无法使用这两个函数,我如何才能获得当前检查出标签的标签。例如v1.2.3
单独使用php ?
更新04-01-2022修剪内容以删除(尾随)空白。
您可以从.git/HEAD
获得当前HEAD提交哈希值。然后,您可以循环所有标记引用以找到匹配的提交散列。我们首先反转数组,因为你更有可能出现在最近的标签上,而不是旧的标签上。
显然,用变量替换exit
s并将其放到页面中会给您带来更好的结果。
所以如果你的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 = trim(file_get_contents($file));
if($HEAD_hash === $contents)
{
exit('Current tag is ' . basename($file));
}
}
exit('No matching tag');