Skip to content
Newer
Older
100755 62 lines (44 sloc) 1.49 KB
8a8e11c @dustin Tool for counting lines in a tree.
dustin authored Sep 24, 2009
1 #!/usr/bin/env python
2
3 import sys
4 import subprocess
5
6 class Blob(object):
7
8 def __init__(self, blob_id, size):
9 self.blob_id = blob_id
10 self.size = size
11 self.lines = 0
12
13 def count_lines(self):
14 self.lines = 0
15
16 if self.size < 2*1024*1024:
17 args = ['git', 'cat-file', 'blob', self.blob_id]
18 sub=subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
19 for l in sub.stdout:
20 self.lines += 1
21
22 def load_blobs(ref):
23 args = ['git', 'ls-tree', '-z', '-l', '-r', ref]
24 sub=subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
25
26 blobs = sub.stdout.read().split("\0")
27 rv = []
28 for b in (b for b in blobs if b):
29 info, filename = b.split("\t")
30 mode, t, blob_id, size = info.split()
31
32 assert t == 'blob'
33
34 rv.append(Blob(blob_id, int(size)))
35
36 return rv
37
38 def rev_parse(ref):
39 args = ['git', 'rev-parse', ref]
40 sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
41 return sub.stdout.read().strip()
42
43 def name(ref):
44 args = ['git', 'describe', ref]
45 sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
46 return sub.stdout.read().strip()
47
48 if __name__ == '__main__':
49
50 refs = sys.argv[1:]
51 if not refs:
52 refs = ['HEAD']
53
54 for ref in refs:
55 ref = rev_parse(ref)
56
57 blobs = load_blobs(ref)
58 for b in blobs:
59 b.count_lines()
60
61 print "%s %d" % (name(ref), sum(b.lines for b in blobs))
Something went wrong with that request. Please try again.