BlockWatch
BlockWatch is a linter that keeps your code, documentation, and configuration in sync.
It helps you avoid broken docs and messy config files by enforcing rules directly in your comments. You can link code to documentation, auto-sort lists, ensure uniqueness, and even validate content with Regex or AI.
It works with almost any language (Rust, Python, JS, Go, Markdown, YAML, etc.) and can run on your entire repo or just your VCS diffs.
Features
- Drift Detection: Link a block of code to its documentation. If you change the code but forget the docs, BlockWatch alerts you.
- Strict Formatting: Enforce sorted lists (
keep-sorted) and unique entries (keep-unique) so you don't have to nitpick in code reviews. - Content Validation: Check lines against Regex patterns (
line-pattern) or enforce block size limits (line-count). - AI Rules: Use natural language to validate code or text (e.g., "Must mention 'banana'").
- Flexible: Run it on specific files, glob patterns, or just your unstaged changes.
Installation
Homebrew (macOS/Linux)
brew tap mennanov/tap
brew install blockwatch
From Source (Rust)
cargo install blockwatch
Prebuilt Binaries
Check the Releases page for prebuilt binaries.
How It Works
You define rules using XML-like tags inside your comments.
Linking Code Blocks (affects)
This ensures that if you change some block of code, you're forced to look at the other blocks too.
src/lib.rs:
// <block affects="README.md:supported-langs">
// </block>
README.html:
<!-- <block name="supported-langs"> -->
- Rust
- Python
<!-- </block> -->
If you modify the enum in src/lib.rs, BlockWatch will fail until you touch README.md as well.
Enforce Sort Order (keep-sorted)
Keep lists alphabetized. Default is asc (ascending).
# <block keep-sorted>
,
,
,
# </block>
Sort by Regex
You can sort by a specific part of the line using a regex capture group named value.
=
Enforce Unique Lines (keep-unique)
Prevent duplicates in a list.
# <block keep-unique>
,
,
,
# </block>
Uniqueness by Regex
Just like sorting, you can check uniqueness based on a specific regex match.
=
Regex Validation (line-pattern)
Ensure every line matches a specific regex pattern.
=
Enforce Line Count (line-count)
Enforce the number of lines in a block.
Supported operators: <, >, <=, >=, ==.
# <block line-count="<=5">
,
,
# </block>
Validate with AI (check-ai)
Use an LLM to validate logic or style.
<!-- <block check-ai="Must mention the company name 'Acme Corp'"> -->
Welcome to Acme Corp!
<!-- </block> -->
Targeted AI Checks
Use check-ai-pattern to send only specific parts of the text to the LLM.
=
check-ai configuration
BLOCKWATCH_AI_API_KEY: API Key (OpenAI compatible).BLOCKWATCH_AI_MODEL: Model name (default:gpt-5-nano).BLOCKWATCH_AI_API_URL: Custom API URL (optional).
Usage
1. Run Locally
Validate all blocks in your project:
# Check everything
blockwatch
# Check specific files
blockwatch "src/**/*.rs" "**/*.md"
# Ignore stuff
blockwatch "**/*.rs" --ignore "**/generated/**"
Tip: Quote your glob patterns so the shell doesn't expand them before BlockWatch sees them.
1.1 Listing Blocks
You can list all blocks that BlockWatch finds without running any validation. This is useful for auditing your blocks or debugging your configuration.
# List all blocks in the current directory
blockwatch list
# List blocks in specific files
blockwatch list "src/**/*.rs" "**/*.md"
# List only blocks affected by current changes
git diff | blockwatch list
The output is a JSON object where keys are file paths and values are lists of blocks found in those files, including their names, line numbers, and attributes.
Example Output
2. Check Only What Changed
Pipe a git diff to BlockWatch to validate only the blocks you touched. This is perfect for pre-commit hooks.
# Check unstaged changes
git diff --patch | blockwatch
# Check staged changes
git diff --cached --patch | blockwatch
# Check changes in a specific file only
git diff --patch path/to/file | blockwatch
# Check changes and some other (possibly unchanged) files
git diff --patch | blockwatch "src/always_checked.rs" "**/*.md"
3. CI Integration
Pre-commit Hook
Add this to .pre-commit-config.yaml:
- repo: local
hooks:
- id: blockwatch
name: blockwatch
entry: bash -c 'git diff --patch --cached --unified=0 | blockwatch'
language: system
stages:
pass_filenames: false
GitHub Action
Add this to .github/workflows/your_workflow.yml:
- uses: mennanov/blockwatch-action@v1
Supported Languages
BlockWatch supports comments in:
- Bash
- C#
- C/C++
- CSS
- Go (with
go.mod,go.sumandgo.worksupport) - HTML
- Java
- JavaScript
- Kotlin
- Makefile
- Markdown
- PHP
- Python
- Ruby
- Rust
- SQL
- Swift
- TOML
- TypeScript
- XML
- YAML
CLI Options
- List Blocks:
blockwatch listoutputs a JSON report of all found blocks. - Extensions: Map custom extensions:
blockwatch -E cxx=cpp - Disable Validators:
blockwatch -d check-ai - Enable Validators:
blockwatch -e keep-sorted - Ignore Files:
blockwatch --ignore "**/generated/**"
Known Limitations
- Deleted blocks are ignored.
- Files with unsupported grammar are ignored.
Contributing
Contributions are welcome! A good place to start is by adding support for a new grammar.
Run Tests
cargo test