terraformDummyRepo/git_handler.py

78 lines
2.3 KiB
Python
Raw Normal View History

2023-02-21 14:08:39 +00:00
import os
import subprocess as sub
# define semantics for release types by commit messages
## snapshot - snapshot release
## fix/patch - patch release
## bump - version bump release
## feature/feat/minor - minor release
## major/breaking - major release
GIT = 'git'
LOG = 'log'
FORMAT = '"%h %s"'
FORMAT_DEC = "%d"
PRETTY_OPTION = '--pretty='
DECORATE_OPTION = '--decorate=full'
class GitRepo():
def __init__(self):
self.commits = None
self.tags = None
def __clean_commit_string(self, commit_string):
return commit_string.replace('\"', "").replace('\n', "").split()
def __clean_tag_string(self, tag_string):
return tag_string.replace(" ", "").replace('\n', "")
def get_tags(self):
stream = sub.Popen([GIT,
LOG,
PRETTY_OPTION + FORMAT_DEC,
DECORATE_OPTION],
stdout=sub.PIPE,
stderr=sub.PIPE,
text=True,
encoding="UTF-8")
stdout = stream.stdout.readlines()
stderr = stream.stderr.readlines()
if len(stderr) > 0:
raise Exception(f"Git command failed with: {stderr}")
return stdout
def get_commits(self):
stream = sub.Popen([GIT,
LOG,
PRETTY_OPTION + FORMAT],
stdout=sub.PIPE,
stderr=sub.PIPE,
text=True,
encoding="UTF-8")
stdout = stream.stdout.readlines()
stderr = stream.stderr.readlines()
if len(stderr) > 0:
raise Exception(f"Git command failed with: {stderr}")
self.tags = self.get_tags()
self.commits = {}
if len(self.tags) != len(stdout):
raise Exception("Tags list did not match commits list")
for i, elem in enumerate(stdout):
commit_string = self.__clean_commit_string(elem)
commit_id = commit_string[0]
commit_message = " ".join(commit_string[1:len(commit_string)])
commit_tag = self.__clean_tag_string(self.tags[i])
self.commits[commit_id] = [commit_tag, commit_message]
return self.commits