aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gaëtan Gilbert <gaetan.gilbert@skyskimmer.net>2017-11-29 19:26:54 +0100
committerGravatar Gaëtan Gilbert <gaetan.gilbert@skyskimmer.net>2018-02-08 17:14:43 +0100
commit202bb1e4d7c0fb3e55cc31ef4760c67985c9c10f (patch)
tree79e2ff4c34fb19cddcc92fd1698765be5e031470
parent2e798fb83db743ce44350af6f7f9442811f374ad (diff)
A pre-commit hook to magically fix whitespace issues.
-rwxr-xr-xdev/tools/pre-commit36
1 files changed, 36 insertions, 0 deletions
diff --git a/dev/tools/pre-commit b/dev/tools/pre-commit
new file mode 100755
index 000000000..7718b26d5
--- /dev/null
+++ b/dev/tools/pre-commit
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+# Copy to .git/hooks/ to use.
+
+set -e
+
+# NB: even though we use --cached it seems to exit code 1 with
+# unstaged whitespace errors. That just means they get fixed though.
+if git diff-index --check --cached HEAD --quiet;
+then
+ :
+else
+ 1>&2 echo "Auto fixing whitespace issues..."
+
+ TEMP=$(mktemp)
+ # We fix whitespace in the index and in the working tree
+ # separately to preserve non-added changes.
+ git diff-index -p --cached HEAD > "$TEMP"
+ git apply --cached -R "$TEMP"
+ git apply --cached --whitespace=fix "$TEMP"
+
+ git diff-index -p HEAD > "$TEMP"
+ git apply -R "$TEMP"
+ git apply --whitespace=fix "$TEMP"
+ rm "$TEMP"
+
+ # Check that we did fix whitespace
+ if ! git diff-index --check --cached HEAD;
+ then
+ 1>&2 echo "Auto-fixing whitespace failed: errors remain."
+ 1>&2 echo "This may fix itself if you try again."
+ 1>&2 echo "(Consider whether the number of errors decreases after each run.)"
+ exit 1
+ fi
+ 1>&2 echo "Whitespace issues fixed!"
+fi