Skip to content

feat(#5): --staged flag for pre-commit git hook integration - #13

Merged
machado144 merged 1 commit into
mainfrom
feat/staged-flag
Apr 2, 2026
Merged

machado144 merged 1 commit into
mainfrom
feat/staged-flag

Conversation

@machado144

Copy link
Copy Markdown
Contributor

Summary

Closes #5

  • New --staged flag restricts clone reporting to files staged for commit
  • Scans ALL files in --path for cross-comparison, but only reports clones where at least one instance is in a staged file
  • Uses git diff --cached --name-only --diff-filter=ACM to resolve staged files
  • Graceful fallback: warns and returns 0 clones if not inside a git repo
  • Enables fast, focused pre-commit hooks that flag new duplication without flagging existing debt

Example pre-commit hook

#!/bin/sh
dupehound scan --staged --min-tokens 50

Test plan

  • 161 tests pass (154 existing + 7 new)
  • Unit: StagedFiles returns correct files, handles non-git dirs
  • Unit: filterStagedClones keeps only clones touching staged files
  • Integration: staged duplicates → exit 1
  • Integration: no staged files → exit 0
  • Integration: staged file duplicating committed file → clone reported
  • Manual: --staged on dupehound repo with no staged files → 0 clones

- 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
@github-actions

github-actions Bot commented Apr 2, 2026

Copy link
Copy Markdown

StructLint — All checks passed

50 rules validated against .structlint.yaml. No violations found.

View full run · Powered by StructLint

@github-actions

github-actions Bot commented Apr 2, 2026

Copy link
Copy Markdown

dupehound scan results

Metric Value
Files scanned 12 / 12
Total lines 2097
Clones found 6
Duplicate lines 99 (4.7%)
Breakdown 1 type-1, 4 type-2, 1 type-3

Hotspots

Dup % Dup Lines Total Lines File
19.6% 85 434 services/reporter.go
2.4% 14 586 services/detector.go

Top clones (by impact)

#1   type-2   similarity: 1.00   17 lines   2 instances

Locations:

  • services/reporter.go:94-110
  • services/reporter.go:170-188

Preview:

	fmt.Fprintf(&b, "dupehound scan results\n")
	fmt.Fprintf(&b, "======================\n")
	fmt.Fprintf(&b, "Files scanned : %d / %d\n", report.ScannedFiles, report.TotalFiles)
	fmt.Fprintf(&b, "Total lines   : %d\n", report.TotalLines)
	fmt.Fprintf(&b, "Clones found  : %d\n", report.TotalClones)
	fmt.Fprintf(&b, "Duplicate lines: %d (%.1f%%)\n", report.DuplicateLines, report.DuplicationPct)

	if report.TotalClones == 0 {
		fmt.Fprintf(&b, "\nNo duplicates found.\n")
		return b.String()
// ... 7 more lines
#2   type-2   similarity: 1.00   10 lines   2 instances

Locations:

  • services/reporter.go:117-126
  • services/reporter.go:206-216

Preview:

			fmt.Fprintf(&b, "  ... %d more files (use --verbose to show all)\n", len(report.FileStats)-limit)
		}
	}

	sorted := sortClonesByImpact(report.Clones)
	cloneLimit := topN(len(sorted), defaultTopClones, opts.Verbose)

	fmt.Fprintf(&b, "\nTop clones (by impact):\n")
	for i := 0; i < cloneLimit; i++ {
		writeCloneText(&b, i+1, sorted[i], opts, true)
#3   type-2   similarity: 1.00   9 lines   2 instances

Locations:

  • services/reporter.go:109-117
  • services/reporter.go:189-198

Preview:

		limit := topN(len(report.FileStats), defaultTopHotspots, opts.Verbose)
		fmt.Fprintf(&b, "\nHotspots (files by duplication %%):\n")
		for i := 0; i < limit; i++ {
			fs := report.FileStats[i]
			rel := relPath(fs.File, opts.ScanPath)
			fmt.Fprintf(&b, "  %5.1f%%  %4d/%4d lines  %s\n", fs.DuplicationPct, fs.DuplicateLines, fs.TotalLines, rel)
		}
		if len(report.FileStats) > limit {
			fmt.Fprintf(&b, "  ... %d more files (use --verbose to show all)\n", len(report.FileStats)-limit)
#4   type-1   similarity: 1.00   6 lines   2 instances

Locations:

  • services/detector.go:544-549
  • services/detector.go:559-564

Preview:

	h := fnv.New64a()
	for _, t := range tokens {
		_, _ = h.Write([]byte{byte(t.Kind)})
		if t.Text != "" {
			_, _ = h.Write([]byte(t.Text))
		}
#5   type-2   similarity: 1.00   6 lines   2 instances

Locations:

  • services/reporter.go:153-158
  • services/reporter.go:251-256

Preview:

			for _, line := range lines[:limit] {
				fmt.Fprintf(b, "        | %s\n", strings.TrimRight(line, " \t"))
			}
			if len(lines) > previewMaxLines {
				fmt.Fprintf(b, "        ... (%d more lines)\n", len(lines)-previewMaxLines)
			}
#6   type-3   similarity: 0.94   6 lines   2 instances

Locations:

  • services/detector.go:545-550
  • services/detector.go:560-565

Preview:

	for _, t := range tokens {
		_, _ = h.Write([]byte{byte(t.Kind)})
		if t.Text != "" {
			_, _ = h.Write([]byte(t.Text))
		}
		_, _ = h.Write([]byte{0}) // separator

View full run · Scanned by dupehound

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Staged option: Added to ScanOptions in services/scanner.go and exposed via flags.go and actions/scan.go.
  • Git integration: A new services/git.go file provides a StagedFiles function to query Git for staged files using git diff --cached --name-only --diff-filter=ACM.
  • Clone filtering: The services/scanner.go now 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 dupehound is run with --staged outside a Git repository or if git is 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 --staged CLI 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.

Comment thread services/git.go
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@machado144
machado144 merged commit 3d23343 into main Apr 2, 2026
7 checks passed
@machado144
machado144 deleted the feat/staged-flag branch April 2, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: --staged flag for pre-commit git hook integration

1 participant

⌂ 换网址