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 dulwich.object_store import (
23
from .. import osutils
24
from ..bzr.versionedfile import UnavailableRepresentation
25
from ..errors import (
28
from ..graph import Graph
29
from ..revision import (
33
from .mapping import (
39
class GitBlobContentFactory(object):
40
"""Static data content factory.
42
This takes a fulltext when created and just returns that during
43
get_bytes_as('fulltext').
45
:ivar sha1: None, or the sha1 of the content fulltext.
46
:ivar storage_kind: The native storage kind of this factory. Always
48
:ivar key: The key of this content. Each key is a tuple with a single
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
55
def __init__(self, store, path, revision, blob_id):
56
"""Create a ContentFactory."""
58
self.key = (path, revision)
59
self.storage_kind = 'git-blob'
61
self.blob_id = blob_id
64
def get_bytes_as(self, storage_kind):
65
if storage_kind == 'fulltext':
66
return self.store[self.blob_id].as_raw_string()
67
elif storage_kind == 'lines':
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,
74
def iter_bytes_as(self, storage_kind):
75
if storage_kind == 'lines':
76
return iter(osutils.chunks_to_lines(self.store[self.blob_id].as_raw_chunks()))
77
elif storage_kind == 'chunked':
78
return iter(self.store[self.blob_id].as_raw_chunks())
79
raise UnavailableRepresentation(self.key, storage_kind,
83
class GitAbsentContentFactory(object):
84
"""Absent data content factory.
86
:ivar sha1: None, or the sha1 of the content fulltext.
87
:ivar storage_kind: The native storage kind of this factory. Always
89
:ivar key: The key of this content. Each key is a tuple with a single
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
96
def __init__(self, store, path, revision):
97
"""Create a ContentFactory."""
99
self.key = (path, revision)
100
self.storage_kind = 'absent'
104
def get_bytes_as(self, storage_kind):
107
def iter_bytes_as(self, storage_kind):
111
class AnnotateProvider(object):
113
def __init__(self, change_scanner):
114
self.change_scanner = change_scanner
115
self.store = self.change_scanner.repository._git.object_store
117
def _get_parents(self, path, text_revision):
118
commit_id, mapping = (
119
self.change_scanner.repository.lookup_bzr_revision_id(
122
path = encode_git_path(path)
123
for commit_parent in self.store[commit_id].parents:
125
(path, text_parent) = (
126
self.change_scanner.find_last_change_revision(
127
path, commit_parent))
130
if text_parent not in text_parents:
131
text_parents.append(text_parent)
133
(decode_git_path(path),
134
self.change_scanner.repository.lookup_foreign_revision_id(p))
135
for p in text_parents])
137
def get_parent_map(self, keys):
140
(path, text_revision) = key
141
if text_revision == NULL_REVISION:
145
ret[key] = self._get_parents(path, text_revision)
150
def get_record_stream(self, keys, ordering, include_delta_closure):
151
if ordering == 'topological':
153
keys = graph.iter_topo_order(keys)
154
store = self.change_scanner.repository._git.object_store
155
for (path, text_revision) in keys:
157
commit_id, mapping = (
158
self.change_scanner.repository.lookup_bzr_revision_id(
160
except NoSuchRevision:
161
yield GitAbsentContentFactory(store, path, text_revision)
165
tree_id = store[commit_id].tree
167
yield GitAbsentContentFactory(store, path, text_revision)
170
(mode, blob_sha) = tree_lookup_path(
171
store.__getitem__, tree_id, encode_git_path(path))
173
yield GitAbsentContentFactory(store, path, text_revision)
175
yield GitBlobContentFactory(
176
store, path, text_revision, blob_sha)