~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/annotate_ui.py

  • Committer: Francesco 'pr0gg3d' Del Degan
  • Date: 2011-08-08 20:58:09 UTC
  • mto: This revision was merged to the branch mainline in revision 454.
  • Revision ID: f.deldegan@pr0gg3d.net-20110808205809-n3eg73n80ymyd6kj
Fixes bug in annotate that occurs when file is zero-sized

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2010 Canonical Ltd.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
#
 
18
 
 
19
import itertools
 
20
 
 
21
from loggerhead.controllers.view_ui import ViewUI
 
22
from loggerhead import util
 
23
 
 
24
class AnnotateUI(ViewUI):
 
25
 
 
26
    def annotate_file(self, info):
 
27
        file_id = info['file_id']
 
28
        revid = info['change'].revid
 
29
        
 
30
        tree = self.tree_for(file_id, revid)
 
31
        
 
32
        change_cache = {}
 
33
        last_line_revid = None
 
34
        last_lineno = None
 
35
        message = ""
 
36
 
 
37
        revisions = {}
 
38
 
 
39
        lineno = 0
 
40
        for (line_revid, text), lineno in zip(tree.annotate_iter(file_id), itertools.count(1)):
 
41
            if line_revid != last_line_revid:
 
42
                last_line_revid = line_revid
 
43
 
 
44
                change = change_cache.get(line_revid, None)
 
45
                if change is None:
 
46
                    change = self._history.get_changes([line_revid])[0]
 
47
                    change_cache[line_revid] = change
 
48
 
 
49
                message = change.comment.splitlines()[0]
 
50
 
 
51
                if last_lineno:
 
52
                    # The revspan is of lines between the last revision and this one.
 
53
                    # We set the one for the previous revision when we're creating the current revision.
 
54
                    revisions[last_lineno].revspan = lineno - last_lineno
 
55
 
 
56
                revisions[lineno] = util.Container(change=change, message=message)
 
57
 
 
58
                last_lineno = lineno
 
59
                last_line_revid = line_revid
 
60
 
 
61
        # Zero-size file. Return empty revisions.
 
62
        if last_lineno is None:
 
63
            return revisions
 
64
 
 
65
        # We never set a revspan for the last revision during the loop above, so set it here.
 
66
        revisions[last_lineno].revspan = lineno - last_lineno + 1
 
67
 
 
68
        return revisions
 
69
            
 
70
    def get_values(self, path, kwargs, headers):
 
71
        values = super(AnnotateUI, self).get_values(path, kwargs, headers)
 
72
        values['annotated'] = self.annotate_file(values)
 
73
        
 
74
        return values