summaryrefslogtreecommitdiff
path: root/gitdm
blob: 47d19669f152e6f25daf69321e320c883a88a8cc (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python
#

#
# This code is part of the LWN git data miner.
#
# Copyright 2007-9 LWN.net
# Copyright 2007-9 Jonathan Corbet <corbet@lwn.net>
#
# This file may be distributed under the terms of the GNU General
# Public License, version 2.


import database, csv, ConfigFile, reports
import getopt, datetime
import os, re, sys, rfc822, string
from patterns import *

Today = datetime.date.today()

#
# Remember author names we have griped about.
#
GripedAuthorNames = [ ]

#
# Control options.
#
MapUnknown = 0
DevReports = 1
DateStats = 0
AuthorSOBs = 1
FileFilter = None
CSVFile = None
AkpmOverLt = 0
DumpDB = 0
CFName = 'gitdm.config'
DirName = ''

#
# Options:
#
# -a		Andrew Morton's signoffs shadow Linus's
# -b dir	Specify the base directory to fetch the configuration files
# -c cfile	Specify a configuration file
# -d		Output individual developer stats
# -D		Output date statistics
# -h hfile	HTML output to hfile
# -l count	Maximum length for output lists
# -o file	File for text output
# -r pattern	Restrict to files matching pattern
# -s		Ignore author SOB lines
# -u		Map unknown employers to '(Unknown)'
# -x file.csv   Export raw statistics as CSV
# -z		Dump out the hacker database at completion

def ParseOpts ():
    global MapUnknown, DevReports
    global DateStats, AuthorSOBs, FileFilter, AkpmOverLt, DumpDB
    global CFName, CSVFile, DirName

    opts, rest = getopt.getopt (sys.argv[1:], 'ab:dc:Dh:l:o:r:sux:z')
    for opt in opts:
        if opt[0] == '-a':
            AkpmOverLt = 1
        elif opt[0] == '-b':
            DirName = opt[1]
        elif opt[0] == '-c':
            CFName = opt[1]
        elif opt[0] == '-d':
            DevReports = 0
        elif opt[0] == '-D':
            DateStats = 1
        elif opt[0] == '-h':
            reports.SetHTMLOutput (open (opt[1], 'w'))
        elif opt[0] == '-l':
            reports.SetMaxList (int (opt[1]))
        elif opt[0] == '-o':
            reports.SetOutput (open (opt[1], 'w'))
        elif opt[0] == '-r':
            print 'Filter on "%s"' % (opt[1])
            FileFilter = re.compile (opt[1])
        elif opt[0] == '-s':
            AuthorSOBs = 0
        elif opt[0] == '-u':
            MapUnknown = 1
        elif opt[0] == '-x':
            CSVFile = open (opt[1], 'w')
            print "open output file " + opt[1] + "\n"
        elif opt[0] == '-z':
            DumpDB = 1
        


def LookupStoreHacker (name, email):
    email = database.RemapEmail (email)
    h = database.LookupEmail (email)
    if h: # already there
        return h
    elist = database.LookupEmployer (email, MapUnknown)
    h = database.LookupName (name)
    if h: # new email
        h.addemail (email, elist)
        return h
    return database.StoreHacker(name, elist, email)

#
# Date tracking.
#

DateMap = { }

def AddDateLines(date, lines):
    if lines > 1000000:
        print 'Skip big patch (%d)' % lines
        return
    try:
        DateMap[date] += lines
    except KeyError:
        DateMap[date] = lines

def PrintDateStats():
    dates = DateMap.keys ()
    dates.sort ()
    total = 0
    datef = open ('datelc.csv', 'w')
    datef.write('Date,Changed,Total Changed\n')
    for date in dates:
        total += DateMap[date]
        datef.write ('%d/%02d/%02d,%d,%d\n' % (date.year, date.month, date.day,
                                    DateMap[date], total))


#
# Let's slowly try to move some smarts into this class.
#
class patch:
    def __init__ (self, commit):
        self.commit = commit
        self.merge = self.added = self.removed = 0
        self.author = LookupStoreHacker('Unknown hacker', 'unknown@hacker.net')
        self.email = 'unknown@hacker.net'
        self.sobs = [ ]
        self.reviews = [ ]
        self.testers = [ ]
        self.reports = [ ]

    def addreviewer (self, reviewer):
        self.reviews.append (reviewer)

    def addtester (self, tester):
        self.testers.append (tester)

    def addreporter (self, reporter):
        self.reports.append (reporter)
#
# The core hack for grabbing the information about a changeset.
#
def grabpatch():
    global NextLine
    
    while (1):
        m = Pcommit.match (NextLine)
        if m:
            break;
        NextLine = sys.stdin.readline ()
        if not NextLine:
            return

    p = patch(m.group (1))
    NextLine = sys.stdin.readline ()
    ignore = (FileFilter is not None)
    while NextLine:
        Line = NextLine
        #
        # If this line starts a new commit, drop out.
        #
        m = Pcommit.match (Line)
        if m:
            break
        NextLine = sys.stdin.readline ()
        #
        # Maybe it's an author line?
        #
        m = Pauthor.match (Line)
        if m:
            p.email = database.RemapEmail (m.group (2))
            p.author = LookupStoreHacker(m.group (1), p.email)
            continue
        #
        # Could be a signed-off-by:
        #
        m = Psob.match (Line)
        if m:
            email = database.RemapEmail (m.group (2))
            sobber = LookupStoreHacker(m.group (1), email)
            if sobber != p.author or AuthorSOBs:
                p.sobs.append ((email, LookupStoreHacker(m.group (1), m.group (2))))
            continue
        #
        # Various other tags of interest.
        #
        m = Preview.match (Line)  # Reviewed-by:
        if m:
            email = database.RemapEmail (m.group (2))
            p.addreviewer (LookupStoreHacker(m.group (1), email))
            continue
        m = Ptest.match (Line)    # Tested-by:
        if m:
            email = database.RemapEmail (m.group (2))
            p.addtester (LookupStoreHacker (m.group (1), email))
            p.author.testcredit (patch)
            continue
        m = Prep.match (Line)     # Reported-by:
        if m:
            email = database.RemapEmail (m.group (2))
            p.addreporter (LookupStoreHacker (m.group (1), email))
            p.author.reportcredit (patch)
            continue
        m = Preptest.match (Line)  # Reported-and-tested-by:
        if m:
            email = database.RemapEmail (m.group (2))
            h = LookupStoreHacker (m.group (1), email)
            p.addreporter (h)
            p.addtester (h)
            p.author.reportcredit (patch)
            p.author.testcredit (patch)
            continue
        #
        # If this one is a merge, make note of the fact.
        #
        m = Pmerge.match (Line)
        if m:
            p.merge = 1
            continue
        #
        # See if it's the date.
        #
        m = Pdate.match (Line)
        if m:
            dt = rfc822.parsedate(m.group (2))
            p.date = datetime.date (dt[0], dt[1], dt[2])
            if p.date > Today:
                sys.stderr.write ('Funky date: %s\n' % p.date)
                p.date = Today
            continue
        #
        # If we have a file filter, check for file lines.
        #
        if FileFilter:
            ignore = ApplyFileFilter (Line, ignore)
        #
        # OK, maybe it's part of the diff itself.
        #
        if not ignore:
            if Padd.match (Line):
                p.added += 1
                continue
            if Prem.match (Line):
                p.removed += 1

    if '@' in p.author.name:
        GripeAboutAuthorName (p.author.name)

    return p

def GripeAboutAuthorName (name):
    if name in GripedAuthorNames:
        return
    GripedAuthorNames.append (name)
    print '%s is an author name, probably not what you want' % (name)

def ApplyFileFilter (line, ignore):
    #
    # If this is the first file line (--- a/), set ignore one way
    # or the other.
    #
    m = Pfilea.match (line)
    if m:
        file = m.group (1)
        if FileFilter.search (file):
            return 0
        return 1
    #
    # For the second line, we can turn ignore off, but not on
    #
    m = Pfileb.match (line)
    if m:
        file = m.group (1)
        if FileFilter.search (file):
            return 0
    return ignore

#
# If this patch is signed off by both Andrew Morton and Linus Torvalds,
# remove the (redundant) Linus signoff.
#
def TrimLTSOBs (p):
    if AkpmOverLt == 1 and Linus in p.sobs and Akpm in p.sobs:
        p.sobs.remove (Linus)


#
# Here starts the real program.
#
ParseOpts ()

#
# Read the config files.
#
ConfigFile.ConfigFile (CFName, DirName)

#
# Let's pre-seed the database with a couple of hackers
# we want to remember.
#
if AkpmOverLt == 1:
    Linus = ('torvalds@linux-foundation.org',
         LookupStoreHacker ('Linus Torvalds', 'torvalds@linux-foundation.org'))
    Akpm = ('akpm@linux-foundation.org',
        LookupStoreHacker ('Andrew Morton', 'akpm@linux-foundation.org'))

NextLine = sys.stdin.readline ()
TotalChanged = TotalAdded = TotalRemoved = 0

#
# Snarf changesets.
#
print >> sys.stderr, 'Grabbing changesets...\r',

printcount = CSCount = 0
while (1):
    if (printcount % 50) == 0:
        print >> sys.stderr, 'Grabbing changesets...%d\r' % printcount,
    printcount += 1
    p = grabpatch()
    if not p:
        break
#    if p.added > 100000 or p.removed > 100000:
#        print 'Skipping massive add', p.commit
#        continue
    if FileFilter and p.added == 0 and p.removed == 0:
        continue

    #
    # Record some global information - but only if this patch had
    # stuff which wasn't ignored.
    #
    if ((p.added + p.removed) > 0 or not FileFilter) and not p.merge:
        TotalAdded += p.added
        TotalRemoved += p.removed
        TotalChanged += max (p.added, p.removed)
        AddDateLines (p.date, max (p.added, p.removed))
        empl = p.author.emailemployer (p.email, p.date)
        empl.AddCSet (p)
        if AkpmOverLt:
            TrimLTSOBs (p)
        for sobemail, sobber in p.sobs:
            empl = sobber.emailemployer (sobemail, p.date)
            empl.AddSOB()

    if not p.merge:
        p.author.addpatch (p)
        for sobemail, sob in p.sobs:
            sob.addsob (p)
        for hacker in p.reviews:
            hacker.addreview (p)
        for hacker in p.testers:
            hacker.addtested (p)
        for hacker in p.reports:
            hacker.addreport (p)
    CSCount += 1
    csv.AccumulatePatch (p)
print >> sys.stderr, 'Grabbing changesets...done       '

if DumpDB:
    database.DumpDB ()
#
# Say something
#
hlist = database.AllHackers ()
elist = database.AllEmployers ()
ndev = nempl = 0
for h in hlist:
    if len (h.patches) > 0:
        ndev += 1
for e in elist:
    if e.count > 0:
        nempl += 1
reports.Write ('Processed %d csets from %d developers\n' % (CSCount,
                                                            ndev))
reports.Write ('%d employers found\n' % (nempl))
reports.Write ('A total of %d lines added, %d removed (delta %d)\n' %
               (TotalAdded, TotalRemoved, TotalAdded - TotalRemoved))
if TotalChanged == 0:
    TotalChanged = 1 # HACK to avoid div by zero
if DateStats:
    PrintDateStats ()

csv.OutputCSV (CSVFile)
if CSVFile is not None:
        CSVFile.close ()

if DevReports:
    reports.DevReports (hlist, TotalChanged, CSCount, TotalRemoved)
reports.EmplReports (elist, TotalChanged, CSCount)