summaryrefslogtreecommitdiff
path: root/hooks/pre-commit.mix_format
diff options
context:
space:
mode:
authorGuillermo Ramos2020-04-23 13:57:33 +0200
committerGuillermo Ramos2020-04-23 13:57:33 +0200
commit9737166ce9bdb188d51912f7e4e34f6e941b35d4 (patch)
tree5ed642b6644d364c1b30d9fa2d87dcab13dbb57d /hooks/pre-commit.mix_format
downloadgit-utils-9737166ce9bdb188d51912f7e4e34f6e941b35d4.tar.gz
First commit
Diffstat (limited to 'hooks/pre-commit.mix_format')
-rw-r--r--hooks/pre-commit.mix_format30
1 files changed, 30 insertions, 0 deletions
diff --git a/hooks/pre-commit.mix_format b/hooks/pre-commit.mix_format
new file mode 100644
index 0000000..c201707
--- /dev/null
+++ b/hooks/pre-commit.mix_format
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+# Ensure this script works with Magit, which sets GIT_LITERAL_PATHSPECS=1 and
+# breaks git-stash
+export GIT_LITERAL_PATHSPECS=0
+
+################################################################################
+# Here is a fine piece of Git wizardry. It uses git stash to make sure that the
+# staged changes are correctly formatted:
+#
+# 1) Stash all changes (worktree + index)
+git stash push -u > /dev/null
+# 2) Apply only changes from index
+git show -p stash^2 | git apply 2> /dev/null
+# 3) Check with 'mix format' the staged changes
+mix format --check-formatted > /dev/null 2>&1
+retval=$?
+# 4) Undo changes brought from step 1
+git reset --hard > /dev/null
+git clean -f . > /dev/null
+# 5) Leave the repository as it was before
+git stash pop --index > /dev/null
+################################################################################
+
+# Don't allow the commit if 'mix format' complained
+if [ $retval -ne 0 ]; then
+ # Please keep this format ("error: XXXX") as it has special meaning to Magit
+ echo "error: Failed formatting checks. Run 'mix format' (+ git add?) and try again.";
+ exit 1;
+fi