Issue
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?
Solution
UPDATED 04-01-2022 to trim contents to remove (trailing) whitespace.
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 variables 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 = trim(file_get_contents($file));
if($HEAD_hash === $contents)
{
exit('Current tag is ' . basename($file));
}
}
exit('No matching tag');
Answered By - Harry B
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.