mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 12:26:52 +00:00
Post-review fixes
This commit is contained in:
parent
c3c870c398
commit
cb23c48b0d
2 changed files with 16 additions and 17 deletions
|
|
@ -3,26 +3,25 @@ from __future__ import print_function
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
def _fix_file(filename, markdown=False):
|
def _fix_file(filename, is_markdown):
|
||||||
with tempfile.NamedTemporaryFile(mode='w+b', delete=False) as tmp_file:
|
with open(filename, mode='rb') as file_processed:
|
||||||
with open(filename, mode='rb') as original_file:
|
lines = file_processed.readlines()
|
||||||
for line in original_file.readlines():
|
lines = [_process_line(line, is_markdown) for line in lines]
|
||||||
# preserve trailing two-space for non-blank lines in markdown files
|
with open(filename, mode='wb') as file_processed:
|
||||||
eol = b'\r\n' if line[-2:] == b'\r\n' else b'\n'
|
for line in lines:
|
||||||
if markdown and (not line.isspace()) and line.endswith(b' ' + eol):
|
file_processed.write(line)
|
||||||
line = line.rstrip(b' \r\n') # restricted stripping: e.g. \t are not stripped
|
|
||||||
# only preserve if there are no trailing tabs or unusual whitespace
|
|
||||||
if not line[-1:].isspace():
|
def _process_line(line, is_markdown):
|
||||||
tmp_file.write(line + b' ' + eol)
|
# preserve trailing two-space for non-blank lines in markdown files
|
||||||
continue
|
eol = b'\r\n' if line[-2:] == b'\r\n' else b'\n'
|
||||||
tmp_file.write(line.rstrip() + eol)
|
if is_markdown and (not line.isspace()) and line.endswith(b' ' + eol):
|
||||||
os.remove(filename)
|
return line.rstrip() + b' ' + eol
|
||||||
os.rename(tmp_file.name, filename)
|
return line.rstrip() + eol
|
||||||
|
|
||||||
|
|
||||||
def fix_trailing_whitespace(argv=None):
|
def fix_trailing_whitespace(argv=None):
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
|
||||||
MD_TESTS_2 = (
|
MD_TESTS_2 = (
|
||||||
('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'),
|
('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'),
|
||||||
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
|
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
|
||||||
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz\n\n'),
|
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz \n\n'),
|
||||||
('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
|
('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
|
||||||
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
|
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue