Skip to content
Snippets Groups Projects
Commit a725149b authored by Christoph Alt's avatar Christoph Alt
Browse files

added git utility to util

parent 883fe6f8
No related branches found
No related tags found
No related merge requests found
Pipeline #41924 passed
import git
from typing import Union, Tuple
from pathlib import Path
def remove_newline(string: str) -> str:
return string.replace("\n", " ").strip()
def read_file_line_wise(path): def read_file_line_wise(path):
"""Linewise generator of a file """ """Linewise generator of a file """
with open(path, "r") as file_to_read: with open(path, "r") as file_to_read:
for line in file_to_read.readlines(): for line in file_to_read.readlines():
yield line.strip("\n") yield line.strip("\n")
def get_repo(repo_path: Union[Path, str]) -> git.Repo:
return git.Repo(repo_path)
def get_current_head(repo: git.Repo) -> git.Commit:
return repo.head.commit
def get_commit_infos(commit) -> Tuple[str, str]:
return commit.hexsha, commit.message
def get_head_info(repo_path: Path):
return get_commit_infos(get_current_head(get_repo(repo_path)))
def get_submodule(submodule_name: str, repo: git.Repo) -> git.Submodule:
return repo.submodule(submodule_name)
def get_submodule_path(submodule_name: str, repo_path: Union[Path, str] = ".") -> Path:
submodule_path = get_submodule(submodule_name, get_repo(repo_path)).path
return Path(repo_path, submodule_path)
def get_git_infos(repo_path: Union[Path, str], *, commit_key="commit", commit_msg_key="commit_message"):
commit, commit_msg = get_commit_infos(get_current_head(get_repo(repo_path)))
commit_msg = remove_newline(commit_msg)
return {commit_key: commit, commit_msg_key: commit_msg}
...@@ -9,7 +9,8 @@ setup(name="cb-util", ...@@ -9,7 +9,8 @@ setup(name="cb-util",
packages=find_packages(include=["cbutil"]), packages=find_packages(include=["cbutil"]),
install_requires=[ install_requires=[
"python-dotenv", "python-dotenv",
"influxdb" "influxdb",
"gitpython",
], ],
setup_requires=['pytest-runner'], setup_requires=['pytest-runner'],
tests_require=['pytest'] tests_require=['pytest']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment