From a7270f8c1c8ec40e9d895f04f3cd4ee7ca1041ca Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@gmail.com>
Date: Fri, 29 Mar 2019 14:46:10 +0000
Subject: [PATCH] Formatting: prefer a Git config variable to env var

The keeps all the formatting config together in the Git ecosystem
and keeps the config on a per-repo basis, unless the user explicitly
sets it --global.

Keep the old env var behaviour for now for backwards compatibility.
---
 .githooks/pre-commit.d/50-check-format        | 22 +++++++++++++++----
 .../development/coding-style-policy.md        | 10 ++++++---
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/.githooks/pre-commit.d/50-check-format b/.githooks/pre-commit.d/50-check-format
index 5995aa6ea4..e50e8a5eaf 100755
--- a/.githooks/pre-commit.d/50-check-format
+++ b/.githooks/pre-commit.d/50-check-format
@@ -7,10 +7,24 @@
 # Based on clang-format pre-commit hook by Alex Eagle
 # https://gist.github.com/alexeagle/c8ed91b14a407342d9a8e112b5ac7dab
 
-# Set KICAD_CHECK_FORMAT to allow this hook to run
-# if not set, the hook always succeeds.
-if [ -z "$KICAD_CHECK_FORMAT" ]; then
-    exit 0
+# Set kicad.check-format to allow this hook to run
+# If not set, the hook always succeeds.
+
+do_format=false
+
+# Check if formatting is configured
+if [ "$(git config --get kicad.check-format)" = true ]; then
+    do_format=true
+fi
+
+# Older env variable method
+if [ ! -z "$KICAD_CHECK_FORMAT" ]; then
+    do_format=true
+fi
+
+if [ ! ${do_format} = true ]; then
+    # No formatting required
+    exit 0;
 fi
 
 check_clang_format() {
diff --git a/Documentation/development/coding-style-policy.md b/Documentation/development/coding-style-policy.md
index a4da373637..8836f6a27e 100644
--- a/Documentation/development/coding-style-policy.md
+++ b/Documentation/development/coding-style-policy.md
@@ -70,9 +70,13 @@ Set the `git clang-format` tool to use the provided `_clang-format` file:
 
     git config clangFormat.style file
 
-Then, to enable the format checker, set the `KICAD_CHECK_FORMAT` environment
-variable in your shell. Without this variable, the format checker will not
-run on commit, but you can still check files staged for commit manually:
+Then, to enable the format checker, set the `kicad.check-format` Git config
+to "true" for the KiCad repo:
+
+    git config kicad.check-format true
+
+Without this config, the format checker will not run on commit, but you can
+still check files staged for commit manually:
 
     tools/check_coding.sh --diff