git match tag with multiple words

ぐ巨炮叔叔 提交于 2021-02-07 09:19:12

问题


We can get the last git tag, which starts by a word(e.g TEST) as follow:

git describe --tag --dirty --match 'TEST*'

I am wondering how can I get the last tag, which starts by word1 or word2 (e.g. TEST OR RUN)? I've tried to use regex or following but it does not work:

git describe --tag --dirty --match 'TEST*|RUN*'

SOLUTION: We can get number of commits to HEAD and compare those numbers, the one which has less commits is the more recently. You can find its script in the answer, which I have added.


回答1:


The pattern is matched using fnmatch(), which uses shell wildcard patterns, not regular expressions.

As shell wildcards don't support alternation you can't do this without changing the implementation of describe.

Source: git describe source :P




回答2:


We can get number of commits to HEAD and compare those numbers, the one which has less commits is the more recently, as follow:

#!/bin/bash
V_TEST=$(git describe --tag --dirty --match 'TEST*' | awk 'BEGIN { FS = "-" }; {print $2}')
V_RUN=$(git describe --tag --dirty --match 'RUN*' | awk 'BEGIN { FS = "-" }; {print $2}')
if [[ $V_TEST -gt $V_RUN ]] ; then
  VERSION=$(git describe --tag --dirty --match 'RUN*')
  echo $VERSION
else
  VERSION=$(git describe --tag --dirty --match 'TEST*')
fi
echo $VERSION



回答3:


Note: only Git 2.15 (Q4 2017) will allow multiple patterns:

"git describe --match" learned to take multiple patterns in v2.13 series, but the feature ignored the patterns after the first one and did not work at all. This has been fixed.

See commit da769d2 (16 Sep 2017) by Max Kirillov (max630).
(Merged by Junio C Hamano -- gitster -- in commit a515136, 28 Sep 2017)

git describe --long --tags --match="test1-*" --match="test2-*" HEAD^

In your case:

git describe --long --tags --match="TEST*" --match="RUN*"



回答4:


If I understand you correctly, you could probably do something along the lines of:

/^git describe --tag --dirty --match '(?:TEST|RUN)\*'$/

Live demo and explanation: http://regex101.com/r/qC3gW5



来源:https://stackoverflow.com/questions/17813749/git-match-tag-with-multiple-words

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!