Skip to content

Instantly share code, notes, and snippets.

@adamobeng
Created November 19, 2016 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamobeng/9cce7345cb826d3ddab77620a900f3b3 to your computer and use it in GitHub Desktop.
Save adamobeng/9cce7345cb826d3ddab77620a900f3b3 to your computer and use it in GitHub Desktop.
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
Copy link

kbenoit commented Nov 19, 2016

nice

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