forked from dustin/bindir
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-linecount
executable file
·61 lines (44 loc) · 1.49 KB
/
git-linecount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
import sys
import subprocess
class Blob(object):
def __init__(self, blob_id, size):
self.blob_id = blob_id
self.size = size
self.lines = 0
def count_lines(self):
self.lines = 0
if self.size < 2*1024*1024:
args = ['git', 'cat-file', 'blob', self.blob_id]
sub=subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
for l in sub.stdout:
self.lines += 1
def load_blobs(ref):
args = ['git', 'ls-tree', '-z', '-l', '-r', ref]
sub=subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
blobs = sub.stdout.read().split("\0")
rv = []
for b in (b for b in blobs if b):
info, filename = b.split("\t")
mode, t, blob_id, size = info.split()
assert t == 'blob'
rv.append(Blob(blob_id, int(size)))
return rv
def rev_parse(ref):
args = ['git', 'rev-parse', ref]
sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
return sub.stdout.read().strip()
def name(ref):
args = ['git', 'describe', ref]
sub = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
return sub.stdout.read().strip()
if __name__ == '__main__':
refs = sys.argv[1:]
if not refs:
refs = ['HEAD']
for ref in refs:
ref = rev_parse(ref)
blobs = load_blobs(ref)
for b in blobs:
b.count_lines()
print "%s %d" % (name(ref), sum(b.lines for b in blobs))