/brz/remove-bazaar

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