Skip to content

ericodx/swift-cpd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

148 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Swift Code Duplication Detector

Swift Package Index Swift Package Index CI Quality Gate Status Coverage mutation score

Detect and eliminate duplicated logic in Swift and Objective-C/C codebases to improve maintainability and code quality.

swift-cpd performs structural analysis to detect duplication patterns across Swift, Objective-C, and C codebases, going beyond simple text-based detection.


Why

Code duplication leads to:

  • inconsistent behavior across features
  • fragile refactors and hidden regressions
  • increased maintenance cost and cognitive load

swift-cpd helps you detect and address duplication early, supporting long-term code health and developer productivity.


Features

  • Structural duplication detection (AST-based)
  • Works with Swift, Objective-C, and C codebases
  • Enforces duplication rules in CI pipelines
  • Supports code quality and governance practices

Install

brew tap ericodx/homebrew-tools
brew install swift-cpd

Other installation methods — pre-built binary, build from source, pre-commit hook, Xcode plugin — are covered in the Installation Guide.

Quick start

# Generate a config file (auto-detects your source directories)
swift-cpd init

# Run
swift-cpd

Example output:

Clone detected — Type 2 | 15 lines | 120 tokens | 100.0% similarity
  Sources/App/Services/UserService.swift    :  34 –  48
  Sources/App/Services/ProductService.swift :  71 –  85

1 clone(s) found in 32 file(s) — 2.1% duplication — 0.8s

Configuration

Drop a .swift-cpd.yml in the project root to control paths, thresholds, excluded files, and output format:

paths:
  - Sources/
minimumTokenCount: 50
minimumLineCount: 5
enabledCloneTypes: [1, 2, 3, 4]
ignoreSameFile: true
exclude:
  - "**/*Tests*"
  - "**/*.generated.swift"

Full reference in the Usage & Configuration Guide.

Documentation

Document Description
Installation Homebrew, binary, source, pre-commit, Xcode plugin
Usage & Configuration CLI options, YAML config, output formats, CI integration
Xcode Plugin Step-by-step Xcode and SPM plugin setup
Architecture System design, pipeline, detection algorithms
CodeBase Reference Every type, protocol, and algorithm documented

About

Detect and eliminate duplicated logic in Swift and Objective-C/C codebases to improve maintainability and code quality.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages