Last active
November 8, 2017 11:57
-
-
Save shacker/2fdf105f2d51bebedc8c3ecd99176677 to your computer and use it in GitHub Desktop.
Blog: github version template tag 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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