-
Notifications
You must be signed in to change notification settings - Fork 1
prompt: reduce false positives + add pre-push hook #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5f08339
prompt: reduce false positives with 5 quality improvements
brucearctor 7f8b914
feat: add pre-push hook with install/uninstall commands
brucearctor bffae0a
fix: address CI lint failures and CodeRabbit review findings
brucearctor f205026
test: add core.hooksPath coverage per CodeRabbit review
brucearctor 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Pre-commit hook for code-reviewer. | ||
| # See: https://pre-commit.com | ||
| # Usage in .pre-commit-config.yaml: | ||
| # | ||
| # - repo: https://github.com/OpticDiff/code-reviewer | ||
| # rev: v0.6.0 | ||
| # hooks: | ||
| # - id: code-review | ||
| # stages: [pre-push] | ||
| # args: [--min-severity, high] | ||
| # | ||
| - id: code-review | ||
| name: code-review | ||
| description: AI-powered code review before push | ||
| entry: code-reviewer --diff | ||
| language: golang | ||
| pass_filenames: false | ||
| stages: [pre-push] | ||
| always_run: true |
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,127 @@ | ||
| // Package hook provides git hook installation for code-reviewer. | ||
| package hook | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // managedSentinel is the exact marker that identifies hooks managed by code-reviewer. | ||
| // Ownership checks match this sentinel, not a loose substring, so a foreign hook | ||
| // that happens to mention "code-reviewer" in a comment is never overwritten. | ||
| const managedSentinel = "# managed-by: code-reviewer" | ||
|
|
||
| const preCommitHookContent = `#!/bin/sh | ||
| ` + managedSentinel + ` | ||
| # code-reviewer pre-push hook | ||
| # Installed by: code-reviewer hook install | ||
| # Remove with: code-reviewer hook uninstall | ||
| # | ||
| # This hook reviews your changes before pushing. | ||
| # To skip: git push --no-verify | ||
|
|
||
| set -e | ||
|
|
||
| # Only run if there are commits to push. | ||
| if ! git diff --quiet @{push} 2>/dev/null; then | ||
| echo "🔍 code-reviewer: reviewing changes before push..." | ||
| code-reviewer --diff --min-severity high --no-color | ||
| fi | ||
| ` | ||
|
|
||
| // Install writes the pre-push hook to the repository's hooks directory. | ||
| // If a hook already exists and wasn't installed by code-reviewer, it returns an error. | ||
| func Install() error { | ||
| hooksDir, err := resolveHooksDir() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| hookPath := filepath.Join(hooksDir, "pre-push") | ||
|
|
||
| // Check for existing hook. | ||
| if data, err := os.ReadFile(hookPath); err == nil { | ||
| if !strings.Contains(string(data), managedSentinel) { | ||
| return fmt.Errorf("pre-push hook already exists at %s\n\nTo overwrite, remove it first:\n rm %s", hookPath, hookPath) | ||
| } | ||
| // Our hook — safe to overwrite. | ||
| } | ||
|
|
||
| // Ensure hooks directory exists. | ||
| if err := os.MkdirAll(hooksDir, 0o755); err != nil { | ||
| return fmt.Errorf("creating hooks directory: %w", err) | ||
| } | ||
|
|
||
| if err := os.WriteFile(hookPath, []byte(preCommitHookContent), 0o755); err != nil { | ||
| return fmt.Errorf("writing pre-push hook: %w", err) | ||
| } | ||
|
|
||
| fmt.Printf("✅ Installed pre-push hook at %s\n", hookPath) | ||
| fmt.Println(" Reviews will run automatically on git push.") | ||
| fmt.Println(" Skip with: git push --no-verify") | ||
| return nil | ||
| } | ||
|
|
||
| // Uninstall removes the pre-push hook if it was installed by code-reviewer. | ||
| func Uninstall() error { | ||
| hooksDir, err := resolveHooksDir() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Document Add a doc comment immediately above this exported function. As per coding guidelines, “All exported Go functions and types MUST have doc comments.” 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| hookPath := filepath.Join(hooksDir, "pre-push") | ||
|
|
||
| data, err := os.ReadFile(hookPath) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| fmt.Println("No pre-push hook found.") | ||
| return nil | ||
| } | ||
| return fmt.Errorf("reading hook: %w", err) | ||
| } | ||
|
|
||
| if !strings.Contains(string(data), managedSentinel) { | ||
| return fmt.Errorf("pre-push hook at %s was not installed by code-reviewer; refusing to remove", hookPath) | ||
| } | ||
|
|
||
| if err := os.Remove(hookPath); err != nil { | ||
| return fmt.Errorf("removing hook: %w", err) | ||
| } | ||
|
|
||
| fmt.Printf("✅ Removed pre-push hook from %s\n", hookPath) | ||
| return nil | ||
| } | ||
|
|
||
| // resolveHooksDir returns the hooks directory for the current repository, | ||
| // honoring Git's core.hooksPath configuration if set. | ||
| func resolveHooksDir() (string, error) { | ||
| // Try core.hooksPath first. | ||
| cmd := exec.Command("git", "rev-parse", "--git-path", "hooks") | ||
| out, err := cmd.Output() | ||
| if err == nil { | ||
| resolved := strings.TrimSpace(string(out)) | ||
| if resolved != "" { | ||
| return resolved, nil | ||
| } | ||
| } | ||
|
|
||
| // Fallback: <gitdir>/hooks. | ||
| gitDir, err := findGitDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(gitDir, "hooks"), nil | ||
| } | ||
|
|
||
| // findGitDir locates the .git directory by running git rev-parse. | ||
| func findGitDir() (string, error) { | ||
| cmd := exec.Command("git", "rev-parse", "--git-dir") | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| return "", fmt.Errorf("not a git repository (run this from inside a git repo): %w", err) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return strings.TrimSpace(string(out)), nil | ||
| } | ||
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: OpticDiff/code-reviewer
Length of output: 48354
Review against the same base used by the push guard.
The guard checks
@{push}, butcode-reviewer --diffwithout an explicit ref compares against its documented default,origin/HEAD. A branch tracking a different remote branch can therefore fail on unrelated findings, and a first-push scenario needs an explicit fallback. Pass an explicit@{push}-equivalent ref and define a first-push behavior.🤖 Prompt for AI Agents