Add support for code format checking using rustfmt in optional

pre-commit hook and in ci pipeline. Found issues can be fixed
automatically via make fmt.
This commit is contained in:
Dmitry Rodionov
2021-08-18 18:22:09 +03:00
committed by Dmitry
parent d989580c1c
commit 35b60d509f
3 changed files with 55 additions and 0 deletions

View File

@@ -10,6 +10,16 @@ executors:
- image: cimg/rust:1.52.1
jobs:
check-codestyle:
executor: zenith-build-executor
steps:
- checkout
- run:
name: rustfmt
when: always
command: |
cargo fmt --all -- --check
# A job to build postgres
build-postgres:
@@ -257,6 +267,7 @@ jobs:
workflows:
build_and_test:
jobs:
- check-codestyle
- build-postgres
- build-zenith:
name: build-zenith-<< matrix.build_type >>

View File

@@ -67,4 +67,14 @@ distclean:
rm -rf tmp_install
cargo clean
fmt:
@files=$$(git diff --cached --name-only --diff-filter=ACM | grep ".rs"); \
if [ "$$files" ]; then \
rustfmt $$files --edition=2018; \
fi;
setup-pre-commit-hook:
ln -s -f ../../pre-commit.sh.tpl .git/hooks/pre-commit
.PHONY: postgres-configure postgres postgres-headers zenith

34
pre-commit.sh.tpl Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
function check {
name=$1
prefix=$2
files=$(git diff --cached --name-only --diff-filter=ACM | grep $prefix)
shift; shift;
echo -n "checking $name ";
if [ -z "$files" ]; then
echo -e "${CYAN}[NOT APPLICABLE]${NC}"
exit 0;
fi
cd $(git rev-parse --show-toplevel)
out=$($@ $files)
if [ $? -eq 0 ];
then
echo -e "${GREEN}[OK]${NC}"
cd -
else
echo -e "${RED}[FAILED]${NC}"
echo -e "Please inspect the output below and run make fmt to fix automatically\n"
echo -e "$out"
exit 1
fi
}
check rustfmt .rs rustfmt --check --edition=2018