Skip to content

Commit 25229a3

Browse files
committed
Commit git hooks
Use the [Githooks] convention for storing git hooks in the repository. Add commit hooks to check for ASCII filenames and whitespace errors (from the git sample hook) and for running ruff checks. [Githooks]: https://github.com/rycus86/githooks Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
1 parent 4b814ed commit 25229a3

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
#
3+
# Enforce hooks.allownonascii and core.whitespace configuration on commit.
4+
#
5+
# Copied from:
6+
# https://github.com/git/git/blob/v2.18.0/templates/hooks--pre-commit.sample
7+
8+
if git rev-parse --verify HEAD >/dev/null 2>&1
9+
then
10+
against=HEAD
11+
else
12+
# Initial commit: diff against an empty tree object
13+
against=$(git hash-object -t tree /dev/null)
14+
fi
15+
16+
# If you want to allow non-ASCII filenames set this variable to true.
17+
allownonascii=$(git config --bool hooks.allownonascii)
18+
19+
# Redirect output to stderr.
20+
exec 1>&2
21+
22+
# Cross platform projects tend to avoid non-ASCII filenames; prevent
23+
# them from being added to the repository. We exploit the fact that the
24+
# printable range starts at the space character and ends with tilde.
25+
if [ "$allownonascii" != "true" ] &&
26+
# Note that the use of brackets around a tr range is ok here, (it's
27+
# even required, for portability to Solaris 10's /usr/bin/tr), since
28+
# the square bracket bytes happen to fall in the designated range.
29+
test $(git diff --cached --name-only --diff-filter=A -z $against |
30+
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
31+
then
32+
cat <<\EOF
33+
Error: Attempt to add a non-ASCII file name.
34+
35+
This can cause problems if you want to work with people on other platforms.
36+
37+
To be portable it is advisable to rename the file.
38+
39+
If you know what you are doing you can disable this check using:
40+
41+
git config hooks.allownonascii true
42+
EOF
43+
exit 1
44+
fi
45+
46+
# If there are whitespace errors, print the offending file names and fail.
47+
exec git diff-index --check --cached $against --

.githooks/pre-commit/20-ruff.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
# Run ruff check (and format --check) on changed .py and .pyi files
3+
4+
set -Ceu
5+
6+
# Ensure ruff is available.
7+
# Could use tox/venv, but they are too heavy/slow for my tastes
8+
if ! command -v ruff >/dev/null ; then
9+
# shellcheck disable=SC2016
10+
echo 'Warning: ruff not on $PATH. Skipping pre-commit check.' >&2
11+
exit 0
12+
fi
13+
14+
git diff -z --cached --name-only --diff-filter=AM -- '*.py' '*.pyi' |
15+
xargs -0r ruff check --
16+
17+
git diff -z --cached --name-only --diff-filter=AM -- '*.py' '*.pyi' |
18+
xargs -0r ruff format --check --diff --

0 commit comments

Comments
 (0)