From 0c255105ab075ec7205449e2ed6c0606589190d4 Mon Sep 17 00:00:00 2001
From: Martin Burchell <martinb@aptivate.org>
Date: Thu, 30 May 2019 15:30:04 +0100
Subject: [PATCH] Add pre-commit hook for isort / pylava

---
 Makefile               | 17 +++++++++++++++++
 bin/pre-commit-hook.sh | 27 +++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 Makefile
 create mode 100755 bin/pre-commit-hook.sh

diff --git a/Makefile b/Makefile
new file mode 100644
index 00000000..b89b5fc1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+PROJECT_NAME := internewshid
+MANAGEPY     := python manage.py
+PIPENVRUN    := pipenv run
+PROJECT_ROOT := .
+SOURCE_DIR   := $(PROJECT_ROOT)/$(PROJECT_NAME)/
+
+lint:
+	@$(PIPENVRUN) pylava -o setup.cfg $(SOURCE_DIR)
+.PHONY: lint
+
+sort:
+	$(PIPENVRUN) isort -c -rc --diff -sp setup.cfg $(SOURCE_DIR)
+.PHONY: sort
+
+test:
+	@$(PIPENVRUN) pytest --cov=$(PROJECT_NAME)
+.PHONY: test
diff --git a/bin/pre-commit-hook.sh b/bin/pre-commit-hook.sh
new file mode 100755
index 00000000..fe3a5c52
--- /dev/null
+++ b/bin/pre-commit-hook.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# pre-commit hook script that runs lint & sort
+#
+# Usage: ln -s ./bin/pre-commit-hook.sh .git/hooks/pre-commit
+#
+# This does not run tests, as it would be slow to do at every commit.
+# Linting and sorting however ensures we don't get cosmetic commits later
+# that break git blame history for no reason.
+#
+# This script doesn't stash changes to avoid un-expected side effects.
+# So if you have non-commited changes that break this you'll need to
+# stash your changes before commiting.
+
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+NC='\033[0m' # No Color
+
+make lint && make sort
+RESULT=$?
+
+if [ $RESULT -ne 0 ]; then
+  echo -e "${RED}lint/sort failed; commit aborted. See errors above.${NC}\n"
+  exit 1
+fi
+echo -e "${GREEN}lint/sort success.${NC}\n"
+exit 0
-- 
GitLab