Skip to content

Commit

Permalink
Add rust lint and format hooks (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tateexon authored Sep 19, 2024
1 parent d9f2489 commit f3a26e1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# golang
- id: go-lint
name: lint golang files
description: ensures golang code meets the configured lint criteria.
Expand All @@ -12,6 +13,22 @@
language: script
stages: [pre-push, manual]
pass_filenames: false
# rust
- id: rust-lint
name: lint rust files
description: ensures rust code meets the configured lint criteria.
entry: pre-commit-hooks/rust-lint.sh
language: script
pass_filenames: false
stages: [pre-commit, pre-push, manual]
- id: rust-fmt
name: format rust files
description: ensures rust code is formatted as the project expects.
entry: pre-commit-hooks/rust-fmt.sh
language: script
pass_filenames: false
stages: [pre-commit, pre-push, manual]
# typos
- id: detect-typos
name: Check for typos
description: Check the code for any typos
Expand Down
23 changes: 23 additions & 0 deletions pre-commit-hooks/rust-fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Exit immediately if a command exits with a non-zero status
set -e

# Function to format rust code
run_cargo_fmt() {
echo "Executing 'cargo fmt'"
cargo fmt
}

run_cargo_fmt

# Utilize pre-commit's stash functionality to get a diff
output=$(git diff --stat)

if [ -z "$output" ]; then
echo "No changes in any files."
else
echo "files that weren't formatted:"
echo "$output" | awk -F '|' '/\|/ { print $1 }'
exit 1
fi
26 changes: 26 additions & 0 deletions pre-commit-hooks/rust-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

# Exit immediately if a command exits with a non-zero status.
set -e

# Function to run linters on the rust project
run_linter() {
echo "Executing clippy"

# Run the linter and capture the exit status
set +e
cargo clippy -- -D warnings
local linting_result=$?
set -e

# Check the linter result and provide feedback
if [[ $linting_result -ne 0 ]]; then
echo -e "\e[31mNOK!\e[0m"
echo "Run 'cargo clippy -- -D warnings' and fix the issues"
exit 1
else
echo -e "\e[32mOK!\e[0m"
fi
}

run_linter

0 comments on commit f3a26e1

Please sign in to comment.