alex_halford
alex_halford

Reputation: 439

Get penultimate git tag on branch, matching a given glob

I have a git repo with commits on the master branch, each of which may have multiple tags:

HASH: TAGS
hash7: [v6.0.0, foo1, foo2, foo3]
hash6: []
hash5: []
hash4: [v5.0.0, bar1, bar2, bar3]
hash3: [v4.0.0, baz1, baz2, baz3]
hash2: []
hash1: [v3.0.0, foobar1, foobar2, foobar3]

I have a github action that is triggered by the creation of version tag (v.*.*.*). This action needs to be able to figure out the previous version tag. So, for example, if the action runs on hash7 (v6.0.0), it should return v5.0.0. If it runs on hash4 (v5.0.0), it should return v4.0.0, etc.

There are a number of stackoverflow questions that relate to this. I have tried various combinations of:

The latter, for example, when run on v6.0.0 gives the latest tag (v6.0.0) when run without the skip option, but gives v4.0.0 when the skip option is set as 1.

I've done quite a bit of googling, but I can't find anything reliable.

To summarise, I'm after: The nearest version tag (not including the one attached to the commit that is currently at the HEAD), on this branch

Upvotes: 1

Views: 220

Answers (1)

phd
phd

Reputation: 94483

Just skip HEAD:

git describe --abbrev=0 --match="v*" --tags HEAD~

Upvotes: 2

Related Questions