Skip to content

Instantly share code, notes, and snippets.

@HerrSpace
Created October 19, 2018 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HerrSpace/456ddfb9b64cdb27711c6477d623bca9 to your computer and use it in GitHub Desktop.
Save HerrSpace/456ddfb9b64cdb27711c6477d623bca9 to your computer and use it in GitHub Desktop.
def git_version(path):
# Like v1.0.0 or v1.0.0-1-g963fc39.dirty
git_describe = check_output([
'git', '-C', path, 'describe', '--tags', '--dirty=.dirty']
).decode().strip()
# Like v1.0.0
last_tag = check_output([
'git', '-C', path, 'describe', '--tags', '--abbrev=0']
).decode().strip()
# Strip leading v often used in tag names.
last_version = last_tag.lstrip('v')
# Like .dev1+g963fc39.dirty
diff_to_tag = (
git_describe[len(last_tag):]
.replace('-', '.dev', 1)
.replace('-', '+', 1)
)
# Like 1.0.0.dev1+g963fc39.dirty, compatible to PEP440
return last_version + diff_to_tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment