-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·21 lines (17 loc) · 928 Bytes
/
pre-commit
File metadata and controls
executable file
·21 lines (17 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# Redirect output to stderr.
exec 1>&2
if ! find $(git diff-index --cached --name-only --diff-filter=AM $against) \( -name '*.cpp' -o -name '*.hpp' \) -exec bash -c 'echo -n $0 $@ | xargs -d" " -I% bash -c "clang-format --dry-run -Werror % 2> /dev/null"' {} \+ ; then
find $(git diff-index --cached --name-only --diff-filter=AM $against) \( -name '*.cpp' -o -name '*.hpp' \) -exec bash -c 'echo -n $0 $@ | xargs -d" " -I% bash -c "clang-format --dry-run -Werror % 2> /dev/null || echo Formatting %... && clang-format -i % && git add %"' {} \+
echo "Files have been formatted. Retry the commit."
exit 1
fi
# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --