[llvm] code-format: Improve the code-format-helper to be able to run as a git hook (PR #73957)

Tobias Hieta via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 06:40:54 PST 2023


================
@@ -85,17 +99,25 @@ def update_pr(
         elif create_new:
             pr.as_issue().create_comment(comment_text)
 
-    def run(self, changed_files: list[str], args: argparse.Namespace) -> bool:
+    def run(self, changed_files: list[str], args: FormatArgs) -> bool:
         diff = self.format_run(changed_files, args)
+        should_update_gh = args.token is not None and args.repo is not None
+
         if diff is None:
-            comment_text = f"""
-:white_check_mark: With the latest revision this PR passed the {self.friendly_name}.
-"""
-            self.update_pr(comment_text, args, create_new=False)
+            if should_update_gh:
+                comment_text = f"""
+    :white_check_mark: With the latest revision this PR passed the {self.friendly_name}.
+    """
+                self.update_pr(comment_text, args, create_new=False)
             return True
         elif len(diff) > 0:
-            comment_text = self.pr_comment_text_for_diff(diff)
-            self.update_pr(comment_text, args, create_new=True)
+            if should_update_gh:
+                comment_text = self.pr_comment_text_for_diff(diff)
+                self.update_pr(comment_text, args, create_new=True)
+            else:
+                print(
+                    f"Warning: {self.friendly_name}, {self.name} detected some issues with your code formatting..."
----------------
tru wrote:

I don't know. The script @EricWF wrote asked the user for input (Y/N) if they wanted to reformat the file. Maybe this is what we want to do here as well? I probably would be annoyed about having to press Y each time I committed, but I understand some people don't want it to automatically format without their input either. I have no strong opinion as long as there is a way to override it easily.

https://github.com/llvm/llvm-project/pull/73957


More information about the llvm-commits mailing list