gpt4 book ai didi

php - 在 PHP 中显示当前 Git 'version'

转载 作者:IT王子 更新时间:2023-10-29 00:14:05 27 4
gpt4 key购买 nike

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

我如何显示来自 Git 的语义版本号,以便网站的非技术用户在提出问题时可以轻松引用?

最佳答案

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

  • commit hash long
    • git log --pretty="%H"-n1 HEAD
  • commit hash short
    • git log --pretty="%h"-n1 HEAD
  • 提交日期
    • git log --pretty="%ci"-n1 HEAD
  • 标签
    • git describe --tags --abbrev=0
  • 用散列标记 long
    • git describe --tags

其次,使用 exec() 结合您从上面选择的 git 命令来构建版本标识符:

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 - 在 PHP 中显示当前 Git 'version',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16334310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com