Created
July 6, 2012 10:57
-
-
Save htgoebel/3059500 to your computer and use it in GitHub Desktop.
Filter all unchanged and all only-comment lines from a diff-output.
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/python | |
""" | |
Filter all unchanged and all only-comment lines from a diff-output. | |
and be more terse. | |
""" | |
import sys | |
import re | |
only_space_before_comment = re.compile(r'^[+-]\s*#') | |
empty_diff_line = re.compile(r'^[+-]\s*$') | |
state = 0 | |
save_line = None | |
for line in sys.stdin.readlines(): | |
# ignore the header (following after a `diff ...` line) like: | |
# index fa1e684..32e2c2b 100755 | |
# --- a/contrib/livestatus/splitlivelogs.py | |
# +++ b/contrib/livestatus/splitlivelogs.py | |
if state == 1 and line.startswith('index '): | |
state += 1 | |
continue | |
elif state == 2 and line.startswith('--- '): | |
save_line = line # print this line later if there is further output | |
state += 1 | |
continue | |
elif state == 3 and line.startswith('+++ '): | |
state = 0 | |
continue | |
# header lines should have filtered by the code obove, so just | |
# reset state | |
state = 0 | |
if line.startswith('diff '): | |
#save_line = line -- print the '--- ' line instead (more terse) | |
# expect the header to come next | |
state = 1 | |
continue | |
elif line.startswith('@@ ') and ' @@' in line: | |
# ignore position lines | |
continue | |
elif line.startswith(' '): | |
# ignore context lines | |
continue | |
elif only_space_before_comment.match(line): | |
continue | |
elif empty_diff_line.match(line): | |
continue | |
elif line.startswith('+') or line.startswith('-'): | |
# diff add line | |
# only strip add lines, so we can see what has been removed | |
line = line.rsplit('#',1)[0] # remove comment (from the right) | |
line = line.rstrip('#') # remove further trailing hash signs | |
## # to be used in config files | |
## line = line.rsplit(';',1)[0] # remove comment (from the right) | |
## line = line.rstrip(';') # remove further trailing hash signs | |
line = line.rstrip() + '\n' # re-append stripped newline | |
if save_line: | |
sys.stdout.write(save_line) | |
save_line = None | |
sys.stdout.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment