Implement git handler
This commit is contained in:
parent
e9301dedd1
commit
fce565d2d5
2 changed files with 107 additions and 0 deletions
77
git_handler.py
Normal file
77
git_handler.py
Normal file
|
@ -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
|
||||||
|
|
30
test/test_git_handler.py
Normal file
30
test/test_git_handler.py
Normal file
|
@ -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"]
|
Loading…
Reference in a new issue