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.
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?
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.
Obviously replacing the exit
s with variabled and spitting it to the page will give you a better result.
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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.