Skip to content

Commit f3a26e1

Browse files
authored
Add rust lint and format hooks (#4)
1 parent d9f2489 commit f3a26e1

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# golang
12
- id: go-lint
23
name: lint golang files
34
description: ensures golang code meets the configured lint criteria.
@@ -12,6 +13,22 @@
1213
language: script
1314
stages: [pre-push, manual]
1415
pass_filenames: false
16+
# rust
17+
- id: rust-lint
18+
name: lint rust files
19+
description: ensures rust code meets the configured lint criteria.
20+
entry: pre-commit-hooks/rust-lint.sh
21+
language: script
22+
pass_filenames: false
23+
stages: [pre-commit, pre-push, manual]
24+
- id: rust-fmt
25+
name: format rust files
26+
description: ensures rust code is formatted as the project expects.
27+
entry: pre-commit-hooks/rust-fmt.sh
28+
language: script
29+
pass_filenames: false
30+
stages: [pre-commit, pre-push, manual]
31+
# typos
1532
- id: detect-typos
1633
name: Check for typos
1734
description: Check the code for any typos

pre-commit-hooks/rust-fmt.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# Function to format rust code
7+
run_cargo_fmt() {
8+
echo "Executing 'cargo fmt'"
9+
cargo fmt
10+
}
11+
12+
run_cargo_fmt
13+
14+
# Utilize pre-commit's stash functionality to get a diff
15+
output=$(git diff --stat)
16+
17+
if [ -z "$output" ]; then
18+
echo "No changes in any files."
19+
else
20+
echo "files that weren't formatted:"
21+
echo "$output" | awk -F '|' '/\|/ { print $1 }'
22+
exit 1
23+
fi

pre-commit-hooks/rust-lint.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Exit immediately if a command exits with a non-zero status.
4+
set -e
5+
6+
# Function to run linters on the rust project
7+
run_linter() {
8+
echo "Executing clippy"
9+
10+
# Run the linter and capture the exit status
11+
set +e
12+
cargo clippy -- -D warnings
13+
local linting_result=$?
14+
set -e
15+
16+
# Check the linter result and provide feedback
17+
if [[ $linting_result -ne 0 ]]; then
18+
echo -e "\e[31mNOK!\e[0m"
19+
echo "Run 'cargo clippy -- -D warnings' and fix the issues"
20+
exit 1
21+
else
22+
echo -e "\e[32mOK!\e[0m"
23+
fi
24+
}
25+
26+
run_linter

0 commit comments

Comments
 (0)