Skip to contentSkip to conversation

.b4-config: check patches with ./utils/check-package

PATCH Applied Brandon Maier via buildroot 1 participant 5 messages Nov 5, 2024 – Feb 4, 2025

lore
Share & tools
lore
#0 lore raw

cover .b4-config: check patches with ./utils/check-package

NOTE: This requires b4 version 0.14.2 or later as earlier versions had a bug that broke custom prep-perpatch-check-cmd[1].

This series registers check-package as a b4 "pre-flight check"[1]. This way a patch series can be checked with b4 prep --check, and b4 will warn the developer if they try to send a patch series without running the check.

The first patch adds support for checking patch files to check-package.

The second patch registers check-package as a b4 check-cmd.

[1] https://github.com/mricon/b4/commit/0741bc9a5b45578e4d3804588dd3f63fe4848059
[2] https://b4.docs.kernel.org/en/latest/contributor/send.html#passing-pre-flight-checks-v0-14

---
Brandon Maier (2):
utils/check-package: support finding files from patches .b4-config: check patches with ./utils/check-package

 .b4-config          |  1 +
 utils/check-package | 37 +++++++++++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 2 deletions(-)

---
base-commit: d74a5a218fabee928abb347086648ad142f7d5a5
change-id: 20240626-support-b4-41d61b8e4d60

Best regards,

Signature · 6 lines
Brandon Maier <[email protected]>

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw

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

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.

---
 utils/check-package | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
utils/check-package +35 -2
index 6a5e89daa3ce4f173cb21beb75122bcd40b80123..589891310ef6ef610ae9583c48672329560743a6 100755
@@ -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")
Signature · 6 lines
2.47.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#2 lore raw

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

On 05/11/2024 15:37, Brandon Maier via buildroot wrote:
[snip]

utils/check-package def __main__(): · line 333

Quoted 2 lines
utils/check-package +2 -0
+    if flags.patch:
+        files = patch_modified_files(flags.files)

I renamed this variable to files_to_check...

utils/check-package def __main__(): · line 344

Quoted 13 lines
utils/check-package +5 -2
+    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

... which allows us to remove the else branch entirely.

Regards,
Arnout

Quoted 4 lines

if len(files_to_check) == 0:
print("No files to check style")

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot

#3 lore raw

PATCH 2/2 .b4-config: check patches with ./utils/check-package

In reply to Brandon Maier via buildroot

NOTE: This requires b4 version 0.14.2 or later as earlier versions had a bug that broke custom prep-perpatch-check-cmd[1].

If a check-cmd is declared, b4 will warn developers to run the patch checker before sending a patch series[2][3]. Developers can then run b4 prep --check to automatically run ./utils/check-package on only files modified in the patch series.

[1] https://github.com/mricon/b4/commit/0741bc9a5b45578e4d3804588dd3f63fe4848059
[2] https://b4.docs.kernel.org/en/latest/contributor/send.html#passing-pre-flight-checks-v0-14
[3] https://b4.docs.kernel.org/en/latest/config.html#contributor-oriented-settings

---
 .b4-config | 1 +
 1 file changed, 1 insertion(+)
.b4-config +1 -0
index 1943458f2fb17cf9bef32f84758c6e0606d80277..d56866986420a744d87dd77d5acd09791d51ab42 100644
@@ -3,5 +3,6 @@
 [b4]
     send-series-to = [email protected]
     send-auto-cc-cmd = "./utils/get-developers -e -"
+    prep-perpatch-check-cmd = "./utils/check-package -q -p -"
 
 # vim: set filetype=gitconfig:
Signature · 6 lines
2.47.0

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#4 lore raw
In reply to Brandon Maier via buildroot
Quoted 8 lines · Brandon Maier via buildroot

NOTE: This requires b4 version 0.14.2 or later as earlier versions had a bug that broke custom prep-perpatch-check-cmd[1].

This series registers check-package as a b4 "pre-flight check"[1]. This way a patch series can be checked with b4 prep --check, and b4 will warn the developer if they try to send a patch series without running the check.

Quoted 8 lines

The first patch adds support for checking patch files to check-package.

The second patch registers check-package as a b4 check-cmd.

[1] https://github.com/mricon/b4/commit/0741bc9a5b45578e4d3804588dd3f63fe4848059
[2] https://b4.docs.kernel.org/en/latest/contributor/send.html#passing-pre-flight-checks-v0-14

Applied both to master, thanks, with a small modification to the first one.

Regards,
Arnout

Quoted 5 lines

---
Brandon Maier (2):
utils/check-package: support finding files from patches .b4-config: check patches with ./utils/check-package

Quoted 8 lines
  .b4-config          |  1 +
  utils/check-package | 37 +++++++++++++++++++++++++++++++++++--
  2 files changed, 36 insertions(+), 2 deletions(-)

---
base-commit: d74a5a218fabee928abb347086648ad142f7d5a5
change-id: 20240626-support-b4-41d61b8e4d60

Best regards,

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot