3
class DiffStat(object):
4
def __init__(self, lines):
12
def __parse(self, lines):
19
if line.startswith('+') and not line.startswith('+++'):
21
elif line.startswith('-') and not line.startswith('---'):
23
elif line.startswith('=== '):
24
self.__add_stats(current, adds, removes)
30
elif line.startswith('--- ') and current is None:
31
current = line[4:].strip()
33
self.__add_stats(current, adds, removes)
41
def __add_stats(self, file, adds, removes):
44
elif file in self.stats:
45
fstat = self.stats[file]
47
fstat = self.Filestat()
50
fstat.removes += removes
51
fstat.total = adds + removes
52
self.stats[file] = fstat
54
self.maxname = max(self.maxname, len(file))
55
self.maxtotal = max(self.maxtotal, fstat.total)
56
self.total_adds += adds
57
self.total_removes += removes
62
countwidth = len(str(self.maxtotal))
63
graphwidth = width - countwidth - self.maxname
66
# The graph width can be <= 0 if there is a modified file with a
67
# filename longer than 'width'. Use a minimum of 10.
71
while (self.maxtotal / factor) > graphwidth:
76
for file, fstat in self.stats.iteritems():
77
s += ' %-*s | %*.d ' % (self.maxname, file, countwidth, fstat.total)
79
# If diffstat runs out of room it doesn't print anything, which
80
# isn't very useful, so always print at least one + or 1
81
s += '+' * max(fstat.adds / factor, 1)
82
s += '-' * max(fstat.removes / factor, 1)
85
s += ' %d files changed, %d insertions(+), %d deletions(-)' % \
86
(len(self.stats), self.total_adds, self.total_removes)
89
if __name__ == '__main__':
91
ds = DiffStat(sys.stdin.readlines())