/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
1
# Copyright (C) 2006-2010 Canonical Ltd
0.140.26 by Jelmer Vernooij
Add copyright headers.
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
16
"""A Simple bzr plugin to generate statistics about the history."""
17
0.140.49 by Jelmer Vernooij
use absolute_import everywhere.
18
from __future__ import absolute_import
19
6645.1.1 by Jelmer Vernooij
Bundle the stats plugin.
20
from ... import (
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
21
    branch,
22
    commands,
23
    config,
24
    errors,
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
25
    option,
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
26
    trace,
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
27
    tsort,
0.144.1 by Wesley J. Landaker
Added ui to bzrlib lazy imports.
28
    ui,
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
29
    workingtree,
30
    )
6645.1.1 by Jelmer Vernooij
Bundle the stats plugin.
31
from ...revision import NULL_REVISION
32
from .classify import classify_delta
0.140.36 by John Arbash Meinel
Clean up the test suite infrastructure, add a version tuple
33
0.140.3 by John Arbash Meinel
Updated to combine by author name, as well as by email address, and report on multiple names/addresses
34
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
35
def collapse_by_person(revisions, canonical_committer):
0.140.16 by Jelmer Vernooij
Rename collapse_by_author -> collapse_by_person since author has an unambigous meaning
36
    """The committers list is sorted by email, fix it up by person.
0.140.3 by John Arbash Meinel
Updated to combine by author name, as well as by email address, and report on multiple names/addresses
37
38
    Some people commit with a similar username, but different email
39
    address. Which makes it hard to sort out when they have multiple
40
    entries. Email is actually more stable, though, since people
41
    frequently forget to set their name properly.
42
43
    So take the most common username for each email address, and
44
    combine them into one new list.
45
    """
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
46
    # Map from canonical committer to
47
    # {committer: ([rev_list], {email: count}, {fname:count})}
48
    committer_to_info = {}
49
    for rev in revisions:
50
        authors = rev.get_apparent_authors()
51
        for author in authors:
52
            username, email = config.parse_username(author)
0.140.42 by Stewart Smith
Fix authors loop when an author is blank.
53
            if len(username) == 0 and len(email) == 0:
54
                continue
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
55
            canon_author = canonical_committer[(username, email)]
56
            info = committer_to_info.setdefault(canon_author, ([], {}, {}))
57
            info[0].append(rev)
58
            info[1][email] = info[1].setdefault(email, 0) + 1
59
            info[2][username] = info[2].setdefault(username, 0) + 1
60
    res = [(len(revs), revs, emails, fnames)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
61
           for revs, emails, fnames in committer_to_info.values()]
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
62
    res.sort(reverse=True)
63
    return res
64
65
66
def collapse_email_and_users(email_users, combo_count):
67
    """Combine the mapping of User Name to email and email to User Name.
68
69
    If a given User Name is used for multiple emails, try to map it all to one
70
    entry.
71
    """
72
    id_to_combos = {}
73
    username_to_id = {}
74
    email_to_id = {}
75
    id_counter = 0
76
77
    def collapse_ids(old_id, new_id, new_combos):
78
        old_combos = id_to_combos.pop(old_id)
79
        new_combos.update(old_combos)
80
        for old_user, old_email in old_combos:
81
            if (old_user and old_user != user):
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
82
                low_old_user = old_user.lower()
83
                old_user_id = username_to_id[low_old_user]
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
84
                assert old_user_id in (old_id, new_id)
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
85
                username_to_id[low_old_user] = new_id
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
86
            if (old_email and old_email != email):
87
                old_email_id = email_to_id[old_email]
88
                assert old_email_id in (old_id, new_id)
89
                email_to_id[old_email] = cur_id
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
90
    for email, usernames in email_users.items():
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
91
        assert email not in email_to_id
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
92
        if not email:
93
            # We use a different algorithm for usernames that have no email
94
            # address, we just try to match by username, and not at all by
95
            # email
96
            for user in usernames:
97
                if not user:
98
                    continue # The mysterious ('', '') user
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
99
                # When mapping, use case-insensitive names
100
                low_user = user.lower()
101
                user_id = username_to_id.get(low_user)
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
102
                if user_id is None:
103
                    id_counter += 1
104
                    user_id = id_counter
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
105
                    username_to_id[low_user] = user_id
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
106
                    id_to_combos[user_id] = id_combos = set()
107
                else:
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
108
                    id_combos = id_to_combos[user_id]
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
109
                id_combos.add((user, email))
110
            continue
111
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
112
        id_counter += 1
113
        cur_id = id_counter
114
        id_to_combos[cur_id] = id_combos = set()
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
115
        email_to_id[email] = cur_id
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
116
117
        for user in usernames:
118
            combo = (user, email)
119
            id_combos.add(combo)
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
120
            if not user:
121
                # We don't match on empty usernames
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
122
                continue
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
123
            low_user = user.lower()
124
            user_id = username_to_id.get(low_user)
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
125
            if user_id is not None:
126
                # This UserName was matched to an cur_id
127
                if user_id != cur_id:
128
                    # And it is a different identity than the current email
129
                    collapse_ids(user_id, cur_id, id_combos)
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
130
            username_to_id[low_user] = cur_id
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
131
    combo_to_best_combo = {}
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
132
    for cur_id, combos in id_to_combos.items():
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
133
        best_combo = sorted(combos,
134
                            key=lambda x:combo_count[x],
135
                            reverse=True)[0]
136
        for combo in combos:
137
            combo_to_best_combo[combo] = best_combo
138
    return combo_to_best_combo
139
140
141
def get_revisions_and_committers(a_repo, revids):
142
    """Get the Revision information, and the best-match for committer."""
143
144
    email_users = {} # user@email.com => User Name
145
    combo_count = {}
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
146
    with ui.ui_factory.nested_progress_bar() as pb:
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
147
        trace.note('getting revisions')
6714.1.4 by Jelmer Vernooij
More use of iter_revisions.
148
        revisions = a_repo.iter_revisions(revids)
149
        for count, (revid, rev) in enumerate(revisions):
0.142.2 by Jelmer Vernooij
Split out functionality that sorts revids by commmitter.
150
            pb.update('checking', count, len(revids))
0.140.29 by Jelmer Vernooij
Remove some uses of get_apparent_author.
151
            for author in rev.get_apparent_authors():
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
152
                # XXX: There is a chance sometimes with svn imports that the
153
                #      full name and email can BOTH be blank.
154
                username, email = config.parse_username(author)
155
                email_users.setdefault(email, set()).add(username)
156
                combo = (username, email)
157
                combo_count[combo] = combo_count.setdefault(combo, 0) + 1
158
    return revisions, collapse_email_and_users(email_users, combo_count)
0.142.2 by Jelmer Vernooij
Split out functionality that sorts revids by commmitter.
159
160
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
161
def get_info(a_repo, revision):
162
    """Get all of the information for a particular revision"""
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
163
    with ui.ui_factory.nested_progress_bar() as pb, a_repo.lock_read():
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
164
        trace.note('getting ancestry')
0.151.2 by Jelmer Vernooij
Remove another use of get_ancestry.
165
        graph = a_repo.get_graph()
166
        ancestry = [
167
            r for (r, ps) in graph.iter_ancestry([revision])
168
            if ps is not None and r != NULL_REVISION]
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
169
        revs, canonical_committer = get_revisions_and_committers(a_repo, ancestry)
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
170
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
171
    return collapse_by_person(revs, canonical_committer)
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
172
173
0.140.7 by John Arbash Meinel
Compute the revisions using a difference check
174
def get_diff_info(a_repo, start_rev, end_rev):
175
    """Get only the info for new revisions between the two revisions
0.150.3 by Lukáš Lalinský
Reuse sort_by_committer in get_diff_info
176
0.140.7 by John Arbash Meinel
Compute the revisions using a difference check
177
    This lets us figure out what has actually changed between 2 revisions.
178
    """
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
179
    with ui.ui_factory.nested_progress_bar() as pb, a_repo.lock_read():
0.151.3 by Jelmer Vernooij
Avoid use of get_ancestry.
180
        graph = a_repo.get_graph()
181
        trace.note('getting ancestry diff')
182
        ancestry = graph.find_difference(start_rev, end_rev)[1]
0.140.35 by John Arbash Meinel
Merge Lukas's extra tests, update for the new code.
183
        revs, canonical_committer = get_revisions_and_committers(a_repo, ancestry)
0.140.7 by John Arbash Meinel
Compute the revisions using a difference check
184
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
185
    return collapse_by_person(revs, canonical_committer)
0.140.7 by John Arbash Meinel
Compute the revisions using a difference check
186
0.140.16 by Jelmer Vernooij
Rename collapse_by_author -> collapse_by_person since author has an unambigous meaning
187
0.140.20 by Jelmer Vernooij
Add --show-class argument to stats command.
188
def display_info(info, to_file, gather_class_stats=None):
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
189
    """Write out the information"""
190
191
    for count, revs, emails, fullnames in info:
192
        # Get the most common email name
193
        sorted_emails = sorted(((count, email)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
194
                               for email, count in emails.items()),
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
195
                               reverse=True)
196
        sorted_fullnames = sorted(((count, fullname)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
197
                                  for fullname, count in fullnames.items()),
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
198
                                  reverse=True)
0.147.2 by John Arbash Meinel
Handle the case where we have a user but no email.
199
        if sorted_fullnames[0][1] == '' and sorted_emails[0][1] == '':
0.146.1 by Paul Hummer
Revisions with missing emails are no longer all attributed to the same person
200
            to_file.write('%4d %s\n'
201
                          % (count, 'Unknown'))
202
        else:
203
            to_file.write('%4d %s <%s>\n'
204
                          % (count, sorted_fullnames[0][1],
205
                             sorted_emails[0][1]))
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
206
        if len(sorted_fullnames) > 1:
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
207
            to_file.write('     Other names:\n')
208
            for count, fname in sorted_fullnames:
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
209
                to_file.write('     %4d ' % (count,))
210
                if fname == '':
211
                    to_file.write("''\n")
212
                else:
213
                    to_file.write("%s\n" % (fname,))
214
        if len(sorted_emails) > 1:
0.147.1 by John Arbash Meinel
Improve the committer matcher tremendously.
215
            to_file.write('     Other email addresses:\n')
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
216
            for count, email in sorted_emails:
217
                to_file.write('     %4d ' % (count,))
218
                if email == '':
219
                    to_file.write("''\n")
220
                else:
221
                    to_file.write("%s\n" % (email,))
0.140.20 by Jelmer Vernooij
Add --show-class argument to stats command.
222
        if gather_class_stats is not None:
0.140.39 by Jelmer Vernooij
Eliminate print command.
223
            to_file.write('     Contributions:\n')
0.140.20 by Jelmer Vernooij
Add --show-class argument to stats command.
224
            classes, total = gather_class_stats(revs)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
225
            for name, count in sorted(classes.items(), key=classify_key):
0.140.24 by Jelmer Vernooij
Remove 2.5ism.
226
                if name is None:
227
                    name = "Unknown"
228
                to_file.write("     %4.0f%% %s\n" % ((float(count) / total) * 100.0, name))
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
229
230
0.140.14 by Jelmer Vernooij
Merge upstream.
231
class cmd_committer_statistics(commands.Command):
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
232
    """Generate statistics for LOCATION."""
233
0.140.12 by Jelmer Vernooij
Change name to committer-stats, to allow for other sorts of stats too.
234
    aliases = ['stats', 'committer-stats']
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
235
    takes_args = ['location?']
0.140.20 by Jelmer Vernooij
Add --show-class argument to stats command.
236
    takes_options = ['revision', 
0.148.1 by Petr Viktorin
Typo fix
237
            option.Option('show-class', help="Show the class of contributions.")]
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
238
0.140.3 by John Arbash Meinel
Updated to combine by author name, as well as by email address, and report on multiple names/addresses
239
    encoding_type = 'replace'
240
0.140.20 by Jelmer Vernooij
Add --show-class argument to stats command.
241
    def run(self, location='.', revision=None, show_class=False):
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
242
        alternate_rev = None
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
243
        try:
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
244
            wt = workingtree.WorkingTree.open_containing(location)[0]
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
245
        except errors.NoWorkingTree:
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
246
            a_branch = branch.Branch.open(location)
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
247
            last_rev = a_branch.last_revision()
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
248
        else:
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
249
            a_branch = wt.branch
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
250
            last_rev = wt.last_revision()
0.140.7 by John Arbash Meinel
Compute the revisions using a difference check
251
0.140.8 by John Arbash Meinel
Allow branch: to work, which needs a write lock
252
        if revision is not None:
253
            last_rev = revision[0].in_history(a_branch).rev_id
254
            if len(revision) > 1:
255
                alternate_rev = revision[1].in_history(a_branch).rev_id
256
6754.8.4 by Jelmer Vernooij
Use new context stuff.
257
        with a_branch.lock_read():
0.140.7 by John Arbash Meinel
Compute the revisions using a difference check
258
            if alternate_rev:
259
                info = get_diff_info(a_branch.repository, last_rev,
260
                                     alternate_rev)
261
            else:
262
                info = get_info(a_branch.repository, last_rev)
0.140.25 by Jelmer Vernooij
Merge support for Python2.4.
263
        if show_class:
264
            def fetch_class_stats(revs):
265
                return gather_class_stats(a_branch.repository, revs)
266
        else:
267
            fetch_class_stats = None
0.145.1 by Russ Brown
Made to work with python 2.4
268
        display_info(info, self.outf, fetch_class_stats)
0.140.1 by John Arbash Meinel
A simple plugin for generating author statistics, may grow into more.
269
270
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
271
class cmd_ancestor_growth(commands.Command):
0.140.4 by John Arbash Meinel
added ancestry_growth to generate a csv of ancestors.
272
    """Figure out the ancestor graph for LOCATION"""
273
274
    takes_args = ['location?']
275
276
    encoding_type = 'replace'
277
278
    def run(self, location='.'):
279
        try:
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
280
            wt = workingtree.WorkingTree.open_containing(location)[0]
0.140.4 by John Arbash Meinel
added ancestry_growth to generate a csv of ancestors.
281
        except errors.NoWorkingTree:
0.143.1 by John Arbash Meinel
Make a lot of imports lazy since they may not actually be used.
282
            a_branch = branch.Branch.open(location)
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
283
            last_rev = a_branch.last_revision()
0.140.4 by John Arbash Meinel
added ancestry_growth to generate a csv of ancestors.
284
        else:
0.140.6 by John Arbash Meinel
refactor in preparation for supporting 2 revision specs
285
            a_branch = wt.branch
0.140.4 by John Arbash Meinel
added ancestry_growth to generate a csv of ancestors.
286
            last_rev = wt.last_revision()
287
6754.8.4 by Jelmer Vernooij
Use new context stuff.
288
        with a_branch.lock_read():
0.140.38 by Jelmer Vernooij
Avoid using Repository.get_revision_graph().
289
            graph = a_branch.repository.get_graph()
290
            revno = 0
291
            cur_parents = 0
292
            sorted_graph = tsort.merge_sort(graph.iter_ancestry([last_rev]),
293
                                            last_rev)
294
            for num, node_name, depth, isend in reversed(sorted_graph):
295
                cur_parents += 1
296
                if depth == 0:
297
                    revno += 1
298
                    self.outf.write('%4d, %4d\n' % (revno, cur_parents))
0.140.4 by John Arbash Meinel
added ancestry_growth to generate a csv of ancestors.
299
300
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
301
def gather_class_stats(repository, revs):
302
    ret = {}
303
    total = 0
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
304
    with ui.ui_factory.nested_progress_bar() as pb:
6754.8.4 by Jelmer Vernooij
Use new context stuff.
305
        with repository.lock_read():
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
306
            i = 0
307
            for delta in repository.get_deltas_for_revisions(revs):
308
                pb.update("classifying commits", i, len(revs))
309
                for c in classify_delta(delta):
310
                    if not c in ret:
311
                        ret[c] = 0
312
                    ret[c] += 1
313
                    total += 1
314
                i += 1
315
    return ret, total
316
317
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
318
def classify_key(item):
319
    """Sort key for item of (author, count) from classify_delta."""
320
    return -item[1], item[0]
321
322
0.140.37 by John Arbash Meinel
Update display_credits to handle unprintable chars.
323
def display_credits(credits, to_file):
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
324
    (coders, documenters, artists, translators) = credits
325
    def print_section(name, lst):
326
        if len(lst) == 0:
327
            return
0.140.37 by John Arbash Meinel
Update display_credits to handle unprintable chars.
328
        to_file.write("%s:\n" % name)
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
329
        for name in lst:
0.140.37 by John Arbash Meinel
Update display_credits to handle unprintable chars.
330
            to_file.write("%s\n" % name)
331
        to_file.write('\n')
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
332
    print_section("Code", coders)
333
    print_section("Documentation", documenters)
334
    print_section("Art", artists)
335
    print_section("Translations", translators)
336
337
338
def find_credits(repository, revid):
339
    """Find the credits of the contributors to a revision.
340
341
    :return: tuple with (authors, documenters, artists, translators)
342
    """
343
    ret = {"documentation": {},
344
           "code": {},
345
           "art": {},
346
           "translation": {},
347
           None: {}
348
           }
6754.8.4 by Jelmer Vernooij
Use new context stuff.
349
    with repository.lock_read():
0.151.1 by Jelmer Vernooij
Remove use of .get_ancestry.
350
        graph = repository.get_graph()
0.140.54 by Jelmer Vernooij
Filter out NULL_REVISION.
351
        ancestry = [r for (r, ps) in graph.iter_ancestry([revid])
352
                    if ps is not None and r != NULL_REVISION]
0.140.23 by Jelmer Vernooij
Add another progress bar.
353
        revs = repository.get_revisions(ancestry)
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
354
        with ui.ui_factory.nested_progress_bar() as pb:
6754.5.1 by Jelmer Vernooij
Fix some python3 compatibility issues that break 'make check-nodocs3' for me.
355
            iterator = zip(revs, repository.get_deltas_for_revisions(revs))
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
356
            for i, (rev, delta) in enumerate(iterator):
0.140.23 by Jelmer Vernooij
Add another progress bar.
357
                pb.update("analysing revisions", i, len(revs))
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
358
                # Don't count merges
359
                if len(rev.parent_ids) > 1:
360
                    continue
361
                for c in set(classify_delta(delta)):
0.140.29 by Jelmer Vernooij
Remove some uses of get_apparent_author.
362
                    for author in rev.get_apparent_authors():
363
                        if not author in ret[c]:
364
                            ret[c][author] = 0
365
                        ret[c][author] += 1
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
366
    def sort_class(name):
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
367
        return [author
6656.1.2 by Martin
Biggest change ever, honestly it's huge, the biggest
368
            for author, _  in sorted(ret[name].items(), key=classify_key)]
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
369
    return (sort_class("code"), sort_class("documentation"), sort_class("art"), sort_class("translation"))
370
371
372
class cmd_credits(commands.Command):
373
    """Determine credits for LOCATION."""
374
375
    takes_args = ['location?']
376
    takes_options = ['revision']
377
378
    encoding_type = 'replace'
379
380
    def run(self, location='.', revision=None):
381
        try:
382
            wt = workingtree.WorkingTree.open_containing(location)[0]
383
        except errors.NoWorkingTree:
384
            a_branch = branch.Branch.open(location)
385
            last_rev = a_branch.last_revision()
386
        else:
387
            a_branch = wt.branch
388
            last_rev = wt.last_revision()
389
390
        if revision is not None:
391
            last_rev = revision[0].in_history(a_branch).rev_id
392
6754.8.4 by Jelmer Vernooij
Use new context stuff.
393
        with a_branch.lock_read():
0.140.18 by Jelmer Vernooij
Add credits command, test classify code by default, add comments to classify code.
394
            credits = find_credits(a_branch.repository, last_rev)
0.140.37 by John Arbash Meinel
Update display_credits to handle unprintable chars.
395
            display_credits(credits, self.outf)