aboutsummaryrefslogtreecommitdiff
path: root/dev/tools/pre-commit
blob: 0cd0a0b7057eda61f1aad8bedabd2b382cf37b63 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh

# configure automatically sets up a wrapper at .git/hooks/pre-commit
# which calls this script (if it exists).

set -e

CODE=0
git diff --cached --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix || CODE=1

if [ $CODE -ne 0 ]
then
    1>&2 echo "Some files had newline errors; they have been fixed in the working tree."
    1>&2 echo "Make sure to add them before committing."
    1>&2 echo "This may fix itself if you were using git commit -a, and you try again."
    exit 1
fi

if git diff-index --check --cached HEAD >/dev/null 2>&1 ;
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