Skip to content

Instantly share code, notes, and snippets.

@shacker
Last active November 8, 2017 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shacker/2fdf105f2d51bebedc8c3ecd99176677 to your computer and use it in GitHub Desktop.
Save shacker/2fdf105f2d51bebedc8c3ecd99176677 to your computer and use it in GitHub Desktop.
Blog: github version template tag 1
@register.simple_tag
def git_ver():
'''
Retrieve and return the latest git commit hash ID and tag as a dict.
'''
git_dir = os.path.dirname(settings.BASE_DIR)
try:
# Date and hash ID
head = subprocess.Popen(
"git -C {dir} log -1 --pretty=format:\"%h on %cd\" --date=short".format(dir=git_dir),
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
version = head.stdout.readline().strip().decode('utf-8')
# Latest tag
head = subprocess.Popen(
"git -C {dir} describe --tags $(git -C {dir} rev-list --tags --max-count=1)".format(dir=git_dir),
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
latest_tag = head.stdout.readline().strip().decode('utf-8')
git_string = "{v}, {t}".format(v=version, t=latest_tag)
except:
git_string = u'unknown'
return git_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment