-
-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add pre-commit configuration file to run clang-format
Also include a setup script to install precommit to operate with the "legacy" hooks.
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fail_fast: true | ||
default_stages: [commit] | ||
repos: | ||
- repo: https://github.com/pre-commit/mirrors-clang-format | ||
rev: v16.0.6 | ||
hooks: | ||
- id: clang-format | ||
args: ['--style=file'] | ||
files: '\.(c|cc|h|cxx|hxx)$' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env bash | ||
|
||
MIN_PYTHON_VERSION_MAJOR=3 | ||
MIN_PYTHON_VERSION_MINOR=8 | ||
MIN_PRECOMMIT_VERSION=3.5.0 | ||
|
||
die() { | ||
echo 1>&2 "$@" ; exit 1 | ||
} | ||
|
||
# Make sure we are inside the repository. | ||
cd "${BASH_SOURCE%/*}" && | ||
|
||
# check if python executable exists and is at least MIN_PYTHON_VERSION | ||
if ! command -v python3 &> /dev/null; then | ||
die "Python $MIN_PYTHON_VERSION or later is required for pre-commit." | ||
fi && | ||
|
||
# get python major version and minor version into array | ||
python_version=$(python3 --version | cut -d ' ' -f 2) && | ||
declare -a python_version_arr && | ||
python_version_arr=(`echo ${python_version//./ }`) && | ||
if test ${python_version_arr[0]} -lt $MIN_PYTHON_VERSION_MAJOR; then | ||
die "Python $MIN_PYTHON_VERSION_MAJOR or later is required for pre-commit." | ||
elif test ${python_version_arr[0]} -eq $MIN_PYTHON_VERSION_MAJOR; then | ||
if test ${python_version_arr[1]} -lt $MIN_PYTHON_VERSION_MINOR; then | ||
die "Python $MIN_PYTHON_VERSION_MAJOR.$MIN_PYTHON_VERSION_MINOR or later is for pre-commit" | ||
fi | ||
fi && | ||
echo "Python version is $python_version" && | ||
git_dir=$(git rev-parse --git-dir) && | ||
mkdir -p "$git_dir/hooks" && | ||
( | ||
cd "$git_dir/hooks" && | ||
# remove venv if python version is different | ||
if [ -d venv ]; then | ||
source venv/bin/activate && | ||
python_version_venv=$(python --version | cut -d ' ' -f 2) && | ||
if [ "$python_version" != "$python_version_venv" ]; then | ||
deactivate && | ||
rm -rf venv | ||
fi | ||
fi && | ||
if [ ! -d venv ]; then | ||
echo "Setting up python venv..." && | ||
python3 -m venv venv | ||
fi | ||
) && | ||
# activate the venv and install pre-commit with the min version in subshell | ||
( | ||
echo "Setting up pre-commit..." && | ||
source ${git_dir}/hooks/venv/bin/activate && | ||
python -m pip install --disable-pip-version-check -q -U "pre-commit>=$MIN_PRECOMMIT_VERSION" && | ||
# Chaing to the previous hooks after renaming them with a ".legacy" suffix | ||
pre-commit install -t pre-commit -t prepare-commit-msg -t commit-msg | ||
) |