devutils/lint: add rule to detect whitespace-only changes (#510)

This commit is contained in:
jj
2025-11-24 09:38:36 +01:00
committed by GitHub
parent 6312142913
commit 1a266da9a3

View File

@@ -16,6 +16,10 @@ def _read_text(path):
return filter(str, f.read().splitlines())
def _read_patch(path):
return unidiff.PatchSet('\n'.join(_read_text(path)))
def _init(root):
global patches_dir
global series
@@ -59,8 +63,7 @@ def c_all_new_files_have_license_header():
if 'helium' not in patch:
continue
patch_set = unidiff.PatchSet('\n'.join(_read_text(patch)))
added_files = filter(lambda f: f.is_added_file, patch_set)
added_files = filter(lambda f: f.is_added_file, _read_patch(patch))
for file in added_files:
if any(p in file.path.lower() for p in LICENSE_HEADER_IGNORES):
@@ -75,8 +78,8 @@ def c_all_new_headers_have_correct_guard():
if 'helium' not in patch:
continue
patch_set = unidiff.PatchSet('\n'.join(_read_text(patch)))
added_files = filter(lambda f: f.is_added_file and f.path.endswith('.h'), patch_set)
added_files = filter(lambda f: f.is_added_file and f.path.endswith('.h'),
_read_patch(patch))
for file in added_files:
expected_macro_name = file.path.upper() \
@@ -112,3 +115,22 @@ def c_all_new_headers_have_correct_guard():
assert value == f"+{expected[macro_type]}\n", \
f"Patch {patch} has unexpected {macro_type} in {file.path}:" \
f"{value_print}, expecting: {expected[macro_type]}"
def d_no_whitespace_only_changes():
for patch in series:
if 'helium' not in patch:
continue
for file in _read_patch(patch):
for hunk in file:
seen_nonws = False
for line in hunk:
line = str(line)
if line.startswith('+') or line.startswith('-'):
seen_nonws = seen_nonws or len(line.rstrip()) > 1
assert seen_nonws, \
f"Patch {patch} contains hunk consisting of "\
f"only whitespace characters in {file.path}: {hunk}"