[llvm] [Github] Fix revisions in code format action reproducers (PR #155193)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 24 13:56:23 PDT 2025


https://github.com/boomanaiden154 updated https://github.com/llvm/llvm-project/pull/155193

>From 694884d9979bf59debd55fe05a3da691697fba91 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Thu, 21 Aug 2025 01:41:13 +0000
Subject: [PATCH 1/2] [Github] Fix revisions in code format action reproducers

This patch makes it so the revisions that the code format action returns
in its reproducers actually work when applying them locally given the
differences in how revisions are setup in CI.

Fixes #154294
---
 llvm/lib/IR/StructuralHash.cpp       |  2 +
 llvm/utils/git/code-format-helper.py | 72 ++++++++++++++++------------
 2 files changed, 44 insertions(+), 30 deletions(-)

diff --git a/llvm/lib/IR/StructuralHash.cpp b/llvm/lib/IR/StructuralHash.cpp
index 1c617c100c7dc..1d369d10a3486 100644
--- a/llvm/lib/IR/StructuralHash.cpp
+++ b/llvm/lib/IR/StructuralHash.cpp
@@ -14,6 +14,8 @@
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
 
+// This comment is going to be way too long for its own good and blow past the 80 characters per line limit causing a reformat.
+
 using namespace llvm;
 
 namespace {
diff --git a/llvm/utils/git/code-format-helper.py b/llvm/utils/git/code-format-helper.py
index 4e4145dcbb8c2..6fd4d7df25571 100755
--- a/llvm/utils/git/code-format-helper.py
+++ b/llvm/utils/git/code-format-helper.py
@@ -175,9 +175,29 @@ class ClangFormatHelper(FormatHelper):
     name = "clang-format"
     friendly_name = "C/C++ code formatter"
 
+    def _construct_command(self, diff_expression: list[str] | None):
+        cf_cmd = [self.clang_fmt_path, "--diff"]
+
+        if diff_expression:
+          cf_cmd.extend(diff_expression)
+
+        # Gather the extension of all modified files and pass them explicitly to git-clang-format.
+        # This prevents git-clang-format from applying its own filtering rules on top of ours.
+        extensions = set()
+        for file in self._cpp_files:
+            _, ext = os.path.splitext(file)
+            extensions.add(
+                ext.strip(".")
+            )  # Exclude periods since git-clang-format takes extensions without them
+        cf_cmd.append("--extensions")
+        cf_cmd.append(",".join(extensions))
+
+        cf_cmd.append("--")
+        cf_cmd += self._cpp_files
+
     @property
     def instructions(self) -> str:
-        return " ".join(self.cf_cmd)
+        return " ".join(self._construct_command("origin/main...HEAD"))
 
     def should_include_extensionless_file(self, path: str) -> bool:
         return path.startswith("libcxx/include")
@@ -218,33 +238,19 @@ def has_tool(self) -> bool:
         return proc.returncode == 0
 
     def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str]:
-        cpp_files = self.filter_changed_files(changed_files)
-        if not cpp_files:
+        self._cpp_files = self.filter_changed_files(changed_files)
+        if not self._cpp_files:
             return None
 
-        cf_cmd = [self.clang_fmt_path, "--diff"]
-
+        diff_expression = []
         if args.start_rev and args.end_rev:
-            cf_cmd.append(args.start_rev)
-            cf_cmd.append(args.end_rev)
+            diff_expression.append(args.start_rev)
+            diff_expression.append(args.end_rev)
 
-        # Gather the extension of all modified files and pass them explicitly to git-clang-format.
-        # This prevents git-clang-format from applying its own filtering rules on top of ours.
-        extensions = set()
-        for file in cpp_files:
-            _, ext = os.path.splitext(file)
-            extensions.add(
-                ext.strip(".")
-            )  # Exclude periods since git-clang-format takes extensions without them
-        cf_cmd.append("--extensions")
-        cf_cmd.append(",".join(extensions))
-
-        cf_cmd.append("--")
-        cf_cmd += cpp_files
+        cf_cmd = self._construct_command(diff_expression)
 
         if args.verbose:
             print(f"Running: {' '.join(cf_cmd)}")
-        self.cf_cmd = cf_cmd
         proc = subprocess.run(cf_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         sys.stdout.write(proc.stderr.decode("utf-8"))
 
@@ -263,9 +269,19 @@ class DarkerFormatHelper(FormatHelper):
     name = "darker"
     friendly_name = "Python code formatter"
 
+    def _construct_command(self, diff_expression: str | None) -> str:
+        darker_cmd = [
+            self.darker_fmt_path,
+            "--check",
+            "--diff",
+        ]
+        if diff_expression:
+            darker_cmd += ["-r", diff_expression]
+        darker_cmd += self._py_files
+
     @property
     def instructions(self) -> str:
-        return " ".join(self.darker_cmd)
+        return " ".join(self._construct_command("origin/main...HEAD"))
 
     def filter_changed_files(self, changed_files: List[str]) -> List[str]:
         filtered_files = []
@@ -295,17 +311,13 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
         py_files = self.filter_changed_files(changed_files)
         if not py_files:
             return None
-        darker_cmd = [
-            self.darker_fmt_path,
-            "--check",
-            "--diff",
-        ]
+        self._py_files = py_files
+        diff_expression = None
         if args.start_rev and args.end_rev:
-            darker_cmd += ["-r", f"{args.start_rev}...{args.end_rev}"]
-        darker_cmd += py_files
+            diff_expression = f"{args.start_rev}...{args.end_rev}"
+        darker_cmd = self._construct_command(diff_expression)
         if args.verbose:
             print(f"Running: {' '.join(darker_cmd)}")
-        self.darker_cmd = darker_cmd
         proc = subprocess.run(
             darker_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
         )

>From f23d0d00fa0bfbd67df9377ebdd9e2469c7d3a52 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Sun, 24 Aug 2025 20:56:12 +0000
Subject: [PATCH 2/2] try thing

---
 .github/workflows/pr-code-format.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index 5540555ae05ed..66eda6d42af49 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -38,7 +38,7 @@ jobs:
         uses: actions/checkout at 08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
         with:
           repository: ${{ github.repository }}
-          ref: ${{ github.base_ref }}
+          ref: ${{ github.ref }}
           sparse-checkout: |
             llvm/utils/git/requirements_formatting.txt
             llvm/utils/git/code-format-helper.py



More information about the llvm-commits mailing list