Created
November 19, 2016 14:48
-
-
Save adamobeng/9cce7345cb826d3ddab77620a900f3b3 to your computer and use it in GitHub Desktop.
Git pre-commit hook to remove non-breaking spaces from R files
This file contains 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
#!/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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice