get branch name with dot, dot output git errors

This commit is contained in:
Derek DeJonghe 2018-06-06 09:56:45 -04:00
parent f456c928d7
commit 1997b5caa6

View File

@ -1,32 +1,44 @@
import argparse
import subprocess import subprocess
try: try:
from configparser import ConfigParser from configparser import ConfigParser
except ImportError: except ImportError:
# Python < 3 # Python < 3
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
try:
from subprocess import DEVNULL # py3k
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
def get_version(): def get_version(dot=False):
config = ConfigParser() config = ConfigParser()
config.read('./.bumpversion.cfg') config.read('./.bumpversion.cfg')
version = config.get('bumpversion', 'current_version') version = config.get('bumpversion', 'current_version')
# Get the commit hash of the version # Get the commit hash of the version
v_hash = subprocess.Popen(['git', 'rev-list', '-n', '1', version], stdout=subprocess.PIPE, v_hash = subprocess.Popen(['git', 'rev-list', '-n', '1', version], stdout=subprocess.PIPE,
cwd='.').stdout.read().decode('utf-8').rstrip() stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
# Get the current commit hash # Get the current commit hash
c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE,
cwd='.').stdout.read().decode('utf-8').rstrip() stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
# If the version commit hash and current commit hash # If the version commit hash and current commit hash
# do not match return the branch name else return the version # do not match return the branch name else return the version
if v_hash != c_hash: if v_hash != c_hash:
b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, b = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE,
cwd='.').stdout.read().decode('utf-8').rstrip() stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip()
if dot:
b = b.replace('/','.')
return b return b
return version return version
def main(): def main():
print(get_version()) parser = argparse.ArgumentParser(description='Get Version or Branch.')
parser.add_argument('-d','--dot', help='Switch out / for . to be used in docker tag', action='store_true', dest='dot')
args = parser.parse_args()
print(get_version(args.dot))
if __name__ == '__main__': if __name__ == '__main__':
try: main() try: main()