Skip to content

Commit 22edf29

Browse files
Linuxdazhaoclaude
andcommitted
fix(pre-commit): align cargo-fmt hook with CI and apply rustfmt 1.9.0
Three things needed to be fixed together for the pre-commit guard to match what CI runs: 1. ccs-proxy/tests/store_fs.rs: re-format with rustfmt 1.9.0 (the version that ships with Rust 1.95 bumped in 208cfae). CI was red on all three platforms because of this single file. 2. .pre-commit-config.yaml: change `cargo fmt --check` to `cargo fmt --all -- --check`. Without `--all`, fmt only scans the root package and silently ignores the `ccs-proxy` workspace member, which is exactly why the bad commit slipped through the hook. 3. scripts/setup-pre-commit.sh + CLAUDE.md: document that we now use `prek` (a Rust-native, drop-in compatible replacement for the Python pre-commit tool) and point the setup script at it. The old .git/hooks/pre-commit was a hand-written shell that bypassed prek entirely; `prek install --overwrite` now manages the hook. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 552757a commit 22edf29

4 files changed

Lines changed: 78 additions & 74 deletions

File tree

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
repos:
44
- repo: local
55
hooks:
6-
# Format code with rustfmt
6+
# Format code with rustfmt (must match CI: `cargo fmt --all -- --check`)
77
- id: cargo-fmt
88
name: cargo fmt
9-
entry: cargo fmt --check
9+
entry: cargo fmt --all -- --check
1010
language: system
1111
types: [rust]
1212
pass_filenames: false

CLAUDE.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,27 @@ cargo clippy -- -W warnings
7979
cargo audit
8080
```
8181

82-
### Pre-commit Hooks
82+
### Pre-commit Hooks (prek)
83+
84+
We use [`prek`](https://github.com/j178/prek), a Rust-native drop-in
85+
replacement for the Python `pre-commit` tool. It reads the same
86+
`.pre-commit-config.yaml`.
8387

8488
```bash
85-
# One-time setup
89+
# One-time setup (installs prek + the git hook)
8690
./scripts/setup-pre-commit.sh
8791

8892
# Run on all files
89-
pre-commit run --all-files
93+
prek run --all-files
9094

91-
# Run on specific files
92-
pre-commit run --files src/main.rs
95+
# Run a single hook
96+
prek run cargo-fmt --all-files
9397

94-
# Update hooks
95-
pre-commit autoupdate
98+
# Update hook versions
99+
prek autoupdate
96100

97-
# Uninstall hooks
98-
pre-commit uninstall
101+
# Uninstall the git hook
102+
prek uninstall
99103
```
100104

101105
### Version Management and Release
@@ -451,12 +455,12 @@ cargo test -- --nocapture
451455

452456
**Automatic Checks** (run on every commit):
453457

454-
- `cargo check` - Compilation verification
455-
- `cargo fmt --check` - Code formatting
458+
- `cargo fmt --all -- --check` - Code formatting (matches CI; `--all` covers the whole workspace including `ccs-proxy`)
456459
- `cargo clippy -- -D warnings` - Linting (warnings as errors)
457460
- `cargo test` - All tests
458461
- `cargo audit` - Security vulnerability scan
459462
- `cargo doc --no-deps` - Documentation build
463+
- `cargo build --release` - Release build (artifacts cleaned after)
460464

461465
**Setup**: `./scripts/setup-pre-commit.sh`
462466

ccs-proxy/tests/store_fs.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,19 @@ async fn append_does_not_overwrite_existing_cwd() {
134134
store.init_session(meta).await.unwrap();
135135
store
136136
.append(rec_with_system(
137-
1, "s_cwd2", "claude-opus-4-7",
137+
1,
138+
"s_cwd2",
139+
"claude-opus-4-7",
138140
"Primary working directory: /first\n",
139141
))
140142
.await
141143
.unwrap();
142144
// Second request with a different cwd marker — should be ignored.
143145
store
144146
.append(rec_with_system(
145-
2, "s_cwd2", "claude-opus-4-7",
147+
2,
148+
"s_cwd2",
149+
"claude-opus-4-7",
146150
"Primary working directory: /second\n",
147151
))
148152
.await
@@ -170,14 +174,26 @@ async fn append_dedupes_and_appends_models() {
170174
};
171175
let store: Arc<dyn Store> = Arc::new(FsStore::open(dir.path().to_path_buf()).unwrap());
172176
store.init_session(meta).await.unwrap();
173-
store.append(rec_with_system(1, "s_m", "claude-opus-4-7", "")).await.unwrap();
174-
store.append(rec_with_system(2, "s_m", "claude-sonnet-4-6", "")).await.unwrap();
175-
store.append(rec_with_system(3, "s_m", "claude-opus-4-7", "")).await.unwrap();
177+
store
178+
.append(rec_with_system(1, "s_m", "claude-opus-4-7", ""))
179+
.await
180+
.unwrap();
181+
store
182+
.append(rec_with_system(2, "s_m", "claude-sonnet-4-6", ""))
183+
.await
184+
.unwrap();
185+
store
186+
.append(rec_with_system(3, "s_m", "claude-opus-4-7", ""))
187+
.await
188+
.unwrap();
176189

177190
let sessions = store.list_sessions().await.unwrap();
178191
assert_eq!(
179192
sessions[0].models,
180-
vec!["claude-opus-4-7".to_string(), "claude-sonnet-4-6".to_string()]
193+
vec![
194+
"claude-opus-4-7".to_string(),
195+
"claude-sonnet-4-6".to_string()
196+
]
181197
);
182198
}
183199

scripts/setup-pre-commit.sh

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,52 @@
11
#!/bin/bash
22

3-
# Pre-commit setup script for cc_auto_switch
4-
# This script installs pre-commit and sets up the hooks
3+
# Pre-commit setup script for cc_auto_switch.
4+
# Installs `prek` (Rust-based, drop-in compatible with .pre-commit-config.yaml)
5+
# and wires up its hook into .git/hooks/pre-commit.
56

67
set -e
78

8-
echo "🔧 Setting up pre-commit hooks for cc_auto_switch..."
9+
echo "🔧 Setting up prek hooks for cc_auto_switch..."
910

10-
# Check if pre-commit is installed
11-
if ! command -v pre-commit &> /dev/null; then
12-
echo "📦 Installing pre-commit..."
13-
14-
# Try different installation methods based on what's available
15-
if command -v pipx &> /dev/null; then
16-
echo " Using pipx to install pre-commit..."
17-
pipx install pre-commit
18-
elif command -v brew &> /dev/null; then
19-
echo " Using Homebrew to install pre-commit..."
20-
brew install pre-commit
21-
elif command -v pip3 &> /dev/null; then
22-
echo " Using pip3 with --user flag..."
23-
pip3 install --user pre-commit
24-
elif command -v pip &> /dev/null; then
25-
echo " Using pip with --user flag..."
26-
pip install --user pre-commit
11+
if ! command -v prek &> /dev/null; then
12+
echo "📦 Installing prek..."
13+
14+
if command -v brew &> /dev/null; then
15+
echo " Using Homebrew to install prek..."
16+
brew install prek
17+
elif command -v cargo &> /dev/null; then
18+
echo " Using cargo to install prek..."
19+
cargo install prek
2720
else
28-
echo "❌ Error: No package manager found. Please install pre-commit manually:"
29-
echo " Option 1: brew install pre-commit"
30-
echo " Option 2: pipx install pre-commit"
31-
echo " Option 3: pip3 install --user pre-commit"
21+
echo "❌ Error: No supported installer found. Install prek manually:"
22+
echo " Option 1: brew install prek"
23+
echo " Option 2: cargo install prek"
24+
echo " See https://github.com/j178/prek for other options."
3225
exit 1
3326
fi
3427
else
35-
echo "pre-commit is already installed"
28+
echo "prek is already installed ($(prek --version))"
3629
fi
3730

38-
# Install the pre-commit hooks
39-
echo "🔗 Installing pre-commit hooks..."
40-
pre-commit install
31+
echo "🔗 Installing prek git hook..."
32+
prek install --overwrite
4133

42-
# Verify installation
43-
echo "🔍 Verifying installation..."
44-
if pre-commit --version &> /dev/null; then
45-
echo "✅ Pre-commit setup completed successfully!"
46-
echo ""
47-
echo "📋 Available hooks:"
48-
echo " • cargo-fmt: Code formatting check"
49-
echo " • cargo-clippy: Linting with warnings as errors"
50-
echo " • cargo-test: Run all tests"
51-
echo " • cargo-audit: Security vulnerability scan (auto-installs if needed)"
52-
echo " • cargo-doc: Documentation build check"
53-
echo " • cargo-build-release: Release build + compile verification (removes artifacts after)"
54-
echo ""
55-
echo "🔄 Testing hooks (dry run)..."
56-
pre-commit run --all-files --show-diff-on-failure || echo "⚠️ Some hooks may need fixes before committing"
57-
echo ""
58-
echo "💡 Usage:"
59-
echo " • Run manually: pre-commit run --all-files"
60-
echo " • Run on specific files: pre-commit run --files src/main.rs"
61-
echo " • Skip hooks: git commit --no-verify"
62-
echo " • Update hooks: pre-commit autoupdate"
63-
echo ""
64-
echo "🚀 Ready to go! All CI checks will now run locally before commits."
65-
else
66-
echo "❌ Error: pre-commit installation failed"
67-
exit 1
68-
fi
34+
echo ""
35+
echo "📋 Configured hooks (from .pre-commit-config.yaml):"
36+
echo " • cargo-fmt: cargo fmt --all -- --check (matches CI)"
37+
echo " • cargo-clippy: Linting with warnings as errors"
38+
echo " • cargo-test: Run all tests"
39+
echo " • cargo-audit: Security vulnerability scan (auto-installs if needed)"
40+
echo " • cargo-doc: Documentation build check"
41+
echo " • cargo-build-release: Release build + compile verification"
42+
echo ""
43+
echo "🔄 Dry run on all files..."
44+
prek run --all-files || echo "⚠️ Some hooks reported issues — fix before committing"
45+
echo ""
46+
echo "💡 Usage:"
47+
echo " • Run manually: prek run --all-files"
48+
echo " • Run a single hook: prek run cargo-fmt --all-files"
49+
echo " • Skip hooks for one commit: git commit --no-verify"
50+
echo " • Update hook versions: prek autoupdate"
51+
echo ""
52+
echo "🚀 Ready. CI-equivalent checks will run before every commit."

0 commit comments

Comments
 (0)