Skip to main content

treepeat: a treesitter-based CLI tool to detect similar code

Project description

Treepeat - finding groups of similar code

treepeat is a CLI tool that detects similar code using treesitter AST analysis with locality-sensitive hashing:

  • Find duplicate code blocks meaningful to the language (classes/functions), not just lines.
  • near-duplicates
  • structurally similar text.

Helpers: This is very much an proof of concept - I'm happy with it, but I haven't supported very many languages at present. PRs welcome!

Usage

detect

Scan a codebase for similar or duplicate code blocks using tree-sitter AST analysis and locality-sensitive hashing.

Key flags:

  • --ruleset: Normalization ruleset to use (none, default, loose) - controls how code is normalized before comparison
  • --similarity: Percent similarity from 1-100 (default: 100 for exact duplicates)
  • --min-lines: Minimum number of lines for a match (default: 5)
  • --diff: Show side-by-side comparisons of similar blocks
  • --format: Output format - console (default) or sarif for CI integration
# Find exact duplicates
treepeat detect /path/to/codebase

# Find near-duplicates with 80% similarity threshold
treepeat detect --similarity 80 /path/to/codebase

# Show diffs between similar blocks and use loose ruleset
treepeat --ruleset loose detect --diff --min-lines 10 /path/to/codebase

# Output results in SARIF format for CI tools
treepeat detect --format sarif -o results.sarif /path/to/codebase

Other sub commands

list-ruleset

List all rules in a ruleset, along with their descriptions. Use --language to see which rules apply to a specific language.

treesitter

Display how treepeat normalizes source code into tree-sitter tokens for similarity detection -- helpful for debugging why a certain section of a file might be similar to another. Shows the original source code side-by-side with the normalized token representation.

Dev setup

make setup
make test

Benchmarks

treepeat includes a testing framework for comparing duplication detection tools against real-world codebases.

# Run all benchmark tests
make benchmark

# Compare results across tools
make benchmark-compare

ADRs

Architecture Decision Records live in docs/adr.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treepeat-0.0.1.tar.gz (49.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

treepeat-0.0.1-py3-none-any.whl (63.9 kB view details)

Uploaded Python 3

File details

Details for the file treepeat-0.0.1.tar.gz.

File metadata

  • Download URL: treepeat-0.0.1.tar.gz
  • Upload date:
  • Size: 49.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.24

File hashes

Hashes for treepeat-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9df6b6b08fa213b975d4b2f82cde90fd0f06514b13971f97c96a95e9b5f0db8f
MD5 d2875643e6b82c721621f9d9835ed543
BLAKE2b-256 70d51a7557f9779a804fbc4c46794b0006245ffaad3d29488a458402e69473de

See more details on using hashes here.

File details

Details for the file treepeat-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: treepeat-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 63.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.24

File hashes

Hashes for treepeat-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ddbbaff5896c7bf6d88138b340f85e53b1614af65ff6642a25eec54cfc6e92c
MD5 39244285af6b1876d0938b70978ab801
BLAKE2b-256 a4a02150f1b4cb783276a35823fe8121ca381804a4706bdd3b2d8c1fcef1bb6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page