/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
16
17
"""Annotate."""
18
19
from __future__ import absolute_import
20
21
from dulwich.object_store import (
22
    tree_lookup_path,
23
    )
24
7459.3.3 by Jelmer Vernooij
Add a 'lines' storage kind.
25
from .. import osutils
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
26
from ..errors import (
27
    NoSuchRevision,
28
    UnavailableRepresentation,
29
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
30
from ..graph import Graph
31
from ..revision import (
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
32
    NULL_REVISION,
33
    )
34
35
7459.3.3 by Jelmer Vernooij
Add a 'lines' storage kind.
36
class GitBlobContentFactory(object):
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
37
    """Static data content factory.
38
39
    This takes a fulltext when created and just returns that during
40
    get_bytes_as('fulltext').
41
42
    :ivar sha1: None, or the sha1 of the content fulltext.
43
    :ivar storage_kind: The native storage kind of this factory. Always
44
        'fulltext'.
45
    :ivar key: The key of this content. Each key is a tuple with a single
46
        string in it.
47
    :ivar parents: A tuple of parent keys for self.key. If the object has
48
        no parent information, None (as opposed to () for an empty list of
49
        parents).
50
     """
51
52
    def __init__(self, store, path, revision, blob_id):
53
        """Create a ContentFactory."""
54
        self.store = store
55
        self.key = (path, revision)
7459.3.3 by Jelmer Vernooij
Add a 'lines' storage kind.
56
        self.storage_kind = 'git-blob'
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
57
        self.parents = None
58
        self.blob_id = blob_id
7463.1.1 by Jelmer Vernooij
Add ContentFactory.size.
59
        self.size = None
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
60
61
    def get_bytes_as(self, storage_kind):
62
        if storage_kind == 'fulltext':
63
            return self.store[self.blob_id].as_raw_string()
7459.3.3 by Jelmer Vernooij
Add a 'lines' storage kind.
64
        elif storage_kind == 'lines':
7462.1.1 by Jelmer Vernooij
Add VersionedFile.iter_bytes_as.
65
            return list(osutils.chunks_to_lines(self.store[self.blob_id].as_raw_chunks()))
66
        elif storage_kind == 'chunked':
67
            return self.store[self.blob_id].as_raw_chunks()
68
        raise UnavailableRepresentation(self.key, storage_kind,
69
                                        self.storage_kind)
70
71
    def iter_bytes_as(self, storage_kind):
72
        if storage_kind == 'lines':
7486.1.2 by Jelmer Vernooij
Return iterators from iter_bytes_as.
73
            return iter(osutils.chunks_to_lines(self.store[self.blob_id].as_raw_chunks()))
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
74
        elif storage_kind == 'chunked':
7462.1.1 by Jelmer Vernooij
Add VersionedFile.iter_bytes_as.
75
            return iter(self.store[self.blob_id].as_raw_chunks())
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
76
        raise UnavailableRepresentation(self.key, storage_kind,
7459.3.3 by Jelmer Vernooij
Add a 'lines' storage kind.
77
                                        self.storage_kind)
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
78
79
80
class GitAbsentContentFactory(object):
81
    """Absent data content factory.
82
83
    :ivar sha1: None, or the sha1 of the content fulltext.
84
    :ivar storage_kind: The native storage kind of this factory. Always
85
        'fulltext'.
86
    :ivar key: The key of this content. Each key is a tuple with a single
87
        string in it.
88
    :ivar parents: A tuple of parent keys for self.key. If the object has
89
        no parent information, None (as opposed to () for an empty list of
90
        parents).
91
     """
92
93
    def __init__(self, store, path, revision):
94
        """Create a ContentFactory."""
95
        self.store = store
96
        self.key = (path, revision)
97
        self.storage_kind = 'absent'
98
        self.parents = None
7463.1.1 by Jelmer Vernooij
Add ContentFactory.size.
99
        self.size = None
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
100
101
    def get_bytes_as(self, storage_kind):
102
        raise ValueError
103
7462.1.1 by Jelmer Vernooij
Add VersionedFile.iter_bytes_as.
104
    def iter_bytes_as(self, storage_kind):
105
        raise ValueError
106
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
107
108
class AnnotateProvider(object):
109
110
    def __init__(self, change_scanner):
111
        self.change_scanner = change_scanner
112
        self.store = self.change_scanner.repository._git.object_store
113
114
    def _get_parents(self, path, text_revision):
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
115
        commit_id, mapping = (
116
            self.change_scanner.repository.lookup_bzr_revision_id(
117
                text_revision))
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
118
        text_parents = []
7290.36.2 by Jelmer Vernooij
Fix annotate as well.
119
        path = path.encode('utf-8')
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
120
        for commit_parent in self.store[commit_id].parents:
121
            try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
122
                (path, text_parent) = (
123
                    self.change_scanner.find_last_change_revision(
7290.36.2 by Jelmer Vernooij
Fix annotate as well.
124
                        path, commit_parent))
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
125
            except KeyError:
126
                continue
127
            if text_parent not in text_parents:
128
                text_parents.append(text_parent)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
129
        return tuple([
130
            (path.decode('utf-8'),
131
                self.change_scanner.repository.lookup_foreign_revision_id(p))
132
            for p in text_parents])
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
133
134
    def get_parent_map(self, keys):
135
        ret = {}
136
        for key in keys:
137
            (path, text_revision) = key
138
            if text_revision == NULL_REVISION:
139
                ret[key] = ()
140
                continue
141
            try:
142
                ret[key] = self._get_parents(path, text_revision)
143
            except KeyError:
144
                pass
145
        return ret
146
147
    def get_record_stream(self, keys, ordering, include_delta_closure):
148
        if ordering == 'topological':
149
            graph = Graph(self)
150
            keys = graph.iter_topo_order(keys)
151
        store = self.change_scanner.repository._git.object_store
152
        for (path, text_revision) in keys:
153
            try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
154
                commit_id, mapping = (
155
                    self.change_scanner.repository.lookup_bzr_revision_id(
156
                        text_revision))
157
            except NoSuchRevision:
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
158
                yield GitAbsentContentFactory(store, path, text_revision)
159
                continue
160
161
            try:
162
                tree_id = store[commit_id].tree
163
            except KeyError:
164
                yield GitAbsentContentFactory(store, path, text_revision)
165
                continue
166
            try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
167
                (mode, blob_sha) = tree_lookup_path(
168
                    store.__getitem__, tree_id, path.encode('utf-8'))
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
169
            except KeyError:
170
                yield GitAbsentContentFactory(store, path, text_revision)
171
            else:
7459.3.3 by Jelmer Vernooij
Add a 'lines' storage kind.
172
                yield GitBlobContentFactory(
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
173
                    store, path, text_revision, blob_sha)