Skip to content

Commit 442a452

Browse files
fkokosinskifabiobaltieri
authored andcommitted
scripts/ci/check_compliance: add GitDiffCheck compliance check
This commit adds a new `GitDiffCheck` compliance check that checks the newly added commits with `git diff --check` and reports them back if an error is found. This check is needed for some files (e.g. Kconfig) as they are not checked by `Kconfig` and `KconfigBasic` checks on every commit in a pull request. Signed-off-by: Filip Kokosinski <[email protected]>
1 parent 83d031c commit 442a452

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

scripts/ci/check_compliance.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727

2828
logger = None
2929

30-
def git(*args, cwd=None):
30+
def git(*args, cwd=None, ignore_non_zero=False):
3131
# Helper for running a Git command. Returns the rstrip()ed stdout output.
3232
# Called like git("diff"). Exits with SystemError (raised by sys.exit()) on
33-
# errors. 'cwd' is the working directory to use (default: current
34-
# directory).
33+
# errors if 'ignore_non_zero' is set to False (default: False). 'cwd' is the
34+
# working directory to use (default: current directory).
3535

3636
git_cmd = ("git",) + args
3737
try:
3838
cp = subprocess.run(git_cmd, capture_output=True, cwd=cwd)
3939
except OSError as e:
4040
err(f"failed to run '{cmd2str(git_cmd)}': {e}")
4141

42-
if cp.returncode or cp.stderr:
42+
if not ignore_non_zero and (cp.returncode or cp.stderr):
4343
err(f"'{cmd2str(git_cmd)}' exited with status {cp.returncode} and/or "
4444
f"wrote to stderr.\n"
4545
f"==stdout==\n"
@@ -798,6 +798,33 @@ def check_source_file(self, fname):
798798
self.failure(f"Please remove blank lines at end of '{fname}'")
799799

800800

801+
class GitDiffCheck(ComplianceTest):
802+
"""
803+
Checks for conflict markers or whitespace errors with git diff --check
804+
"""
805+
name = "GitDiffCheck"
806+
doc = "Git conflict markers and whitespace errors are not allowed in added changes"
807+
path_hint = "<git-top>"
808+
809+
def run(self):
810+
offending_lines = []
811+
# Use regex to filter out unnecessay output
812+
# Reason: `--check` is mutually exclusive with `--name-only` and `-s`
813+
p = re.compile(r"\S+\: .*\.")
814+
815+
for shaidx in get_shas(COMMIT_RANGE):
816+
# Ignore non-zero return status code
817+
# Reason: `git diff --check` sets the return code to the number of offending lines
818+
diff = git("diff", f"{shaidx}^!", "--check", ignore_non_zero=True)
819+
820+
lines = p.findall(diff)
821+
lines = map(lambda x: f"{shaidx}: {x}", lines)
822+
offending_lines.extend(lines)
823+
824+
if len(offending_lines) > 0:
825+
self.failure("\n".join(offending_lines))
826+
827+
801828
class GitLint(ComplianceTest):
802829
"""
803830
Runs gitlint on the commits and finds issues with style and syntax

0 commit comments

Comments
 (0)