diff mbox series

[1/2] utils/check-package: support finding files from patches

Message ID 20241105-support-b4-v1-1-2963298c1a58@collins.com
State New
Headers show
Series .b4-config: check patches with ./utils/check-package | expand

Commit Message

Brandon Maier Nov. 5, 2024, 2:37 p.m. UTC
For the `b4` tool to support check-package, check-package must support
reading patch files from stdin.

It would be complicated to make check-package actually run checks on
patch files. So instead we search the patch files to figure out what
files are modified in the repo, then run check-package on the modified
files directly.

Signed-off-by: Brandon Maier <brandon.maier@collins.com>
---
 utils/check-package | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/utils/check-package b/utils/check-package
index 6a5e89daa3ce4f173cb21beb75122bcd40b80123..589891310ef6ef610ae9583c48672329560743a6 100755
--- a/utils/check-package
+++ b/utils/check-package
@@ -10,6 +10,7 @@ 
 
 import argparse
 import inspect
+import fileinput
 import magic
 import os
 import re
@@ -91,6 +92,9 @@  def parse_args():
                         "functions that would be called for each file (debug)")
     parser.add_argument("--failed-only", action="store_true", help="print only"
                         " the name of the functions that failed (debug)")
+    parser.add_argument("--patch", "-p", action="store_true",
+                        help="The 'files' are patch files to be sent to the"
+                        " Buildroot mailing list")
 
     parser.add_argument("--test-suite", action="store_true", help="Run the"
                         " test-suite")
@@ -294,6 +298,30 @@  def check_file_using_lib(fname):
     return nwarnings, nlines
 
 
+def patch_modified_files(patches):
+    """
+    Find files modified in a patch file
+
+    :param patches: Patch files to read, as a list of paths or '-' for stdin
+    :returns: List of modified filenames
+    """
+
+    files = []
+    with fileinput.input(files=patches) as fp:
+        # Search for unified-diff to-file lines
+        for line in fp:
+            if line.startswith('+++'):
+                line = line.removeprefix('+++').strip()
+
+                # Remove the prefix git adds to filenames
+                if line.startswith('b/'):
+                    line = line.removeprefix('b/')
+
+                files.append(line)
+    files.sort()
+    return files
+
+
 def __main__():
     global flags
     flags = parse_args()
@@ -301,14 +329,19 @@  def __main__():
     if flags.test_suite:
         return checkpackagelib.base.run_test_suite()
 
+    if flags.patch:
+        files = patch_modified_files(flags.files)
+    else:
+        files = flags.files
+
     if flags.intree_only:
         # change all paths received to be relative to the base dir
         base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
-        files_to_check = [os.path.relpath(os.path.abspath(f), base_dir) for f in flags.files]
+        files_to_check = [os.path.relpath(os.path.abspath(f), base_dir) for f in files]
         # move current dir so the script find the files
         os.chdir(base_dir)
     else:
-        files_to_check = flags.files
+        files_to_check = files
 
     if len(files_to_check) == 0:
         print("No files to check style")