如何以编程方式获取Git的版本?

如何以编程方式获取Git的版本?,git,version,Git,Version,如中所述,您可以通过 $ git version git version 2.30.0 但看起来这是人类的消费,而不是程序。对吗?如果是,是否有一个“Pimping”git版本命令可用于版本的数值比较?管道 git--version | awk'{print$3} 我检查了源代码。有一条评论是关于保持git版本的格式稳定,以便安全地解析它 void get_version_info(struct strbuf *buf, int show_build_options) { /*

如中所述,您可以通过

$ git version
git version 2.30.0
但看起来这是人类的消费,而不是程序。对吗?如果是,是否有一个“Pimping”
git版本
命令可用于版本的数值比较?

管道

git--version | awk'{print$3}

我检查了源代码。有一条评论是关于保持git版本的格式稳定,以便安全地解析它

void get_version_info(struct strbuf *buf, int show_build_options)
{
    /*
     * The format of this string should be kept stable for compatibility
     * with external projects that rely on the output of "git version".
     *
     * Always show the version, even if other options are given.
     */
    strbuf_addf(buf, "git version %s\n", git_version_string);
...
我问的是这样做是否安全,而不是如何解析特定的值(这一点微不足道)。你要求的是“管道”和数字比较,你没有要求API稳定性……这就是“管道”命令。它们是无法以编程方式使用的稳定命令。API稳定性是以编程方式运行命令的先决条件。看