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

Monday, January 17, 2022

[FIXED] Display the current Git 'version' in PHP

 January 17, 2022     git, github, php, versioning     No comments   

Issue

I want to display the Git version on my site.

How can I display a semantic version number from Git, that non-technical users of a site can easily reference when raising issues?


Solution

Firstly, some git commands to fetch version information:

  • commit hash long
    • git log --pretty="%H" -n1 HEAD
  • commit hash short
    • git log --pretty="%h" -n1 HEAD
  • commit date
    • git log --pretty="%ci" -n1 HEAD
  • tag
    • git describe --tags --abbrev=0
  • tag long with hash
    • git describe --tags

Secondly, use exec() combined with the git commands of your choice from above to build the version identifier:

class ApplicationVersion
{
    const MAJOR = 1;
    const MINOR = 2;
    const PATCH = 3;

    public static function get()
    {
        $commitHash = trim(exec('git log --pretty="%h" -n1 HEAD'));

        $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD')));
        $commitDate->setTimezone(new \DateTimeZone('UTC'));

        return sprintf('v%s.%s.%s-dev.%s (%s)', self::MAJOR, self::MINOR, self::PATCH, $commitHash, $commitDate->format('Y-m-d H:i:s'));
    }
}

// Usage: echo 'MyApplication ' . ApplicationVersion::get();

// MyApplication v1.2.3-dev.b576fd7 (2016-11-02 14:11:22)


Answered By - Jens A. Koch
  • 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