问题描述
假设当前已签出标签,并且.git文件夹位于站点代码库的根目录中,我想使用php将标签包含在页面的呈现html中.
Assuming a tag is currently checked out, and the .git folder sits in the root of the site's codebase, I'd like to include the tag in the rendered html of a page using php.
我已经看到了使用shell_exec/shell的解决方案,但是假设我无法使用这两个函数,如何获得当前已检出标签的标签.例如v1.2.3
仅使用php吗?
I've seen solutions which make use of shell_exec / shell, but assuming I'm unable to use those two functions, how can I get the label of the currently checked out tag. For example v1.2.3
using php alone?
推荐答案
您可以从.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');
这篇关于如何在没有exec/shell_exec的情况下使用php显示活动的git标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!