[llvm] Improve slightly the pre-commit git hook usage of the auto-format helper (PR #117326)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 22 06:03:49 PST 2024


https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/117326

The default mode does not provide a way to see the actual failure of the formatters without modifying the code. Instead offer the user the option to rerun with a `FORMAT_HOOK_VERBOSE=1` environment variable to print the actual formatting diff.

>From 0eb5ceaca02fa8605f64c1089b46f7d5f8bf03db Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Fri, 22 Nov 2024 15:01:32 +0100
Subject: [PATCH] Improve slightly the pre-commit git hook usage of the
 auto-format helper

The default mode does not provide a way to see the actual failure of the
formatters without modifying the code. Instead offer the user the option
to rerun with a `FORMAT_HOOK_VERBOSE=1` environment variable to print the
actual formatting diff.
---
 llvm/utils/git/code-format-helper.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/utils/git/code-format-helper.py b/llvm/utils/git/code-format-helper.py
index 26b5ce250bb7ab..76b2a3e26be28a 100755
--- a/llvm/utils/git/code-format-helper.py
+++ b/llvm/utils/git/code-format-helper.py
@@ -318,7 +318,7 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
 def hook_main():
     # fill out args
     args = FormatArgs()
-    args.verbose = False
+    args.verbose = os.getenv("FORMAT_HOOK_VERBOSE", False)
 
     # find the changed files
     cmd = ["git", "diff", "--cached", "--name-only", "--diff-filter=d"]
@@ -338,6 +338,9 @@ def hook_main():
             print(f"Couldn't find {fmt.name}, can't check " + fmt.friendly_name.lower())
 
     if len(failed_fmts) > 0:
+        print(
+            "Pre-commit format hook failed, rerun with FORMAT_HOOK_VERBOSE=1 environment for verbose output"
+        )
         sys.exit(1)
 
     sys.exit(0)



More information about the llvm-commits mailing list