linetags: python 3 port
[git-dm.git] / committags
blobd01523767045f1870a3b60f080cfccb88f0c7925
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
7 # It has been pretty much superseded by inittags, though.
9 # committags [git-args]
11 # This code is part of the LWN git data miner.
13 # Copyright 2007-13 Eklektix, Inc.
14 # Copyright 2007-13 Jonathan Corbet <corbet@lwn.net>
16 # This file may be distributed under the terms of the GNU General
17 # Public License, version 2.
19 import sys
20 import re
21 import os
22 import pickle
23 import argparse
26 # Arg parsing stuff.
28 def setupargs():
29     p = argparse.ArgumentParser()
30     #
31     # -d for the database
32     # -l to load it before running
33     #
34     p.add_argument('-d', '--database', help = 'Database name',
35                    required = False, default = 'committags.db')
36     p.add_argument('-l', '--load', help = 'Load database at startup',
37                    default = False, action = 'store_true')
38     #
39     # Args for git?
40     #
41     p.add_argument('-g', '--git', help = 'Arguments to git',
42                    default = '')
43     #
44     # Where's the repo?
45     #
46     p.add_argument('-r', '--repository', help = 'Repository location',
47                    default = '')
48     return p
51 p = setupargs()
52 args = p.parse_args()
55 # Pull in an existing database if requested.
57 if args.load:
58     DB = pickle.load(open(args.database, 'r'))
59 else:
60     DB = { }
61 out = open(args.database, 'w')
64 # Time to fire up git.
66 git = 'git log --pretty=format:%H ' + args.git
67 if args.repository:
68     os.chdir(args.repository)
69 input = os.popen(git, 'r')
71 nc = 0
72 for line in input.readlines():
73     commit = line.strip()
74     #
75     # If we loaded a database and this commit is already there, we
76     # can quit.
77     #
78     if args.load and DB.has_key(commit):
79         break
80     #
81     # Figure out which version this one came from.
82     #
83     desc = os.popen('git describe --contains --match v\\* ' + commit, 'r')
84     tag = desc.readline().strip()
85     desc.close()
86     dash = tag.find('-')
87     if dash > 0:
88         DB[commit] = tag[:dash]
89     else:
90         DB[commit] = tag
91     #
92     # Give them something to watch.
93     #
94     nc += 1
95     if (nc % 25) == 0:
96         print '%6d %s %s           \r' % (nc, commit[:8], tag),
97         sys.stdout.flush()
99 print '\nFound %d/%d commits' % (nc, len(DB.keys()))
100 pickle.dump(DB, out)
101 out.close()