Skip to main content

treepeat: a tool to find similarities in a codebase

Project description

Overview

treepeat is a tool that analyzes 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, ignoring 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!

Current languages supported so far: bash, css, go, html, javascript, markdown, python, sql, typescript, java, kotlin

Usage

Installation

pip insteall 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
# 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

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.3.0.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.3.0-py3-none-any.whl (63.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: treepeat-0.3.0.tar.gz
  • Upload date:
  • Size: 49.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"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.3.0.tar.gz
Algorithm Hash digest
SHA256 df42926dcf85cb81e639c618f8d8114e89185ca4545730b4a2928d222ee0a5b9
MD5 c7681bcd3ed5d0c13e7443e45f704a5b
BLAKE2b-256 bb8a9ff0b6b8c188c040b364592462de62b114fe96cb2f20c89716bdcb22237f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: treepeat-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 63.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64e9683a89963770fc5d74154205b8f1a3c3074989ad66dd59604da3f5251d95
MD5 32e9d0443845955daf210d250248c8a8
BLAKE2b-256 8b1ebdcbd4ecb0740e715682ad34ff55aa14895656f0c377e91e5f56ff95480e

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