From d07d37d08e5b7504ade18ec18100a364d06761fe Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Sat, 6 Nov 2021 13:34:33 -0400 Subject: [PATCH] Revamp formatting and linting on commit Remove the prepare script, and run the two stages manually. Better handle Black reformatting by doing a check (for the errcode) then reformat and abort commit to review. --- .hooks/pre-commit | 11 +++++++++-- build-and-deploy.sh | 3 ++- format | 6 +++++- prepare | 6 ------ 4 files changed, 16 insertions(+), 10 deletions(-) delete mode 100755 prepare diff --git a/.hooks/pre-commit b/.hooks/pre-commit index bc88cea8..caf166cc 100755 --- a/.hooks/pre-commit +++ b/.hooks/pre-commit @@ -4,9 +4,16 @@ pushd $( git rev-parse --show-toplevel ) &>/dev/null ex=0 -./prepare +./format check if [[ $? -ne 0 ]]; then - echo "Aborting commit due to formatting or linting errors." + ./format + echo "Black formatting change detected; review and recommit" + ex=1 +fi + +./lint +if [[ $? -ne 0 ]]; then + echo "Linting error detected; correct and recommit" ex=1 fi diff --git a/build-and-deploy.sh b/build-and-deploy.sh index ca1daf24..23c617f2 100755 --- a/build-and-deploy.sh +++ b/build-and-deploy.sh @@ -32,7 +32,8 @@ pushd $( git rev-parse --show-toplevel ) &>/dev/null # Prepare code echo "Preparing code (format and lint)..." -./prepare || exit +./format || exit 1 +./lint || exit 1 # Build the packages echo -n "Building packages... " diff --git a/format b/format index 22b684e1..fdc3e267 100755 --- a/format +++ b/format @@ -5,10 +5,14 @@ if ! which black &>/dev/null; then exit 1 fi +if [[ $1 == "check" ]]; then + check="--check" +fi + pushd $( git rev-parse --show-toplevel ) &>/dev/null echo "Formatting..." -black --safe --exclude api-daemon/migrations . +black --safe ${check} --exclude api-daemon/migrations . ret=$? if [[ $ret -eq 0 ]]; then echo "Successfully formatted project!" diff --git a/prepare b/prepare deleted file mode 100755 index 20f628d2..00000000 --- a/prepare +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -#set -o errexit - -./format -./lint