2023-02-22 09:26:49 +00:00
|
|
|
import os
|
|
|
|
import subprocess as sub
|
2023-02-22 11:04:46 +00:00
|
|
|
from pathlib import Path
|
2023-02-22 09:26:49 +00:00
|
|
|
from system_repository import SystemRepository
|
|
|
|
from release_type import ReleaseType
|
|
|
|
|
|
|
|
class GitRepository():
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.latest_commit = None
|
|
|
|
self.system_repository = SystemRepository()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_from_commit_string(cls, commit_string):
|
|
|
|
inst = cls()
|
|
|
|
inst.latest_commit = commit_string
|
|
|
|
return inst
|
|
|
|
|
2023-02-23 14:33:00 +00:00
|
|
|
def get_latest_n_commits(self, n: int):
|
|
|
|
self.system_repository.run_checked('git', 'log', '--oneline', '--format="%s %b"', f'-n {n}')
|
|
|
|
return self.system_repository.stdout
|
|
|
|
|
2023-02-22 09:26:49 +00:00
|
|
|
def get_latest_commit(self):
|
2023-02-23 14:33:00 +00:00
|
|
|
output = self.get_latest_n_commits(1)
|
|
|
|
self.latest_commit = " ".join(output) # returns a list of strings otherwise
|
|
|
|
return self.latest_commit
|
2023-02-22 09:26:49 +00:00
|
|
|
|
|
|
|
def get_release_type_from_latest_commit(self):
|
|
|
|
if self.latest_commit is None:
|
|
|
|
self.get_latest_commit()
|
|
|
|
|
|
|
|
if ReleaseType.MAJOR.name in self.latest_commit.upper():
|
|
|
|
return ReleaseType.MAJOR
|
|
|
|
elif ReleaseType.MINOR.name in self.latest_commit.upper():
|
|
|
|
return ReleaseType.MINOR
|
|
|
|
elif ReleaseType.PATCH.name in self.latest_commit.upper():
|
|
|
|
return ReleaseType.PATCH
|
|
|
|
elif ReleaseType.SNAPSHOT.name in self.latest_commit.upper():
|
|
|
|
return ReleaseType.SNAPSHOT
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2023-02-22 13:10:53 +00:00
|
|
|
def tag_annotated(self, annotation: str, message: str):
|
|
|
|
self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message)
|
2023-02-23 15:30:43 +00:00
|
|
|
return self.system_repository.stdout
|
2023-02-22 13:10:53 +00:00
|
|
|
|
2023-02-23 14:33:00 +00:00
|
|
|
def tag_annotated(self, annotation: str, message: str, count: int):
|
|
|
|
self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message, f'HEAD~{count}')
|
2023-02-23 15:30:43 +00:00
|
|
|
return self.system_repository.stdout
|
2023-02-23 14:33:00 +00:00
|
|
|
|
2023-02-22 11:04:46 +00:00
|
|
|
def get_current_branch(self):
|
|
|
|
self.system_repository.run_checked('git', 'branch', '--show-current')
|
2023-02-23 15:30:43 +00:00
|
|
|
return ''.join(self.system_repository.stdout).rstrip()
|
2023-02-22 11:04:46 +00:00
|
|
|
|
|
|
|
def add_file(self, file_path: Path):
|
|
|
|
self.system_repository.run_checked('git', 'add', file_path)
|
2023-02-23 15:30:43 +00:00
|
|
|
return self.system_repository.stdout
|
2023-02-22 11:04:46 +00:00
|
|
|
|
|
|
|
def commit(self, commit_message: str):
|
|
|
|
self.system_repository.run_checked('git', 'commit', '-m', commit_message)
|
2023-02-23 15:30:43 +00:00
|
|
|
return self.system_repository.stdout
|
2023-02-22 11:04:46 +00:00
|
|
|
|
|
|
|
def push(self):
|
|
|
|
self.system_repository.run_checked('git', 'push')
|
2023-02-23 15:30:43 +00:00
|
|
|
return self.system_repository.stdout
|
2023-02-22 11:04:46 +00:00
|
|
|
|
|
|
|
def checkout(self, branch: str):
|
|
|
|
self.system_repository.run_checked('git', 'checkout', branch)
|
2023-02-23 15:30:43 +00:00
|
|
|
return self.system_repository.stdout
|