| @@ -0,0 +1,82 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | import os | |
| 3 | + | import re | |
| 4 | + | ||
| 5 | + | ||
| 6 | + | class GitVersion: | |
| 7 | + | def __init__(self): | |
| 8 | + | self._default_version = "0.1.0" | |
| 9 | + | os.chdir(os.path.dirname(os.path.realpath(__file__))) | |
| 10 | + | ||
| 11 | + | @property | |
| 12 | + | def tag(self): | |
| 13 | + | stream = os.popen("git describe --match v[0-9]* --abbrev=0 --tags") | |
| 14 | + | return stream.read().strip() | |
| 15 | + | ||
| 16 | + | @property | |
| 17 | + | def version(self): | |
| 18 | + | version = f"{self.tag[1:]}.{self.build}" | |
| 19 | + | ||
| 20 | + | if version == ".": | |
| 21 | + | return self._default_version | |
| 22 | + | ||
| 23 | + | return version | |
| 24 | + | ||
| 25 | + | @property | |
| 26 | + | def default_branch(self): | |
| 27 | + | stream = os.popen("git config --get init.defaultBranch") | |
| 28 | + | result = stream.read().strip() | |
| 29 | + | ||
| 30 | + | if not result: | |
| 31 | + | result = "main" | |
| 32 | + | ||
| 33 | + | return result | |
| 34 | + | ||
| 35 | + | @property | |
| 36 | + | def build(self): | |
| 37 | + | stream = os.popen(f"git rev-list {self.tag}.. --count") | |
| 38 | + | return stream.read().strip() | |
| 39 | + | ||
| 40 | + | @property | |
| 41 | + | def branch(self): | |
| 42 | + | stream = os.popen("git branch --show-current") | |
| 43 | + | return stream.read().strip() | |
| 44 | + | ||
| 45 | + | @property | |
| 46 | + | def full(self): | |
| 47 | + | return f"{self.version}-{self.branch}" | |
| 48 | + | ||
| 49 | + | @property | |
| 50 | + | def standard(self): | |
| 51 | + | standard = f"{self.version}-{self.branch}" | |
| 52 | + | if self.branch == self.default_branch or re.match("release/.*", self.branch): | |
| 53 | + | standard = f"{self.version}" | |
| 54 | + | return standard | |
| 55 | + | ||
| 56 | + | @property | |
| 57 | + | def commit(self): | |
| 58 | + | stream = os.popen("git rev-parse HEAD") | |
| 59 | + | return stream.read().strip() | |
| 60 | + | ||
| 61 | + | @property | |
| 62 | + | def commit_hash(self): | |
| 63 | + | stream = os.popen("git rev-parse --short HEAD") | |
| 64 | + | return stream.read().strip() | |
| 65 | + | ||
| 66 | + | def __str__(self): | |
| 67 | + | return f""" | |
| 68 | + | Tag: {self.tag} | |
| 69 | + | Version: {self.version} | |
| 70 | + | Full: {self.full} | |
| 71 | + | Branch: {self.branch} | |
| 72 | + | Build: {self.build} | |
| 73 | + | Standard: {self.standard} | |
| 74 | + | Commit: {self.commit} | |
| 75 | + | ||
| 76 | + | GitRepo {self.full} {self.commit_hash} | |
| 77 | + | """ | |
| 78 | + | ||
| 79 | + | ||
| 80 | + | if __name__ == "__main__": | |
| 81 | + | git_version = GitVersion() | |
| 82 | + | print(git_version) | |
aquafire / repo_info.py
Last active 6 months ago
aquafire revised this gist 9 months ago · f6c2bb5
1 file changed, 82 insertions