/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1613 by Jelmer Vernooij
Handle encoding better in working tree iter changes.
1
# Copyright (C) 2009-2012 Jelmer Vernooij <jelmer@samba.org>
2
# Copyright (C) 2012 Canonical Ltd
0.200.228 by Jelmer Vernooij
Split out map.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""Map from Git sha's to Bazaar objects."""
19
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
20
from __future__ import absolute_import
21
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
22
from dulwich.objects import (
23
    Blob,
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
24
    Commit,
0.200.864 by Jelmer Vernooij
Cope with the first commit being pointless.
25
    Tree,
0.200.586 by Jelmer Vernooij
Fix issues pointed out by pyflakes.
26
    sha_to_hex,
0.200.1153 by Jelmer Vernooij
Import ZERO_SHA from dulwich.objects.
27
    ZERO_SHA,
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
28
    )
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
29
from dulwich.object_store import (
0.200.457 by Jelmer Vernooij
Use BaseObjectStore.
30
    BaseObjectStore,
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
31
    )
0.200.249 by Jelmer Vernooij
Implement Tree.
32
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
33
from ... import (
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
34
    errors,
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
35
    lru_cache,
0.200.478 by Jelmer Vernooij
Cope with disappeared revisions.
36
    trace,
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
37
    ui,
0.200.773 by Jelmer Vernooij
Implement inventory_to_objects
38
    urlutils,
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
39
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
40
from ...lock import LogicalLockResult
41
from ...revision import (
0.200.541 by Jelmer Vernooij
Cope with NULL_REVISION.
42
    NULL_REVISION,
43
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
44
from ...testament import(
0.200.1023 by Jelmer Vernooij
Set and verify testament.
45
    StrictTestament3,
46
    )
0.200.228 by Jelmer Vernooij
Split out map.
47
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
48
from .cache import (
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
49
    from_repository as cache_from_repository,
50
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
51
from .mapping import (
0.200.463 by Jelmer Vernooij
Support remote dpush (except for references).
52
    default_mapping,
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
53
    directory_to_tree,
0.200.548 by Jelmer Vernooij
Extract unusual file modes from revision when reconstructing Trees.
54
    extract_unusual_modes,
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
55
    mapping_registry,
0.200.795 by Jelmer Vernooij
simplify sha extraction for blobs, process multiple blobs at once.
56
    symlink_to_blob,
0.200.229 by Jelmer Vernooij
More work on converter.
57
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
58
from .unpeel_map import (
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
59
    UnpeelMap,
0.200.231 by Jelmer Vernooij
Partially fix pull.
60
    )
61
0.200.878 by Jelmer Vernooij
Fix determining of unusual file modes.
62
import posixpath
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
63
import stat
0.200.878 by Jelmer Vernooij
Fix determining of unusual file modes.
64
0.200.228 by Jelmer Vernooij
Split out map.
65
0.200.452 by Jelmer Vernooij
Rename converter -> object_store, provide utility function for getting ObjectStore's.
66
def get_object_store(repo, mapping=None):
67
    git = getattr(repo, "_git", None)
68
    if git is not None:
0.200.1303 by Jelmer Vernooij
Fix locking.
69
        git.object_store.unlock = lambda: None
70
        git.object_store.lock_read = lambda: LogicalLockResult(lambda: None)
71
        git.object_store.lock_write = lambda: LogicalLockResult(lambda: None)
0.200.452 by Jelmer Vernooij
Rename converter -> object_store, provide utility function for getting ObjectStore's.
72
        return git.object_store
73
    return BazaarObjectStore(repo, mapping)
74
75
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
76
MAX_TREE_CACHE_SIZE = 50 * 1024 * 1024
77
78
79
class LRUTreeCache(object):
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
80
81
    def __init__(self, repository):
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
82
        def approx_tree_size(tree):
0.275.1 by Jelmer Vernooij
Use root_inventory.
83
            # Very rough estimate, 250 per inventory entry
0.275.5 by Jelmer Vernooij
Cope with root_inventory and inventory.
84
            try:
85
                inv = tree.root_inventory
86
            except AttributeError:
87
                inv = tree.inventory
88
            return len(inv) * 250
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
89
        self.repository = repository
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
90
        self._cache = lru_cache.LRUSizeCache(max_size=MAX_TREE_CACHE_SIZE,
91
            after_cleanup_size=None, compute_size=approx_tree_size)
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
92
0.200.963 by Jelmer Vernooij
Add some tests for LRUTreeCache.
93
    def revision_tree(self, revid):
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
94
        try:
0.200.989 by Jelmer Vernooij
Add asserts.
95
            tree = self._cache[revid]
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
96
        except KeyError:
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
97
            tree = self.repository.revision_tree(revid)
98
            self.add(tree)
0.200.989 by Jelmer Vernooij
Add asserts.
99
        return tree
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
100
101
    def iter_revision_trees(self, revids):
0.200.989 by Jelmer Vernooij
Add asserts.
102
        trees = {}
103
        todo = []
104
        for revid in revids:
105
            try:
106
                tree = self._cache[revid]
107
            except KeyError:
108
                todo.append(revid)
109
            else:
110
                assert tree.get_revision_id() == revid
111
                trees[revid] = tree
112
        for tree in self.repository.revision_trees(todo):
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
113
            trees[tree.get_revision_id()] = tree
114
            self.add(tree)
115
        return (trees[r] for r in revids)
116
117
    def revision_trees(self, revids):
118
        return list(self.iter_revision_trees(revids))
119
120
    def add(self, tree):
0.270.1 by Martin
Avoid the deprecated LRUSizeCache.add method
121
        self._cache[tree.get_revision_id()] = tree
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
122
123
0.200.1053 by Jelmer Vernooij
Fix find_missing_bzr_revids.
124
def _find_missing_bzr_revids(graph, want, have):
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
125
    """Find the revisions that have to be pushed.
126
127
    :param get_parent_map: Function that returns the parents for a sequence
128
        of revisions.
129
    :param want: Revisions the target wants
130
    :param have: Revisions the target already has
131
    :return: Set of revisions to fetch
132
    """
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
133
    handled = set(have)
0.200.899 by Jelmer Vernooij
Add tests for find_missing_bzr_revids.
134
    todo = set()
0.200.1053 by Jelmer Vernooij
Fix find_missing_bzr_revids.
135
    for rev in want:
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
136
        extra_todo = graph.find_unique_ancestors(rev, handled)
137
        todo.update(extra_todo)
138
        handled.update(extra_todo)
0.200.899 by Jelmer Vernooij
Add tests for find_missing_bzr_revids.
139
    if NULL_REVISION in todo:
140
        todo.remove(NULL_REVISION)
141
    return todo
142
143
0.200.793 by Jelmer Vernooij
Make _check_expected_sha a global fn.
144
def _check_expected_sha(expected_sha, object):
0.200.797 by Jelmer Vernooij
Add docstring, fix formatting.
145
    """Check whether an object matches an expected SHA.
146
147
    :param expected_sha: None or expected SHA as either binary or as hex digest
148
    :param object: Object to verify
149
    """
0.200.793 by Jelmer Vernooij
Make _check_expected_sha a global fn.
150
    if expected_sha is None:
151
        return
152
    if len(expected_sha) == 40:
153
        if expected_sha != object.sha().hexdigest():
0.200.797 by Jelmer Vernooij
Add docstring, fix formatting.
154
            raise AssertionError("Invalid sha for %r: %s" % (object,
155
                expected_sha))
0.200.793 by Jelmer Vernooij
Make _check_expected_sha a global fn.
156
    elif len(expected_sha) == 20:
157
        if expected_sha != object.sha().digest():
0.200.797 by Jelmer Vernooij
Add docstring, fix formatting.
158
            raise AssertionError("Invalid sha for %r: %s" % (object,
159
                sha_to_hex(expected_sha)))
0.200.793 by Jelmer Vernooij
Make _check_expected_sha a global fn.
160
    else:
0.200.797 by Jelmer Vernooij
Add docstring, fix formatting.
161
        raise AssertionError("Unknown length %d for %r" % (len(expected_sha),
162
            expected_sha))
0.200.793 by Jelmer Vernooij
Make _check_expected_sha a global fn.
163
164
0.200.931 by Jelmer Vernooij
Update docstring, deal with kind changes appropriately in _tree_to_objects
165
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes,
166
                     dummy_file_name=None):
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
167
    """Iterate over the objects that were introduced in a revision.
168
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
169
    :param idmap: id map
0.200.931 by Jelmer Vernooij
Update docstring, deal with kind changes appropriately in _tree_to_objects
170
    :param parent_trees: Parent revision trees
171
    :param unusual_modes: Unusual file modes dictionary
0.252.30 by Jelmer Vernooij
Support creating dummy files for empty directories.
172
    :param dummy_file_name: File name to use for dummy files
173
        in empty directories. None to skip empty directories
0.200.837 by Jelmer Vernooij
Return inventory entries when creating git objects for a revision.
174
    :return: Yields (path, object, ie) entries
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
175
    """
0.282.1 by William Grant
Rework _tree_to_objects to work out parents by ID, not path. Fixes weirdness with various directory renames.
176
    dirty_dirs = set()
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
177
    new_blobs = []
178
    shamap = {}
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
179
    try:
180
        base_tree = parent_trees[0]
181
        other_parent_trees = parent_trees[1:]
182
    except IndexError:
183
        base_tree = tree._repository.revision_tree(NULL_REVISION)
184
        other_parent_trees = []
0.275.1 by Jelmer Vernooij
Use root_inventory.
185
    def find_unchanged_parent_ie(file_id, kind, other, parent_trees):
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
186
        for ptree in parent_trees:
187
            try:
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
188
                ppath = ptree.id2path(file_id)
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
189
            except errors.NoSuchId:
190
                pass
191
            else:
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
192
                pkind = ptree.kind(ppath, file_id)
0.275.1 by Jelmer Vernooij
Use root_inventory.
193
                if kind == "file":
0.200.1636 by Jelmer Vernooij
Some formatting fixes.
194
                    if (pkind == "file" and
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
195
                        ptree.get_file_sha1(ppath, file_id) == other):
196
                        return (file_id, ptree.get_file_revision(ppath, file_id))
0.275.1 by Jelmer Vernooij
Use root_inventory.
197
                if kind == "symlink":
198
                    if (pkind == "symlink" and
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
199
                        ptree.get_symlink_target(ppath, file_id) == other):
200
                        return (file_id, ptree.get_file_revision(ppath, file_id))
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
201
        raise KeyError
0.200.965 by Jelmer Vernooij
Formatting fixes.
202
0.200.931 by Jelmer Vernooij
Update docstring, deal with kind changes appropriately in _tree_to_objects
203
    # Find all the changed blobs
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
204
    for (file_id, path, changed_content, versioned, parent, name, kind,
205
         executable) in tree.iter_changes(base_tree):
206
        if kind[1] == "file":
207
            if changed_content:
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
208
                try:
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
209
                    (pfile_id, prevision) = find_unchanged_parent_ie(file_id, kind[1], tree.get_file_sha1(path[1], file_id), other_parent_trees)
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
210
                except KeyError:
211
                    pass
212
                else:
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
213
                    try:
0.275.1 by Jelmer Vernooij
Use root_inventory.
214
                        shamap[file_id] = idmap.lookup_blob_id(
0.200.1575 by Jelmer Vernooij
Fix name error.
215
                            pfile_id, prevision)
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
216
                    except KeyError:
217
                        # no-change merge ?
218
                        blob = Blob()
0.275.1 by Jelmer Vernooij
Use root_inventory.
219
                        blob.data = tree.get_file_text(file_id)
220
                        shamap[file_id] = blob.id
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
221
            if not file_id in shamap:
0.275.1 by Jelmer Vernooij
Use root_inventory.
222
                new_blobs.append((path[1], file_id))
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
223
        elif kind[1] == "symlink":
224
            if changed_content:
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
225
                target = tree.get_symlink_target(path[1], file_id)
0.275.1 by Jelmer Vernooij
Use root_inventory.
226
                blob = symlink_to_blob(target)
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
227
                shamap[file_id] = blob.id
228
                try:
0.275.1 by Jelmer Vernooij
Use root_inventory.
229
                    find_unchanged_parent_ie(file_id, kind[1], target, other_parent_trees)
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
230
                except KeyError:
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
231
                    yield path[1], blob, (file_id, tree.get_file_revision(path[1], file_id))
0.250.3 by Jelmer Vernooij
Simplify..
232
        elif kind[1] not in (None, "directory"):
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
233
            raise AssertionError(kind[1])
0.282.2 by William Grant
Always dirty both parents, fixing weird directory rename cases.
234
        for p in parent:
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
235
            if p and tree.has_id(p) and tree.kind(tree.id2path(p)) == "directory":
0.282.2 by William Grant
Always dirty both parents, fixing weird directory rename cases.
236
                dirty_dirs.add(p)
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
237
0.200.931 by Jelmer Vernooij
Update docstring, deal with kind changes appropriately in _tree_to_objects
238
    # Fetch contents of the blobs that were changed
0.275.1 by Jelmer Vernooij
Use root_inventory.
239
    for (path, file_id), chunks in tree.iter_files_bytes(
240
        [(file_id, (path, file_id)) for (path, file_id) in new_blobs]):
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
241
        obj = Blob()
0.200.851 by Jelmer Vernooij
Use blob.chunked.
242
        obj.chunked = chunks
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
243
        yield path, obj, (file_id, tree.get_file_revision(path, file_id))
0.275.1 by Jelmer Vernooij
Use root_inventory.
244
        shamap[file_id] = obj.id
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
245
0.200.879 by Jelmer Vernooij
Fix unusual modes.
246
    for path in unusual_modes:
247
        parent_path = posixpath.dirname(path)
0.200.1577 by Jelmer Vernooij
Add assertion.
248
        file_id = tree.path2id(parent_path)
249
        assert file_id is not None, "Unable to find file id for %r" % parent_path
0.282.1 by William Grant
Rework _tree_to_objects to work out parents by ID, not path. Fixes weirdness with various directory renames.
250
        dirty_dirs.add(file_id)
251
252
    try:
253
        inv = tree.root_inventory
254
    except AttributeError:
255
        inv = tree.inventory
0.200.989 by Jelmer Vernooij
Add asserts.
256
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
257
    trees = {}
0.282.1 by William Grant
Rework _tree_to_objects to work out parents by ID, not path. Fixes weirdness with various directory renames.
258
    while dirty_dirs:
259
        new_dirs = set()
260
        for file_id in dirty_dirs:
261
            if file_id is None or not inv.has_id(file_id):
262
                continue
263
            trees[inv.id2path(file_id)] = file_id
264
            ie = inv[file_id]
265
            if ie.parent_id is not None:
266
                new_dirs.add(ie.parent_id)
267
        dirty_dirs = new_dirs
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
268
0.200.808 by Jelmer Vernooij
Avoid recalculating tree shas we already have.
269
    def ie_to_hexsha(ie):
270
        try:
271
            return shamap[ie.file_id]
272
        except KeyError:
0.200.884 by Jelmer Vernooij
Cope with -0000 as timezone in Git commits.
273
            # FIXME: Should be the same as in parent
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
274
            if ie.kind in ("file", "symlink"):
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
275
                try:
276
                    return idmap.lookup_blob_id(ie.file_id, ie.revision)
277
                except KeyError:
278
                    # no-change merge ?
279
                    blob = Blob()
280
                    blob.data = tree.get_file_text(ie.file_id)
281
                    return blob.id
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
282
            elif ie.kind == "directory":
0.200.1636 by Jelmer Vernooij
Some formatting fixes.
283
                # Not all cache backends store the tree information,
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
284
                # calculate again from scratch
0.275.4 by Jelmer Vernooij
Pass children list to directory_to_tree .
285
                ret = directory_to_tree(ie.children, ie_to_hexsha,
0.200.1573 by Jelmer Vernooij
Fix regression in allowing empty directory check.
286
                    unusual_modes, dummy_file_name, ie.parent_id is None)
0.250.1 by Jelmer Vernooij
Use iter_changes() rather than iterating over all contents of an inventory.
287
                if ret is None:
288
                    return ret
289
                return ret.id
290
            else:
291
                raise AssertionError
0.200.808 by Jelmer Vernooij
Avoid recalculating tree shas we already have.
292
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
293
    for path in sorted(trees.keys(), reverse=True):
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
294
        file_id = trees[path]
0.285.1 by Jelmer Vernooij
Swap arguments for tree methods.
295
        assert tree.kind(path, file_id) == 'directory'
0.275.5 by Jelmer Vernooij
Cope with root_inventory and inventory.
296
        ie = inv[file_id]
0.275.4 by Jelmer Vernooij
Pass children list to directory_to_tree .
297
        obj = directory_to_tree(ie.children, ie_to_hexsha, unusual_modes,
298
            dummy_file_name, path == "")
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
299
        if obj is not None:
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
300
            yield path, obj, (file_id, )
301
            shamap[file_id] = obj.id
0.200.798 by Jelmer Vernooij
Split out _inventory_to_objects into a function.
302
303
0.200.1290 by Jelmer Vernooij
Avoid storing all objects to push in memory.
304
class PackTupleIterable(object):
305
306
    def __init__(self, store):
307
        self.store = store
0.200.1432 by Jelmer Vernooij
Make sure object store is locked/unlocked.
308
        self.store.lock_read()
0.200.1290 by Jelmer Vernooij
Avoid storing all objects to push in memory.
309
        self.objects = {}
310
0.200.1432 by Jelmer Vernooij
Make sure object store is locked/unlocked.
311
    def __del__(self):
312
        self.store.unlock()
313
0.200.1290 by Jelmer Vernooij
Avoid storing all objects to push in memory.
314
    def add(self, sha, path):
315
        self.objects[sha] = path
316
317
    def __len__(self):
318
        return len(self.objects)
319
320
    def __iter__(self):
321
        return ((self.store[object_id], path) for (object_id, path) in
322
                self.objects.iteritems())
323
324
0.200.457 by Jelmer Vernooij
Use BaseObjectStore.
325
class BazaarObjectStore(BaseObjectStore):
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
326
    """A Git-style object store backed onto a Bazaar repository."""
0.200.228 by Jelmer Vernooij
Split out map.
327
328
    def __init__(self, repository, mapping=None):
329
        self.repository = repository
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
330
        self._map_updated = False
331
        self._locked = None
0.200.228 by Jelmer Vernooij
Split out map.
332
        if mapping is None:
0.200.463 by Jelmer Vernooij
Support remote dpush (except for references).
333
            self.mapping = default_mapping
0.200.228 by Jelmer Vernooij
Split out map.
334
        else:
335
            self.mapping = mapping
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
336
        self._cache = cache_from_repository(repository)
0.200.1291 by Jelmer Vernooij
add hook for updating to local git cache.
337
        self._content_cache_types = ("tree",)
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
338
        self.start_write_group = self._cache.idmap.start_write_group
339
        self.abort_write_group = self._cache.idmap.abort_write_group
340
        self.commit_write_group = self._cache.idmap.commit_write_group
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
341
        self.tree_cache = LRUTreeCache(self.repository)
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
342
        self.unpeel_map = UnpeelMap.from_repository(self.repository)
0.200.228 by Jelmer Vernooij
Split out map.
343
0.200.1319 by Jelmer Vernooij
Only update git cache during post-commit if parents are already in the cache.
344
    def _missing_revisions(self, revisions):
345
        return self._cache.idmap.missing_revisions(revisions)
346
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
347
    def _update_sha_map(self, stop_revision=None):
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
348
        if not self.is_locked():
349
            raise AssertionError()
350
        if self._map_updated:
351
            return
0.200.1264 by Jelmer Vernooij
Fix updating cache for single revision - don't consider it an update of the full cache.
352
        if (stop_revision is not None and
0.200.1319 by Jelmer Vernooij
Only update git cache during post-commit if parents are already in the cache.
353
            not self._missing_revisions([stop_revision])):
0.200.1264 by Jelmer Vernooij
Fix updating cache for single revision - don't consider it an update of the full cache.
354
            return
0.200.683 by Jelmer Vernooij
Lazier checking of which revisions need to be fetched.
355
        graph = self.repository.get_graph()
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
356
        if stop_revision is None:
0.200.1301 by Jelmer Vernooij
Avoid expensive get_parent_map call.
357
            all_revids = self.repository.all_revision_ids()
0.200.1319 by Jelmer Vernooij
Only update git cache during post-commit if parents are already in the cache.
358
            missing_revids = self._missing_revisions(all_revids)
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
359
        else:
0.200.683 by Jelmer Vernooij
Lazier checking of which revisions need to be fetched.
360
            heads = set([stop_revision])
0.200.1319 by Jelmer Vernooij
Only update git cache during post-commit if parents are already in the cache.
361
            missing_revids = self._missing_revisions(heads)
0.200.1301 by Jelmer Vernooij
Avoid expensive get_parent_map call.
362
            while heads:
363
                parents = graph.get_parent_map(heads)
364
                todo = set()
365
                for p in parents.values():
366
                    todo.update([x for x in p if x not in missing_revids])
0.200.1319 by Jelmer Vernooij
Only update git cache during post-commit if parents are already in the cache.
367
                heads = self._missing_revisions(todo)
0.200.1301 by Jelmer Vernooij
Avoid expensive get_parent_map call.
368
                missing_revids.update(heads)
0.200.694 by Jelmer Vernooij
Avoid processing NULL_REVISION.
369
        if NULL_REVISION in missing_revids:
370
            missing_revids.remove(NULL_REVISION)
0.254.16 by Jelmer Vernooij
Add optimization preventing recursive index updating.
371
        missing_revids = self.repository.has_revisions(missing_revids)
372
        if not missing_revids:
0.200.1264 by Jelmer Vernooij
Fix updating cache for single revision - don't consider it an update of the full cache.
373
            if stop_revision is None:
374
                self._map_updated = True
0.254.16 by Jelmer Vernooij
Add optimization preventing recursive index updating.
375
            return
0.200.735 by Jelmer Vernooij
Use convenience functions for start/stop write groups.
376
        self.start_write_group()
0.200.231 by Jelmer Vernooij
Partially fix pull.
377
        try:
0.254.4 by Jelmer Vernooij
Merge trunk.
378
            pb = ui.ui_factory.nested_progress_bar()
379
            try:
380
                for i, revid in enumerate(graph.iter_topo_order(missing_revids)):
0.254.16 by Jelmer Vernooij
Add optimization preventing recursive index updating.
381
                    trace.mutter('processing %r', revid)
0.254.4 by Jelmer Vernooij
Merge trunk.
382
                    pb.update("updating git map", i, len(missing_revids))
383
                    self._update_sha_map_revision(revid)
384
            finally:
385
                pb.finished()
0.200.1264 by Jelmer Vernooij
Fix updating cache for single revision - don't consider it an update of the full cache.
386
            if stop_revision is None:
387
                self._map_updated = True
0.200.735 by Jelmer Vernooij
Use convenience functions for start/stop write groups.
388
        except:
389
            self.abort_write_group()
390
            raise
391
        else:
392
            self.commit_write_group()
0.200.229 by Jelmer Vernooij
More work on converter.
393
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
394
    def __iter__(self):
395
        self._update_sha_map()
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
396
        return iter(self._cache.idmap.sha1s())
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
397
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
398
    def _reconstruct_commit(self, rev, tree_sha, lossy, verifiers):
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
399
        """Reconstruct a Commit object.
400
401
        :param rev: Revision object
402
        :param tree_sha: SHA1 of the root tree object
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
403
        :param lossy: Whether or not to roundtrip bzr metadata
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
404
        :param verifiers: Verifiers for the commits
405
        :return: Commit object
406
        """
0.238.7 by Jelmer Vernooij
Cope with ghosts a bit better.
407
        def parent_lookup(revid):
408
            try:
409
                return self._lookup_revision_sha1(revid)
410
            except errors.NoSuchRevision:
411
                return None
0.252.4 by Jelmer Vernooij
More work on roundtripping.
412
        return self.mapping.export_commit(rev, tree_sha, parent_lookup,
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
413
            lossy, verifiers)
0.238.7 by Jelmer Vernooij
Cope with ghosts a bit better.
414
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
415
    def _create_fileid_map_blob(self, tree):
0.200.1636 by Jelmer Vernooij
Some formatting fixes.
416
        # FIXME: This can probably be a lot more efficient,
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
417
        # not all files necessarily have to be processed.
418
        file_ids = {}
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
419
        for (path, ie) in tree.inventory.iter_entries():
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
420
            if self.mapping.generate_file_id(path) != ie.file_id:
421
                file_ids[path] = ie.file_id
422
        return self.mapping.export_fileid_map(file_ids)
423
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
424
    def _revision_to_objects(self, rev, tree, lossy):
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
425
        """Convert a revision to a set of git objects.
426
427
        :param rev: Bazaar revision object
428
        :param tree: Bazaar revision tree
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
429
        :param lossy: Whether to not roundtrip all Bazaar revision data
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
430
        """
0.200.548 by Jelmer Vernooij
Extract unusual file modes from revision when reconstructing Trees.
431
        unusual_modes = extract_unusual_modes(rev)
0.200.789 by Jelmer Vernooij
Cope with ghosts, cache inventories.
432
        present_parents = self.repository.has_revisions(rev.parent_ids)
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
433
        parent_trees = self.tree_cache.revision_trees(
0.200.797 by Jelmer Vernooij
Add docstring, fix formatting.
434
            [p for p in rev.parent_ids if p in present_parents])
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
435
        root_tree = None
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
436
        for path, obj, bzr_key_data in _tree_to_objects(tree, parent_trees,
0.252.30 by Jelmer Vernooij
Support creating dummy files for empty directories.
437
                self._cache.idmap, unusual_modes, self.mapping.BZR_DUMMY_FILE):
0.200.773 by Jelmer Vernooij
Implement inventory_to_objects
438
            if path == "":
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
439
                root_tree = obj
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
440
                root_key_data = bzr_key_data
0.252.34 by Jelmer Vernooij
Yield the proper object for the tree root.
441
                # Don't yield just yet
442
            else:
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
443
                yield path, obj, bzr_key_data
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
444
        if root_tree is None:
0.250.2 by Jelmer Vernooij
Make it work for evolution.
445
            # Pointless commit - get the tree sha elsewhere
0.200.864 by Jelmer Vernooij
Cope with the first commit being pointless.
446
            if not rev.parent_ids:
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
447
                root_tree = Tree()
0.200.864 by Jelmer Vernooij
Cope with the first commit being pointless.
448
            else:
449
                base_sha1 = self._lookup_revision_sha1(rev.parent_ids[0])
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
450
                root_tree = self[self[base_sha1].tree]
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
451
            root_key_data = (tree.get_root_id(), )
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
452
        if not lossy and self.mapping.BZR_FILE_IDS_FILE is not None:
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
453
            b = self._create_fileid_map_blob(tree)
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
454
            if b is not None:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
455
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
456
                    (stat.S_IFREG | 0644), b.id)
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
457
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
0.275.2 by Jelmer Vernooij
Pass tuples around for cache entries, rather than inventory entries.
458
        yield "", root_tree, root_key_data
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
459
        if not lossy:
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
460
            testament3 = StrictTestament3(rev, tree)
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
461
            verifiers = { "testament3-sha1": testament3.as_sha1() }
0.200.1023 by Jelmer Vernooij
Set and verify testament.
462
        else:
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
463
            verifiers = {}
0.252.43 by Jelmer Vernooij
Some refactoring, support proper file ids in revision deltas.
464
        commit_obj = self._reconstruct_commit(rev, root_tree.id,
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
465
            lossy=lossy, verifiers=verifiers)
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
466
        try:
0.200.841 by Jelmer Vernooij
Eliminate InventorySHAMap.
467
            foreign_revid, mapping = mapping_registry.parse_revision_id(
468
                rev.revision_id)
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
469
        except errors.InvalidRevisionId:
470
            pass
471
        else:
0.200.794 by Jelmer Vernooij
Use _check_expected_sha rather than custom checks.
472
            _check_expected_sha(foreign_revid, commit_obj)
0.200.837 by Jelmer Vernooij
Return inventory entries when creating git objects for a revision.
473
        yield None, commit_obj, None
0.200.783 by Jelmer Vernooij
Move object generation into a separate function.
474
0.200.838 by Jelmer Vernooij
Add convenience object for updating the object store.
475
    def _get_updater(self, rev):
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
476
        return self._cache.get_updater(rev)
0.200.838 by Jelmer Vernooij
Add convenience object for updating the object store.
477
0.200.783 by Jelmer Vernooij
Move object generation into a separate function.
478
    def _update_sha_map_revision(self, revid):
479
        rev = self.repository.get_revision(revid)
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
480
        tree = self.tree_cache.revision_tree(rev.revision_id)
0.200.838 by Jelmer Vernooij
Add convenience object for updating the object store.
481
        updater = self._get_updater(rev)
0.200.1510 by Jelmer Vernooij
Fix tests.
482
        # FIXME JRV 2011-12-15: Shouldn't we try both values for lossy ?
483
        for path, obj, ie in self._revision_to_objects(rev, tree, lossy=(not self.mapping.roundtripping)):
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
484
            if isinstance(obj, Commit):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
485
                testament3 = StrictTestament3(rev, tree)
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
486
                ie = { "testament3-sha1": testament3.as_sha1() }
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
487
            updater.add_object(obj, ie, path)
0.200.838 by Jelmer Vernooij
Add convenience object for updating the object store.
488
        commit_obj = updater.finish()
0.200.781 by Jelmer Vernooij
Return commit id after converting a revision.
489
        return commit_obj.id
0.200.229 by Jelmer Vernooij
More work on converter.
490
0.200.855 by Jelmer Vernooij
_get_ -> _reconstruct_.
491
    def _reconstruct_blobs(self, keys):
0.200.698 by Jelmer Vernooij
Merge fixes for SHA1s of symlinks.
492
        """Return a Git Blob object from a fileid and revision stored in bzr.
493
494
        :param fileid: File id of the text
495
        :param revision: Revision of the text
496
        """
0.250.2 by Jelmer Vernooij
Make it work for evolution.
497
        stream = self.repository.iter_files_bytes(
498
            ((key[0], key[1], key) for key in keys))
0.200.856 by Jelmer Vernooij
Support reconstructing multiple blobs at the same time.
499
        for (fileid, revision, expected_sha), chunks in stream:
0.200.854 by Jelmer Vernooij
_get_blob -> _get_blobs.
500
            blob = Blob()
501
            blob.chunked = chunks
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
502
            if blob.id != expected_sha and blob.data == "":
0.200.854 by Jelmer Vernooij
_get_blob -> _get_blobs.
503
                # Perhaps it's a symlink ?
504
                tree = self.tree_cache.revision_tree(revision)
0.200.1586 by Jelmer Vernooij
Fixes duplicate tag warnings in 'git-remote-bzr' helper.
505
                if tree.kind(fileid) == 'symlink':
506
                    blob = symlink_to_blob(tree.get_symlink_target(fileid))
0.200.854 by Jelmer Vernooij
_get_blob -> _get_blobs.
507
            _check_expected_sha(expected_sha, blob)
508
            yield blob
0.200.229 by Jelmer Vernooij
More work on converter.
509
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
510
    def _reconstruct_tree(self, fileid, revid, bzr_tree, unusual_modes,
0.200.855 by Jelmer Vernooij
_get_ -> _reconstruct_.
511
        expected_sha=None):
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
512
        """Return a Git Tree object from a file id and a revision stored in bzr.
0.200.249 by Jelmer Vernooij
Implement Tree.
513
0.200.343 by Jelmer Vernooij
Use file ids consistently in map.
514
        :param fileid: fileid in the tree.
0.200.249 by Jelmer Vernooij
Implement Tree.
515
        :param revision: Revision of the tree.
516
        """
0.200.776 by Jelmer Vernooij
Remove unnecessary lookups.
517
        def get_ie_sha1(entry):
518
            if entry.kind == "directory":
0.200.808 by Jelmer Vernooij
Avoid recalculating tree shas we already have.
519
                try:
0.200.859 by Jelmer Vernooij
Trivial cleanups.
520
                    return self._cache.idmap.lookup_tree_id(entry.file_id,
521
                        revid)
0.200.812 by Jelmer Vernooij
Catch KeyError from lookup_tree as well - some caches (such as sqlite) don't store all trees, only some.
522
                except (NotImplementedError, KeyError):
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
523
                    obj = self._reconstruct_tree(entry.file_id, revid, bzr_tree,
0.200.808 by Jelmer Vernooij
Avoid recalculating tree shas we already have.
524
                        unusual_modes)
525
                    if obj is None:
526
                        return None
527
                    else:
528
                        return obj.id
0.200.776 by Jelmer Vernooij
Remove unnecessary lookups.
529
            elif entry.kind in ("file", "symlink"):
0.200.868 by Jelmer Vernooij
Cope with no-change merges.
530
                try:
531
                    return self._cache.idmap.lookup_blob_id(entry.file_id,
532
                        entry.revision)
533
                except KeyError:
534
                    # no-change merge?
535
                    return self._reconstruct_blobs(
536
                        [(entry.file_id, entry.revision, None)]).next().id
0.200.1551 by Jelmer Vernooij
Support nested trees in reconstruction code.
537
            elif entry.kind == 'tree-reference':
538
                # FIXME: Make sure the file id is the root id
539
                return self._lookup_revision_sha1(entry.reference_revision)
0.200.776 by Jelmer Vernooij
Remove unnecessary lookups.
540
            else:
541
                raise AssertionError("unknown entry kind '%s'" % entry.kind)
0.275.5 by Jelmer Vernooij
Cope with root_inventory and inventory.
542
        try:
543
            inv = bzr_tree.root_inventory
544
        except AttributeError:
545
            inv = bzr_tree.inventory
546
        tree = directory_to_tree(inv[fileid].children,
0.275.4 by Jelmer Vernooij
Pass children list to directory_to_tree .
547
                get_ie_sha1, unusual_modes, self.mapping.BZR_DUMMY_FILE,
548
                bzr_tree.get_root_id() == fileid)
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
549
        if (bzr_tree.get_root_id() == fileid and
0.200.915 by Jelmer Vernooij
Cope with the fact that the old format didn't export file ids.
550
            self.mapping.BZR_FILE_IDS_FILE is not None):
0.200.1223 by Jelmer Vernooij
Cope with empty directories.
551
            if tree is None:
552
                tree = Tree()
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
553
            b = self._create_fileid_map_blob(bzr_tree)
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
554
            # If this is the root tree, add the file ids
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
555
            tree[self.mapping.BZR_FILE_IDS_FILE] = (
556
                (stat.S_IFREG | 0644), b.id)
0.200.1223 by Jelmer Vernooij
Cope with empty directories.
557
        if tree is not None:
558
            _check_expected_sha(expected_sha, tree)
0.200.249 by Jelmer Vernooij
Implement Tree.
559
        return tree
0.200.229 by Jelmer Vernooij
More work on converter.
560
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
561
    def get_parents(self, sha):
0.200.454 by Jelmer Vernooij
Use ObjectStore.find_missing_objects in server.
562
        """Retrieve the parents of a Git commit by SHA1.
563
564
        :param sha: SHA1 of the commit
565
        :raises: KeyError, NotCommitError
566
        """
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
567
        return self[sha].parents
568
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
569
    def _lookup_revision_sha1(self, revid):
0.200.449 by Jelmer Vernooij
Use BazaarObjectStore to find matching SHA1s for bzr revisions.
570
        """Return the SHA1 matching a Bazaar revision."""
0.200.541 by Jelmer Vernooij
Cope with NULL_REVISION.
571
        if revid == NULL_REVISION:
0.200.891 by Jelmer Vernooij
Use ZERO_SHA constant where possible.
572
            return ZERO_SHA
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
573
        try:
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
574
            return self._cache.idmap.lookup_commit(revid)
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
575
        except KeyError:
0.200.682 by Jelmer Vernooij
Avoid doing a full sha map update if we already know the SHA1.
576
            try:
577
                return mapping_registry.parse_revision_id(revid)[0]
578
            except errors.InvalidRevisionId:
0.200.1264 by Jelmer Vernooij
Fix updating cache for single revision - don't consider it an update of the full cache.
579
                self._update_sha_map(revid)
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
580
                return self._cache.idmap.lookup_commit(revid)
0.200.364 by Jelmer Vernooij
Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.
581
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
582
    def get_raw(self, sha):
0.200.454 by Jelmer Vernooij
Use ObjectStore.find_missing_objects in server.
583
        """Get the raw representation of a Git object by SHA1.
584
585
        :param sha: SHA1 of the git object
586
        """
0.200.1622 by William Grant
BazaarObjectStore.get_raw now copes with non-hex-encoded SHA-1s, as some ref delta resolution in dulwich apparently requires.
587
        if len(sha) == 20:
588
            sha = sha_to_hex(sha)
0.200.566 by Jelmer Vernooij
Fix ObjectStore.get_raw() .
589
        obj = self[sha]
590
        return (obj.type, obj.as_raw_string())
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
591
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
592
    def __contains__(self, sha):
593
        # See if sha is in map
594
        try:
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
595
            for (type, type_data) in self.lookup_git_sha(sha):
596
                if type == "commit":
597
                    if self.repository.has_revision(type_data[0]):
598
                        return True
599
                elif type == "blob":
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
600
                    if type_data in self.repository.texts:
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
601
                        return True
602
                elif type == "tree":
603
                    if self.repository.has_revision(type_data[1]):
604
                        return True
605
                else:
606
                    raise AssertionError("Unknown object type '%s'" % type)
0.200.568 by Jelmer Vernooij
Properly check that matching bzr objects exist.
607
            else:
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
608
                return False
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
609
        except KeyError:
610
            return False
611
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
612
    def lock_read(self):
613
        self._locked = 'r'
614
        self._map_updated = False
615
        self.repository.lock_read()
616
        return LogicalLockResult(self.unlock)
617
618
    def lock_write(self):
619
        self._locked = 'r'
620
        self._map_updated = False
621
        self.repository.lock_write()
622
        return LogicalLockResult(self.unlock)
623
624
    def is_locked(self):
625
        return (self._locked is not None)
626
627
    def unlock(self):
628
        self._locked = None
629
        self._map_updated = False
630
        self.repository.unlock()
631
632
    def lookup_git_shas(self, shas):
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
633
        ret = {}
634
        for sha in shas:
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
635
            if sha == ZERO_SHA:
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
636
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
637
                continue
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
638
            try:
0.261.3 by Jelmer Vernooij
Fix more tests.
639
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
640
            except KeyError:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
641
                # if not, see if there are any unconverted revisions and
642
                # add them to the map, search for sha in map again
643
                self._update_sha_map()
644
                try:
645
                    ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
646
                except KeyError:
647
                    pass
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
648
        return ret
649
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
650
    def lookup_git_sha(self, sha):
651
        return self.lookup_git_shas([sha])[sha]
0.200.437 by Jelmer Vernooij
Implement BazaarObjectStore.__contains__, BazaarObjectStore.iter_shas, BazaarObjectStore.get_parents.
652
653
    def __getitem__(self, sha):
0.200.849 by Jelmer Vernooij
Allow cache backends to decide when to add entries rather than adding once per commit.
654
        if self._cache.content_cache is not None:
0.200.840 by Jelmer Vernooij
Support using content cache.
655
            try:
0.200.847 by Jelmer Vernooij
Add BzrGitCache object.
656
                return self._cache.content_cache[sha]
0.200.840 by Jelmer Vernooij
Support using content cache.
657
            except KeyError:
658
                pass
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
659
        for (kind, type_data) in self.lookup_git_sha(sha):
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
660
            # convert object to git object
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
661
            if kind == "commit":
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
662
                (revid, tree_sha, verifiers) = type_data
663
                try:
664
                    rev = self.repository.get_revision(revid)
665
                except errors.NoSuchRevision:
0.200.1341 by Jelmer Vernooij
Add check that callers don't try to look up NULL_REVISION.
666
                    if revid == NULL_REVISION:
667
                        raise AssertionError(
668
                            "should not try to look up NULL_REVISION")
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
669
                    trace.mutter('entry for %s %s in shamap: %r, but not '
670
                                 'found in repository', kind, sha, type_data)
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
671
                    raise KeyError(sha)
0.200.1510 by Jelmer Vernooij
Fix tests.
672
                # FIXME: the type data should say whether conversion was lossless
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
673
                commit = self._reconstruct_commit(rev, tree_sha,
0.200.1510 by Jelmer Vernooij
Fix tests.
674
                    lossy=(not self.mapping.roundtripping), verifiers=verifiers)
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
675
                _check_expected_sha(sha, commit)
676
                return commit
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
677
            elif kind == "blob":
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
678
                (fileid, revision) = type_data
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
679
                blobs = self._reconstruct_blobs([(fileid, revision, sha)])
680
                return blobs.next()
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
681
            elif kind == "tree":
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
682
                (fileid, revid) = type_data
683
                try:
684
                    tree = self.tree_cache.revision_tree(revid)
685
                    rev = self.repository.get_revision(revid)
686
                except errors.NoSuchRevision:
0.200.1319 by Jelmer Vernooij
Only update git cache during post-commit if parents are already in the cache.
687
                    trace.mutter('entry for %s %s in shamap: %r, but not found in '
688
                        'repository', kind, sha, type_data)
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
689
                    raise KeyError(sha)
690
                unusual_modes = extract_unusual_modes(rev)
691
                try:
692
                    return self._reconstruct_tree(fileid, revid,
0.273.2 by Jelmer Vernooij
use tree objects rather than inventories
693
                        tree, unusual_modes, expected_sha=sha)
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
694
                except errors.NoSuchRevision:
695
                    raise KeyError(sha)
696
            else:
0.200.1169 by Jelmer Vernooij
Fix some sha lookups.
697
                raise AssertionError("Unknown object type '%s'" % kind)
0.200.228 by Jelmer Vernooij
Split out map.
698
        else:
0.261.1 by Jelmer Vernooij
Initial work on supporting multiple results for git shas.
699
            raise KeyError(sha)
0.200.782 by Jelmer Vernooij
Add custom generate_pack_contents implementation.
700
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
701
    def generate_lossy_pack_contents(self, have, want, progress=None,
702
            get_tagged=None):
703
        return self.generate_pack_contents(have, want, progress, get_tagged,
704
            lossy=True)
705
0.200.899 by Jelmer Vernooij
Add tests for find_missing_bzr_revids.
706
    def generate_pack_contents(self, have, want, progress=None,
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
707
            get_tagged=None, lossy=False):
0.200.782 by Jelmer Vernooij
Add custom generate_pack_contents implementation.
708
        """Iterate over the contents of a pack file.
709
710
        :param have: List of SHA1s of objects that should not be sent
711
        :param want: List of SHA1s of objects that should be sent
712
        """
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
713
        processed = set()
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
714
        ret = self.lookup_git_shas(have + want)
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
715
        for commit_sha in have:
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
716
            commit_sha = self.unpeel_map.peel_tag(commit_sha, commit_sha)
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
717
            try:
0.200.1180 by Jelmer Vernooij
Some dpush fixes.
718
                for (type, type_data) in ret[commit_sha]:
719
                    assert type == "commit"
720
                    processed.add(type_data[0])
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
721
            except KeyError:
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
722
                trace.mutter("unable to find remote ref %s", commit_sha)
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
723
        pending = set()
724
        for commit_sha in want:
725
            if commit_sha in have:
726
                continue
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
727
            try:
0.200.1180 by Jelmer Vernooij
Some dpush fixes.
728
                for (type, type_data) in ret[commit_sha]:
729
                    assert type == "commit"
730
                    pending.add(type_data[0])
0.200.898 by Jelmer Vernooij
Optimize finding of git shas.
731
            except KeyError:
732
                pass
0.200.899 by Jelmer Vernooij
Add tests for find_missing_bzr_revids.
733
0.200.1053 by Jelmer Vernooij
Fix find_missing_bzr_revids.
734
        graph = self.repository.get_graph()
735
        todo = _find_missing_bzr_revids(graph, pending, processed)
0.200.1290 by Jelmer Vernooij
Avoid storing all objects to push in memory.
736
        ret = PackTupleIterable(self)
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
737
        pb = ui.ui_factory.nested_progress_bar()
738
        try:
739
            for i, revid in enumerate(todo):
740
                pb.update("generating git objects", i, len(todo))
0.200.1059 by Jelmer Vernooij
Fix graph tests.
741
                try:
742
                    rev = self.repository.get_revision(revid)
743
                except errors.NoSuchRevision:
744
                    continue
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
745
                tree = self.tree_cache.revision_tree(revid)
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
746
                for path, obj, ie in self._revision_to_objects(rev, tree, lossy=lossy):
0.200.1290 by Jelmer Vernooij
Avoid storing all objects to push in memory.
747
                    ret.add(obj.id, path)
0.200.1298 by Jelmer Vernooij
Fix compatibility with newer versions of dulwich.
748
            return ret
0.200.787 by Jelmer Vernooij
Implement custom ObjectWalker.generate_pack_contents.
749
        finally:
750
            pb.finished()
0.251.1 by Jelmer Vernooij
Implement ObjectStore.add_{thin_,}pack.
751
752
    def add_thin_pack(self):
753
        import tempfile
754
        import os
755
        fd, path = tempfile.mkstemp(suffix=".pack")
756
        f = os.fdopen(fd, 'wb')
757
        def commit():
758
            from dulwich.pack import PackData, Pack
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
759
            from .fetch import import_git_objects
0.251.1 by Jelmer Vernooij
Implement ObjectStore.add_{thin_,}pack.
760
            os.fsync(fd)
761
            f.close()
762
            if os.path.getsize(path) == 0:
763
                return
764
            pd = PackData(path)
765
            pd.create_index_v2(path[:-5]+".idx", self.object_store.get_raw)
766
767
            p = Pack(path[:-5])
768
            self.repository.lock_write()
769
            try:
770
                self.repository.start_write_group()
771
                try:
0.200.1289 by Jelmer Vernooij
Switch to dulwich 0.8.0.
772
                    import_git_objects(self.repository, self.mapping,
0.251.1 by Jelmer Vernooij
Implement ObjectStore.add_{thin_,}pack.
773
                        p.iterobjects(get_raw=self.get_raw),
774
                        self.object_store)
775
                except:
776
                    self.repository.abort_write_group()
777
                    raise
778
                else:
779
                    self.repository.commit_write_group()
780
            finally:
781
                self.repository.unlock()
782
        return f, commit
783
0.200.1636 by Jelmer Vernooij
Some formatting fixes.
784
    # The pack isn't kept around anyway, so no point
0.251.1 by Jelmer Vernooij
Implement ObjectStore.add_{thin_,}pack.
785
    # in treating full packs different from thin packs
786
    add_pack = add_thin_pack