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 <fkokosinski@antmicro.com>
This commit is contained in:
Filip Kokosinski 2023-05-17 13:35:43 +02:00 committed by Fabio Baltieri
parent 83d031c024
commit 442a452903

View file

@ -27,11 +27,11 @@ from get_maintainer import Maintainers, MaintainersError
logger = None
def git(*args, cwd=None):
def git(*args, cwd=None, ignore_non_zero=False):
# Helper for running a Git command. Returns the rstrip()ed stdout output.
# Called like git("diff"). Exits with SystemError (raised by sys.exit()) on
# errors. 'cwd' is the working directory to use (default: current
# directory).
# errors if 'ignore_non_zero' is set to False (default: False). 'cwd' is the
# working directory to use (default: current directory).
git_cmd = ("git",) + args
try:
@ -39,7 +39,7 @@ def git(*args, cwd=None):
except OSError as e:
err(f"failed to run '{cmd2str(git_cmd)}': {e}")
if cp.returncode or cp.stderr:
if not ignore_non_zero and (cp.returncode or cp.stderr):
err(f"'{cmd2str(git_cmd)}' exited with status {cp.returncode} and/or "
f"wrote to stderr.\n"
f"==stdout==\n"
@ -798,6 +798,33 @@ concatenated together, so no document separators are needed.""")
self.failure(f"Please remove blank lines at end of '{fname}'")
class GitDiffCheck(ComplianceTest):
"""
Checks for conflict markers or whitespace errors with git diff --check
"""
name = "GitDiffCheck"
doc = "Git conflict markers and whitespace errors are not allowed in added changes"
path_hint = "<git-top>"
def run(self):
offending_lines = []
# Use regex to filter out unnecessay output
# Reason: `--check` is mutually exclusive with `--name-only` and `-s`
p = re.compile(r"\S+\: .*\.")
for shaidx in get_shas(COMMIT_RANGE):
# Ignore non-zero return status code
# Reason: `git diff --check` sets the return code to the number of offending lines
diff = git("diff", f"{shaidx}^!", "--check", ignore_non_zero=True)
lines = p.findall(diff)
lines = map(lambda x: f"{shaidx}: {x}", lines)
offending_lines.extend(lines)
if len(offending_lines) > 0:
self.failure("\n".join(offending_lines))
class GitLint(ComplianceTest):
"""
Runs gitlint on the commits and finds issues with style and syntax