简体   繁体   English

如何解决 git describe --match 限制

[英]How to solve git describe --match limitation

I'm using git describe --long --tags to automatically generate version strings from repo tags.我正在使用git describe --long --tags从 repo 标签自动生成版本字符串。 So that if I have 1.20 tag I get 1.20-5-g27ba6e8 version string in my code.因此,如果我有1.20标签,我会在我的代码中得到1.20-5-g27ba6e8版本字符串。

Now I'm only interested in tags which in regexish look like ^\\d+\\.\\d+$ (I don't want describe to see any other descriptive tags).现在我只对正则表达式中看起来像^\\d+\\.\\d+$标签感兴趣(我不想describe看到任何其他描述性标签)。

git describe has --match switch which would have been exactly what I need if not for the fact that the match is done using glob patterns. git describe有 --match 开关,如果不是因为匹配是使用 glob 模式完成的,这正是我所需要的。

How can I limit a glob pattern only to two numbers (which can have any number of digits!) delimited with a dot?如何将 glob 模式限制为两个用点分隔的数字(可以有任意数量的数字!)?

How can I limit a glob pattern only to two numbers (which can have any number of digits!) delimited with a dot?如何将 glob 模式限制为两个用点分隔的数字(可以有任意数量的数字!)?

You can't.你不能。 The biggest problem is that glob patterns can't repreat last pattern one or more times, there is no + operator like in regex.最大的问题是 glob 模式不能重复一次或多次最后一个模式,没有像正则表达式那样的+运算符。 The closest you can do is:你能做的最接近的是:

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

Sadly the * matches any characters.可悲的是*匹配任何字符。 bash shell solves that issue by introducing extended glob, in the form of +([0-9]).+([0-9]) . bash shell 通过以+([0-9]).+([0-9])的形式引入扩展的 glob 解决了这个问题。 But you can specify --match multiple times and generate patterns for all possible combinations (puff):但是您可以多次指定--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]"

I also recommend to contribute to open source and implement something along --regexmatch to git describe .我还建议为开源做出贡献,并沿着--regexmatch实现一些东西到git describe The other way round is to do the filtering by yourself and pass the tag back to git-describe to describe it.另一种方法是自己进行过滤并将标签传回git-describe来描述它。 In POSIX shell that could be something along:在 POSIX shell 中,可能是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM