forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_utils.py
More file actions
26 lines (21 loc) · 911 Bytes
/
Copy pathgit_utils.py
File metadata and controls
26 lines (21 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# src/codegraphcontext/utils/git_utils.py
"""Lightweight git helpers used across the indexing pipeline."""
from __future__ import annotations
import subprocess
from pathlib import Path
from typing import Optional
def get_repo_commit_hash(repo_path: Path) -> Optional[str]:
"""Return the full HEAD commit SHA for *repo_path*, or ``None`` if the path
is not inside a git working tree or the ``git`` binary is unavailable.
This function is intentionally silent — it never raises; callers that need
the hash for non-critical enrichment can safely ignore a ``None`` return.
"""
try:
sha = subprocess.check_output(
["git", "rev-parse", "HEAD"],
cwd=repo_path,
stderr=subprocess.DEVNULL,
).decode().strip()
return sha if sha else None
except (subprocess.CalledProcessError, FileNotFoundError, OSError):
return None