3

I want to extract the number of latest version of a Git repository. This what I've done so far:

#--- Checking out latest tag printf "%bChecking out tag...%b\n" "${YELLOW}" "${GRAY}" LATEST_TAG=$(git describe --tags) printf "\nChecking out tag %b${LATEST_TAG}%b\n" "${YELLOW}" "${GRAY}" git checkout -b V${LATEST_TAG%-*} ${LATEST_TAG} 

My problem is that if git describe --tags returns something like this 1.0.0-39-gf8f8306 I end up creating a branch V1.0.0-39.

What I want is to create a branch named V1.0.0.

1
  • Thank you very much. Add it as an answer please. Commented Feb 9, 2018 at 10:44

4 Answers 4

8

It’s a bit of a secret,* but you can use --abbrev=0:

git describe --tags --abbrev=0 

*git help describe mentions it, but you have to know where to look:

--abbrev=<n>

[…] An <n> of 0 will suppress long format, only showing the closest tag.

Sign up to request clarification or add additional context in comments.

Comments

3

I had an issue with git describe on GitHub Actions, namely: "fatal: No tags can describe '160ef4560d8855c9c05f4cae207baeb71b7791f3'." Which apparently has something to do with the entire repo not being available.

But I found another solution that works and doesn't have this issue:

git tag --sort=-refname --list "v[0-9]*" | head -n 1 

Comments

2

You can use awk:

LATEST_TAG=$(git describe --tags | awk -F - '{print $1}') 

1 Comment

Works but I will take Biffen answer cause it's the shortest.
1

In your line

git checkout -b V${LATEST_TAG%-*} ${LATEST_TAG}

you can double up the % symbol which will match the longest string starting with a - rather than the shortest which is what a single % does.

eg git checkout -b V${LATEST_TAG%%-*} ${LATEST_TAG}

You can see a full list of parameter expansions here: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

1 Comment

Works but I will take Biffen answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.