Merge pull request #10348 from mhilbrunner/pre-commit

CI: Integrate pre-commit for style checks
This commit is contained in:
Max Hilbrunner
2024-11-30 09:16:50 +01:00
committed by GitHub
4 changed files with 29 additions and 54 deletions

View File

@@ -15,19 +15,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Style checks via pre-commit
uses: pre-commit/action@v3.0.1
- name: Install dependencies
run: |
# Install tools used by `_tools/format.sh`.
sudo apt-get -qq update
sudo apt-get -qq install dos2unix recode
sudo pip3 install -r requirements.txt
sudo pip3 install codespell
- name: Linter checks
run: |
bash _tools/format.sh
codespell -D- -D _tools/codespell-dict.txt -I _tools/codespell-ignore.txt -x _tools/codespell-ignore-lines.txt -S tutorials/i18n/locales.rst {about,community,contributing,getting_started,tutorials}/{*.rst,**/*.rst,**/**/*.rst,**/**/**/*.rst}
run: sudo pip3 install -r requirements.txt
# Use dummy builder to improve performance as we don't need the generated HTML in this workflow.
- name: Sphinx build

17
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,17 @@
default_language_version:
python: python3
repos:
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
- id: codespell
files: ^(about|community|contributing|getting_started|tutorials)/.*\.rst$
additional_dependencies: [tomli]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: fix-byte-order-marker
- id: mixed-line-ending
args: ['--fix=lf']

View File

@@ -1,42 +0,0 @@
#!/usr/bin/env bash
set -uo pipefail
IFS=$'\n\t'
# Loops through all text files tracked by Git.
git grep -zIl '' |
while IFS= read -rd '' f; do
# Exclude csproj and hdr files.
if [[ "$f" == *"csproj" ]]; then
continue
elif [[ "$f" == *"hdr" ]]; then
continue
fi
# Ensures that files are UTF-8 formatted.
recode UTF-8 "$f" 2> /dev/null
# Ensures that files have LF line endings.
dos2unix "$f" 2> /dev/null
# Ensures that files do not contain a BOM.
sed -i '1s/^\xEF\xBB\xBF//' "$f"
# Ensures that files end with newline characters.
tail -c1 < "$f" | read -r _ || echo >> "$f";
done
git diff > patch.patch
FILESIZE="$(stat -c%s patch.patch)"
MAXSIZE=5
# If no patch has been generated all is OK, clean up, and exit.
if (( FILESIZE < MAXSIZE )); then
printf "Files in this commit comply with the formatting rules.\n"
rm -f patch.patch
exit 0
fi
# A patch has been created, notify the user, clean up, and exit.
printf "\n*** The following differences were found between the code "
printf "and the formatting rules:\n\n"
cat patch.patch
printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n"
rm -f patch.patch
exit 1

8
pyproject.toml Normal file
View File

@@ -0,0 +1,8 @@
[tool.ruff]
line-length = 120
[tool.codespell]
dictionary = ["_tools/codespell-dict.txt", "-"]
ignore-words = "_tools/codespell-ignore.txt"
exclude-file = "_tools/codespell-ignore-lines.txt"
skip = "tutorials/i18n/locales.rst"