From: Tom de Vries Date: Mon, 15 Dec 2025 16:19:26 +0000 (+0100) Subject: [pre-commit] Move codespell-log to post-commit X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7acd390ffd97070eaed468c6e21c2f1bcf5b8820;p=thirdparty%2Fbinutils-gdb.git [pre-commit] Move codespell-log to post-commit 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. --- diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6757d872ce3..e0c80428875 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/gdb/contrib/codespell-log.sh b/gdb/contrib/codespell-log.sh index 10780f86c5f..3873fd7a5a9 100755 --- a/gdb/contrib/codespell-log.sh +++ b/gdb/contrib/codespell-log.sh @@ -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 @@ -14,82 +14,33 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -# 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" < "$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"