Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git pre-commit hook to remove non-breaking spaces from R files
#!/usr/bin/env python3
import subprocess
files = subprocess.check_output(
'git diff --cached --name-only --diff-filter=ACM *R', shell=True
).decode().strip().split('\n')
files = [f for f in files if f!='']
for f in files:
try:
spaces = subprocess.check_output(
'pcregrep --color=\'auto\' "[\\xa0]" %s' % f, shell=True
).decode().strip().split('\n')
except subprocess.CalledProcessError:
continue
print('Replacing nonbreaking spaces:\n')
if spaces:
print(f)
for s in spaces:
print(s.encode('unicode-escape'))
subprocess.call(
'sed -i -e \'s/[\u00a0]/ /g\' %s' % f, shell=True
)
subprocess.call(
'git add %s' % f, shell=True
)
@kbenoit

This comment has been minimized.

kbenoit commented Nov 19, 2016

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment