/breezy-svn/trunk

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy-svn/trunk
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
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Annotate support."""

from __future__ import absolute_import

import subvertpy

from cStringIO import (
    StringIO,
    )
from subvertpy.delta import (
    apply_txdelta_window,
    )

from breezy import (
    urlutils,
    )
from breezy.annotate import (
    reannotate,
    )
from breezy.errors import (
    NoSuchId,
    )

from breezy.plugins.svn import (
    changes,
    )


class Annotater(object):

    def __init__(self, repository):
        self._text = ""
        self._annotated = []
        self._repository = repository
        self._related_revs = {}

    def get_annotated(self):
        return self._annotated

    def check_file_revs(self, revid, branch_path, revnum, mapping, relpath):
        self._annotated = []
        for (revmeta, hidden, mapping) in self._repository._revmeta_provider._iter_reverse_revmeta_mapping_history(
                branch_path, revnum, to_revnum=0, mapping=mapping):
            if hidden:
                continue
            self._related_revs[revmeta.metarev.revnum] = revmeta, mapping
        self._text = ""
        path = urlutils.join(branch_path, relpath.encode("utf-8")).strip("/")
        try:
            self._repository.transport.get_file_revs(path, -1, revnum,
                self._handler, include_merged_revisions=True)
        except subvertpy.SubversionException, (msg, num):
            if num == subvertpy.ERR_FS_NOT_FILE:
                return []
            raise
        return self._annotated

    def _get_ids(self, path, rev, revprops):
        revmeta, mapping = self._related_revs[rev]
        revmeta.metarev._revprops = revprops
        path = path.strip("/")
        if not changes.path_is_child(revmeta.metarev.branch_path, path):
            raise KeyError(path)
        ip = path[len(revmeta.metarev.branch_path):].strip("/")
        idmap = self._repository.get_fileid_map(revmeta, mapping)
        return idmap.lookup(mapping, ip)

    def check_file(self, lines, revid, parent_lines):
        return reannotate(parent_lines, lines, revid)

    def _handler(self, path, rev, revprops, result_of_merge=None):
        try:
            fileid, revid, _ = self._get_ids(path, rev, revprops)
            assert revid is not None
        except KeyError:
            # Related file in Subversion but not in Bazaar
            # We still apply the delta since we'll need the fulltext later
            fileid = None
        stream = StringIO()
        def apply_window(window):
            if window is None:
                stream.seek(0)
                lines = stream.readlines()
                self._text = "".join(lines)
                self._annotated = self.check_file(lines, revid, [self._annotated])
                return
            stream.write(apply_txdelta_window(self._text, window))
        return apply_window