Skip to content

Instantly share code, notes, and snippets.

@lifuzu
Created December 14, 2013 01:49
Show Gist options
  • Save lifuzu/7954656 to your computer and use it in GitHub Desktop.
Save lifuzu/7954656 to your computer and use it in GitHub Desktop.
Get the most recent tag from the current branch, which made by 'git tag -a master_1.2.3.123'
/*
* Gets the version name from the latest Git tag
*/
def getVersionName = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
return stdout.toString().trim()
}
/*
* Gets the short version name from the latest Git tag
*/
def getShortVersionName = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--abbrev=0'
standardOutput = stdout
}
return stdout.toString().trim()
}
task version << {
println getVersionName()
}
task shortVersion << {
println getShortVersionName()
}
@lifuzu
Copy link
Author

lifuzu commented Dec 14, 2013

git describe --match git rev-parse --abbrev-ref HEAD* --tags

This command can exactly get the latest tag on the current branch even there are many tags in a same commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment