diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a7ea992 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: + - repo: local + hooks: + - id: rustfmt + name: autoformat + entry: .pre-commit-hooks/rustfmt.sh + language: script + files: \.rs$ + + - id: clippy + name: clippy_linter + entry: .pre-commit-hooks/clippy.sh + language: script + files: \.rs$ diff --git a/.pre-commit-hooks/clippy.sh b/.pre-commit-hooks/clippy.sh new file mode 100755 index 0000000..91ca4dc --- /dev/null +++ b/.pre-commit-hooks/clippy.sh @@ -0,0 +1,6 @@ +#!/bin/sh +cargo clippy -- -Dwarnings 2> /dev/null +clippy_exit_code=$? +if [ $clippy_exit_code != 0 ]; then + echo -e "\e[31mYour code seems to contains style violations! Run cargo clippy before committing!\e[0m" ; exit 1 +fi \ No newline at end of file diff --git a/.pre-commit-hooks/rustfmt.sh b/.pre-commit-hooks/rustfmt.sh new file mode 100755 index 0000000..3b10a0d --- /dev/null +++ b/.pre-commit-hooks/rustfmt.sh @@ -0,0 +1,7 @@ +#!/bin/sh +STAGED=$(git diff --name-only --cached | grep '.*\.rs') +if ! [ "$STAGED" = '' ]; then + rustfmt --check "$STAGED" || { + echo -e "\e[31mYour code is not formatted correctly! Please run rustfmt on all staged files before committing!\e[0m" ; exit 1 + } +fi \ No newline at end of file