在PHP中显示当前的Git'版本' [英] Display the current Git 'version' in PHP

查看:170
本文介绍了在PHP中显示当前的Git'版本'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上显示Git版本.

I want to display the Git version on my site.

如何显示Git的语义版本号,以便站点的非技术用户在提出问题时可以轻松参考?

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

推荐答案

首先,一些git命令用于获取版本信息:

Firstly, some git commands to fetch version information:

  • 长时间提交哈希
    • git log --pretty="%H" -n1 HEAD
    • commit hash long
      • git log --pretty="%H" -n1 HEAD
      • git log --pretty="%h" -n1 HEAD
      • git log --pretty="%ci" -n1 HEAD
      • git describe --tags --abbrev=0
      • git describe --tags

      第二,将exec()与上面选择的git命令结合使用以构建版本标识符:

      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)
      

      这篇关于在PHP中显示当前的Git'版本'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆