【问题标题】:How to get the version of Git programmatically?如何以编程方式获取 Git 的版本?
【发布时间】:2021-08-20 23:53:59
【问题描述】:

How can I find out what version of git I'm running? 中详述,您可以通过以下方式获取 git 版本

$ git version
git version 2.30.0

但看起来这是供人类使用的,而不是程序使用的。那是对的吗?如果是这样,是否有一个“管道”git version 命令可用于版本的数值比较?

【问题讨论】:

    标签: git version


    【解决方案1】:

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

    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);
    ...
    

    【讨论】:

      【解决方案2】:

      管道?

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

      【讨论】:

      • 我问的是这样做是否安全,而不是如何 解析该特定值(该位是微不足道的)。
      • 您要求“管道”和数字比较,您没有要求 API 稳定性...
      • 这就是“管道”命令的。它们是不能以编程方式使用的稳定命令。 API 稳定性是以编程方式运行命令的先决条件。见git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
      猜你喜欢
      • 2010-09-08
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      相关资源
      最近更新 更多