Add pre-commit config.

This patch adds `clang-format` as only linter for now. This replaces the
previously used script from `auxil/run-clang-format` which we remove.

This requires the Python program `pre-commit`
(https://pypi.org/project/pre-commit/). With that one can then run
`clang-format` on the whole codebase with

    $ pre-commit run -a clang-format

or on just the staged files

    # Explicitly selecting linter.
    $ pre-commit run clang-format

    # Run all linters (currently just `clang-format`).
    $ pre-commit

`pre-commit` supports managing Git commit hooks so that linters are run
on commit. Linters can be installed with

    $ pre-commit install

The documentation at https://pre-commit.com/ covers these topics in
addition to more information.
This commit is contained in:
Benjamin Bannier 2021-11-05 11:01:18 +01:00
parent 4e16037acd
commit 77e2e8278f
6 changed files with 9 additions and 77 deletions

View file

@ -1,10 +1,5 @@
# Clang-format configuration for Zeek. This configuration requires # Clang-format configuration for Zeek. This configuration requires
# at least clang-format 12.0.1 to format correctly. # at least clang-format 12.0.1 to format correctly.
#
# The easiest way to run this from the command-line is using the
# python script in auxil/run-clang-format:
#
# python3 auxil/run-clang-format/run-clang-format.py --clang-format-executable /path/to/clang-format -r src -i
Language: Cpp Language: Cpp
Standard: c++17 Standard: c++17
@ -102,4 +97,4 @@ IncludeCategories:
- Regex: '^"zeek/' - Regex: '^"zeek/'
Priority: 4 Priority: 4
- Regex: '.*' - Regex: '.*'
Priority: 5 Priority: 5

View file

@ -1,2 +0,0 @@
# Ignore everything 3rdparty
src/3rdparty/*

3
.gitmodules vendored
View file

@ -49,6 +49,3 @@
[submodule "auxil/zeek-client"] [submodule "auxil/zeek-client"]
path = auxil/zeek-client path = auxil/zeek-client
url = https://github.com/zeek/zeek-client url = https://github.com/zeek/zeek-client
[submodule "auxil/run-clang-format"]
path = auxil/run-clang-format
url = https://github.com/Sarcasm/run-clang-format

8
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,8 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
#
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: 'v13.0.0'
hooks:
- id: clang-format

@ -1 +0,0 @@
Subproject commit 39081c9c42768ab5e8321127a7494ad1647c6a2f

View file

@ -1,65 +0,0 @@
#! /bin/sh
#
# Copyright (c) 2020 by the Zeek Project. See LICENSE for details.
base=$(git rev-parse --show-toplevel)
fix=0
pre_commit_hook=0
# Directories to run on by default. When changing, adapt .pre-commit-config.yam
# as well.
files="src"
error() {
test "${pre_commit_hook}" = 0 && echo "$@" >&2 && exit 1
exit 0
}
if [ $# != 0 ]; then
case "$1" in
--fixit)
shift
fix=1
;;
--pre-commit-hook)
shift
fix=1
pre_commit_hook=1
;;
-*)
echo "usage: $(basename $0) [--fixit | --pre-commit-hook] [<files>]"
exit 1
esac
fi
test $# != 0 && files="$@"
if [ -z "${CLANG_FORMAT}" ]; then
CLANG_FORMAT=$(which clang-format 2>/dev/null)
fi
if [ -z "${CLANG_FORMAT}" -o ! -x "${CLANG_FORMAT}" ]; then
error "Cannot find clang-format. If not in PATH, set CLANG_FORMAT."
fi
if ! (cd / && ${CLANG_FORMAT} -dump-config | grep -q SpacesInConditionalStatement); then
error "${CLANG_FORMAT} does not support SpacesInConditionalStatement. Install custom version and put it into PATH, or point CLANG_FORMAT to it."
fi
if [ ! -e .clang-format ]; then
error "Must execute in top-level directory."
fi
cmd="${base}/auxil/run-clang-format/run-clang-format.py -r --clang-format-executable ${CLANG_FORMAT} --exclude '*/3rdparty/*' ${files}"
tmp=/tmp/$(basename $0).$$.tmp
trap "rm -f ${tmp}" EXIT
eval "${cmd}" >"${tmp}"
if [ "${fix}" = 1 ]; then
test -s "${tmp}" && cat "${tmp}" | git apply -p0
true
else
cat "${tmp}"
fi