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 merge requests found
Pipeline #41924 passed with stage
in 25 seconds
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):
"""Linewise generator of a file """
with open(path, "r") as file_to_read:
for line in file_to_read.readlines():
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",
packages=find_packages(include=["cbutil"]),
install_requires=[
"python-dotenv",
"influxdb"
"influxdb",
"gitpython",
],
setup_requires=['pytest-runner'],
tests_require=['pytest']
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment