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