1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
from __future__ import absolute_import
21
from dulwich.object_store import (
25
from .. import osutils
26
from ..errors import (
28
UnavailableRepresentation,
30
from ..graph import Graph
31
from ..revision import (
36
class GitBlobContentFactory(object):
37
"""Static data content factory.
39
This takes a fulltext when created and just returns that during
40
get_bytes_as('fulltext').
42
:ivar sha1: None, or the sha1 of the content fulltext.
43
:ivar storage_kind: The native storage kind of this factory. Always
45
:ivar key: The key of this content. Each key is a tuple with a single
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
52
def __init__(self, store, path, revision, blob_id):
53
"""Create a ContentFactory."""
55
self.key = (path, revision)
56
self.storage_kind = 'git-blob'
58
self.blob_id = blob_id
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 == 'lines':
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,
72
class GitAbsentContentFactory(object):
73
"""Absent data content factory.
75
:ivar sha1: None, or the sha1 of the content fulltext.
76
:ivar storage_kind: The native storage kind of this factory. Always
78
:ivar key: The key of this content. Each key is a tuple with a single
80
:ivar parents: A tuple of parent keys for self.key. If the object has
81
no parent information, None (as opposed to () for an empty list of
85
def __init__(self, store, path, revision):
86
"""Create a ContentFactory."""
88
self.key = (path, revision)
89
self.storage_kind = 'absent'
93
def get_bytes_as(self, storage_kind):
97
class AnnotateProvider(object):
99
def __init__(self, change_scanner):
100
self.change_scanner = change_scanner
101
self.store = self.change_scanner.repository._git.object_store
103
def _get_parents(self, path, text_revision):
104
commit_id, mapping = (
105
self.change_scanner.repository.lookup_bzr_revision_id(
108
path = path.encode('utf-8')
109
for commit_parent in self.store[commit_id].parents:
111
(path, text_parent) = (
112
self.change_scanner.find_last_change_revision(
113
path, commit_parent))
116
if text_parent not in text_parents:
117
text_parents.append(text_parent)
119
(path.decode('utf-8'),
120
self.change_scanner.repository.lookup_foreign_revision_id(p))
121
for p in text_parents])
123
def get_parent_map(self, keys):
126
(path, text_revision) = key
127
if text_revision == NULL_REVISION:
131
ret[key] = self._get_parents(path, text_revision)
136
def get_record_stream(self, keys, ordering, include_delta_closure):
137
if ordering == 'topological':
139
keys = graph.iter_topo_order(keys)
140
store = self.change_scanner.repository._git.object_store
141
for (path, text_revision) in keys:
143
commit_id, mapping = (
144
self.change_scanner.repository.lookup_bzr_revision_id(
146
except NoSuchRevision:
147
yield GitAbsentContentFactory(store, path, text_revision)
151
tree_id = store[commit_id].tree
153
yield GitAbsentContentFactory(store, path, text_revision)
156
(mode, blob_sha) = tree_lookup_path(
157
store.__getitem__, tree_id, path.encode('utf-8'))
159
yield GitAbsentContentFactory(store, path, text_revision)
161
yield GitBlobContentFactory(
162
store, path, text_revision, blob_sha)