PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] How do you display the active git tag with php without exec / shell_exec

 January 17, 2022     git, php     No comments   

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 exits 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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing