/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.910 by Jelmer Vernooij
update copyright years
1
# Copyright (C) 2008-2010 Jelmer Vernooij <jelmer@samba.org>
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.200.261 by Jelmer Vernooij
More formatting fixes.
17
from dulwich.objects import (
18
    Commit,
0.200.303 by Jelmer Vernooij
Cope with tags during fetch.
19
    Tag,
0.200.814 by Jelmer Vernooij
Avoid the use of InventoryDirectory.children. This speeds up
20
    Tree,
0.200.1407 by Jelmer Vernooij
Don't consider submodule modes unusual.
21
    S_IFGITLINK,
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
22
    S_ISGITLINK,
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
23
    ZERO_SHA,
0.200.261 by Jelmer Vernooij
More formatting fixes.
24
    )
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
25
from dulwich.object_store import (
26
    tree_lookup_path,
27
    )
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
28
from dulwich.walk import Walker
0.200.830 by Jelmer Vernooij
Bump minimum dulwich version.
29
from itertools import (
30
    imap,
31
    )
0.200.819 by Jelmer Vernooij
Avoid decoding basename twice.
32
import posixpath
0.200.563 by Jelmer Vernooij
Attempt to parse progress indication from git status reports.
33
import re
0.200.352 by Jelmer Vernooij
Simplify mode handling.
34
import stat
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
35
36
from bzrlib import (
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
37
    debug,
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
38
    errors,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
39
    osutils,
0.200.261 by Jelmer Vernooij
More formatting fixes.
40
    trace,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
41
    ui,
42
    )
43
from bzrlib.errors import (
0.239.5 by Jelmer Vernooij
Print user-understandable error message when encountering submodules.
44
    BzrError,
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
45
    NoSuchId,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
46
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
47
from bzrlib.inventory import (
48
    Inventory,
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
49
    InventoryDirectory,
50
    InventoryFile,
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
51
    InventoryLink,
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
52
    TreeReference,
0.200.261 by Jelmer Vernooij
More formatting fixes.
53
    )
54
from bzrlib.repository import (
55
    InterRepository,
56
    )
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
57
from bzrlib.revision import (
58
    NULL_REVISION,
59
    )
0.200.1187 by Jelmer Vernooij
Use InventoryRevisionTree.
60
try:
61
    from bzrlib.revisiontree import InventoryRevisionTree
62
except ImportError: # bzr < 2.4
63
    from bzrlib.revisiontree import RevisionTree as InventoryRevisionTree
0.200.1023 by Jelmer Vernooij
Set and verify testament.
64
from bzrlib.testament import (
65
    StrictTestament3,
66
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
67
from bzrlib.tsort import (
68
    topo_sort,
69
    )
0.200.417 by Jelmer Vernooij
use insert_record_stream rather than add_lines.
70
from bzrlib.versionedfile import (
0.200.811 by Jelmer Vernooij
Use ChunkedContentFactory when possible.
71
    ChunkedContentFactory,
0.200.417 by Jelmer Vernooij
use insert_record_stream rather than add_lines.
72
    )
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
73
0.200.1403 by Jelmer Vernooij
Cope with tags pointing at tree objects when cloning local git repositories.
74
from bzrlib.plugins.git.errors import (
75
    NotCommitError,
76
    )
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
77
from bzrlib.plugins.git.mapping import (
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
78
    DEFAULT_FILE_MODE,
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
79
    mode_is_executable,
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
80
    mode_kind,
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
81
    warn_unusual_mode,
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
82
    )
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
83
from bzrlib.plugins.git.object_store import (
84
    BazaarObjectStore,
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
85
    LRUTreeCache,
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
86
    _tree_to_objects,
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
87
    )
0.200.1458 by Jelmer Vernooij
Gather peeled shas rather than unpeeled.
88
from bzrlib.plugins.git.refs import (
89
    extract_tags,
90
    gather_peeled,
91
    )
0.200.426 by Jelmer Vernooij
Fix import of RemoteGitRepository.
92
from bzrlib.plugins.git.remote import (
93
    RemoteGitRepository,
94
    )
0.200.169 by Jelmer Vernooij
Fix branch cloning.
95
from bzrlib.plugins.git.repository import (
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
96
    GitRepository,
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
97
    GitRepositoryFormat,
0.200.426 by Jelmer Vernooij
Fix import of RemoteGitRepository.
98
    LocalGitRepository,
0.200.261 by Jelmer Vernooij
More formatting fixes.
99
    )
0.216.4 by Jelmer Vernooij
Add basic pack fetch infrastructure.
100
101
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
102
def import_git_blob(texts, mapping, path, name, (base_hexsha, hexsha), 
0.200.848 by Jelmer Vernooij
remove unnecessary parent_inv_shamap.
103
        base_inv, parent_id, revision_id,
0.200.896 by Jelmer Vernooij
Add separate function for looking up file ids.
104
        parent_invs, lookup_object, (base_mode, mode), store_updater,
105
        lookup_file_id):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
106
    """Import a git blob object into a bzr repository.
107
0.200.261 by Jelmer Vernooij
More formatting fixes.
108
    :param texts: VersionedFiles to add to
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
109
    :param path: Path in the tree
110
    :param blob: A git blob
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
111
    :return: Inventory delta for this file
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
112
    """
0.252.28 by Jelmer Vernooij
Don't import control files.
113
    if mapping.is_control_file(path):
114
        return []
0.200.816 by Jelmer Vernooij
Leave mode handling for blobs to import_git_blob.
115
    if base_hexsha == hexsha and base_mode == mode:
116
        # If nothing has changed since the base revision, we're done
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
117
        return []
0.200.896 by Jelmer Vernooij
Add separate function for looking up file ids.
118
    file_id = lookup_file_id(path)
0.200.816 by Jelmer Vernooij
Leave mode handling for blobs to import_git_blob.
119
    if stat.S_ISLNK(mode):
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
120
        cls = InventoryLink
121
    else:
122
        cls = InventoryFile
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
123
    ie = cls(file_id, name.decode("utf-8"), parent_id)
0.200.995 by Jelmer Vernooij
Support newer versions of bzr where only some InventoryFile/InventoryLink attributes are writable.
124
    if ie.kind == "file":
125
        ie.executable = mode_is_executable(mode)
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
126
    if base_hexsha == hexsha and mode_kind(base_mode) == mode_kind(mode):
127
        base_ie = base_inv[base_inv.path2id(path)]
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
128
        ie.text_size = base_ie.text_size
129
        ie.text_sha1 = base_ie.text_sha1
0.200.995 by Jelmer Vernooij
Support newer versions of bzr where only some InventoryFile/InventoryLink attributes are writable.
130
        if ie.kind == "symlink":
131
            ie.symlink_target = base_ie.symlink_target
0.200.537 by Jelmer Vernooij
Fix handling of not-executable files becoming executable without any other changes.
132
        if ie.executable == base_ie.executable:
133
            ie.revision = base_ie.revision
134
        else:
135
            blob = lookup_object(hexsha)
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
136
    else:
137
        blob = lookup_object(hexsha)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
138
        if ie.kind == "symlink":
0.200.551 by Jelmer Vernooij
Properly set InventoryEntry revision when changing symlink targets.
139
            ie.revision = None
0.200.1344 by Jelmer Vernooij
Unicode symlinks should be unicode in inventory entries.
140
            ie.symlink_target = blob.data.decode("utf-8")
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
141
        else:
0.200.830 by Jelmer Vernooij
Bump minimum dulwich version.
142
            ie.text_size = sum(imap(len, blob.chunked))
143
            ie.text_sha1 = osutils.sha_strings(blob.chunked)
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
144
    # Check what revision we should store
0.200.283 by Jelmer Vernooij
Avoid storing repeated texts for blobs.
145
    parent_keys = []
0.200.904 by Jelmer Vernooij
Fix inconsistent parents.
146
    for pinv in parent_invs:
0.200.829 by Jelmer Vernooij
Cope with the fact that _type is gone in upstream dulwich.
147
        try:
148
            pie = pinv[file_id]
149
        except NoSuchId:
150
            continue
0.252.25 by Jelmer Vernooij
Reformatting.
151
        if (pie.text_sha1 == ie.text_sha1 and
152
            pie.executable == ie.executable and
153
            pie.symlink_target == ie.symlink_target):
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
154
            # found a revision in one of the parents to use
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
155
            ie.revision = pie.revision
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
156
            break
0.200.904 by Jelmer Vernooij
Fix inconsistent parents.
157
        parent_key = (file_id, pie.revision)
158
        if not parent_key in parent_keys:
159
            parent_keys.append(parent_key)
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
160
    if ie.revision is None:
161
        # Need to store a new revision
162
        ie.revision = revision_id
163
        assert ie.revision is not None
0.200.698 by Jelmer Vernooij
Merge fixes for SHA1s of symlinks.
164
        if ie.kind == 'symlink':
0.200.811 by Jelmer Vernooij
Use ChunkedContentFactory when possible.
165
            chunks = []
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
166
        else:
0.200.830 by Jelmer Vernooij
Bump minimum dulwich version.
167
            chunks = blob.chunked
0.252.25 by Jelmer Vernooij
Reformatting.
168
        texts.insert_record_stream([
169
            ChunkedContentFactory((file_id, ie.revision),
170
                tuple(parent_keys), ie.text_sha1, chunks)])
0.200.572 by Jelmer Vernooij
Avoid some extra path lookups.
171
    invdelta = []
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
172
    if base_hexsha is not None:
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
173
        old_path = path.decode("utf-8") # Renames are not supported yet
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
174
        if stat.S_ISDIR(base_mode):
0.200.826 by Jelmer Vernooij
Fix some long lines.
175
            invdelta.extend(remove_disappeared_children(base_inv, old_path,
176
                lookup_object(base_hexsha), [], lookup_object))
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
177
    else:
178
        old_path = None
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
179
    new_path = path.decode("utf-8")
180
    invdelta.append((old_path, new_path, file_id, ie))
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
181
    if base_hexsha != hexsha:
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
182
        store_updater.add_object(blob, ie, path)
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
183
    return invdelta
0.200.261 by Jelmer Vernooij
More formatting fixes.
184
185
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
186
class SubmodulesRequireSubtrees(BzrError):
0.200.1309 by Jelmer Vernooij
Break some more long lines.
187
    _fmt = ("The repository you are fetching from contains submodules. "
188
            "To continue, upgrade your Bazaar repository to a format that "
189
            "supports nested trees, such as 'development-subtree'.")
0.239.5 by Jelmer Vernooij
Print user-understandable error message when encountering submodules.
190
    internal = False
191
192
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
193
def import_git_submodule(texts, mapping, path, name, (base_hexsha, hexsha),
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
194
    base_inv, parent_id, revision_id, parent_invs, lookup_object,
0.200.896 by Jelmer Vernooij
Add separate function for looking up file ids.
195
    (base_mode, mode), store_updater, lookup_file_id):
0.200.1309 by Jelmer Vernooij
Break some more long lines.
196
    """Import a git submodule."""
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
197
    if base_hexsha == hexsha and base_mode == mode:
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
198
        return [], {}
0.200.896 by Jelmer Vernooij
Add separate function for looking up file ids.
199
    file_id = lookup_file_id(path)
0.200.1408 by Jelmer Vernooij
Remove old ie children when converting directory into tree reference.
200
    invdelta = []
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
201
    ie = TreeReference(file_id, name.decode("utf-8"), parent_id)
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
202
    ie.revision = revision_id
0.200.1408 by Jelmer Vernooij
Remove old ie children when converting directory into tree reference.
203
    if base_hexsha is not None:
204
        old_path = path.decode("utf-8") # Renames are not supported yet
205
        if stat.S_ISDIR(base_mode):
206
            invdelta.extend(remove_disappeared_children(base_inv, old_path,
207
                lookup_object(base_hexsha), [], lookup_object))
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
208
    else:
0.200.1408 by Jelmer Vernooij
Remove old ie children when converting directory into tree reference.
209
        old_path = None
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
210
    ie.reference_revision = mapping.revision_id_foreign_to_bzr(hexsha)
0.252.25 by Jelmer Vernooij
Reformatting.
211
    texts.insert_record_stream([
212
        ChunkedContentFactory((file_id, ie.revision), (), None, [])])
0.200.1408 by Jelmer Vernooij
Remove old ie children when converting directory into tree reference.
213
    invdelta.append((old_path, path, file_id, ie))
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
214
    return invdelta, {}
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
215
216
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
217
def remove_disappeared_children(base_inv, path, base_tree, existing_children,
218
        lookup_object):
0.200.930 by Jelmer Vernooij
Add assert demonstrating 571055 and triggering it for all target formats.
219
    """Generate an inventory delta for removed children.
220
221
    :param base_inv: Base inventory against which to generate the 
222
        inventory delta.
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
223
    :param path: Path to process (unicode)
0.200.930 by Jelmer Vernooij
Add assert demonstrating 571055 and triggering it for all target formats.
224
    :param base_tree: Git Tree base object
225
    :param existing_children: Children that still exist
226
    :param lookup_object: Lookup a git object by its SHA1
227
    :return: Inventory delta, as list
228
    """
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
229
    assert type(path) is unicode
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
230
    ret = []
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
231
    for name, mode, hexsha in base_tree.iteritems():
232
        if name in existing_children:
233
            continue
234
        c_path = posixpath.join(path, name.decode("utf-8"))
0.200.930 by Jelmer Vernooij
Add assert demonstrating 571055 and triggering it for all target formats.
235
        file_id = base_inv.path2id(c_path)
236
        assert file_id is not None
237
        ret.append((c_path, None, file_id, None))
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
238
        if stat.S_ISDIR(mode):
239
            ret.extend(remove_disappeared_children(
240
                base_inv, c_path, lookup_object(hexsha), [], lookup_object))
0.200.552 by Jelmer Vernooij
Cope with directories becoming symlinks.
241
    return ret
242
243
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
244
def import_git_tree(texts, mapping, path, name, (base_hexsha, hexsha),
0.200.848 by Jelmer Vernooij
remove unnecessary parent_inv_shamap.
245
        base_inv, parent_id, revision_id, parent_invs,
0.200.896 by Jelmer Vernooij
Add separate function for looking up file ids.
246
        lookup_object, (base_mode, mode), store_updater,
247
        lookup_file_id, allow_submodules=False):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
248
    """Import a git tree object into a bzr repository.
249
0.200.261 by Jelmer Vernooij
More formatting fixes.
250
    :param texts: VersionedFiles object to add to
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
251
    :param path: Path in the tree (str)
252
    :param name: Name of the tree (str)
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
253
    :param tree: A git tree object
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
254
    :param base_inv: Base inventory against which to return inventory delta
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
255
    :return: Inventory delta for this subtree
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
256
    """
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
257
    assert type(path) is str
258
    assert type(name) is str
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
259
    if base_hexsha == hexsha and base_mode == mode:
260
        # If nothing has changed since the base revision, we're done
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
261
        return [], {}
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
262
    invdelta = []
0.200.896 by Jelmer Vernooij
Add separate function for looking up file ids.
263
    file_id = lookup_file_id(path)
0.200.297 by Jelmer Vernooij
Cope with non-ascii characters in filenames (needs a test..).
264
    # We just have to hope this is indeed utf-8:
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
265
    ie = InventoryDirectory(file_id, name.decode("utf-8"), parent_id)
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
266
    tree = lookup_object(hexsha)
267
    if base_hexsha is None:
268
        base_tree = None
0.200.823 by Jelmer Vernooij
Simplify logic in import_git_tree a bit.
269
        old_path = None # Newly appeared here
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
270
    else:
271
        base_tree = lookup_object(base_hexsha)
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
272
        old_path = path.decode("utf-8") # Renames aren't supported yet
273
    new_path = path.decode("utf-8")
0.200.823 by Jelmer Vernooij
Simplify logic in import_git_tree a bit.
274
    if base_tree is None or type(base_tree) is not Tree:
275
        ie.revision = revision_id
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
276
        invdelta.append((old_path, new_path, ie.file_id, ie))
0.252.24 by Jelmer Vernooij
Support reading fileid map.
277
        texts.insert_record_stream([
278
            ChunkedContentFactory((ie.file_id, ie.revision), (), None, [])])
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
279
    # Remember for next time
0.200.300 by Jelmer Vernooij
Fix recursive deletion of dirs.
280
    existing_children = set()
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
281
    child_modes = {}
0.200.1147 by Jelmer Vernooij
Use Tree.items() rather than Tree.entries().
282
    for name, child_mode, child_hexsha in tree.iteritems():
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
283
        existing_children.add(name)
0.200.819 by Jelmer Vernooij
Avoid decoding basename twice.
284
        child_path = posixpath.join(path, name)
0.200.814 by Jelmer Vernooij
Avoid the use of InventoryDirectory.children. This speeds up
285
        if type(base_tree) is Tree:
286
            try:
287
                child_base_mode, child_base_hexsha = base_tree[name]
288
            except KeyError:
289
                child_base_hexsha = None
290
                child_base_mode = 0
291
        else:
292
            child_base_hexsha = None
293
            child_base_mode = 0
0.200.816 by Jelmer Vernooij
Leave mode handling for blobs to import_git_blob.
294
        if stat.S_ISDIR(child_mode):
0.252.25 by Jelmer Vernooij
Reformatting.
295
            subinvdelta, grandchildmodes = import_git_tree(texts, mapping,
296
                child_path, name, (child_base_hexsha, child_hexsha), base_inv,
0.200.1059 by Jelmer Vernooij
Fix graph tests.
297
                file_id, revision_id, parent_invs, lookup_object,
0.252.25 by Jelmer Vernooij
Reformatting.
298
                (child_base_mode, child_mode), store_updater, lookup_file_id,
299
                allow_submodules=allow_submodules)
0.200.816 by Jelmer Vernooij
Leave mode handling for blobs to import_git_blob.
300
        elif S_ISGITLINK(child_mode): # submodule
0.200.666 by Jelmer Vernooij
Refuse to add tree references to non-subtree formats.
301
            if not allow_submodules:
302
                raise SubmodulesRequireSubtrees()
0.252.25 by Jelmer Vernooij
Reformatting.
303
            subinvdelta, grandchildmodes = import_git_submodule(texts, mapping,
304
                child_path, name, (child_base_hexsha, child_hexsha), base_inv,
305
                file_id, revision_id, parent_invs, lookup_object,
306
                (child_base_mode, child_mode), store_updater, lookup_file_id)
0.200.352 by Jelmer Vernooij
Simplify mode handling.
307
        else:
0.200.1328 by Jelmer Vernooij
More test fixes.
308
            if not mapping.is_special_file(name):
309
                subinvdelta = import_git_blob(texts, mapping, child_path, name,
310
                    (child_base_hexsha, child_hexsha), base_inv, file_id,
311
                    revision_id, parent_invs, lookup_object,
312
                    (child_base_mode, child_mode), store_updater, lookup_file_id)
313
            else:
314
                subinvdelta = []
0.200.757 by Jelmer Vernooij
Use inventory deltas.
315
            grandchildmodes = {}
316
        child_modes.update(grandchildmodes)
317
        invdelta.extend(subinvdelta)
0.200.816 by Jelmer Vernooij
Leave mode handling for blobs to import_git_blob.
318
        if child_mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
0.200.1407 by Jelmer Vernooij
Don't consider submodule modes unusual.
319
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0111,
320
                        S_IFGITLINK):
0.200.879 by Jelmer Vernooij
Fix unusual modes.
321
            child_modes[child_path] = child_mode
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
322
    # Remove any children that have disappeared
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
323
    if base_tree is not None and type(base_tree) is Tree:
0.200.984 by Jelmer Vernooij
Handle non-ascii characters in filenames.
324
        invdelta.extend(remove_disappeared_children(base_inv, old_path,
0.200.820 by Jelmer Vernooij
Avoid relying on InventoryDirectory.children.
325
            base_tree, existing_children, lookup_object))
0.200.952 by Jelmer Vernooij
Write git pack files rather than loose objects.
326
    store_updater.add_object(tree, ie, path)
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
327
    return invdelta, child_modes
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
328
329
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
330
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
0.200.1047 by Jelmer Vernooij
Fix -Dverify.
331
    o, rev, ret_tree, parent_trees, mapping, unusual_modes, verifiers):
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
332
    new_unusual_modes = mapping.export_unusual_file_modes(rev)
333
    if new_unusual_modes != unusual_modes:
334
        raise AssertionError("unusual modes don't match: %r != %r" % (
335
            unusual_modes, new_unusual_modes))
336
    # Verify that we can reconstruct the commit properly
0.200.1047 by Jelmer Vernooij
Fix -Dverify.
337
    rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True,
338
        verifiers)
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
339
    if rec_o != o:
340
        raise AssertionError("Reconstructed commit differs: %r != %r" % (
341
            rec_o, o))
342
    diff = []
343
    new_objs = {}
344
    for path, obj, ie in _tree_to_objects(ret_tree, parent_trees,
0.200.1309 by Jelmer Vernooij
Break some more long lines.
345
        target_git_object_retriever._cache.idmap, unusual_modes,
346
        mapping.BZR_DUMMY_FILE):
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
347
        old_obj_id = tree_lookup_path(lookup_object, o.tree, path)[1]
348
        new_objs[path] = obj
349
        if obj.id != old_obj_id:
350
            diff.append((path, lookup_object(old_obj_id), obj))
351
    for (path, old_obj, new_obj) in diff:
352
        while (old_obj.type_name == "tree" and
353
               new_obj.type_name == "tree" and
354
               sorted(old_obj) == sorted(new_obj)):
355
            for name in old_obj:
356
                if old_obj[name][0] != new_obj[name][0]:
0.252.25 by Jelmer Vernooij
Reformatting.
357
                    raise AssertionError("Modes for %s differ: %o != %o" %
358
                        (path, old_obj[name][0], new_obj[name][0]))
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
359
                if old_obj[name][1] != new_obj[name][1]:
360
                    # Found a differing child, delve deeper
361
                    path = posixpath.join(path, name)
362
                    old_obj = lookup_object(old_obj[name][1])
363
                    new_obj = new_objs[path]
364
                    break
365
        raise AssertionError("objects differ for %s: %r != %r" % (path,
366
            old_obj, new_obj))
367
368
0.200.1409 by Jelmer Vernooij
Support fetching into repositories that are stacked.
369
def ensure_inventories_in_repo(repo, trees):
370
    real_inv_vf = repo.inventories.without_fallbacks()
371
    for t in trees:
372
        revid = t.get_revision_id()
373
        if not real_inv_vf.get_parent_map([(revid, )]):
374
            repo.add_inventory(revid, t.inventory, t.get_parent_ids())
375
376
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
377
def import_git_commit(repo, mapping, head, lookup_object,
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
378
                      target_git_object_retriever, trees_cache):
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
379
    o = lookup_object(head)
0.261.5 by Jelmer Vernooij
Fix looking up of parents during fetch.
380
    # Note that this uses mapping.revision_id_foreign_to_bzr. If the parents
381
    # were bzr roundtripped revisions they would be specified in the
382
    # roundtrip data.
0.261.4 by Jelmer Vernooij
Fix tests.
383
    rev, roundtrip_revid, verifiers = mapping.import_commit(
0.261.5 by Jelmer Vernooij
Fix looking up of parents during fetch.
384
        o, mapping.revision_id_foreign_to_bzr)
0.200.1329 by Jelmer Vernooij
Fix more tests.
385
    if roundtrip_revid is not None:
386
        original_revid = rev.revision_id
387
        rev.revision_id = roundtrip_revid
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
388
    # We have to do this here, since we have to walk the tree and
389
    # we need to make sure to import the blobs / trees with the right
390
    # path; this may involve adding them more than once.
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
391
    parent_trees = trees_cache.revision_trees(rev.parent_ids)
0.200.1409 by Jelmer Vernooij
Support fetching into repositories that are stacked.
392
    ensure_inventories_in_repo(repo, parent_trees)
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
393
    if parent_trees == []:
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
394
        base_inv = Inventory(root_id=None)
0.200.814 by Jelmer Vernooij
Avoid the use of InventoryDirectory.children. This speeds up
395
        base_tree = None
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
396
        base_mode = None
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
397
    else:
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
398
        base_inv = parent_trees[0].inventory
0.200.814 by Jelmer Vernooij
Avoid the use of InventoryDirectory.children. This speeds up
399
        base_tree = lookup_object(o.parents[0]).tree
0.200.817 by Jelmer Vernooij
Deal with all modes locally.
400
        base_mode = stat.S_IFDIR
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
401
    store_updater = target_git_object_retriever._get_updater(rev)
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
402
    tree_supplement = mapping.get_fileid_map(lookup_object, o.tree)
0.200.839 by Jelmer Vernooij
Add convenience object for updating the object store caching layer.
403
    inv_delta, unusual_modes = import_git_tree(repo.texts,
0.200.988 by Jelmer Vernooij
Some formatting cleanups.
404
            mapping, "", "", (base_tree, o.tree), base_inv,
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
405
            None, rev.revision_id, [p.inventory for p in parent_trees],
406
            lookup_object, (base_mode, stat.S_IFDIR), store_updater,
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
407
            tree_supplement.lookup_file_id,
0.200.1309 by Jelmer Vernooij
Break some more long lines.
408
            allow_submodules=getattr(repo._format, "supports_tree_reference",
409
                False))
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
410
    if unusual_modes != {}:
411
        for path, mode in unusual_modes.iteritems():
412
            warn_unusual_mode(rev.foreign_revid, path, mode)
413
        mapping.import_unusual_file_modes(rev, unusual_modes)
414
    try:
415
        basis_id = rev.parent_ids[0]
416
    except IndexError:
417
        basis_id = NULL_REVISION
418
        base_inv = None
419
    rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
0.200.988 by Jelmer Vernooij
Some formatting cleanups.
420
              inv_delta, rev.revision_id, rev.parent_ids, base_inv)
0.200.1195 by Jelmer Vernooij
Cope with new StrictTestament3 arguments.
421
    ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
0.200.1329 by Jelmer Vernooij
Fix more tests.
422
    # Check verifiers
423
    if verifiers and roundtrip_revid is not None:
424
        if getattr(StrictTestament3, "from_revision_tree", None):
425
            testament = StrictTestament3(rev, ret_tree)
426
        else: # bzr < 2.4
427
            testament = StrictTestament3(rev, inv)
428
        calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
429
        if calculated_verifiers != verifiers:
430
            trace.mutter("Testament SHA1 %r for %r did not match %r.",
431
                         calculated_verifiers["testament3-sha1"],
432
                         rev.revision_id, verifiers["testament3-sha1"])
433
            rev.revision_id = original_revid
434
            rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
435
              inv_delta, rev.revision_id, rev.parent_ids, base_inv)
436
            ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
0.200.1179 by Jelmer Vernooij
Avoid using verifiers for natively imported revisions, save a lot of time.
437
    else:
438
        calculated_verifiers = {}
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
439
    store_updater.add_object(o, calculated_verifiers, None)
440
    store_updater.finish()
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
441
    trees_cache.add(ret_tree)
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
442
    repo.add_revision(rev.revision_id, rev)
443
    if "verify" in debug.debug_flags:
0.200.883 by Jelmer Vernooij
Add function for verifying reconstruction of objects still works.
444
        verify_commit_reconstruction(target_git_object_retriever, 
445
            lookup_object, o, rev, ret_tree, parent_trees, mapping,
0.200.1047 by Jelmer Vernooij
Fix -Dverify.
446
            unusual_modes, verifiers)
0.200.679 by Jelmer Vernooij
Moving commit import functionality to a separate function.
447
448
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
449
def import_git_objects(repo, mapping, object_iter,
450
    target_git_object_retriever, heads, pb=None, limit=None):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
451
    """Import a set of git objects into a bzr repository.
452
0.200.483 by Jelmer Vernooij
Add NEWS entry about sha map.
453
    :param repo: Target Bazaar repository
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
454
    :param mapping: Mapping to use
455
    :param object_iter: Iterator over Git objects.
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
456
    :return: Tuple with pack hints and last imported revision id
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
457
    """
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
458
    def lookup_object(sha):
459
        try:
460
            return object_iter[sha]
461
        except KeyError:
462
            return target_git_object_retriever[sha]
0.200.158 by Jelmer Vernooij
fetch works \o/
463
    graph = []
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
464
    checked = set()
0.200.734 by Jelmer Vernooij
Don't import head revision twice when pulling from Git.
465
    heads = list(set(heads))
0.200.852 by Jelmer Vernooij
Cache trees rather than inventories.
466
    trees_cache = LRUTreeCache(repo)
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
467
    # Find and convert commit objects
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
468
    while heads:
469
        if pb is not None:
470
            pb.update("finding revisions to fetch", len(graph), None)
471
        head = heads.pop()
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
472
        if head == ZERO_SHA:
473
            continue
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
474
        assert isinstance(head, str), "head is %r" % (head,)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
475
        try:
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
476
            o = lookup_object(head)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
477
        except KeyError:
478
            continue
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
479
        if isinstance(o, Commit):
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
480
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
0.261.6 by Jelmer Vernooij
Use mapping.revision_id_foreign_to_bzr to find parents everywhere.
481
                mapping.revision_id_foreign_to_bzr)
0.200.1021 by Jelmer Vernooij
Put testament sha1 in revisions.
482
            if (repo.has_revision(rev.revision_id) or
483
                (roundtrip_revid and repo.has_revision(roundtrip_revid))):
0.200.295 by Jelmer Vernooij
Don't re-import revisions already fetched.
484
                continue
0.200.668 by Jelmer Vernooij
Fix some places where we were way too much memory for repositories with a large number of entries in the inventory and a large number of revisions.
485
            graph.append((o.id, o.parents))
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
486
            heads.extend([p for p in o.parents if p not in checked])
0.200.303 by Jelmer Vernooij
Cope with tags during fetch.
487
        elif isinstance(o, Tag):
0.200.734 by Jelmer Vernooij
Don't import head revision twice when pulling from Git.
488
            if o.object[1] not in checked:
489
                heads.append(o.object[1])
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
490
        else:
491
            trace.warning("Unable to import head object %r" % o)
0.200.668 by Jelmer Vernooij
Fix some places where we were way too much memory for repositories with a large number of entries in the inventory and a large number of revisions.
492
        checked.add(o.id)
493
    del checked
0.200.158 by Jelmer Vernooij
fetch works \o/
494
    # Order the revisions
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
495
    # Create the inventory objects
0.200.821 by Jelmer Vernooij
Remove last references to ID.children.
496
    batch_size = 1000
0.200.680 by Jelmer Vernooij
fetch revisions in batches
497
    revision_ids = topo_sort(graph)
498
    pack_hints = []
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
499
    if limit is not None:
500
        revision_ids = revision_ids[:limit]
0.247.3 by Michael Hudson
oh, so it wasn't (particularly) wrong, but it was a bit obscure
501
    last_imported = None
0.200.680 by Jelmer Vernooij
fetch revisions in batches
502
    for offset in range(0, len(revision_ids), batch_size):
0.254.33 by Jelmer Vernooij
Merge trunk.
503
        target_git_object_retriever.start_write_group() 
0.200.680 by Jelmer Vernooij
fetch revisions in batches
504
        try:
0.254.33 by Jelmer Vernooij
Merge trunk.
505
            repo.start_write_group()
506
            try:
0.200.824 by Jelmer Vernooij
Commit cache data in batches as well.
507
                for i, head in enumerate(
508
                    revision_ids[offset:offset+batch_size]):
0.254.33 by Jelmer Vernooij
Merge trunk.
509
                    if pb is not None:
0.200.824 by Jelmer Vernooij
Commit cache data in batches as well.
510
                        pb.update("fetching revisions", offset+i,
511
                                  len(revision_ids))
0.254.33 by Jelmer Vernooij
Merge trunk.
512
                    import_git_commit(repo, mapping, head, lookup_object,
0.252.25 by Jelmer Vernooij
Reformatting.
513
                        target_git_object_retriever, trees_cache)
0.254.33 by Jelmer Vernooij
Merge trunk.
514
                    last_imported = head
515
            except:
516
                repo.abort_write_group()
517
                raise
518
            else:
519
                hint = repo.commit_write_group()
520
                if hint is not None:
521
                    pack_hints.extend(hint)
0.200.680 by Jelmer Vernooij
fetch revisions in batches
522
        except:
0.254.33 by Jelmer Vernooij
Merge trunk.
523
            target_git_object_retriever.abort_write_group()
0.200.680 by Jelmer Vernooij
fetch revisions in batches
524
            raise
525
        else:
0.254.33 by Jelmer Vernooij
Merge trunk.
526
            target_git_object_retriever.commit_write_group()
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
527
    return pack_hints, last_imported
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
528
529
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
530
class InterFromGitRepository(InterRepository):
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
531
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
532
    _matching_repo_format = GitRepositoryFormat()
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
533
0.200.1154 by Jelmer Vernooij
Share more code in InterGitRepository.
534
    def _target_has_shas(self, shas):
535
        raise NotImplementedError(self._target_has_shas)
536
537
    def get_determine_wants_heads(self, wants, include_tags=False):
538
        wants = set(wants)
539
        def determine_wants(refs):
540
            potential = set(wants)
541
            if include_tags:
0.200.1300 by Jelmer Vernooij
Fix formatting.
542
                potential.update(
543
                    [v[1] or v[0] for v in extract_tags(refs).itervalues()])
0.200.1154 by Jelmer Vernooij
Share more code in InterGitRepository.
544
            return list(potential - self._target_has_shas(potential))
545
        return determine_wants
546
547
    def determine_wants_all(self, refs):
0.200.1458 by Jelmer Vernooij
Gather peeled shas rather than unpeeled.
548
        potential = set([peeled for (peeled, unpeeled) in
549
            gather_peeled(refs).itervalues()])
0.200.1154 by Jelmer Vernooij
Share more code in InterGitRepository.
550
        return list(potential - self._target_has_shas(potential))
551
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
552
    @staticmethod
553
    def _get_repo_format_to_test():
554
        return None
555
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
556
    def copy_content(self, revision_id=None, pb=None):
557
        """See InterRepository.copy_content."""
558
        self.fetch(revision_id, pb, find_ghosts=False)
559
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
560
    def search_missing_revision_ids(self,
561
            find_ghosts=True, revision_ids=None, if_present_ids=None,
562
            limit=None):
563
        git_shas = []
564
        todo = []
565
        if revision_ids:
566
            todo.extend(revision_ids)
567
        if if_present_ids:
568
            todo.extend(revision_ids)
569
        for revid in revision_ids:
570
            if revid == NULL_REVISION:
571
                continue
572
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
573
            git_shas.append(git_sha)
574
        walker = Walker(self.source._git.object_store,
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
575
            include=git_shas, exclude=[sha for sha in self.target.bzrdir.get_refs().values() if sha != ZERO_SHA])
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
576
        missing_revids = set()
577
        for entry in walker:
578
            missing_revids.add(self.source.lookup_foreign_revision_id(entry.commit.id))
579
        return self.source.revision_ids_to_search_result(missing_revids)
580
581
582
class InterGitNonGitRepository(InterFromGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
583
    """Base InterRepository that copies revisions from a Git into a non-Git
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
584
    repository."""
585
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
586
    def _target_has_shas(self, shas):
0.200.1456 by Jelmer Vernooij
Fix target_has_shas.
587
        revids = {}
0.200.1403 by Jelmer Vernooij
Cope with tags pointing at tree objects when cloning local git repositories.
588
        for sha in shas:
589
            try:
590
                revid = self.source.lookup_foreign_revision_id(sha)
591
            except NotCommitError:
0.200.1456 by Jelmer Vernooij
Fix target_has_shas.
592
                # Commit is definitely not present
0.200.1403 by Jelmer Vernooij
Cope with tags pointing at tree objects when cloning local git repositories.
593
                continue
594
            else:
0.200.1456 by Jelmer Vernooij
Fix target_has_shas.
595
                revids[revid] = sha
596
        return set([revids[r] for r in self.target.has_revisions(revids)])
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
597
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
598
    def get_determine_wants_revids(self, revids, include_tags=False):
599
        wants = set()
600
        for revid in set(revids):
0.200.1388 by Jelmer Vernooij
Don't fetch revision already present.
601
            if self.target.has_revision(revid):
602
                continue
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
603
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
604
            wants.add(git_sha)
0.200.1309 by Jelmer Vernooij
Break some more long lines.
605
        return self.get_determine_wants_heads(wants,
606
            include_tags=include_tags)
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
607
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
608
    def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
609
        """Fetch objects from a remote server.
610
611
        :param determine_wants: determine_wants callback
612
        :param mapping: BzrGitMapping to use
613
        :param pb: Optional progress bar
614
        :param limit: Maximum number of commits to import.
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
615
        :return: Tuple with pack hint, last imported revision id and remote refs
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
616
        """
617
        raise NotImplementedError(self.fetch_objects)
618
0.200.940 by Jelmer Vernooij
Avoid confusion between different fetch functions with different semantics.
619
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
0.200.247 by Jelmer Vernooij
Fix git-import.
620
              mapping=None, fetch_spec=None):
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
621
        if mapping is None:
622
            mapping = self.source.get_mapping()
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
623
        if revision_id is not None:
624
            interesting_heads = [revision_id]
625
        elif fetch_spec is not None:
0.200.1089 by Jelmer Vernooij
Cope with fancy fetch_spec behaviour.
626
            recipe = fetch_spec.get_recipe()
627
            if recipe[0] in ("search", "proxy-search"):
628
                interesting_heads = recipe[1]
629
            else:
0.200.1300 by Jelmer Vernooij
Fix formatting.
630
                raise AssertionError("Unsupported search result type %s" %
631
                        recipe[0])
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
632
        else:
633
            interesting_heads = None
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
634
635
        if interesting_heads is not None:
0.200.1309 by Jelmer Vernooij
Break some more long lines.
636
            determine_wants = self.get_determine_wants_revids(
637
                interesting_heads, include_tags=False)
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
638
        else:
639
            determine_wants = self.determine_wants_all
0.200.1079 by Jelmer Vernooij
Avoid looking up revid if not necessary.
640
641
        (pack_hint, _, remote_refs) = self.fetch_objects(determine_wants,
642
            mapping, pb)
0.200.579 by Jelmer Vernooij
Only pack if it makes the target repo smaller.
643
        if pack_hint is not None and self.target._format.pack_compresses:
0.200.578 by Jelmer Vernooij
Only do optimal packing on bzr >= 1.17.
644
            self.target.pack(hint=pack_hint)
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
645
        assert isinstance(remote_refs, dict)
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
646
        return remote_refs
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
647
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
648
0.200.563 by Jelmer Vernooij
Attempt to parse progress indication from git status reports.
649
_GIT_PROGRESS_RE = re.compile(r"(.*?): +(\d+)% \((\d+)/(\d+)\)")
650
def report_git_progress(pb, text):
651
    text = text.rstrip("\r\n")
652
    g = _GIT_PROGRESS_RE.match(text)
653
    if g is not None:
654
        (text, pct, current, total) = g.groups()
655
        pb.update(text, int(current), int(total))
656
    else:
657
        pb.update(text, 0, 0)
658
659
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
660
class DetermineWantsRecorder(object):
661
662
    def __init__(self, actual):
663
        self.actual = actual
664
        self.wants = []
665
        self.remote_refs = {}
666
667
    def __call__(self, refs):
668
        self.remote_refs = refs
669
        self.wants = self.actual(refs)
670
        return self.wants
671
672
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
673
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
674
    """InterRepository that copies revisions from a remote Git into a non-Git
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
675
    repository."""
676
0.200.582 by Jelmer Vernooij
Use more efficient algorithm for finding out heads.
677
    def get_target_heads(self):
678
        # FIXME: This should be more efficient
679
        all_revs = self.target.all_revision_ids()
680
        parent_map = self.target.get_parent_map(all_revs)
681
        all_parents = set()
682
        map(all_parents.update, parent_map.itervalues())
683
        return set(all_revs) - all_parents
684
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
685
    def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
686
        """See `InterGitNonGitRepository`."""
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
687
        def progress(text):
0.200.563 by Jelmer Vernooij
Attempt to parse progress indication from git status reports.
688
            report_git_progress(pb, text)
0.200.466 by Jelmer Vernooij
Fix finding of heads for fetch_objects.
689
        store = BazaarObjectStore(self.target, mapping)
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
690
        store.lock_write()
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
691
        try:
0.200.582 by Jelmer Vernooij
Use more efficient algorithm for finding out heads.
692
            heads = self.get_target_heads()
0.200.484 by Jelmer Vernooij
Cope with kind changes.
693
            graph_walker = store.get_graph_walker(
694
                    [store._lookup_revision_sha1(head) for head in heads])
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
695
            wants_recorder = DetermineWantsRecorder(determine_wants)
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
696
0.200.484 by Jelmer Vernooij
Cope with kind changes.
697
            create_pb = None
698
            if pb is None:
699
                create_pb = pb = ui.ui_factory.nested_progress_bar()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
700
            try:
0.200.1000 by Jelmer Vernooij
Fix fetch between local and remote git branches.
701
                objects_iter = self.source.fetch_objects(
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
702
                    wants_recorder, graph_walker, store.get_raw,
0.200.1000 by Jelmer Vernooij
Fix fetch between local and remote git branches.
703
                    progress)
0.200.1300 by Jelmer Vernooij
Fix formatting.
704
                trace.mutter("Importing %d new revisions",
705
                             len(wants_recorder.wants))
706
                (pack_hint, last_rev) = import_git_objects(self.target,
707
                    mapping, objects_iter, store, wants_recorder.wants, pb,
708
                    limit)
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
709
                return (pack_hint, last_rev, wants_recorder.remote_refs)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
710
            finally:
0.200.484 by Jelmer Vernooij
Cope with kind changes.
711
                if create_pb:
712
                    create_pb.finished()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
713
        finally:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
714
            store.unlock()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
715
716
    @staticmethod
717
    def is_compatible(source, target):
718
        """Be compatible with GitRepository."""
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
719
        if not isinstance(source, RemoteGitRepository):
720
            return False
721
        if not target.supports_rich_root():
722
            return False
723
        if isinstance(target, GitRepository):
724
            return False
0.200.1270 by Jelmer Vernooij
Cope with older versions of bzr.
725
        if not getattr(target._format, "supports_full_versioned_files", True):
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
726
            return False
727
        return True
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
728
729
730
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
731
    """InterRepository that copies revisions from a local Git into a non-Git
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
732
    repository."""
733
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
734
    def fetch_objects(self, determine_wants, mapping, pb=None, limit=None):
0.200.1001 by Jelmer Vernooij
Simplify handling of determine wants, add stub for fetch_objects().
735
        """See `InterGitNonGitRepository`."""
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
736
        remote_refs = self.source.bzrdir.get_refs()
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
737
        wants = determine_wants(remote_refs)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
738
        create_pb = None
739
        if pb is None:
740
            create_pb = pb = ui.ui_factory.nested_progress_bar()
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
741
        target_git_object_retriever = BazaarObjectStore(self.target, mapping)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
742
        try:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
743
            target_git_object_retriever.lock_write()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
744
            try:
0.200.1300 by Jelmer Vernooij
Fix formatting.
745
                (pack_hint, last_rev) = import_git_objects(self.target,
746
                    mapping, self.source._git.object_store,
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
747
                    target_git_object_retriever, wants, pb, limit)
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
748
                return (pack_hint, last_rev, remote_refs)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
749
            finally:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
750
                target_git_object_retriever.unlock()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
751
        finally:
752
            if create_pb:
753
                create_pb.finished()
754
755
    @staticmethod
756
    def is_compatible(source, target):
757
        """Be compatible with GitRepository."""
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
758
        if not isinstance(source, LocalGitRepository):
759
            return False
760
        if not target.supports_rich_root():
761
            return False
762
        if isinstance(target, GitRepository):
763
            return False
0.200.1266 by Jelmer Vernooij
Fix 2.3 support.
764
        if not getattr(target._format, "supports_full_versioned_files", True):
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
765
            return False
766
        return True
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
767
768
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
769
class InterGitGitRepository(InterFromGitRepository):
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
770
    """InterRepository that copies between Git repositories."""
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
771
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
772
    def fetch_refs(self, update_refs, lossy=False):
773
        if lossy:
774
            raise errors.LossyPushToSameVCS(self.source, self.target)
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
775
        old_refs = self.target.bzrdir.get_refs()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
776
        ref_changes = {}
777
        def determine_wants(heads):
778
            old_refs = dict([(k, (v, None)) for (k, v) in heads.iteritems()])
779
            new_refs = update_refs(old_refs)
780
            ref_changes.update(new_refs)
781
            return [sha1 for (sha1, bzr_revid) in new_refs.itervalues()]
782
        self.fetch_objects(determine_wants)
783
        for k, (git_sha, bzr_revid) in ref_changes.iteritems():
784
            self.target._git.refs[k] = git_sha
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
785
        new_refs = self.target.bzrdir.get_refs()
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
786
        return None, old_refs, new_refs
787
788
    def fetch_objects(self, determine_wants, mapping=None, pb=None):
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
789
        def progress(text):
790
            trace.note("git: %s", text)
791
        graphwalker = self.target._git.get_graph_walker()
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
792
        if (isinstance(self.source, LocalGitRepository) and
793
            isinstance(self.target, LocalGitRepository)):
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
794
            refs = self.source._git.fetch(self.target._git, determine_wants,
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
795
                progress)
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
796
            return (None, None, refs)
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
797
        elif (isinstance(self.source, LocalGitRepository) and
798
              isinstance(self.target, RemoteGitRepository)):
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
799
            raise NotImplementedError
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
800
        elif (isinstance(self.source, RemoteGitRepository) and
801
              isinstance(self.target, LocalGitRepository)):
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
802
            f, commit = self.target._git.object_store.add_pack()
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
803
            try:
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
804
                refs = self.source.bzrdir.fetch_pack(
0.200.1003 by Jelmer Vernooij
Initial work on supporting move_in_thin_pack.
805
                    determine_wants, graphwalker, f.write, progress)
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
806
                commit()
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
807
                return (None, None, refs)
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
808
            except:
809
                f.close()
810
                raise
811
        else:
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
812
            raise AssertionError("fetching between %r and %r not supported" %
813
                    (self.source, self.target))
0.200.635 by Jelmer Vernooij
Fix fetching between git repositories.
814
0.200.1154 by Jelmer Vernooij
Share more code in InterGitRepository.
815
    def _target_has_shas(self, shas):
816
        return set([sha for sha in shas if self.target._git.object_store])
817
0.200.940 by Jelmer Vernooij
Avoid confusion between different fetch functions with different semantics.
818
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
819
              mapping=None, fetch_spec=None, branches=None):
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
820
        if mapping is None:
821
            mapping = self.source.get_mapping()
822
        r = self.target._git
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
823
        if revision_id is not None:
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
824
            args = [self.source.lookup_bzr_revision_id(revision_id)[0]]
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
825
        elif fetch_spec is not None:
0.200.1089 by Jelmer Vernooij
Cope with fancy fetch_spec behaviour.
826
            recipe = fetch_spec.get_recipe()
827
            if recipe[0] in ("search", "proxy-search"):
828
                heads = recipe[1]
829
            else:
0.200.1309 by Jelmer Vernooij
Break some more long lines.
830
                raise AssertionError(
831
                    "Unsupported search result type %s" % recipe[0])
0.200.1350 by Jelmer Vernooij
Implement search_missing_revision_ids.
832
            args = [self.source.lookup_bzr_revision_id(revid)[0] for revid in heads]
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
833
        if branches is not None:
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
834
            determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store and x[y] != ZERO_SHA]
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
835
        elif fetch_spec is None and revision_id is None:
0.200.1154 by Jelmer Vernooij
Share more code in InterGitRepository.
836
            determine_wants = self.determine_wants_all
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
837
        else:
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
838
            determine_wants = lambda x: [y for y in args if not y in r.object_store and y != ZERO_SHA]
839
        wants_recorder = DetermineWantsRecorder(determine_wants)
840
        self.fetch_objects(wants_recorder, mapping)
841
        return wants_recorder.remote_refs
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
842
843
    @staticmethod
844
    def is_compatible(source, target):
845
        """Be compatible with GitRepository."""
0.200.664 by Jelmer Vernooij
Support submodules during fetch.
846
        return (isinstance(source, GitRepository) and
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
847
                isinstance(target, GitRepository))
0.200.1174 by Jelmer Vernooij
Fix specific revision fetching between git repositories.
848
849
    def get_determine_wants_revids(self, revids, include_tags=False):
850
        wants = set()
851
        for revid in set(revids):
0.200.1388 by Jelmer Vernooij
Don't fetch revision already present.
852
            if self.target.has_revision(revid):
853
                continue
0.200.1174 by Jelmer Vernooij
Fix specific revision fetching between git repositories.
854
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
855
            wants.add(git_sha)
0.200.1309 by Jelmer Vernooij
Break some more long lines.
856
        return self.get_determine_wants_heads(wants,
857
            include_tags=include_tags)