from pathlib import Path def get_active_branch_name(base_path="."): head_dir = Path(base_path) / ".git" / "HEAD" with head_dir.open("r") as f: content = f.read().splitlines() for line in content: if line[0:4] == "ref:": return line.partition("refs/heads/")[2] def get_git_revision(base_path=".", short = True): git_dir = Path(base_path) / '.git' with (git_dir / 'HEAD').open('r') as head: ref = head.readline().split(' ')[-1].strip() with (git_dir / ref).open('r') as git_hash: hash = git_hash.readline().strip() if short: return hash[:7] else: return hash