Code similarity detection using treesitter. Contribute to dsummersl/treepeat development by creating an account on GitHub.
Find duplicate code blocks meaningful to the language (classes/functions), not just lines.
Find near-duplicates: ignore whitespace, strings, high level AST nodes such as function and names.
Find structurally similar code: anonymize identifiers, constants, etc.
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.
Languages supported: astro, bash, css, go, html, javascript, lua, markdown (plus codeblocks), python, sql, typescript, java, kotlin, rust, yaml
8 comments
I ended up writing my own tool [1] that uses vector search, which works but can be unusably slow in large codebases.
I will give this a shot.
I was fortunate to have a contributor push me to consider optimizations a few months back, and it definitely helped improve the speed/memory metrics.
I see the CLI knobs are somewhat similar ('min lines', 'thresholds'), however your 'search' is fascinating. I could see myself wondering "how much error handling is duplicated for HTTP responses?" and your tool would give me a start at an answer. Nice!
For active projects I setup a weekly-ish 'code audit' agent: it runs treepeat to find opportunities to refactor and bumps up enforced coverage numbers. Over time for some projects I lower the '--similarity' threshold to more broadly find similar structured code (potentially more ambitious refactoring)
Read the full thread on Hacker News →
Related stories
- Lobsters · 86 points · about 1 year ago
- Hacker News · 8 points · 6 days ago
- Hacker News · 14 points · 8 days ago
- DEV Community · 55 points · 9 days ago
- DEV Community · 14 points · 11 days ago
- Hacker News · 7 points · 10 days ago