]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[pre-commit] Add codespell hook
authorTom de Vries <tdevries@suse.de>
Mon, 31 Mar 2025 07:30:00 +0000 (09:30 +0200)
committerTom de Vries <tdevries@suse.de>
Mon, 31 Mar 2025 07:30:00 +0000 (09:30 +0200)
Add a pre-commit codespell hook for directories gdbsupport and gdbserver,
which are codespell-clean:
...
$ pre-commit run codespell --all-files
codespell................................................................Passed
...

A non-trivial question is where the codespell configuration goes.

Currently we have codespell sections in gdbsupport/setup.cfg and
gdbserver/setup.cfg, but codespell doesn't automatically use those because the
pre-commit hook runs codespell at the root of the repository.

A solution would be to replace those 2 setup.cfg files with a setup.cfg in the
root of the repository.  Not ideal because generally we try to avoid adding
files related to subdirectories at the root.

Another solution would be to add two codespell hooks, one using
--config gdbsupport/setup.cfg and one using --config gdbserver/setup.cfg, and
add a third one once we start supporting gdb.  Not ideal because it creates
duplication, but certainly possible.

I went with the following solution: a setup.cfg file in gdb/contrib (alongside
codespell-ignore-words.txt) which is used for both gdbserver and gdbsupport.

So, what can this new setup do for us?  Let's demonstrate by simulating a typo:
...
$ echo "/* aways */" >> gdbsupport/agent.cc
...

We can check unstaged changes before committing:
...
$ pre-commit run codespell --all-files
codespell................................................................Failed
- hook id: codespell
- exit code: 65

gdbsupport/agent.cc:282: aways ==> always, away
...

Likewise, staged changes (no need for the --all-files):
...
$ git add gdbsupport/agent.cc
$ pre-commit run codespell
codespell................................................................Failed
- hook id: codespell
- exit code: 65

gdbsupport/agent.cc:282: aways ==> always, away
...

Or we can try to commit, and run into the codespell failure:
...
$ git commit -a
black................................................(no files to check)Skipped
flake8...............................................(no files to check)Skipped
isort................................................(no files to check)Skipped
codespell................................................................Failed
- hook id: codespell
- exit code: 65

gdbsupport/agent.cc:282: aways ==> always, away

check-include-guards.................................(no files to check)Skipped
...
which makes the commit fail.

Approved-By: Tom Tromey <tom@tromey.com>
.pre-commit-config.yaml
gdb/contrib/setup.cfg [moved from gdbserver/setup.cfg with 64% similarity]
gdbsupport/setup.cfg [deleted file]

index be3ee8255754a25f586f48e94434e99e19529e47..88288e044ecc57272461570fe56b50a63c529f72 100644 (file)
@@ -65,6 +65,12 @@ repos:
     - 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)/'
+      args: [--config, gdb/contrib/setup.cfg]
   - repo: local
     hooks:
     - id: check-include-guards
similarity index 64%
rename from gdbserver/setup.cfg
rename to gdb/contrib/setup.cfg
index 08646b8ea198e78fb27695327d4f95a87a65e54d..71459fe54712d2696b61acabc93b1d109f533902 100644 (file)
@@ -1,4 +1,6 @@
 [codespell]
+
 # Skip ChangeLogs and generated files.
-skip = ChangeLog*,configure
+skip = */ChangeLog*,*/configure,gdbsupport/Makefile.in
+
 ignore-words = gdb/contrib/codespell-ignore-words.txt
diff --git a/gdbsupport/setup.cfg b/gdbsupport/setup.cfg
deleted file mode 100644 (file)
index e3e9298..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-[codespell]
-# Skip ChangeLogs and generated files.
-skip = ChangeLog*,Makefile.in,configure
-ignore-words = gdb/contrib/codespell-ignore-words.txt