From fce565d2d508a9bf9fccd383c0a0038e0107bb4e Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 21 Feb 2023 15:08:39 +0100 Subject: [PATCH] Implement git handler --- git_handler.py | 77 ++++++++++++++++++++++++++++++++++++++++ test/test_git_handler.py | 30 ++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 git_handler.py create mode 100644 test/test_git_handler.py diff --git a/git_handler.py b/git_handler.py new file mode 100644 index 0000000..3f30551 --- /dev/null +++ b/git_handler.py @@ -0,0 +1,77 @@ +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 + diff --git a/test/test_git_handler.py b/test/test_git_handler.py new file mode 100644 index 0000000..77d95c9 --- /dev/null +++ b/test/test_git_handler.py @@ -0,0 +1,30 @@ +from pathlib import Path +import sys +import os + +# getting the name of the directory +# where the this file is present. +current = os.path.dirname(os.path.realpath(__file__)) + +# Getting the parent directory name +# where the current directory is present. +parent = os.path.dirname(current) + +# adding the parent directory to +# the sys.path. +sys.path.append(parent) + +# now we can import the module in the parent +# directory. + +from git_handler import * + +def test_git_handler(): + + # init + repo = GitRepo() + repo.get_commits() + + #test + assert type(repo.commits) == dict + assert repo.commits["decd36b"] == ["(tag:refs/tags/TEST)", "Initial commit"]