Skip to content

Instantly share code, notes, and snippets.

@zynaxsoft
Created March 6, 2020 05:09
Show Gist options
  • Save zynaxsoft/3510dd449b8e11140613d37c168d2d04 to your computer and use it in GitHub Desktop.
Save zynaxsoft/3510dd449b8e11140613d37c168d2d04 to your computer and use it in GitHub Desktop.
Getting version from git repository
class Version:
def __init__(self, master_ver, branch, git_hash):
self.master_ver = master_ver
self.branch = branch
self.hash = git_hash
@classmethod
def from_str(cls, version_str):
version = version_str.split('-')
return cls(*version)
def __str__(self):
git_version = f'{self.master_ver}-{self.branch}' \
f'-{self.hash}'
return git_version
def git_version(force=False):
if os.path.isfile('.git_version') and not force:
with open('.git_version', 'r') as f:
git_version = Version.from_str(f.readline())
return git_version
current_git_branch = subprocess.getoutput(
'git symbolic-ref --short HEAD'
)
if current_git_branch == 'master':
git_version = subprocess.getoutput(
'git describe --tags'
)
else:
git_current_hash = subprocess.getoutput(
'git rev-parse --short HEAD'
)
git_master_version = subprocess.getoutput(
'git describe --tags origin/master'
)
return Version(git_master_version,
current_git_branch,
git_current_hash,
)
version = git_version()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment