Created
March 6, 2020 05:09
-
-
Save zynaxsoft/3510dd449b8e11140613d37c168d2d04 to your computer and use it in GitHub Desktop.
Getting version from git repository
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
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