]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[pre-commit] Fix codespell-log hook
authorTom de Vries <tdevries@suse.de>
Fri, 17 Jul 2026 11:18:28 +0000 (13:18 +0200)
committerTom de Vries <tdevries@suse.de>
Fri, 17 Jul 2026 11:18:28 +0000 (13:18 +0200)
A recent commit added this top-level setting to .pre-commit-config.yaml:
...
files: '^(gdb|gdbserver|gdbsupport)/'
...

This broke the codespell-log hook, which is a commit-msg hook, which is called
with the commit message as first argument, typically .git/COMMIT_EDITMSG.

However, the top-level files setting filters out .git/COMMIT_EDITMSG, with the
consequence that the commit-msg hook is no longer called.

It seems obvious to me that this is a pre-commit bug: the files field is there
to filter files in the repository, which .git/COMMIT_EDITMSG is not one of.
But upstream disagrees [1].

The fix suggested upstream is to include .git/COMMIT_EDITMSG in the default
files setting.

That indeed works for a regular commit, but not for something like this:
...
$ tmp=$(mktemp)
$ echo 'msg' > $tmp
$ pre-commit run --hook-stage commit-msg --commit-msg-filename $tmp
...
which is roughly what we're using in the regression test.

We can't use .git/COMMIT_EDITMSG in the regression test, because the user may
be editing it, or using it in some other way.

We also cannot use say gdb/testsuite/gdb.src/commit-msg.txt, because using
that filename doesn't detect the regression.

[1] https://github.com/pre-commit/pre-commit/issues/3720

.pre-commit-config.yaml
gdb/testsuite/gdb.src/pre-commit.exp

index 2f86a333ab5fc8554655a17e706c491a4dcc61ea..141f14ba72cf6f89c86bbf814dfccb7a56656eff 100644 (file)
@@ -40,7 +40,9 @@
 minimum_pre_commit_version: 4.5.1
 default_install_hook_types: [pre-commit, commit-msg]
 default_stages: [pre-commit]
-files: '^(gdb|gdbserver|gdbsupport)/'
+# A default files setting like "files: '^(gdb|gdbserver|gdbsupport)/" would be
+# nice, but that disables commit-msg hooks.  See this pre-commit issue (
+# https://github.com/pre-commit/pre-commit/issues/3720 ).
 
 repos:
   # Python hooks.  Run these for (in glob notation):
@@ -79,6 +81,7 @@ repos:
     hooks:
     - id: codespell
       args: &codespell_args [--toml, gdb/pyproject.toml]
+      files: &gdb_files '^(gdb|gdbserver|gdbsupport)/'
     - id: codespell
       name: codespell-log
       entry: gdb/contrib/codespell-log.sh
@@ -117,6 +120,7 @@ repos:
       language: unsupported_script
       entry: gdb/contrib/check-whitespace-pre-commit.py
       types: ['text']
+      files: *gdb_files
     - id: &id3 pre-commit-setup
       name: *id3
       language: python
@@ -128,3 +132,4 @@ repos:
       name: *id4
       language: unsupported_script
       entry: gdb/contrib/check-file-mode.sh
+      files: *gdb_files
index b84353d3e3342c03ea883debec9eefe6c1ca4da4..82225f3d6526fdf0aa90a6382ae4f1c67ce57672 100644 (file)
@@ -39,4 +39,28 @@ with_cwd $repodir {
     set status [lindex $result 0]
     gdb_assert {$status == 0} "pre-commit checks"
 
+    with_test_prefix commit-msg {
+       set commit_msg [build_standard_output_file "commit-msg.txt"]
+
+       # codespell:ignore-begin.
+       gdb_produce_source $commit_msg {
+           This should aways produce a codespell warning.
+       }
+       # codespell:ignore-end.
+
+       set result \
+           [remote_exec build \
+                "pre-commit run --hook-stage commit-msg --commit-msg-filename $commit_msg -v"]
+       set status [lindex $result 0]
+       set output [lindex $result 1]
+       gdb_assert {$status == 0} "run"
+
+       with_test_prefix codespell-log {
+           # codespell:ignore-begin.
+           set re [string_to_regexp "aways ==> "]
+           # codespell:ignore-end.
+
+           gdb_assert {[regexp $re $output]} "output"
+       }
+    }
 }