Created
August 27, 2013 18:05
-
-
Save fberger/6356927 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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