Skip to content

Commit

Permalink
ENH: Add pre-commit configuration file to run clang-format
Browse files Browse the repository at this point in the history
Also include a setup script to install precommit to operate with the
"legacy" hooks.
  • Loading branch information
blowekamp committed May 8, 2024
1 parent 28de861 commit 8959de2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
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)$'
56 changes: 56 additions & 0 deletions Utilities/GitSetup/setup-precommit
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
)

0 comments on commit 8959de2

Please sign in to comment.