[PATCH v3] [pre-commit] Add shellcheck

Tom de Vries <[email protected]> Thu, 30 Jul 2026 14:35:39 +0200
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
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.

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 --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
---
 .pre-commit-config.yaml   | 10 ++++++
 gdb/contrib/shellcheck.sh | 65 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100755 gdb/contrib/shellcheck.sh

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 8ff84dae274..75eb1edd11d 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -147,3 +147,13 @@ 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
diff --git a/gdb/contrib/shellcheck.sh b/gdb/contrib/shellcheck.sh
new file mode 100755
index 00000000000..259988bec48
--- /dev/null
+++ b/gdb/contrib/shellcheck.sh
@@ -0,0 +1,65 @@
+#!/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/>.
+
+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[@]}"

base-commit: 0d22d419a4b60672acf5555f5479fcf6956018a1
-- 
2.51.0