Add an introductory comment.
[git-dm.git] / committags
blob0ddd36fb9bd86e4556e53a8fbba614abf4abd7ae
1 #!/usr/bin/pypy
3 # Generate a database of commits and major versions they went into.
5 # This is the painfully slow reworked brute-force version that
6 # takes forever to run, but which hopefully gets the right results
8 # committags [git-args]
10 # This code is part of the LWN git data miner.
12 # Copyright 2007-13 Eklektix, Inc.
13 # Copyright 2007-13 Jonathan Corbet <corbet@lwn.net>
15 # This file may be distributed under the terms of the GNU General
16 # Public License, version 2.
18 import sys
19 import re
20 import os
21 import pickle
22 import argparse
25 # Arg parsing stuff.
27 def setupargs():
28     p = argparse.ArgumentParser()
29     #
30     # -d for the database
31     # -l to load it before running
32     #
33     p.add_argument('-d', '--database', help = 'Database name',
34                    required = False, default = 'committags.db')
35     p.add_argument('-l', '--load', help = 'Load database at startup',
36                    default = False, action = 'store_true')
37     #
38     # Args for git?
39     #
40     p.add_argument('-g', '--git', help = 'Arguments to git',
41                    default = '')
42     #
43     # Where's the repo?
44     #
45     p.add_argument('-r', '--repository', help = 'Repository location',
46                    default = '')
47     return p
50 p = setupargs()
51 args = p.parse_args()
54 # Pull in an existing database if requested.
56 if args.load:
57     DB = pickle.load(open(args.database, 'r'))
58 else:
59     DB = { }
60 out = open(args.database, 'w')
63 # Time to fire up git.
65 git = 'git log --pretty=format:%H ' + args.git
66 if args.repository:
67     os.chdir(args.repository)
68 input = os.popen(git, 'r')
70 nc = 0
71 for line in input.readlines():
72     commit = line.strip()
73     #
74     # If we loaded a database and this commit is already there, we
75     # can quit.
76     #
77     if args.load and DB.has_key(commit):
78         break
79     #
80     # Figure out which version this one came from.
81     #
82     desc = os.popen('git describe --contains --match v\\* ' + commit, 'r')
83     tag = desc.readline().strip()
84     desc.close()
85     dash = tag.find('-')
86     if dash > 0:
87         DB[commit] = tag[:dash]
88     else:
89         DB[commit] = tag
90     #
91     # Give them something to watch.
92     #
93     nc += 1
94     if (nc % 25) == 0:
95         print '%6d %s %s\r' % (nc, commit[:8], tag),
96         sys.stdout.flush()
98 print '\nFound %d/%d commits' % (nc, len(DB.keys()))
99 pickle.dump(DB, out)
100 out.close()