Skip to content

Instantly share code, notes, and snippets.

@fberger
Created August 27, 2013 18:05
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 fberger/6356927 to your computer and use it in GitHub Desktop.
Save fberger/6356927 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Add this to: .git/hooks/pre-commit
# if ! git diff-index --check $against > /dev/null 2>&1; then
# git diff --cached --name-status | $(HOME)/bin/linecleaner.py
# fi
import sys
import re
import fileinput
import subprocess
files = []
for line in sys.stdin:
if line[0] in ('A', 'M'):
files.append(line[2:-1])
def edited_line_numbers(contents):
for line in contents.split('\n'):
if line.startswith('0000000000000000000000000000000000000000'):
tokens = line.split(' ')
yield int(tokens[2]) - 1
for f in files:
contents = subprocess.Popen(['git', 'blame', '-p', '--line-porcelain', f],
stdout=subprocess.PIPE).stdout.read()
line_numbers = set(edited_line_numbers(contents))
for number, line in enumerate(fileinput.input(f, inplace=1)):
if number in line_numbers:
match = re.match('(.*?)\s+$', line)
if match:
sys.stdout.write(match.group(1))
if line.endswith('\n'):
sys.stdout.write('\n')
else:
sys.stdout.write(line)
else:
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment