diff --git a/codemcp/git_query.py b/codemcp/git_query.py index 1e3296c..15399a3 100644 --- a/codemcp/git_query.py +++ b/codemcp/git_query.py @@ -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: @@ -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 diff --git a/codemcp/testing.py b/codemcp/testing.py index 7024eef..cc8a028 100644 --- a/codemcp/testing.py +++ b/codemcp/testing.py @@ -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"]) diff --git a/e2e/test_git_query_file_path.py b/e2e/test_git_query_file_path.py new file mode 100644 index 0000000..2efa756 --- /dev/null +++ b/e2e/test_git_query_file_path.py @@ -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()