Files
QuizBot/modules/utils/git_utils.py
2025-04-28 13:06:55 +02:00

28 lines
748 B
Python

from pathlib import Path
import aiofiles
# TODO Add documentation
async def get_current_commit() -> str | None:
head_path: Path = Path(".git/HEAD")
if not head_path.exists():
return None
async with aiofiles.open(head_path, "r", encoding="utf-8") as head_file:
head_content: str = (await head_file.read()).strip()
if not head_content.startswith("ref:"):
return head_content
head_ref_path: Path = Path(".git/").joinpath(" ".join(head_content.split(" ")[1:]))
if not head_ref_path.exists():
return None
async with aiofiles.open(head_ref_path, "r", encoding="utf-8") as head_ref_file:
head_ref_content: str = (await head_ref_file.read()).strip()
return head_ref_content