]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[pre-commit] Move codespell-log to post-commit
authorTom de Vries <tdevries@suse.de>
Mon, 15 Dec 2025 16:19:26 +0000 (17:19 +0100)
committerTom de Vries <tdevries@suse.de>
Mon, 15 Dec 2025 16:19:26 +0000 (17:19 +0100)
The current pre-commit hook codespell-log works with script
gdb/contrib/codespell-log.sh.

During the commit-msg phase of a commit, the following happens:
- git calls pre-commit
- pre-commit calls gdb/contrib/codespell-log.sh,
- gdb/contrib/codespell-log.sh calls pre-commit, and
- precommit calls codespell.

This purpose of this indirection is to:
- ignore the exit status of codespell (to make sure that the commit is not
  aborted), and
- control the version of codespell.

However, if the check fails, the output is a bit messy due to the indirection:
...
$ touch gdb/bla.c
$ git add gdb/bla.c
$ git commit -m "Usuable"
  ...
codespell-log............................................................Passed
- hook id: codespell-log
- duration: 0.43s

codespell-log-internal...................................................Failed
- hook id: codespell
- exit code: 65

.git/COMMIT_EDITMSG:1: Usuable ==> Usable

[detached HEAD 18ec133830f] Usuable
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 gdb/bla.c
...

And if the check passes, the output is still verbose due to the verbose=true
setting.

I found a simpler way to implement this.

Given that we don't want to abort the commit, the post-commit stage is a more
natural place for this check.  Moving it there solves two problems:
- we no longer need to ignore the exit status of codespell
- we no longer need the verbose=true setting

The only issue is that we need to generate the file containing the commit
message ourselves, something that is provided by git in the commit-msg stage.
So we still need a wrapper script.

However, it seems that specifying a shell script as entry point of a codespell
hook is fine, so we no longer need to call pre-commit from codespell-log.sh.

Having simplified codespell-log.sh, we can downgrade it to a /bin/sh script,
instead of requiring bash.

Checked with shellcheck.

.pre-commit-config.yaml
gdb/contrib/codespell-log.sh

index 6757d872ce3fdbc04db20c2b6e17d17f55639b0e..e0c80428875260318b0a51c11e6b9b2ae19d79f4 100644 (file)
@@ -38,7 +38,7 @@
 # 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_install_hook_types: [pre-commit, post-commit]
 default_stages: [pre-commit]
 repos:
   - repo: https://github.com/psf/black-pre-commit-mirror
@@ -73,6 +73,12 @@ repos:
     - id: codespell
       files: '^(gdbsupport|gdbserver|gdb/(dwarf2|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]
+    - id: codespell
+      name: codespell-log
+      entry: gdb/contrib/codespell-log.sh
+      args: [--config, gdb/contrib/setup.cfg]
+      always_run: true
+      stages: [post-commit]
   - repo: local
     hooks:
     - id: check-include-guards
@@ -82,13 +88,6 @@ repos:
       # 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
index 10780f86c5f385966848f2c3ee91b0ef65d15607..3873fd7a5a9fa89c0c36e0aa617ffe23bdeeca44 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/bin/sh
 
 # Copyright (C) 2025 Free Software Foundation, Inc.
 # This program is free software; you can redistribute it and/or modify
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-# Script to be used as pre-commit commit-msg hook to spell-check the commit
-# log using codespell.
+# Script to be used as post-commit hook to spell-check the commit log using
+# codespell.
 #
-# Using codespell directly as a pre-commit commit-msg hook has the drawback
-# that:
-# - if codespell fails, the commit fails
-# - if the commit log mentions a typo correction, it'll require a
-#   codespell:ignore annotation.
-#
-# This script works around these problems by treating codespell output as a
-# hint, and ignoring codespell exit status.
-#
-# Implementation note: rather than using codespell directly, this script uses
-# pre-commit to call codespell, because it allows us to control the codespell
-# version that is used.
+# Given that the post-commit hook gets no arguments from git, we need to
+# provide the commit message ourselves.
 
 # Exit on error.
 set -e
 
-# Initialize temporary file names.
-cfg=""
-output=""
+tmp=
 
 cleanup()
 {
-    for f in "$cfg" "$output"; do
-       if [ "$f" != "" ]; then
-           rm -f "$f"
-       fi
-    done
+    if [ "$tmp" != "" ]; then
+       rm -f "$tmp"
+    fi
 }
 
 # Schedule cleanup.
 trap cleanup EXIT
 
-# Create temporary files.
-cfg=$(mktemp)
-output=$(mktemp)
-
-gen_cfg ()
-{
-    cat > "$1" <<EOF
-repos:
-- repo: https://github.com/codespell-project/codespell
-  rev: v2.4.1
-  hooks:
-  - id: codespell
-    name: codespell-log-internal
-    stages: [manual]
-    args: [--config, gdb/contrib/setup.cfg]
-EOF
-}
-
-# Generate pre-commit configuration file.
-gen_cfg "$cfg"
-
-# Setup pre-commit command to run.
-cmd=(pre-commit \
-    run \
-    -c "$cfg" \
-    codespell \
-    --hook-stage manual \
-    --files "$@")
-
-# Run pre-commit command.
-if "${cmd[@]}" \
-     > "$output" \
-     2>&1; then
-    # Command succeeded quietly, we're done.
-    exit 0
-fi
+tmp=$(mktemp)
+git show \
+    -s \
+    --pretty=%B HEAD \
+    > "$tmp"
 
-# Command failed quietly, now show the output.
-#
-# Simply doing "cat $output" doesn't produce colored output, so we just
-# run the command again, that should be fast enough.
-#
-# Ignore codespell exit status.
-"${cmd[@]}" || true
+codespell \
+    "$@" \
+    "$tmp"