Display the current Git 'version' in PHP

前端 未结 6 699
醉话见心
醉话见心 2021-01-30 11:37

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

相关标签:
6条回答
  • 2021-01-30 12:07

    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)
    
    0 讨论(0)
  • 2021-01-30 12:13

    Run git tag in terminal to preview your tags and say you got i.e:

    v1.0.0
    v1.1.0
    v1.2.4
    

    here's how to get the latest version v1.2.4

    function getVersion() {
      $hash = exec("git rev-list --tags --max-count=1");
      return exec("git describe --tags $hash"); 
    }
    echo getVersion(); // "v1.2.4"
    

    Coincidentally (if your tags are ordered), since exec returns only the last row we could just do:

    function getVersion() {
      return exec("git tag");
    }
    echo getVersion(); // "v1.2.4"
    

    To get all the rows string use shell_exec:

    function getVersions() {
      return shell_exec("git tag");
    }
    echo getVersions(); // "v1.0.0
                        // v1.1.0
                        // v1.2.4"
    

    To get an Array:

    $tagsArray = explode(PHP_EOL, shell_exec("git tag"));
    

    To sort tags by date:

    git tag --sort=committerdate
    

    Docs: git-for-each-ref#_field_names

    For sorting purposes, fields with numeric values sort in numeric order (objectsize, authordate, committerdate, creatordate, taggerdate). All other fields are used to sort in their byte-value order.

    0 讨论(0)
  • 2021-01-30 12:15

    Simple way:

    • Short hash: $rev = exec('git rev-parse --short HEAD');
    • Full hash: $rev = exec('git rev-parse HEAD');
    0 讨论(0)
  • 2021-01-30 12:18

    I did it just as:

    substr(file_get_contents(GIT_DIR.'/refs/heads/master'),0,7)

    resource friendly and the same as i have under eclipse shown

    0 讨论(0)
  • 2021-01-30 12:22

    Gist: https://gist.github.com/lukeoliff/5501074

    <?php
    
    class QuickGit {
    
      public static function version() {
        exec('git describe --always',$version_mini_hash);
        exec('git rev-list HEAD | wc -l',$version_number);
        exec('git log -1',$line);
        $version['short'] = "v1.".trim($version_number[0]).".".$version_mini_hash[0];
        $version['full'] = "v1.".trim($version_number[0]).".$version_mini_hash[0] (".str_replace('commit ','',$line[0]).")";
        return $version;
      }
    
    }
    
    0 讨论(0)
  • 2021-01-30 12:22

    If you'd like to do it without exec() and you're using git lightweight (see comments below) tagging:

    You can get the current HEAD commit hash from .git/HEAD or .git/refs/heads/master. We then loop to find matching. Reversing the array first for speed because you're more likely to at a higher recent tag.

    So if the current 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)
        {
            print 'Current tag is ' . basename($file);
            exit;
        }
    }
    
    print 'No matching tag';
    
    0 讨论(0)
提交回复
热议问题