|
27 | 27 |
|
28 | 28 | logger = None |
29 | 29 |
|
30 | | -def git(*args, cwd=None): |
| 30 | +def git(*args, cwd=None, ignore_non_zero=False): |
31 | 31 | # Helper for running a Git command. Returns the rstrip()ed stdout output. |
32 | 32 | # 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). |
35 | 35 |
|
36 | 36 | git_cmd = ("git",) + args |
37 | 37 | try: |
38 | 38 | cp = subprocess.run(git_cmd, capture_output=True, cwd=cwd) |
39 | 39 | except OSError as e: |
40 | 40 | err(f"failed to run '{cmd2str(git_cmd)}': {e}") |
41 | 41 |
|
42 | | - if cp.returncode or cp.stderr: |
| 42 | + if not ignore_non_zero and (cp.returncode or cp.stderr): |
43 | 43 | err(f"'{cmd2str(git_cmd)}' exited with status {cp.returncode} and/or " |
44 | 44 | f"wrote to stderr.\n" |
45 | 45 | f"==stdout==\n" |
@@ -798,6 +798,33 @@ def check_source_file(self, fname): |
798 | 798 | self.failure(f"Please remove blank lines at end of '{fname}'") |
799 | 799 |
|
800 | 800 |
|
| 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 | + |
801 | 828 | class GitLint(ComplianceTest): |
802 | 829 | """ |
803 | 830 | Runs gitlint on the commits and finds issues with style and syntax |
|
0 commit comments