Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-109408: Move the C file whitespace check from patchcheck to pre-commit #109890

Merged
merged 11 commits into from
Oct 10, 2023
Next Next commit
Move the C file whitespace check to pre-commit
  • Loading branch information
AA-Turner committed Sep 26, 2023
commit f7d8e99cd658f81731fdd349caf6fe98e85123cb
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ repos:
- id: trailing-whitespace
types_or: [c, python, rst]

- repo: local
hooks:
- id: c-file-whitespace
name: "Check C file whitespace"
language: pygrep
entry: '\t'
types_or: ['c', 'c++']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were running on .c and .h files before:

c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]

Both of those are matched by the c type, so this would be closer to parity:

Suggested change
types_or: ['c', 'c++']
types_or: [c]

We do have half a dozen .cpp files in the codebase, do we want to expand to include them? Should we also add c++ for trailing-whitespace above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test passed with 'c++' included, I imagined that it was a previous oversight that they weren't included. I'll check if the trailing whitespace check also passes.

Copy link
Member Author

@AA-Turner AA-Turner Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tools/msi/bundle/bootstrap/PythonBootstrapperApplication.cpp fails with 16 lines changed. Other than that all good (@zooba would you be alright with us enabling the trailing whitespace check here? No real views either way, if you'd prefer to keep the whitespace then that's the status quo anyway!)

A

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We can always remove c++ later if needed.)


- repo: https://github.com/sphinx-contrib/sphinx-lint
rev: v0.6.8
hooks:
Expand Down
28 changes: 6 additions & 22 deletions Tools/patchcheck/patchcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,6 @@ def normalize_whitespace(file_paths):
return fixed


@status("Fixing C file whitespace", info=report_modified_files)
def normalize_c_whitespace(file_paths):
"""Report if any C files """
fixed = []
for path in file_paths:
abspath = os.path.join(SRCDIR, path)
with open(abspath, 'r') as f:
if '\t' not in f.read():
continue
untabify.process(abspath, 8, verbose=False)
fixed.append(path)
return fixed


ws_re = re.compile(br'\s+(\r?\n)$')

@status("Fixing docs whitespace", info=report_modified_files)
Expand Down Expand Up @@ -272,7 +258,6 @@ def ci(pull_request):
fn.endswith(('.rst', '.inc'))]
fixed = []
fixed.extend(normalize_whitespace(python_files))
fixed.extend(normalize_c_whitespace(c_files))
fixed.extend(normalize_docs_whitespace(doc_files))
if not fixed:
print('No whitespace issues found')
Expand All @@ -285,14 +270,11 @@ def main():
base_branch = get_base_branch()
file_paths = changed_files(base_branch)
python_files = [fn for fn in file_paths if fn.endswith('.py')]
c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
doc_files = [fn for fn in file_paths if fn.startswith('Doc') and
fn.endswith(('.rst', '.inc'))]
misc_files = {p for p in file_paths if p.startswith('Misc')}
# PEP 8 whitespace rules enforcement.
normalize_whitespace(python_files)
# C rules enforcement.
normalize_c_whitespace(c_files)
# Doc whitespace enforcement.
normalize_docs_whitespace(doc_files)
# Docs updated.
Expand All @@ -307,10 +289,12 @@ def main():
regenerated_pyconfig_h_in(file_paths)

# Test suite run and passed.
if python_files or c_files:
end = " and check for refleaks?" if c_files else "?"
print()
print("Did you run the test suite" + end)
has_c_files = any(fn for fn in file_paths if fn.endswith(('.c', '.h')))
print()
if has_c_files:
print("Did you run the test suite and check for refleaks?")
elif python_files:
print("Did you run the test suite?")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so much clearer like this! 👍



if __name__ == '__main__':
Expand Down
Loading