[binutils-gdb] [pre-commit] Add shellcheck
Tom de Vries via Binutils-cvs <[email protected]>
| Newsgroups | gmane.comp.gnu.binutils.cvs |
|---|---|
| Message-ID | <[email protected]> |
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c350904323784e09a84e6485e22f2005fe15c247 commit c350904323784e09a84e6485e22f2005fe15c247 Author: Tom de Vries <[email protected]> Date: Thu Aug 27 09:28:07 2026 +0200 [pre-commit] Add shellcheck Add a pre-commit shellcheck hook. Contrary to a usual pre-commit setup, users are expected to provide shellcheck themselves. Since that can be non-trival, as a compromise both versions 0.11.0 and 0.10.0 are allowed, and we set the enabled stages to manual. This can lead to "but it works for me" situations, but people not using it because they don't have the latest version available seems worse. If version 0.10.0 is used, a note is emitted, though this is only visible with -v: .... $ pre-commit run shellcheck --hook-stage manual --all-files -v shellcheck...............................................................Passed - hook id: shellcheck - duration: 0.13s Using shellcheck version 0.10.0, but version 0.11.0 is preferred. ... Using shellcheck version 0.10.0, but version 0.11.0 is preferred. ... Alternative solutions are: - pureshellcheck [1]. Python, but the project seems unmaintained. - shellcheck-py [2]. Downloads an executable and runs it. - shellcheck-precommit [3]. Downloads a docker image and runs it. Changes in v3: - abandoned pureshellcheck approach used in v1 and v2. Versions: - v2 https://sourceware.org/pipermail/gdb-patches/2026-June/228145.html - v1 https://sourceware.org/pipermail/gdb-patches/2026-June/228118.html [1] https://github.com/adam2go/pureshellcheck [2] https://github.com/shellcheck-py/shellcheck-py [3] https://github.com/koalaman/shellcheck-precommit Diff: --- .pre-commit-config.yaml | 11 ++++++++ gdb/contrib/shellcheck.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 24d8ccad604..fcf839caa6b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -151,6 +151,17 @@ repos: language: unsupported_script entry: gdb/contrib/check-file-mode.sh files: *gdb_files + - id: &id5 shellcheck + name: *id5 + files: '^(gdb|gdbsupport|gdbserver)/' + types: ['shell'] + language: unsupported_script + # The gdb/contrib/shellcheck.sh script requires shellcheck 0.11.0 + # (preferred) or 0.10.0. Not pinning this to a single version is a + # compromise to deal with the fact that users themselves need to + # provide the dependency, which may be non-trivial. + entry: gdb/contrib/shellcheck.sh + stages: [manual] # Local Variables: # indent-tabs-mode: nil diff --git a/gdb/contrib/shellcheck.sh b/gdb/contrib/shellcheck.sh new file mode 100755 index 00000000000..64a6b8b6b22 --- /dev/null +++ b/gdb/contrib/shellcheck.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# Copyright (C) 2026 Free Software Foundation, Inc. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +set -e + +version=$(shellcheck --version | grep "^version" | awk '{print $2}') + +case "$version" in + 0.11.0) + # Preferred version. + true + ;; + 0.10.0) + # Allowed version, but mention preferred version. This will be + # visible with "pre-commit run shellcheck --all-files -v". + echo "Using shellcheck version 0.10.0, but version 0.11.0 is preferred." + ;; + *) + echo "Please install shellcheck version 0.11.0 (preferred) or 0.10.0" + exit 1 + ;; +esac + +files=() +for f in "$@"; do + case "$f" in + */configure) + # Skip generated files. + continue + ;; + gdb/config/djgpp/djcheck.sh \ + | gdb/config/djgpp/djconfig.sh \ + | gdb/contrib/cc-with-tweaks.sh \ + | gdb/contrib/gdb-add-index.sh \ + | gdb/gdb_buildall.sh \ + | gdb/gdb_mbuild.sh \ + | gdb/po/gdbtext \ + | gdb/regformats/regdat.sh \ + | gdb/testsuite/lib/pdtrace.in) + # Skip unclean files. + continue + ;; + *) + files=("${files[@]}" "$f") + ;; + esac +done + +if [ ${#files[@]} -eq 0 ]; then + # Nothing to do. + exit 0 +fi + +shellcheck "${files[@]}"