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