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