Skip to content

Commit 675ce03

Browse files
authored
Merge pull request #6 from douglaz/feat/add-git-hooks
feat: add Git hooks for code quality checks
2 parents a75aab0 + 99b7c42 commit 675ce03

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

.githooks/pre-commit

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Check if we're in a nix environment
5+
if command -v nix >/dev/null 2>&1 && [ -f "flake.nix" ]; then
6+
# Use nix develop for consistent environment
7+
if ! nix develop -c cargo fmt --check 2>/dev/null; then
8+
echo "❌ Code is not formatted. Please run 'nix develop -c cargo fmt' before committing."
9+
echo ""
10+
echo "To see what needs formatting:"
11+
echo " nix develop -c cargo fmt --check --verbose"
12+
exit 1
13+
fi
14+
else
15+
# Fallback to regular cargo
16+
if ! cargo fmt --check 2>/dev/null; then
17+
echo "❌ Code is not formatted. Please run 'cargo fmt' before committing."
18+
echo ""
19+
echo "To see what needs formatting:"
20+
echo " cargo fmt --check --verbose"
21+
exit 1
22+
fi
23+
fi
24+
25+
echo "✅ Code formatting check passed"

.githooks/pre-push

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
echo "🔍 Running pre-push checks..."
5+
echo ""
6+
7+
# Check if we're in a nix environment
8+
if command -v nix >/dev/null 2>&1 && [ -f "flake.nix" ]; then
9+
# Check formatting
10+
echo "📝 Checking code formatting..."
11+
if ! nix develop -c cargo fmt --check 2>/dev/null; then
12+
echo ""
13+
echo "❌ Code is not formatted. Please run 'nix develop -c cargo fmt' before pushing."
14+
echo ""
15+
echo "To see what needs formatting:"
16+
echo " nix develop -c cargo fmt --check --verbose"
17+
exit 1
18+
fi
19+
echo "✅ Formatting check passed"
20+
echo ""
21+
22+
# Run clippy
23+
echo "🔎 Running clippy linting..."
24+
if ! nix develop -c cargo clippy --workspace -- -D warnings 2>&1; then
25+
echo ""
26+
echo "❌ Clippy found issues. Please fix them before pushing."
27+
echo ""
28+
echo "To see all issues:"
29+
echo " nix develop -c cargo clippy --workspace"
30+
echo ""
31+
echo "To attempt automatic fixes:"
32+
echo " nix develop -c cargo clippy --workspace --fix --allow-dirty"
33+
exit 1
34+
fi
35+
echo "✅ Clippy check passed"
36+
else
37+
# Fallback to regular cargo
38+
echo "📝 Checking code formatting..."
39+
if ! cargo fmt --check 2>/dev/null; then
40+
echo ""
41+
echo "❌ Code is not formatted. Please run 'cargo fmt' before pushing."
42+
echo ""
43+
echo "To see what needs formatting:"
44+
echo " cargo fmt --check --verbose"
45+
exit 1
46+
fi
47+
echo "✅ Formatting check passed"
48+
echo ""
49+
50+
# Run clippy
51+
echo "🔎 Running clippy linting..."
52+
if ! cargo clippy --workspace -- -D warnings 2>&1; then
53+
echo ""
54+
echo "❌ Clippy found issues. Please fix them before pushing."
55+
echo ""
56+
echo "To see all issues:"
57+
echo " cargo clippy --workspace"
58+
echo ""
59+
echo "To attempt automatic fixes:"
60+
echo " cargo clippy --workspace --fix --allow-dirty"
61+
exit 1
62+
fi
63+
echo "✅ Clippy check passed"
64+
fi
65+
66+
echo ""
67+
echo "✅ All pre-push checks passed! Proceeding with push..."
68+
echo ""

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,55 @@ $ nix develop
5656
$ cargo build --release -F alkali
5757
```
5858

59+
## Development
60+
61+
This project uses Git hooks for maintaining code quality. When you enter the development environment, hooks are automatically configured:
62+
63+
```bash
64+
$ nix develop
65+
📎 Setting up Git hooks for code quality checks...
66+
✅ Git hooks configured automatically!
67+
• pre-commit: Checks code formatting
68+
• pre-push: Runs formatting and clippy checks
69+
```
70+
71+
### Git Hooks
72+
73+
The project includes two Git hooks that help maintain code quality:
74+
75+
1. **pre-commit**: Ensures code is properly formatted before committing
76+
2. **pre-push**: Runs both formatting and clippy checks before pushing
77+
78+
These hooks are automatically configured when you enter the nix development shell. To manually configure them:
79+
80+
```bash
81+
git config core.hooksPath .githooks
82+
```
83+
84+
To disable the hooks temporarily:
85+
86+
```bash
87+
git config --unset core.hooksPath
88+
```
89+
90+
### Running Checks Manually
91+
92+
You can run the quality checks manually at any time:
93+
94+
```bash
95+
# Check formatting
96+
nix develop -c cargo fmt --check
97+
98+
# Fix formatting
99+
nix develop -c cargo fmt
100+
101+
# Run clippy
102+
nix develop -c cargo clippy --workspace -- -D warnings
103+
104+
# Run all checks
105+
nix develop -c cargo fmt --check && nix develop -c cargo clippy --workspace -- -D warnings
106+
```
107+
59108
## CLI quick‑start
60109

61110
Note: salt is a hex encoded string of 16 bytes. It's good enough to generate it once and reuse for multiple keys. You can generate with:

flake.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@
3232

3333
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER = "${pkgs.pkgsStatic.stdenv.cc}/bin/${pkgs.pkgsStatic.stdenv.cc.targetPrefix}cc";
3434
CC_x86_64_unknown_linux_musl = "${pkgs.pkgsStatic.stdenv.cc}/bin/${pkgs.pkgsStatic.stdenv.cc.targetPrefix}cc";
35+
36+
# Automatically configure Git hooks for code quality
37+
shellHook = ''
38+
# Set up Git hooks if not already configured
39+
if [ -d .git ] && [ -d .githooks ]; then
40+
current_hooks_path=$(git config core.hooksPath || echo "")
41+
if [ "$current_hooks_path" != ".githooks" ]; then
42+
echo "📎 Setting up Git hooks for code quality checks..."
43+
git config core.hooksPath .githooks
44+
echo "✅ Git hooks configured automatically!"
45+
echo " • pre-commit: Checks code formatting"
46+
echo " • pre-push: Runs formatting and clippy checks"
47+
echo ""
48+
echo "To disable: git config --unset core.hooksPath"
49+
fi
50+
fi
51+
'';
3552
};
3653
}
3754
);

0 commit comments

Comments
 (0)