Skip to main content

treepeat: a tool to find similarities in a codebase

Project description

Overview

treepeat is a tool that finds similarities in your codebase.

  • Find duplicate code blocks meaningful to the language (classes/functions), not just lines (--ruleset none)
  • Find near-duplicates, ignore whitespace, strings, high level AST nodes such as function and names (--ruleset default)
  • Find structurally similar code, anonymizing identifiers, constants, etc (--ruleset loose)

Pull requests welcome: 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!

Languages supported: astro, bash, css, go, html, javascript, markdown, python, sql, typescript, java, kotlin, rust

Usage

Installation

pip install treepeat

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
  • --verbose: Show additional run metrics, including per-stage timing when available
  • --progress: Show progress bars for long-running pipeline stages
# 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

# Show parse progress and verbose stage timing
treepeat detect --progress --verbose /path/to/codebase

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

--progress is intended primarily as interactive CLI feedback. The current implementation writes tqdm progress bars to stderr, leaving normal command output on stdout or --output.

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

Performance harness for benchmarking real repositories: tools/perf/harness.py (docs).

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.6.0.tar.gz (57.5 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.6.0-py3-none-any.whl (73.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: treepeat-0.6.0.tar.gz
  • Upload date:
  • Size: 57.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for treepeat-0.6.0.tar.gz
Algorithm Hash digest
SHA256 6b34939047bb5f95625cddc82a89b027f6f07726d68e7b9dcb1a5d33470ce426
MD5 b0460fdae3be21360731186f9b5323a4
BLAKE2b-256 ca7e4f8e5662d6e7fa213d500b71796ac06bd0ac353931b210e60c04baf1222b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: treepeat-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 73.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for treepeat-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed17fc5409f913e7e3ea75eb1da1ec95c214311143f1d574dc6a8569fc3e5131
MD5 eb1a4e15d0ea25f9668e821c2b2b3cbc
BLAKE2b-256 387f1b75c74b07e4f10e70adc1e947425c80e29f171044388bdf2d6f8a8a2ac5

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