Skip to content

Instantly share code, notes, and snippets.

@pansila
Last active August 16, 2018 08:58
Show Gist options
  • Save pansila/7329358db10ac82dec6a3cf494d44b1a to your computer and use it in GitHub Desktop.
Save pansila/7329358db10ac82dec6a3cf494d44b1a to your computer and use it in GitHub Desktop.
Cull violations against a patch set of git from a complete cpplint result file
#!/usr/bin/python
import sys, os
from sys import argv
from unidiff import PatchSet
import subprocess
def parse_log_line(line):
"""
parse a line from the cpplint results with --output=vs7
"""
if ":" not in line:
return None
file_lineno = line.split(":")[0]
if "(" not in file_lineno:
return None
(file, lineno) = file_lineno.split("(")
file = file.replace("\\", "/") # for Windows
lineno = lineno[:-1]
try:
lineno = int(lineno)
except:
return None
return (file, lineno)
def cull_violations(inputFile, outputFile, diff):
cull_list = []
with open(inputFile) as input:
patch_files = PatchSet(diff, encoding='utf-8')
for line in input:
parsedLine = parse_log_line(line)
if parsedLine is None:
continue
for patch in patch_files:
if patch.path != parsedLine[0]:
continue
for p in patch:
for l in p.target_lines():
if l.is_context or l.is_removed:
continue
if l.is_added and l.target_line_no != parsedLine[1]:
continue
cull_list.append(line)
with open(outputFile, 'w') as out:
if len(cull_list) == 0:
return 0
for line in cull_list:
out.write(line)
return 1
if __name__ == "__main__":
if len(argv) < 3:
print("Usage: %s {input log file} {output log file} <diff file>" % argv[0])
sys.exit(1)
if len(argv) != 4:
diff = subprocess.check_output("git format-patch -1 --stdout HEAD", shell=True)
else:
with open(argv[3]) as f:
diff = f.read()
ret = cull_violations(argv[1], argv[2], diff)
sys.exit(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment