summaryrefslogtreecommitdiffstats
path: root/.githooks/pre-commit
blob: b9319877bdf003b7de586185308addc5e4119b38 (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
#!/bin/sh
set -e

# Pre-commit hook: run tests before allowing commit
# Skip with: SKIP_PRE_COMMIT=1 git commit
# Skip on force push: git commit --no-verify

if [ "$SKIP_PRE_COMMIT" = "1" ]; then
    exit 0
fi

# Only run tests if C/H/Makefile changed
staged=$(git diff --cached --name-only --diff-filter=d)
if echo "$staged" | grep -qE '\.(c|h)$|^Makefile$'; then
    # Run build check
    if ! make >/dev/null 2>&1; then
        echo "PRE-COMMIT FAILED: build did not succeed."
        exit 1
    fi

    # Run tests
    if ! make test >/dev/null 2>&1; then
        echo "PRE-COMMIT FAILED: tests did not pass."
        exit 1
    fi
fi

exit 0