Repository navigation
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·69 lines (57 loc) · 1.91 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·69 lines (57 loc) · 1.91 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
# Pre-commit hook: clang-format check on staged C/C++ files.
#
# Enable once per clone:
# git config core.hooksPath scripts/git-hooks
#
# Bypass for a single commit: git commit --no-verify
# Bypass for the session: SKIP_FORMAT=1 git commit ...
set -euo pipefail
if [ "${SKIP_FORMAT:-0}" = "1" ]; then
exit 0
fi
repo_root=$(git rev-parse --show-toplevel)
cd "$repo_root"
# shellcheck source=scripts/ci/clang-format-select.sh
. scripts/ci/clang-format-select.sh
if [ -z "$clang_format" ]; then
echo "pre-commit: clang-format ${CLANG_FORMAT_MAJOR} not found; skipping format check." >&2
echo " install clang-format-${CLANG_FORMAT_MAJOR}, or set SKIP_FORMAT=1 to silence." >&2
exit 0
fi
# Staged, added/copied/modified/renamed files only (skip deletes).
mapfile -d '' -t staged < <(
git diff --cached --name-only --diff-filter=ACMR -z -- \
'*.c' '*.C' '*.cpp' '*.CPP' '*.h' '*.H' '*.hpp' '*.HPP'
)
if [ "${#staged[@]}" -eq 0 ]; then
exit 0
fi
# Reuse the CI exclusion filter so local and CI evaluate the same set.
mapfile -d '' -t files < <(
printf '%s\0' "${staged[@]}" | python3 scripts/ci/filter-format-files.py
)
if [ "${#files[@]}" -eq 0 ]; then
exit 0
fi
status=0
bad=()
for file in "${files[@]}"; do
# Check the staged blob, not the working tree, so unrelated unstaged
# edits don't get blamed and partial-stage workflows still work.
if ! git show ":$file" | "$clang_format" --dry-run --Werror \
-style=file --assume-filename="$file" >/dev/null 2>&1; then
bad+=("$file")
status=1
fi
done
if [ "$status" -ne 0 ]; then
echo "pre-commit: clang-format issues in staged content:" >&2
for f in "${bad[@]}"; do
echo " $f" >&2
done
echo >&2
echo "Fix with: bash ./scripts/ci/apply-format.sh && git add -u" >&2
echo "Bypass: git commit --no-verify (or SKIP_FORMAT=1)" >&2
fi
exit "$status"