Add an introductory comment.
[git-dm.git] / linetags
blob5106be23b3c4c7a7c5cc179c6a0e9ca01df17f26
1 #!/usr/bin/pypy
3 # Find out how many lines were introduced in each major release.
5 # linetags <directory>
7 # This code is part of the LWN git data miner.
9 # Copyright 2007-11 Eklektix, Inc.
10 # Copyright 2007-11 Jonathan Corbet <corbet@lwn.net>
12 # This file may be distributed under the terms of the GNU General
13 # Public License, version 2.
15 import sys, re, os, pickle
17 CommitLines = { }
19 commitpat = re.compile(r'^([\da-f][\da-f]+) ')
21 def GetCommitLines(file):
22     print file
23     blame = os.popen('git blame -p ' + file, 'r')
24     for line in blame.readlines():
25         m = commitpat.search(line)
26         #
27         # All-zero commits mean we got fed a file that git doesn't
28         # know about.  We could throw an exception and abort processing
29         # now, or we can just silently ignore it...
30         #
31         if not m or m.group(1) == '0000000000000000000000000000000000000000':
32             continue
33         try:
34             CommitLines[m.group(1)] += 1
35         except KeyError:
36             CommitLines[m.group(1)] = 1
37     blame.close()
40 # Try to figure out which tag is the first to contain each commit.
42 refpat = re.compile(r'^(v2\.6\.\d\d).*$')
43 def CommitToTag(commit):
44     try:
45         return DB[commit]
46     except KeyError:
47         print 'Missing commit %s' % (commit)
48         return 'WTF?'
50 TagLines = { }
51 def MapCommits():
52     print 'Mapping tags...'
53     for commit in CommitLines.keys():
54         tag = CommitToTag(commit)
55         try:
56             TagLines[tag] += CommitLines[commit]
57         except KeyError:
58             TagLines[tag] = CommitLines[commit]
61 # Here we just plow through all the files.
63 if len(sys.argv) != 2:
64     sys.stderr.write('Usage: linetags directory\n')
65     sys.exit(1)
67 # Grab the tags/version database.
69 dbf = open('committags.db', 'r')
70 DB = pickle.load(dbf)
71 dbf.close()
73 out = open('linetags.out', 'w')
74 os.chdir(sys.argv[1])
75 files = os.popen('/usr/bin/find . -type f', 'r')
76 for file in files.readlines():
77     if file.find('.git/') < 0:
78         GetCommitLines(file[:-1])
79 MapCommits()
80 # print TagLines
81 tags = TagLines.keys()
82 tags.sort()
83 for tag in tags:
84     out.write('%s %d\n' % (tag, TagLines[tag]))
85 out.close()