Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion codemcp/git_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,11 @@ async def get_current_commit_hash(directory: str, short: bool = True) -> str | N
Returns:
The current commit hash if available, None otherwise

Raises:
NotADirectoryError: If the provided path is a file instead of a directory

Note:
This function safely returns None if there are any issues getting the hash,
This function safely returns None for other issues getting the hash,
rather than raising exceptions.
"""
try:
Expand All @@ -287,6 +290,9 @@ async def get_current_commit_hash(directory: str, short: bool = True) -> str | N
if result.returncode == 0:
return str(result.stdout.strip())
return None
except NotADirectoryError:
# Re-raise NotADirectoryError as a hard error
raise
except Exception as e:
logging.warning(
f"Exception when getting current commit hash: {e!s}", exc_info=True
Expand Down
4 changes: 3 additions & 1 deletion codemcp/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ async def setup_repository(self):
try:
await self.git_run(["init", "-b", "main"])
except subprocess.CalledProcessError:
self.fail("git version is too old for tests! Please install a newer version of git.")
self.fail(
"git version is too old for tests! Please install a newer version of git."
)
await self.git_run(["config", "user.email", "test@example.com"])
await self.git_run(["config", "user.name", "Test User"])

Expand Down
29 changes: 29 additions & 0 deletions e2e/test_git_query_file_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

"""Tests for the get_current_commit_hash function when passed a file path."""

import os
import unittest

from codemcp.git_query import get_current_commit_hash
from codemcp.testing import MCPEndToEndTestCase


class GitQueryFilePathTest(MCPEndToEndTestCase):
"""Test that get_current_commit_hash raises a NotADirectoryError when passed a file path."""

async def test_file_path_raises_error(self):
"""Test that passing a file path to get_current_commit_hash raises NotADirectoryError."""
# Create a test file in the temp dir
test_file_path = os.path.join(self.temp_dir.name, "test_file.txt")
with open(test_file_path, "w") as f:
f.write("Test content")

# Attempt to get current commit hash using a file path instead of directory
# This should raise a NotADirectoryError
with self.assertRaises(NotADirectoryError):
await get_current_commit_hash(test_file_path)


if __name__ == "__main__":
unittest.main()
Loading