Created
November 8, 2018 15:01
-
-
Save eugene-babichenko/ca9645fa8b579b9c56668f7b0eb74095 to your computer and use it in GitHub Desktop.
Git hook to check Rust code formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init: | |
git config core.hooksPath .githooks | |
format: | |
cargo fmt -- --force --write-mode overwrite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
HAS_ISSUES=0 | |
FIRST_FILE=1 | |
for file in $(git diff --name-only --staged); do | |
FMT_RESULT="$(rustfmt --skip-children --force --write-mode diff $file 2>/dev/null || true)" | |
if [ "$FMT_RESULT" != "" ]; then | |
if [ $FIRST_FILE -eq 0 ]; then | |
echo -n ", " | |
fi | |
echo -n "$file" | |
HAS_ISSUES=1 | |
FIRST_FILE=0 | |
fi | |
done | |
if [ $HAS_ISSUES -eq 0 ]; then | |
exit 0 | |
fi | |
echo ". Your code has formatting issues in files listed above. Format your code with \`make format\` or call rustfmt manually." | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment