【问题标题】:How to solve git describe --match limitation如何解决 git describe --match 限制
【发布时间】:2020-11-09 12:27:38
【问题描述】:

我正在使用git describe --long --tags 自动从 repo 标签生成版本字符串。这样,如果我有 1.20 标签,我的代码中就会得到 1.20-5-g27ba6e8 版本字符串。

现在我只对正则表达式中看起来像 ^\d+\.\d+$ 的标签感兴趣(我不希望 describe 看到任何其他描述性标签)。

git describe 有 --match 开关,如果不是因为匹配是使用 glob 模式完成的,这正是我所需要的。

如何将 glob 模式限制为用点分隔的两个数字(可以有任意位数!)?

【问题讨论】:

    标签: git glob


    【解决方案1】:

    如何将 glob 模式限制为用点分隔的两个数字(可以有任意位数!)?

    你不能。最大的问题是 glob 模式不能重复最后一个模式一次或多次,没有像正则表达式那样的 + 运算符。您可以做的最接近的是:

    [0-9]*.[0-9]*
    

    遗憾的是,* 匹配 任何 个字符。 bash shell 通过以 +([0-9]).+([0-9]) 的形式引入扩展 glob 来解决该问题。 但是您可以多次指定--match 并为所有可能的组合生成模式(粉扑):

    # generated with:
    # p="[0-9] [0-9][0-9] [0-9][0-9][0-9]"; for i in $p; do for j in $p; do echo "--match \"$i.$j\""; done; done | paste -sd' '
    # should handle up numbers up to 3 digits
    git describe --long --tags --match "[0-9].[0-9]" --match "[0-9].[0-9][0-9]" --match "[0-9].[0-9][0-9][0-9]" --match "[0-9][0-9].[0-9]" --match "[0-9][0-9].[0-9][0-9]" --match "[0-9][0-9].[0-9][0-9][0-9]" --match "[0-9][0-9][0-9].[0-9]" --match "[0-9][0-9][0-9].[0-9][0-9]" --match "[0-9][0-9][0-9].[0-9][0-9][0-9]"
    

    我还建议为开源做出贡献,并在 --regexmatchgit describe 上实现一些东西。另一种方法是自己进行过滤并将标签传递回git-describe 来描述它。在 POSIX shell 中可能是这样的:

    git tag --sort=committerdate | grep '^[0-9]\+\.[0-9]\+$' | tail -n1 | xargs git describe --long --tags --exact-match --match
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 2016-04-21
      相关资源
      最近更新 更多