CodeCharter in CI is mandatory. CodeCharter locally before committing is optional — but it saves iteration time because you don't have to wait for the CI build to find out whether a convention was violated.
Using Git hooks directly
.git/hooks/pre-commit:
#!/bin/sh
set -e
# Quick check: validate rules only
codecharter validate || {
echo "❌ CodeCharter rules invalid. Fix .codecharter/rules/ before committing."
exit 1
}
# Optional: full analysis, errors only
codecharter analyze . --fail-on error --output console || {
echo ""
echo "❌ CodeCharter found blocking issues."
echo "Run 'codecharter analyze .' for details, or commit with --no-verify if you really want."
exit 1
}
chmod +x .git/hooks/pre-commit.
On Windows: the same script works with Git for Windows (Git Bash).
Using pre-commit (the Python tool)
If you already use pre-commit, add this to
.pre-commit-config.yaml:
repos:
- repo: local
hooks:
- id: codecharter-validate
name: CodeCharter validate rules
entry: codecharter validate
language: system
pass_filenames: false
files: ^\.codecharter/
- id: codecharter-analyze
name: CodeCharter analyze
entry: codecharter analyze . --fail-on error
language: system
pass_filenames: false
types: [csharp]
Using Husky (Node.js)
For JS/TS repos that also happen to have C#, or mixed teams:
.husky/pre-commit:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
codecharter analyze . --fail-on error
Run the hook only on C# changes
A full analysis before every commit can take longer on large solutions. Skip the run
if the commit doesn't touch any .cs files:
#!/bin/sh
CHANGED=$(git diff --cached --name-only --diff-filter=ACMR -- '*.cs')
if [ -z "$CHANGED" ]; then
exit 0
fi
codecharter analyze MySolution.sln --fail-on error
Recommended strategy
| Step | Hook |
|---|---|
pre-commit |
codecharter validate (one second) |
pre-push |
codecharter analyze . --fail-on error (warm cache: a few seconds) |
| CI | codecharter analyze . --fail-on error (always) |
This keeps the hooks in the hot path fast and catches obvious mistakes without making every commit take fifteen seconds.