Repository navigation
build(style): fast newline check with --fix as part of make format #30028
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
Open
+127
−28
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Check that tracked text files end in exactly one newline: a missing one breaks | ||
| the POSIX definition of a line, more than one fails trailing whitespace checks. | ||
|
|
||
| Usage: | ||
| check_newlines.py check all tracked text files outside docs/ | ||
| check_newlines.py FILE... check only the given files | ||
| check_newlines.py --fix [FILE...] add a missing or remove extra newlines | ||
|
|
||
| Only the last bytes of each file are read, so checking the whole tree is fast | ||
| enough to run as part of make format. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| def text_files(): | ||
| """Tracked files git does not consider binary, excluding docs/, relative to the repository root.""" | ||
| result = subprocess.run(['git', 'grep', '--cached', '-Il', '-z', '', '--', ':!docs/*'], | ||
| capture_output=True, check=False) | ||
|
|
||
| if result.returncode not in (0, 1): # 1: no match | ||
| sys.stderr.write(result.stderr.decode(errors='replace')) | ||
| sys.exit(result.returncode) | ||
|
|
||
| return [p.decode() for p in result.stdout.split(b'\0') if p] | ||
|
|
||
|
|
||
| def line_ending(f, size): | ||
| """Trailing '\\r' and '\\n' bytes at the end of the file.""" | ||
| ending = b'' | ||
| position = size | ||
|
|
||
| while position > 0: | ||
| chunk = min(position, 64) | ||
| position -= chunk | ||
| f.seek(position) | ||
| data = f.read(chunk) | ||
| stripped = data.rstrip(b'\r\n') | ||
| ending = data[len(stripped):] + ending | ||
|
|
||
| if stripped: | ||
| break | ||
|
|
||
| return ending | ||
|
|
||
|
|
||
| def check(path, fix): | ||
| try: | ||
| with open(path, 'rb+' if fix else 'rb') as f: | ||
| size = os.fstat(f.fileno()).st_size | ||
| ending = line_ending(f, size) | ||
| newlines = ending.count(b'\n') | ||
|
|
||
| if size == 0 or (newlines == 1 and ending.endswith(b'\n')): | ||
| return True | ||
|
|
||
| if not fix: | ||
| print(f'{"Multiple newlines" if newlines > 1 else "No new line"} at the end of {path}') | ||
| return False | ||
|
|
||
| if newlines == 0: # replace stray '\r' bytes, if any, by a newline | ||
| f.truncate(size - len(ending)) | ||
| f.seek(0, os.SEEK_END) | ||
| f.write(b'\n') | ||
|
|
||
| elif len(ending) == size: # file contains only line endings | ||
| f.truncate(0) | ||
|
|
||
| else: # keep the first line ending (including a possible '\r'), drop the rest | ||
| f.truncate(size - len(ending) + ending.index(b'\n') + 1) | ||
|
|
||
| print(f'Fixed newlines at the end of {path}') | ||
| return True | ||
|
|
||
| except (FileNotFoundError, IsADirectoryError): # deleted or replaced in the working tree | ||
| return True | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | ||
| parser.add_argument('--fix', action='store_true', help='fix the files in place') | ||
| parser.add_argument('files', nargs='*', help='files to check (default: all tracked text files outside docs/)') | ||
| args = parser.parse_args() | ||
|
|
||
| if args.files: | ||
| files = args.files | ||
|
|
||
| else: | ||
| top = subprocess.run(['git', 'rev-parse', '--show-toplevel'], | ||
| capture_output=True, text=True, check=True).stdout.strip() | ||
| os.chdir(top) | ||
| files = text_files() | ||
|
|
||
| ok = all([check(path, args.fix) for path in files]) | ||
| return 0 if ok else 1 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.