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