mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
trailing-whitespace hook: restoring original file in case of failure - fixes #134
This commit is contained in:
parent
775a7906cd
commit
9bd837efaf
1 changed files with 14 additions and 10 deletions
|
|
@ -9,16 +9,20 @@ from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
def _fix_file(filename, markdown=False):
|
def _fix_file(filename, markdown=False):
|
||||||
for line in fileinput.input([filename], inplace=True):
|
try:
|
||||||
# preserve trailing two-space for non-blank lines in markdown files
|
for line in fileinput.input([filename], inplace=True, backup='.bak'):
|
||||||
if markdown and (not line.isspace()) and (line.endswith(" \n")):
|
# preserve trailing two-space for non-blank lines in markdown files
|
||||||
line = line.rstrip(' \n')
|
if markdown and (not line.isspace()) and (line.endswith(" \n")):
|
||||||
# only preserve if there are no trailing tabs or unusual whitespace
|
line = line.rstrip(' \n')
|
||||||
if not line[-1].isspace():
|
# only preserve if there are no trailing tabs or unusual whitespace
|
||||||
print(line + " ")
|
if not line[-1].isspace():
|
||||||
continue
|
print(line + " ")
|
||||||
|
continue
|
||||||
print(line.rstrip())
|
print(line.rstrip())
|
||||||
|
except Exception: # e.g. for UnicodeDecodeError
|
||||||
|
os.remove(filename) # needed on Windows apparently
|
||||||
|
os.replace(filename + '.bak', filename)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def fix_trailing_whitespace(argv=None):
|
def fix_trailing_whitespace(argv=None):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue