Repository navigation
feat(#5): --staged flag for pre-commit git hook integration - #13
Conversation
- Add --staged flag to restrict clone detection to git-staged files - Scan all files for cross-comparison but only report clones where at least one instance is in a staged file - StagedFiles() uses git diff --cached --name-only --diff-filter=ACM - Graceful fallback when not in a git repo (warning, no error) - Unit tests for StagedFiles, filterStagedClones - Integration tests: staged duplicate exits 1, no staged files exits 0, cross-file staged clone detection with JSON validation
StructLint — All checks passed50 rules validated against
|
dupehound scan results
Hotspots
Top clones (by impact)#1
|
There was a problem hiding this comment.
Core Changes
This Pull Request introduces a new --staged flag to the dupehound scan command. This feature is designed to integrate with pre-commit hooks by restricting clone detection reports to only those clones that involve at least one file currently staged for commit in a Git repository.
Key changes include:
- New
Stagedoption: Added toScanOptionsinservices/scanner.goand exposed viaflags.goandactions/scan.go. - Git integration: A new
services/git.gofile provides aStagedFilesfunction to query Git for staged files usinggit diff --cached --name-only --diff-filter=ACM. - Clone filtering: The
services/scanner.gonow filters the detected clones, keeping only those where at least one instance's file path is present in the list of staged files. - Graceful fallback: If
dupehoundis run with--stagedoutside a Git repository or ifgitis not available, it logs a warning and proceeds as if no files are staged (i.e., reports no clones). - Comprehensive testing: New unit tests for the Git integration and clone filtering, along with integration tests for the
--stagedCLI flag, ensure correct behavior and exit codes.
Concerns (if any)
No critical concerns were identified. The implementation correctly handles the specified use case and edge cases like not being in a Git repository.
Verdict
Approve
Code review performed by GEMINI - gemini-2.5-flash.
| cmd.Dir = repoRoot | ||
| out, err := cmd.Output() | ||
| if err != nil { | ||
| // Not a git repo or git not installed — not an error, just no staged files. |
There was a problem hiding this comment.
[SUGGESTION] The current error handling for cmd.Output() logs a warning and returns nil, nil if the git command fails. While this aligns with the "graceful fallback" described in the PR for cases like not being in a git repo or git not being installed, it might mask other potential git command errors (e.g., permissions issues, corrupted repo state) that could be more indicative of a problem. Consider if a more specific error check for exec.ErrNotFound or exit status 128 (common for not a git repo) could differentiate between a true "not a git repo" scenario and other git command failures, potentially returning an error for the latter if deemed appropriate for debugging or stricter environments. However, for a pre-commit hook, the current behavior is likely acceptable.
Summary
Closes #5
--stagedflag restricts clone reporting to files staged for commit--pathfor cross-comparison, but only reports clones where at least one instance is in a staged filegit diff --cached --name-only --diff-filter=ACMto resolve staged filesExample pre-commit hook
#!/bin/sh dupehound scan --staged --min-tokens 50Test plan
StagedFilesreturns correct files, handles non-git dirsfilterStagedCloneskeeps only clones touching staged files--stagedon dupehound repo with no staged files → 0 clones