Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions codemcp/git_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"is_git_repository",
"get_ref_commit_chat_id",
"find_git_root",
"get_current_commit_hash",
]

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -246,3 +247,46 @@ def find_git_root(start_path: str) -> str | None:
path = parent

return None


async def get_current_commit_hash(directory: str, short: bool = True) -> str | None:
"""Get the current commit hash for the repository.

This function is similar to get_head_commit_hash but designed to be used
after operations to report the latest commit hash.

Args:
directory: The directory to check
short: Whether to get short hash (default) or full hash

Returns:
The current commit hash if available, None otherwise

Note:
This function safely returns None if there are any issues getting the hash,
rather than raising exceptions.
"""
try:
if not await is_git_repository(directory):
return None

# Get the commit hash (short or full)
cmd = ["git", "rev-parse"]
if short:
cmd.append("--short")
cmd.append("HEAD")

result = await run_command(
cmd,
cwd=directory,
check=False, # Don't raise exception if command fails
capture_output=True,
text=True,
)

if result.returncode == 0:
return str(result.stdout.strip())
return None
except Exception as e:
logging.warning(f"Exception when getting current commit hash: {e!s}", exc_info=True)
return None
Loading