Files
binutils-gdb/.pre-commit-config.yaml
Tom de Vries 284ca193a9 [pre-commit] Add check-gnu-style
I got a review comment [1] because I forgot to do "space before paren".

I realized I forgot to run check_GNU_style.py, a script from the GCC repo,
which warns about things like this.

[ The python script has been around since 2017 (and an earlier version written
in shell script since 2010). ]

So for this change in gdb/gdb.c:
...
-  return gdb_main (&args);
+  return gdb_main(&args);
...
we get:
...
$ ./contrib/check_GNU_style.py <(git diff)
=== ERROR type #1: there should be exactly one space between function name \
  and parenthesis (1 error(s)) ===
gdb/gdb.c:38:17:  return gdb_main(&args);
...

Add a pre-commit hook to do this automatically.

This copies two files from the GCC repo to root-level contrib, and adds a
wrapper script gdb/contrib/check-gnu-style-pre-commit.sh (checked with
shellcheck).

The wrapper script is setup to not fail on violations, so the messages are
informational at this point.  I'm not sure all checks are 100% applicable to
our coding style.

The python script check_GNU_style.py has two dependencies: unidiff and
termcolor, which users need to install themselves.

The check is added at the pre-commit stage.  I also considered post-commit,
and I'm still not sure what is the better choice.

As with all pre-commit checks, if the check is not to your liking, you can
use SKIP=check-gnu-style to skip it.

In summary, with the new pre-commit check we get:
...
$ git commit -a -m "style error"
black...............................................(no files to check)Skipped
flake8..............................................(no files to check)Skipped
isort...............................................(no files to check)Skipped
codespell...........................................(no files to check)Skipped
check-include-guards................................(no files to check)Skipped
check-gnu-style.........................................................Passed
- hook id: check-gnu-style
- duration: 0.04s

=== ERROR type #1: there should be exactly one space between function name \
  and parenthesis (1 error(s)) ===
gdb/gdb.c:38:17:  return gdb_main(&args);

tclint..............................................(no files to check)Skipped
black...............................................(no files to check)Skipped
flake8..............................................(no files to check)Skipped
codespell...........................................(no files to check)Skipped
check-include-guards................................(no files to check)Skipped
codespell-log...........................................................Passed
- hook id: codespell-log
- duration: 0.19s
tclint...............................................(no files to check)Skipped
[master $hex] style error
...

Approved-By: Simon Marchi <simon.marchi@efficios.com>

[1] https://sourceware.org/pipermail/gdb-patches/2025-September/220983.html
2025-10-16 12:09:57 +02:00

107 lines
3.7 KiB
YAML

# Git has the notion of hooks, which are custom scripts that are run on
# specific events. One of these hooks is the pre-commit hook (located at
# .git/hooks/pre-commit), which is run when committing, before specifying a
# message.
#
# To ease management of the pre-commit hook, the pre-commit framework
# ( https://pre-commit.com ) was developed. This is the configuration file
# for that framework. It contains a list of hooks, with information on where
# to find them and on which files to run them.
#
# See here ( https://pre-commit.com/#install ) on how to install the
# pre-commit framework.
#
# To run all the hooks specified in this file manually, you can do:
# $ pre-commit run
#
# To run some hooks and skip others, you can use environment variable SKIP:
# $ SKIP=flake8,isort pre-commit run
#
# To install the pre-commit framework in the repository in order to run the
# hooks on every commit, do:
# $ pre-commit install
#
# After installing the pre-commit framework, you can skip running all
# pre-commit hooks using --no-verify, or some using SKIP.
# $ git commit --no-verify ...
# $ SKIP=flake8,isort git commit ...
#
# In case some of these hooks don't work for you, you can make the SKIP
# setting permanent by:
# - setting SKIP in your environment
# (this will affect all repositories where it is set)
# - setting SKIP in .git/hooks/pre-commit by adding "export SKIP=..."
# (this will affect only this repository, but it may have to be re-added
# if .git/hooks/pre-commit is regenerated)
#
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
minimum_pre_commit_version: 3.2.0
default_install_hook_types: [pre-commit, commit-msg]
default_stages: [pre-commit]
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.9.0
hooks:
- id: black
types_or: [file]
files: 'gdb/.*\.py(\.in)?$'
- repo: https://github.com/pycqa/flake8
rev: 7.3.0
hooks:
- id: flake8
types_or: [file]
# Run this for (in glob notation):
#
# - gdb/gdb-gdb.py.in
# - gdb/*.py
# - gdb/python/**/*.py
# - gdb/testsuite/*.py
#
files: '^gdb/(gdb-gdb\.py\.in|[^/]+\.py|python/.+\.py|testsuite/[^/]+\.py)$'
args: [--config, gdb/setup.cfg]
- repo: https://github.com/pycqa/isort
rev: 6.1.0
hooks:
- id: isort
types_or: [file]
files: 'gdb/.*\.py(\.in)?$'
- repo: https://github.com/codespell-project/codespell
rev: v2.4.1
hooks:
- id: codespell
files: '^(gdbsupport|gdbserver|gdb/(tui|target|data-directory|po|system-gdbinit|mi|syscalls|arch|regformats|compile|python|guile|config|unittests|cli|testsuite/gdb.(ctf|dap|debuginfod|gdb|go|guile|mi|modula2|objc|opencl|opt|pascal|perf|replay|reverse|rocm|server|stabs|testsuite|tui|xml)))/'
args: [--config, gdb/contrib/setup.cfg]
- repo: local
hooks:
- id: check-include-guards
name: check-include-guards
language: python
entry: gdb/check-include-guards.py
# All gdb header files, but not headers in the test suite.
files: '^(gdb(support|server)?)/.*\.h$'
exclude: '.*/testsuite/.*'
- id: codespell-log
name: codespell-log
language: script
entry: gdb/contrib/codespell-log.sh
verbose: true
always_run: true
stages: [commit-msg]
- id: check-gnu-style
name: check-gnu-style
language: script
entry: gdb/contrib/check-gnu-style-pre-commit.sh
files: '^(gdb(support|server)?)/.*\.(c|h|cc)$'
exclude: '.*/testsuite/.*'
verbose: true
stages: [pre-commit]
- repo: https://github.com/nmoroze/tclint
rev: v0.6.1
hooks:
- id: tclint
args: [--config, gdb/tclint.toml]
types_or: [file]
files: 'gdb/testsuite/.*\.(exp|tcl)$'