-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·50 lines (43 loc) · 1.65 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·50 lines (43 loc) · 1.65 KB
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
47
48
49
50
#!/usr/bin/env bash
#
# dbopt pre-commit hook — block a commit if any staged .sql file has an
# error-level (or worse) finding.
#
# Install:
# cp editor/hooks/pre-commit .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
# or symlink it:
# ln -sf ../../editor/hooks/pre-commit .git/hooks/pre-commit
#
# Configuration (environment variables):
# DBOPT_BIN path to the dbopt binary (default: dbopt, resolved on PATH)
# DBOPT_FAIL_ON threshold: info|warning|error|critical (default: error)
#
# Skip once with: git commit --no-verify
set -euo pipefail
DBOPT_BIN="${DBOPT_BIN:-dbopt}"
DBOPT_FAIL_ON="${DBOPT_FAIL_ON:-error}"
# Collect staged, still-present .sql files (Added/Copied/Modified — not deletions).
mapfile -t sql_files < <(git diff --cached --name-only --diff-filter=ACM -z \
| tr '\0' '\n' \
| grep -iE '\.sql$' || true)
if [ "${#sql_files[@]}" -eq 0 ]; then
exit 0
fi
# Ensure the binary is available before we try to lint.
if ! command -v "$DBOPT_BIN" >/dev/null 2>&1 && [ ! -x "$DBOPT_BIN" ]; then
echo "dbopt pre-commit: '$DBOPT_BIN' not found on PATH." >&2
echo " Install dbopt or set DBOPT_BIN to the binary path. Skip with: git commit --no-verify" >&2
exit 1
fi
echo "dbopt: linting ${#sql_files[@]} staged .sql file(s) (fail-on: ${DBOPT_FAIL_ON})..."
# dbopt lint exits 1 if any finding is at/above --fail-on; that blocks the commit.
if "$DBOPT_BIN" lint "${sql_files[@]}" --format human --fail-on "$DBOPT_FAIL_ON"; then
exit 0
else
status=$?
echo "" >&2
echo "dbopt pre-commit: blocked — findings at/above '${DBOPT_FAIL_ON}'." >&2
echo " Fix them, or bypass once with: git commit --no-verify" >&2
exit "$status"
fi