[binutils-gdb] [gdb/contrib] Check clean files in check-whitespace-pre-commit.sh

Tom de Vries vries@sourceware.org
Sun Dec 7 09:59:58 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e9b696eda7c26c33eeeb9c52e6109ee7c63a8f57

commit e9b696eda7c26c33eeeb9c52e6109ee7c63a8f57
Author: Tom de Vries <tdevries@suse.de>
Date:   Sun Dec 7 10:59:54 2025 +0100

    [gdb/contrib] Check clean files in check-whitespace-pre-commit.sh
    
    The pre-commit check check-whitespace checks diffs.
    
    Consequently, we detect something like this:
    ...
    $ echo >> gdb/testsuite/lib/gdb.exp
    $ git commit -a -m trailing-empty-line
      ...
    check-whitespace.........................................................Failed
    - hook id: check-whitespace
    - duration: 0.01s
    - exit code: 2
    
    gdb/testsuite/lib/gdb.exp:11717: new blank line at EOF.
    ...
    
    But say we work around the failing check using --no-verify, then we no longer
    detect it after the commit has succeeded:
    ...
    $ git commit -a -m trailing-empty-line --no-verify
    [detached HEAD e6302105522] trailing-empty-line
     1 file changed, 1 insertion(+)
    $ pre-commit run --all-files check-whitespace
    check-whitespace.........................................................Passed
    - hook id: check-whitespace
    - duration: 0.3s
    ...
    
    Fix this in check-whitespace-pre-commit.sh by distinguishing between clean and
    other files.  Doing so is easier to do in a more advanced scripting language,
    so rewrite into python.
    
    Since a recent commit, gdb/testsuite is clean, so I'm using that as
    simple classifier for now.
    
    For the other files we do what we did before, and check just the staging area:
    ...
    $ git --no-pager diff --staged --check "${other[@]}"
    ...
    
    But for clean files, we check the entire file, including staged changes:
    ...
    $ empty=$(git hash-object -t tree /dev/null)
    $ git diff-index --cached --check $empty "${clean[@]}"
    ...
    
    Consequently, we do see:
    ...
    $ git commit -a -m trailing-empty-line --no-verify
    [detached HEAD e6302105522] trailing-empty-line
     1 file changed, 1 insertion(+)
    $ pre-commit run --all-files check-whitespace
    check-whitespace.........................................................Failed
    - hook id: check-whitespace
    - duration: 0.64s
    - exit code: 2
    
    gdb/testsuite/lib/gdb.exp:11717: new blank line at EOF.
    ...
    
    PR build/33616
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33616

Diff:
---
 .pre-commit-config.yaml                    |  2 +-
 gdb/contrib/check-whitespace-pre-commit.py | 52 ++++++++++++++++++++++++++++++
 gdb/contrib/check-whitespace-pre-commit.sh | 19 -----------
 3 files changed, 53 insertions(+), 20 deletions(-)

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 180c330a5f8..a78659cd173 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -100,7 +100,7 @@ repos:
     - id: check-whitespace
       name: check-whitespace
       language: script
-      entry: gdb/contrib/check-whitespace-pre-commit.sh
+      entry: gdb/contrib/check-whitespace-pre-commit.py
       files: '^(gdb(support|server)?)/.*$'
       pass_filenames: true
       stages: [pre-commit]
diff --git a/gdb/contrib/check-whitespace-pre-commit.py b/gdb/contrib/check-whitespace-pre-commit.py
new file mode 100755
index 00000000000..832628095f0
--- /dev/null
+++ b/gdb/contrib/check-whitespace-pre-commit.py
@@ -0,0 +1,52 @@
+#! /usr/bin/env python3
+
+# Copyright (C) 2025 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/>.
+
+import re
+import subprocess
+import sys
+
+re_clean = re.compile("(^|/)gdb/testsuite/")
+re_ignore = re.compile("/configure$")
+
+clean = []
+other = []
+for f in sys.argv[1:]:
+    m = re_ignore.search(f)
+    if m:
+        continue
+
+    m = re_clean.search(f)
+    if m:
+        clean.append(f)
+    else:
+        other.append(f)
+
+if other:
+    cmd = ["git", "--no-pager", "diff", "--staged", "--check"] + other
+    res = subprocess.run(cmd)
+    if res.returncode != 0:
+        sys.exit(res.returncode)
+
+if clean:
+    cmd = ["git", "hash-object", "-t", "tree", "/dev/null"]
+    res = subprocess.run(cmd, capture_output=True, text=True)
+    if res.returncode != 0:
+        sys.exit(res.returncode)
+    null_tree = res.stdout.rstrip("\n")
+
+    cmd = ["git", "diff-index", "--cached", "--check", null_tree] + clean
+    res = subprocess.run(cmd)
+    sys.exit(res.returncode)
diff --git a/gdb/contrib/check-whitespace-pre-commit.sh b/gdb/contrib/check-whitespace-pre-commit.sh
deleted file mode 100755
index db45dc26e65..00000000000
--- a/gdb/contrib/check-whitespace-pre-commit.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/bin/sh
-
-# Copyright (C) 2025 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
-
-git --no-pager diff --staged --check "$@"


More information about the Binutils-cvs mailing list