-
Notifications
You must be signed in to change notification settings - Fork 0
Add integration with git hooks #16
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-15
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,18 @@ | ||
| FROM python:3.12-slim | ||
|
|
||
| # Install git | ||
| RUN apt-get update && apt-get install -y \ | ||
| git \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # Set up work directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy project files | ||
| COPY . . | ||
|
|
||
| # Set up Python path for running tests | ||
| ENV PYTHONPATH="${PYTHONPATH}:/app" | ||
|
|
||
| # Run tests by default (with -k option to specify test module) | ||
| CMD ["pytest", "tests/", "-v"] |
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,25 @@ | ||
| version: '3' | ||
|
|
||
| services: | ||
| pycomet-tests: | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile.test | ||
| volumes: | ||
| - .:/app | ||
| environment: | ||
| # You can uncomment and provide these for integration tests if needed | ||
| # TEST_ANTHROPIC_API_KEY: ${TEST_ANTHROPIC_API_KEY} | ||
| # TEST_OPENAI_API_KEY: ${TEST_OPENAI_API_KEY} | ||
| # TEST_GEMINI_API_KEY: ${TEST_GEMINI_API_KEY} | ||
| # By default, run unit tests only | ||
| command: pytest tests/ -v -m "not integration" | ||
|
|
||
| # Service for running git hooks specific tests | ||
| hooks-tests: | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile.test | ||
| volumes: | ||
| - .:/app | ||
| command: pytest tests/test_git.py tests/test_cli_hooks.py -v |
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 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| # Colors for terminal output | ||
| GREEN='\033[0;32m' | ||
| BLUE='\033[0;34m' | ||
| RED='\033[0;31m' | ||
| NC='\033[0m' # No Color | ||
|
|
||
| print_section() { | ||
| echo -e "\n${BLUE}===============================================${NC}" | ||
| echo -e "${GREEN}$1${NC}" | ||
| echo -e "${BLUE}===============================================${NC}\n" | ||
| } | ||
|
|
||
| # Build the Docker image | ||
| print_section "Building Docker test image..." | ||
| docker build -f Dockerfile.test -t pycomet-test . | ||
|
|
||
| # Run specific tests based on arguments | ||
| if [ "$1" == "hooks" ]; then | ||
| print_section "Running Git Hooks tests..." | ||
| docker run --rm -v "$(pwd):/app" pycomet-test pytest tests/test_git.py tests/test_cli_hooks.py -v | ||
| elif [ "$1" == "all" ]; then | ||
| print_section "Running all tests (excluding integration tests)..." | ||
| docker run --rm -v "$(pwd):/app" pycomet-test pytest tests/ -v -m "not integration" | ||
| else | ||
| print_section "Running tests with custom command..." | ||
| docker run --rm -v "$(pwd):/app" pycomet-test pytest "$@" | ||
| fi |
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,7 +1,9 @@ | ||||||
| import os | ||||||
| import stat | ||||||
| import subprocess | ||||||
| import tempfile | ||||||
| from typing import List, Optional | ||||||
| from pathlib import Path | ||||||
| from typing import List, Optional, Tuple | ||||||
|
|
||||||
|
|
||||||
| class GitRepo: | ||||||
|
|
@@ -62,3 +64,119 @@ def has_staged_changes() -> bool: | |||||
| # Log unexpected errors but assume no changes for safety | ||||||
| print(f"Error checking staged changes: {str(e)}") | ||||||
| return False | ||||||
|
|
||||||
| @staticmethod | ||||||
| def get_git_root() -> Optional[str]: | ||||||
| """Get the git repository root directory. | ||||||
| Returns the path to the repository root or None if not in a git repo. | ||||||
| """ | ||||||
| try: | ||||||
| return GitRepo._run_git_command(["rev-parse", "--show-toplevel"]).strip() | ||||||
| except Exception: | ||||||
| return None | ||||||
|
|
||||||
| @staticmethod | ||||||
| def install_prepare_commit_msg_hook() -> Tuple[bool, str]: | ||||||
| """Install the prepare-commit-msg git hook. | ||||||
| Returns (success, message) tuple. | ||||||
| """ | ||||||
| git_root = GitRepo.get_git_root() | ||||||
| if not git_root: | ||||||
| return False, "Not in a git repository" | ||||||
|
|
||||||
| hooks_dir = Path(git_root) / ".git" / "hooks" | ||||||
| hook_path = hooks_dir / "prepare-commit-msg" | ||||||
|
|
||||||
| # Create the hook script | ||||||
| hook_content = """#!/bin/sh | ||||||
| # PyComet AI-powered commit message hook | ||||||
| # https://github.com/JayDoubleu/PyComet | ||||||
|
|
||||||
| # Get the commit message file path from Git | ||||||
| COMMIT_MSG_FILE=$1 | ||||||
| COMMIT_SOURCE=$2 | ||||||
|
|
||||||
| # Skip if not an interactive commit (e.g., merge commit, commit with -m) | ||||||
| if [ "$COMMIT_SOURCE" = "message" ] || [ "$COMMIT_SOURCE" = "template" ] || \\ | ||||||
| [ "$COMMIT_SOURCE" = "merge" ] || [ "$COMMIT_SOURCE" = "squash" ]; then | ||||||
| exit 0 | ||||||
| fi | ||||||
|
|
||||||
| # Generate commit message with PyComet and save to commit message file | ||||||
| pycomet preview --no-detailed 2>/dev/null | \\ | ||||||
| awk 'BEGIN{f=0} /^-+$/{f=!f; next} f{print}' > "$COMMIT_MSG_FILE.pycomet" | ||||||
| if [ -s "$COMMIT_MSG_FILE.pycomet" ]; then | ||||||
| cat "$COMMIT_MSG_FILE.pycomet" > "$COMMIT_MSG_FILE" | ||||||
| rm "$COMMIT_MSG_FILE.pycomet" | ||||||
| echo "# PyComet: Generated AI commit message. Edit as needed." >> "$COMMIT_MSG_FILE" | ||||||
| echo "# To disable the PyComet hook: git config --local core.hooksPath /dev/null" \\ | ||||||
| >> "$COMMIT_MSG_FILE" | ||||||
| fi | ||||||
| """ | ||||||
| try: | ||||||
| # Ensure hooks directory exists | ||||||
| hooks_dir.mkdir(exist_ok=True, parents=True) | ||||||
|
|
||||||
| # Check if hook already exists | ||||||
| if hook_path.exists(): | ||||||
| with open(hook_path, "r") as f: | ||||||
| existing_content = f.read() | ||||||
| if "PyComet" in existing_content: | ||||||
| return True, "Prepare-commit-msg hook is already installed" | ||||||
| else: | ||||||
| # Backup existing hook | ||||||
| backup_path = Path(str(hook_path) + ".backup") | ||||||
| hook_path.rename(backup_path) | ||||||
| backup_msg = f" (existing hook backed up to {backup_path})" | ||||||
| else: | ||||||
| backup_msg = "" | ||||||
|
|
||||||
| # Write the hook script | ||||||
| with open(hook_path, "w") as f: | ||||||
| f.write(hook_content) | ||||||
|
|
||||||
| # Make the hook executable | ||||||
| hook_mode = os.stat(hook_path).st_mode | ||||||
| executable_mode = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH | ||||||
| os.chmod(hook_path, hook_mode | executable_mode) | ||||||
|
|
||||||
| return True, f"Prepare-commit-msg hook installed successfully{backup_msg}" | ||||||
|
|
||||||
| except Exception as e: | ||||||
| return False, f"Failed to install hook: {str(e)}" | ||||||
|
|
||||||
| @staticmethod | ||||||
| def uninstall_prepare_commit_msg_hook() -> Tuple[bool, str]: | ||||||
| """Uninstall the prepare-commit-msg git hook. | ||||||
| Returns (success, message) tuple. | ||||||
| """ | ||||||
| git_root = GitRepo.get_git_root() | ||||||
| if not git_root: | ||||||
| return False, "Not in a git repository" | ||||||
|
|
||||||
| hook_path = Path(git_root) / ".git" / "hooks" / "prepare-commit-msg" | ||||||
| backup_path = Path(str(hook_path) + ".backup") | ||||||
|
||||||
| backup_path = Path(str(hook_path) + ".backup") | |
| backup_path = Path(f"{hook_path}.backup") |
Oops, something went wrong.
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.
[nitpick] Consider using an f-string for constructing backup_path for improved readability, e.g., Path(f"{hook_path}.backup").