-
Notifications
You must be signed in to change notification settings - Fork 4
feat(cli): add doctor_handler to check git hooks directory #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Seif-Mamdouh
wants to merge
4
commits into
main
Choose a base branch
from
seif/main/feat/gacDoctor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−4
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
31fc8f0
feat(cli): add doctor_handler to check git hooks directory
Seif-Mamdouh 9806351
Merge branch 'main' into seif/main/feat/gacDoctor
Seif-Mamdouh 9cad264
"test(cli): implement hook directory creation if missing and update p…
Seif-Mamdouh 4077de9
add local git hooks testing functionality
Seif-Mamdouh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import os | ||
| from rich.console import Console | ||
| from rich.panel import Panel | ||
| from rich.text import Text | ||
| from rich import box | ||
|
|
||
| from ai_commit_msg.services.git_service import GitService | ||
| from ai_commit_msg.utils.logger import Logger | ||
| from ai_commit_msg.utils.utils import execute_cli_command | ||
|
|
||
| console = Console() | ||
|
|
||
|
|
||
| def check_git_hooks_directory(): | ||
| """Check if we can determine the correct git hooks directory""" | ||
| try: | ||
| # Get the hooks directory using git's built-in command | ||
| hooks_dir = execute_cli_command( | ||
| ["git", "rev-parse", "--git-path", "hooks"] | ||
| ).stdout.strip() | ||
| git_dir = execute_cli_command(["git", "rev-parse", "--git-dir"]).stdout.strip() | ||
|
|
||
| return { | ||
| "status": "ok", | ||
| "hooks_dir": hooks_dir, | ||
| "git_dir": git_dir, | ||
| "is_worktree": "worktrees" in git_dir, | ||
| } | ||
| except Exception as e: | ||
| return {"status": "error", "error": str(e)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class LLMService(ABC): | ||
| @abstractmethod | ||
| def chat_completion(self, messages): | ||
| pass | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import os | ||
| import tempfile | ||
| import shutil | ||
| from ai_commit_msg.cli.hook_handler import handle_setup_hook | ||
|
|
||
|
|
||
| def test_hook_setup_with_missing_directory(): | ||
| """Test that handle_setup_hook creates the hooks directory if it doesn't exist""" | ||
|
|
||
| # Create a temporary directory for testing | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| # Create a fake hooks path that doesn't exist | ||
| hooks_dir = os.path.join(temp_dir, "hooks") | ||
| hook_file_path = os.path.join(hooks_dir, "prepare-commit-msg") | ||
|
|
||
| print(f"Testing hook setup with path: {hook_file_path}") | ||
| print(f"Hooks directory exists before: {os.path.exists(hooks_dir)}") | ||
|
|
||
| # This should create the directory and the hook file | ||
| handle_setup_hook(hook_file_path) | ||
|
|
||
| print(f"Hooks directory exists after: {os.path.exists(hooks_dir)}") | ||
| print(f"Hook file exists: {os.path.exists(hook_file_path)}") | ||
| print(f"Hook file is executable: {os.access(hook_file_path, os.X_OK)}") | ||
|
|
||
| # Verify the content | ||
| if os.path.exists(hook_file_path): | ||
| with open(hook_file_path, "r") as f: | ||
| content = f.read() | ||
| print(f"Hook file contains git-ai-commit: {'git-ai-commit' in content}") | ||
|
|
||
| print("✅ Test completed successfully!") | ||
|
|
||
|
|
||
| def test_local_git_hooks(): | ||
| """Test the actual local .git/hooks directory""" | ||
|
|
||
| # Check if we're in a git repository | ||
| git_dir = ".git" | ||
| if not os.path.exists(git_dir): | ||
| print("❌ Not in a git repository - .git directory not found") | ||
| return | ||
|
|
||
| hooks_dir = os.path.join(git_dir, "hooks") | ||
| hook_file_path = os.path.join(hooks_dir, "prepare-commit-msg") | ||
|
|
||
| print(f"🔍 Checking local git hooks in: {os.path.abspath(hooks_dir)}") | ||
| print(f"Hooks directory exists: {os.path.exists(hooks_dir)}") | ||
|
|
||
| if os.path.exists(hooks_dir): | ||
| print(f"Files in hooks directory:") | ||
| for file in os.listdir(hooks_dir): | ||
| file_path = os.path.join(hooks_dir, file) | ||
| is_executable = os.access(file_path, os.X_OK) | ||
| print( | ||
| f" - {file} {'(executable)' if is_executable else '(not executable)'}" | ||
| ) | ||
|
|
||
| print(f"\n📋 prepare-commit-msg hook status:") | ||
| print(f"Hook file exists: {os.path.exists(hook_file_path)}") | ||
|
|
||
| if os.path.exists(hook_file_path): | ||
| print(f"Hook file is executable: {os.access(hook_file_path, os.X_OK)}") | ||
|
|
||
| # Check content | ||
| with open(hook_file_path, "r") as f: | ||
| content = f.read() | ||
| has_git_ai_commit = "git-ai-commit" in content | ||
| print(f"Hook file contains 'git-ai-commit': {has_git_ai_commit}") | ||
|
|
||
| if has_git_ai_commit: | ||
| print("✅ git-ai-commit hook is installed!") | ||
| else: | ||
| print("❌ git-ai-commit not found in hook file") | ||
| print("Hook content preview:") | ||
| print("-" * 40) | ||
| print(content[:200] + "..." if len(content) > 200 else content) | ||
| print("-" * 40) | ||
| else: | ||
| print("❌ prepare-commit-msg hook not found") | ||
| print("\n🔧 To install the hook, you can run:") | ||
| print(f"handle_setup_hook('{hook_file_path}')") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print("=== Testing Local Git Hooks ===") | ||
| test_local_git_hooks() | ||
|
|
||
| print("\n" + "=" * 50) | ||
| print("=== Testing with Temporary Directory ===") | ||
| test_hook_setup_with_missing_directory() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using os.path.join(hooks_dir, 'prepare-commit-msg') to build the path, which improves cross-platform compatibility.