summaryrefslogtreecommitdiff
path: root/hooks/pre-commit.mix_format
blob: 8db706fb2fa604491cb4918eb9ac4023e8616dc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/sh

# Ensure this script works with Magit, which sets GIT_LITERAL_PATHSPECS=1 and
# breaks git-stash
export GIT_LITERAL_PATHSPECS=0

# Skip hook if there are no changes to be committed
[ -z "$(git status --porcelain=v1)" ] && exit 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