[binutils-gdb] [pre-commit] Fix codespell-log hook

Tom de Vries via Gdb-cvs <[email protected]> Fri, 17 Jul 2026 11:18:33 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D0cdde1399d2d=
453569fe91c55b34b4879f8a6501

commit 0cdde1399d2d453569fe91c55b34b4879f8a6501
Author: Tom de Vries <[email protected]>
Date:   Fri Jul 17 13:18:28 2026 +0200

    [pre-commit] Fix codespell-log hook
   =20
    A recent commit added this top-level setting to .pre-commit-config.yaml:
    ...
    files: '^(gdb|gdbserver|gdbsupport)/'
    ...
   =20
    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_EDITMS=
G.
   =20
    However, the top-level files setting filters out .git/COMMIT_EDITMSG, w=
ith the
    consequence that the commit-msg hook is no longer called.
   =20
    It seems obvious to me that this is a pre-commit bug: the files field i=
s there
    to filter files in the repository, which .git/COMMIT_EDITMSG is not one=
 of.
    But upstream disagrees [1].
   =20
    The fix suggested upstream is to include .git/COMMIT_EDITMSG in the def=
ault
    files setting.
   =20
    That indeed works for a regular commit, but not for something like this:
    ...
    $ tmp=3D$(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.
   =20
    We can't use .git/COMMIT_EDITMSG in the regression test, because the us=
er may
    be editing it, or using it in some other way.
   =20
    We also cannot use say gdb/testsuite/gdb.src/commit-msg.txt, because us=
ing
    that filename doesn't detect the regression.
   =20
    [1] https://github.com/pre-commit/pre-commit/issues/3720

Diff:
---
 .pre-commit-config.yaml              |  7 ++++++-
 gdb/testsuite/gdb.src/pre-commit.exp | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 2f86a333ab5..141f14ba72c 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -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)/" woul=
d be
+# nice, but that disables commit-msg hooks.  See this pre-commit issue (
+# https://github.com/pre-commit/pre-commit/issues/3720 ).
=20
 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
diff --git a/gdb/testsuite/gdb.src/pre-commit.exp b/gdb/testsuite/gdb.src/p=
re-commit.exp
index b84353d3e33..82225f3d652 100644
--- a/gdb/testsuite/gdb.src/pre-commit.exp
+++ b/gdb/testsuite/gdb.src/pre-commit.exp
@@ -39,4 +39,28 @@ with_cwd $repodir {
     set status [lindex $result 0]
     gdb_assert {$status =3D=3D 0} "pre-commit checks"
=20
+    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_m=
sg -v"]
+	set status [lindex $result 0]
+	set output [lindex $result 1]
+	gdb_assert {$status =3D=3D 0} "run"
+
+	with_test_prefix codespell-log {
+	    # codespell:ignore-begin.
+	    set re [string_to_regexp "aways =3D=3D> "]
+	    # codespell:ignore-end.
+
+	    gdb_assert {[regexp $re $output]} "output"
+	}
+    }
 }