Skip to content

Instantly share code, notes, and snippets.

@pjhjohn
Last active February 11, 2017 10:10
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 pjhjohn/193c76c22fa167c0841116fb53c0d1eb to your computer and use it in GitHub Desktop.
Save pjhjohn/193c76c22fa167c0841116fb53c0d1eb to your computer and use it in GitHub Desktop.
gitget allows you to get current git commit hash or tag name
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os, subprocess, shlex
# param 'short' for shortening hash (slice for commit hash, abbreviation for tag name)
# param 'tag and not commit' for returning tag name only
# param 'commit and not tag' for returning commit hash only
def gitget(short=False, tag=False, commit=False):
if tag and commit : tag, commit = False, False
# Cosntants
devnull = open(os.devnull, 'w')
cmd_git_tag_exact = [shlex.split('git describe --tags --exact-match --abbrev=0'), shlex.split('git describe --tags --exact-match --abbrev=1')]
cmd_git_tag = [shlex.split('git describe --tags --abbrev=1'), shlex.split('git describe --tags --abbrev=0')]
cmd_git_commit = shlex.split('git rev-parse HEAD')
# Git Tag Name
tag_name = None
if tag: tag_name = subprocess.check_output(cmd_git_tag[short], stderr=devnull)
try:
success = subprocess.check_call(cmd_git_tag_exact[short], stdout=devnull, stderr=devnull) == 0
if success: tag_name = subprocess.check_output(cmd_git_tag_exact[short], stderr=devnull)
except subprocess.CalledProcessError:
pass
# Git Commit Hash
commit_hash = subprocess.check_output(cmd_git_commit, stderr=devnull)
if short: commit_hash = commit_hash[:7]
if tag_name is not None: tag_name = tag_name.strip()
if commit_hash is not None: commit_hash = commit_hash.strip()
if tag: return tag_name
if commit: return commit_hash
return [tag_name, commit_hash][tag_name is None]
# Only Commit Hash
def commit_hash(short=False):
return gitget(short=short, commit=True)
# Only Tag Name
def tag_name(short=False):
return gitget(short=short, tag=True)
# Tag Name if exactly matches, Commit Hash otherwise
def mixed(short=False):
return gitget(short=short)
if __name__ == '__main__':
print('\n === gitget.gitget === \n')
for tag in [False, True]:
for commit in [False, True]:
for short in [False, True]:
print('gitget(short={}, tag={}, commit={})'.format(short, tag, commit))
print('>> ' + str(gitget(short=short, tag=tag, commit=commit)))
print('\n === gitget.commit_hash === \n')
for short in [False, True]:
print('gitget.commit_hash(short={})'.format(short))
print('>> ' + str(commit_hash(short=short)))
print('\n === gitget.tag_name === \n')
for short in [False, True]:
print('gitget.tag_name(short={})'.format(short))
print('>> ' + str(tag_name(short=short)))
print('\n === gitget.mixed === \n')
for short in [False, True]:
print('gitget.mixed(short={})'.format(short))
print('>> ' + str(mixed(short=short)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment