3

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.

csuo
  • 820
  • 3
  • 16
  • 31

4 Answers4

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

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • Thanks for your reply, it seems you are right, but what do you suggest? – csuo Jul 23 '13 at 15:16
  • @csuo: You could modify the `describe` implementation to support multiple patterns (or regular expressions) – Hasturkun Jul 23 '13 at 15:17
  • Thanks again. I've wrote a script which finds the recent commit with mentioned tags, and it solves the problem. – csuo Jul 24 '13 at 11:57
1

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
csuo
  • 820
  • 3
  • 16
  • 31
1

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*"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-1

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

Firas Dib
  • 2,743
  • 19
  • 38
  • 1
    I don't think he wants to match git command lines, in this case. – Hasturkun Jul 23 '13 at 15:11
  • This does not work, as I mentioned in the question, I want to get the last tag, which starts by word1 or word2 – csuo Jul 23 '13 at 15:15
  • @csuo: I think you need to clarify it for me, I don't understand what you want or what you're trying to do. If you change `(?:`into `(` the regex will capture the data. – Firas Dib Jul 23 '13 at 15:17
  • No, I think it does not, if you see the Hasturkun's answer, he've mentioned that this match does not use regular exp. What I am trying to do is, suppose we have two tags which one of them starts by TEST and the other one by RUN, I want to catch the one which is tagged more recently. – csuo Jul 23 '13 at 15:25
  • @csuo: then you should untag your question from regex. – Firas Dib Jul 23 '13 at 15:39
  • @ Lindrian: Thank you for your answer. I've wrote a script which finds the recent commit with mentioned tags, and it solves the problem – csuo Jul 24 '13 at 11:58