From 35b60d509f1bfe5fd77de3ff8e1a3b79b00d9f69 Mon Sep 17 00:00:00 2001 From: Dmitry Rodionov Date: Wed, 18 Aug 2021 18:22:09 +0300 Subject: [PATCH] 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. --- .circleci/config.yml | 11 +++++++++++ Makefile | 10 ++++++++++ pre-commit.sh.tpl | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100755 pre-commit.sh.tpl diff --git a/.circleci/config.yml b/.circleci/config.yml index 7414317fbd..8c005fe7a2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 >> diff --git a/Makefile b/Makefile index 676d49f6de..e81804fc29 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pre-commit.sh.tpl b/pre-commit.sh.tpl new file mode 100755 index 0000000000..ca0e76ab06 --- /dev/null +++ b/pre-commit.sh.tpl @@ -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