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
39 lines (32 loc) · 1.35 KB
/
Copy pathgit_utils.py
File metadata and controls
39 lines (32 loc) · 1.35 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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
def get_repo_branch_name(repo_path: Path) -> Optional[str]:
"""Return the active git branch name for *repo_path*, or ``None``."""
try:
branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
cwd=repo_path,
stderr=subprocess.DEVNULL,
).decode().strip()
return branch if branch else None
except (subprocess.CalledProcessError, FileNotFoundError, OSError):
return None