/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2008 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.292 by Jelmer Vernooij
Fix formatting.
17
from cStringIO import (
18
    StringIO,
19
    )
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
20
import dulwich as git
0.200.261 by Jelmer Vernooij
More formatting fixes.
21
from dulwich.objects import (
22
    Commit,
0.200.303 by Jelmer Vernooij
Cope with tags during fetch.
23
    Tag,
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
24
    S_ISGITLINK,
0.200.261 by Jelmer Vernooij
More formatting fixes.
25
    )
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
26
from dulwich.object_store import (
27
    tree_lookup_path,
28
    )
0.200.352 by Jelmer Vernooij
Simplify mode handling.
29
import stat
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
30
31
from bzrlib import (
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
32
    debug,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
33
    osutils,
0.200.261 by Jelmer Vernooij
More formatting fixes.
34
    trace,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
35
    ui,
36
    urlutils,
37
    )
38
from bzrlib.errors import (
39
    InvalidRevisionId,
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
40
    NoSuchId,
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
41
    NoSuchRevision,
42
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
43
from bzrlib.inventory import (
44
    Inventory,
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
45
    InventoryDirectory,
46
    InventoryFile,
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
47
    InventoryLink,
0.200.261 by Jelmer Vernooij
More formatting fixes.
48
    )
0.200.301 by Jelmer Vernooij
Cache inventories created.
49
from bzrlib.lru_cache import (
50
    LRUCache,
51
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
52
from bzrlib.repository import (
53
    InterRepository,
54
    )
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
55
from bzrlib.revision import (
56
    NULL_REVISION,
57
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
58
from bzrlib.tsort import (
59
    topo_sort,
60
    )
0.200.417 by Jelmer Vernooij
use insert_record_stream rather than add_lines.
61
from bzrlib.versionedfile import (
62
    FulltextContentFactory,
63
    )
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
64
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
65
from bzrlib.plugins.git.mapping import (
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
66
    DEFAULT_FILE_MODE,
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
67
    inventory_to_tree_and_blobs,
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
68
    mode_is_executable,
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
69
    text_to_blob,
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
70
    warn_unusual_mode,
0.231.2 by Jelmer Vernooij
Add -Dverify flag (not fully implemented yet).
71
    )
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
72
from bzrlib.plugins.git.object_store import (
73
    BazaarObjectStore,
74
    )
0.200.426 by Jelmer Vernooij
Fix import of RemoteGitRepository.
75
from bzrlib.plugins.git.remote import (
76
    RemoteGitRepository,
77
    )
0.200.169 by Jelmer Vernooij
Fix branch cloning.
78
from bzrlib.plugins.git.repository import (
0.200.261 by Jelmer Vernooij
More formatting fixes.
79
    GitRepository, 
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
80
    GitRepositoryFormat,
0.200.426 by Jelmer Vernooij
Fix import of RemoteGitRepository.
81
    LocalGitRepository,
0.200.261 by Jelmer Vernooij
More formatting fixes.
82
    )
0.216.4 by Jelmer Vernooij
Add basic pack fetch infrastructure.
83
84
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
85
def import_git_blob(texts, mapping, path, hexsha, base_inv, parent_id, 
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
86
    revision_id, parent_invs, shagitmap, lookup_object, executable, symlink):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
87
    """Import a git blob object into a bzr repository.
88
0.200.261 by Jelmer Vernooij
More formatting fixes.
89
    :param texts: VersionedFiles to add to
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
90
    :param path: Path in the tree
91
    :param blob: A git blob
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
92
    :return: Inventory delta for this file
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
93
    """
94
    file_id = mapping.generate_file_id(path)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
95
    if symlink:
96
        cls = InventoryLink
97
    else:
98
        cls = InventoryFile
99
    # We just have to hope this is indeed utf-8:
0.200.354 by Jelmer Vernooij
Support symlinks in conversion to git.
100
    ie = cls(file_id, urlutils.basename(path).decode("utf-8"), parent_id)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
101
    ie.executable = executable
0.200.375 by Jelmer Vernooij
Store object hex sha's in InventoryEntry.text_id during fetch.
102
    ie.text_id = hexsha
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
103
    # See if this has changed at all
104
    try:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
105
        base_ie = base_inv[file_id]
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
106
    except NoSuchId:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
107
        base_ie = None
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
108
        base_sha = None
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
109
    else:
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
110
        base_sha = base_ie.text_id
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
111
        try:
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
112
            if base_sha is None:
113
                base_sha = shagitmap.lookup_blob(file_id, base_ie.revision)
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
114
        except KeyError:
115
            base_sha = None
116
        else:
117
            if (base_sha == hexsha and base_ie.executable == ie.executable
118
                and base_ie.kind == ie.kind):
119
                # If nothing has changed since the base revision, we're done
120
                return [], []
0.200.484 by Jelmer Vernooij
Cope with kind changes.
121
    if base_sha == hexsha and base_ie.kind == ie.kind:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
122
        ie.text_size = base_ie.text_size
123
        ie.text_sha1 = base_ie.text_sha1
124
        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.
125
        if ie.executable == base_ie.executable:
126
            ie.revision = base_ie.revision
127
        else:
128
            blob = lookup_object(hexsha)
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
129
    else:
130
        blob = lookup_object(hexsha)
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
131
        if ie.kind == "symlink":
132
            ie.symlink_target = blob.data
133
            ie.text_size = None
134
            ie.text_sha1 = None
135
        else:
136
            ie.text_size = len(blob.data)
137
            ie.text_sha1 = osutils.sha_string(blob.data)
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
138
    # Check what revision we should store
0.200.283 by Jelmer Vernooij
Avoid storing repeated texts for blobs.
139
    parent_keys = []
140
    for pinv in parent_invs:
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
141
        if pinv.revision_id == base_inv.revision_id:
0.200.374 by Jelmer Vernooij
Also avoid lookups in the inventory in the tree fetch functions.
142
            pie = base_ie
143
            if pie is None:
144
                continue
145
        else:
146
            try:
147
                pie = pinv[file_id]
148
            except NoSuchId:
149
                continue
0.200.537 by Jelmer Vernooij
Fix handling of not-executable files becoming executable without any other changes.
150
        if pie.text_sha1 == ie.text_sha1 and pie.executable == ie.executable:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
151
            # 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.
152
            ie.revision = pie.revision
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
153
            break
0.200.373 by Jelmer Vernooij
Re-use inventory entries rather than looking them up again and again.
154
        parent_keys.append((file_id, pie.revision))
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
155
    if ie.revision is None:
156
        # Need to store a new revision
157
        ie.revision = revision_id
158
        assert file_id is not None
159
        assert ie.revision is not None
0.200.417 by Jelmer Vernooij
use insert_record_stream rather than add_lines.
160
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), tuple(parent_keys), ie.text_sha1, blob.data)])
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
161
        shamap = [(hexsha, "blob", (ie.file_id, ie.revision))]
162
    else:
163
        shamap = []
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
164
    if file_id in base_inv:
165
        old_path = base_inv.id2path(file_id)
166
    else:
167
        old_path = None
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
168
    return ([(old_path, path, file_id, ie)], shamap)
0.200.261 by Jelmer Vernooij
More formatting fixes.
169
170
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
171
def import_git_submodule(texts, mapping, path, hexsha, base_inv, parent_id, 
172
    revision_id, parent_invs, shagitmap, lookup_object):
173
    raise NotImplementedError(import_git_submodule)
174
175
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
176
def import_git_tree(texts, mapping, path, hexsha, base_inv, parent_id, 
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
177
    revision_id, parent_invs, shagitmap, lookup_object):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
178
    """Import a git tree object into a bzr repository.
179
0.200.261 by Jelmer Vernooij
More formatting fixes.
180
    :param texts: VersionedFiles object to add to
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
181
    :param path: Path in the tree
182
    :param tree: A git tree object
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
183
    :param base_inv: Base inventory against which to return inventory delta
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
184
    :return: Inventory delta for this subtree
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
185
    """
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
186
    invdelta = []
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
187
    file_id = mapping.generate_file_id(path)
0.200.297 by Jelmer Vernooij
Cope with non-ascii characters in filenames (needs a test..).
188
    # We just have to hope this is indeed utf-8:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
189
    ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")), 
190
        parent_id)
0.200.375 by Jelmer Vernooij
Store object hex sha's in InventoryEntry.text_id during fetch.
191
    ie.text_id = hexsha
0.200.374 by Jelmer Vernooij
Also avoid lookups in the inventory in the tree fetch functions.
192
    try:
193
        base_ie = base_inv[file_id]
194
    except NoSuchId:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
195
        # Newly appeared here
0.200.374 by Jelmer Vernooij
Also avoid lookups in the inventory in the tree fetch functions.
196
        base_ie = None
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
197
        ie.revision = revision_id
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
198
        texts.add_lines((file_id, ie.revision), (), [])
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
199
        invdelta.append((None, path, file_id, ie))
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
200
    else:
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
201
        base_sha = base_ie.text_id
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
202
        # See if this has changed at all
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
203
        try:
0.200.378 by Jelmer Vernooij
Simplify checks against base hex sha a bit.
204
            if base_sha is None:
205
                base_sha = shagitmap.lookup_tree(file_id, base_inv.revision_id)
0.200.287 by Jelmer Vernooij
Skip tree sha's already in the git sha map.
206
        except KeyError:
207
            pass
0.200.346 by Jelmer Vernooij
Track unusual file modes.
208
        else:
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
209
            if base_sha == hexsha:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
210
                # If nothing has changed since the base revision, we're done
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
211
                return [], {}, []
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
212
    # Remember for next time
0.200.300 by Jelmer Vernooij
Fix recursive deletion of dirs.
213
    existing_children = set()
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
214
    child_modes = {}
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
215
    shamap = []
0.200.304 by Jelmer Vernooij
Try a bit harder to avoid fetching objects we don't need.
216
    tree = lookup_object(hexsha)
0.200.363 by Jelmer Vernooij
Fix updates of sha map during fetch.
217
    for mode, name, child_hexsha in tree.entries():
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
218
        basename = name.decode("utf-8")
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
219
        existing_children.add(basename)
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
220
        child_path = osutils.pathjoin(path, name)
0.200.352 by Jelmer Vernooij
Simplify mode handling.
221
        if stat.S_ISDIR(mode):
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
222
            subinvdelta, grandchildmodes, subshamap = import_git_tree(texts, 
0.200.363 by Jelmer Vernooij
Fix updates of sha map during fetch.
223
                    mapping, child_path, child_hexsha, base_inv, file_id, 
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
224
                    revision_id, parent_invs, shagitmap, lookup_object)
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
225
            invdelta.extend(subinvdelta)
0.200.345 by Jelmer Vernooij
Keep track of file modes to use.
226
            child_modes.update(grandchildmodes)
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
227
            shamap.extend(subshamap)
0.200.540 by Jelmer Vernooij
Handle submodules explicitly.
228
        elif S_ISGITLINK(mode): # submodule
229
            subinvdelta, grandchildmodes, subshamap = import_git_submodule(
230
                    texts, mapping, child_path, child_hexsha, base_inv,
231
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
232
            invdelta.extend(subinvdelta)
233
            child_modes.update(grandchildmodes)
234
            shamap.extend(subshamap)
0.200.352 by Jelmer Vernooij
Simplify mode handling.
235
        else:
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
236
            subinvdelta, subshamap = import_git_blob(texts, mapping, 
0.200.363 by Jelmer Vernooij
Fix updates of sha map during fetch.
237
                    child_path, child_hexsha, base_inv, file_id, revision_id, 
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
238
                    parent_invs, shagitmap, lookup_object, 
0.200.521 by Jelmer Vernooij
Abstract out kind mapping a bit, initial work on support tree-references.
239
                    mode_is_executable(mode), stat.S_ISLNK(mode))
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
240
            invdelta.extend(subinvdelta)
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
241
            shamap.extend(subshamap)
0.200.359 by Jelmer Vernooij
Simplify file mode handling, avoid inventory_to_tree_and_blobs as it is expensive if trees/blobs have already been converted.
242
        if mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
243
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0111):
0.200.352 by Jelmer Vernooij
Simplify mode handling.
244
            child_modes[child_path] = mode
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
245
    # Remove any children that have disappeared
0.200.484 by Jelmer Vernooij
Cope with kind changes.
246
    if base_ie is not None and base_ie.kind == 'directory':
0.200.374 by Jelmer Vernooij
Also avoid lookups in the inventory in the tree fetch functions.
247
        deletable = [v for k,v in base_ie.children.iteritems() if k not in existing_children]
0.200.300 by Jelmer Vernooij
Fix recursive deletion of dirs.
248
        while deletable:
249
            ie = deletable.pop()
0.200.344 by Jelmer Vernooij
Clarify names, use convenience function
250
            invdelta.append((base_inv.id2path(ie.file_id), None, ie.file_id, None))
0.200.300 by Jelmer Vernooij
Fix recursive deletion of dirs.
251
            if ie.kind == "directory":
252
                deletable.extend(ie.children.values())
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
253
    shamap.append((hexsha, "tree", (file_id, revision_id)))
254
    return invdelta, child_modes, shamap
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
255
256
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
257
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever, 
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
258
        heads, pb=None):
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
259
    """Import a set of git objects into a bzr repository.
260
0.200.483 by Jelmer Vernooij
Add NEWS entry about sha map.
261
    :param repo: Target Bazaar repository
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
262
    :param mapping: Mapping to use
263
    :param object_iter: Iterator over Git objects.
264
    """
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
265
    def lookup_object(sha):
266
        try:
267
            return object_iter[sha]
268
        except KeyError:
269
            return target_git_object_retriever[sha]
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
270
    # TODO: a more (memory-)efficient implementation of this
0.200.158 by Jelmer Vernooij
fetch works \o/
271
    graph = []
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
272
    root_trees = {}
0.200.158 by Jelmer Vernooij
fetch works \o/
273
    revisions = {}
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
274
    checked = set()
275
    heads = list(heads)
0.200.301 by Jelmer Vernooij
Cache inventories created.
276
    parent_invs_cache = LRUCache(50)
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
277
    # Find and convert commit objects
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
278
    while heads:
279
        if pb is not None:
280
            pb.update("finding revisions to fetch", len(graph), None)
281
        head = heads.pop()
282
        assert isinstance(head, str)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
283
        try:
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
284
            o = lookup_object(head)
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
285
        except KeyError:
286
            continue
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
287
        if isinstance(o, Commit):
288
            rev = mapping.import_commit(o)
0.200.295 by Jelmer Vernooij
Don't re-import revisions already fetched.
289
            if repo.has_revision(rev.revision_id):
290
                continue
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
291
            root_trees[rev.revision_id] = o.tree
0.200.158 by Jelmer Vernooij
fetch works \o/
292
            revisions[rev.revision_id] = rev
293
            graph.append((rev.revision_id, rev.parent_ids))
0.200.372 by Jelmer Vernooij
Fix key when looking up old sha's in cache.
294
            target_git_object_retriever._idmap.add_entry(o.id, "commit", 
295
                    (rev.revision_id, o.tree))
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
296
            heads.extend([p for p in o.parents if p not in checked])
0.200.303 by Jelmer Vernooij
Cope with tags during fetch.
297
        elif isinstance(o, Tag):
298
            heads.append(o.object[1])
0.200.296 by Jelmer Vernooij
Avoid iterating over all objects just to find the *Commits* to retrieve.
299
        else:
300
            trace.warning("Unable to import head object %r" % o)
301
        checked.add(head)
0.200.158 by Jelmer Vernooij
fetch works \o/
302
    # Order the revisions
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
303
    # Create the inventory objects
0.200.158 by Jelmer Vernooij
fetch works \o/
304
    for i, revid in enumerate(topo_sort(graph)):
305
        if pb is not None:
306
            pb.update("fetching revisions", i, len(graph))
307
        rev = revisions[revid]
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
308
        # We have to do this here, since we have to walk the tree and 
0.200.295 by Jelmer Vernooij
Don't re-import revisions already fetched.
309
        # we need to make sure to import the blobs / trees with the right 
0.200.151 by Jelmer Vernooij
Support converting git objects to bzr objects.
310
        # path; this may involve adding them more than once.
0.200.301 by Jelmer Vernooij
Cache inventories created.
311
        parent_invs = []
312
        for parent_id in rev.parent_ids:
313
            try:
314
                parent_invs.append(parent_invs_cache[parent_id])
315
            except KeyError:
316
                parent_inv = repo.get_inventory(parent_id)
317
                parent_invs.append(parent_inv)
318
                parent_invs_cache[parent_id] = parent_inv
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
319
        if parent_invs == []:
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
320
            base_inv = Inventory(root_id=None)
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
321
        else:
322
            base_inv = parent_invs[0]
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
323
        inv_delta, unusual_modes, shamap = import_git_tree(repo.texts, 
324
                mapping, "", root_trees[revid], base_inv, None, revid, 
325
                parent_invs, target_git_object_retriever._idmap, lookup_object)
326
        target_git_object_retriever._idmap.add_entries(shamap)
0.200.346 by Jelmer Vernooij
Track unusual file modes.
327
        if unusual_modes != {}:
0.200.490 by Jelmer Vernooij
Warn about unusual modes and escaped XML-invalid characters.
328
            for path, mode in unusual_modes.iteritems():
329
                warn_unusual_mode(rev.foreign_revid, path, mode)
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
330
        try:
331
            basis_id = rev.parent_ids[0]
332
        except IndexError:
333
            basis_id = NULL_REVISION
0.229.3 by Jelmer Vernooij
Use inventory deltas internally so fetch is O(changes) rather than O(tree).
334
        rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
0.229.2 by Jelmer Vernooij
Initial work relying on inventory deltas.
335
                  inv_delta, rev.revision_id, rev.parent_ids)
0.200.301 by Jelmer Vernooij
Cache inventories created.
336
        parent_invs_cache[rev.revision_id] = inv
0.229.1 by Jelmer Vernooij
Start working with inventory deltas.
337
        repo.add_revision(rev.revision_id, rev)
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
338
        if "verify" in debug.debug_flags:
339
            objs = inventory_to_tree_and_blobs(inv, repo.texts, mapping)
340
            for sha1, newobj, path in objs:
341
                assert path is not None
0.200.361 by Jelmer Vernooij
Fix existing object lookup issues when pulling from remote branches.
342
                oldobj = tree_lookup_path(lookup_object, root_trees[revid], path)
0.200.355 by Jelmer Vernooij
Allow paranoia checking with -Dverify.
343
                assert oldobj == newobj, "%r != %r in %s" % (oldobj, newobj, path)
344
0.200.272 by Jelmer Vernooij
Actually store idmap.
345
    target_git_object_retriever._idmap.commit()
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
346
347
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
348
class InterGitRepository(InterRepository):
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
349
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
350
    _matching_repo_format = GitRepositoryFormat()
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
351
352
    @staticmethod
353
    def _get_repo_format_to_test():
354
        return None
355
0.200.135 by Jelmer Vernooij
Add stub for fetching data.
356
    def copy_content(self, revision_id=None, pb=None):
357
        """See InterRepository.copy_content."""
358
        self.fetch(revision_id, pb, find_ghosts=False)
359
0.200.295 by Jelmer Vernooij
Don't re-import revisions already fetched.
360
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, mapping=None,
361
            fetch_spec=None):
0.200.247 by Jelmer Vernooij
Fix git-import.
362
        self.fetch_refs(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts,
363
                mapping=mapping, fetch_spec=fetch_spec)
364
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
365
366
class InterGitNonGitRepository(InterGitRepository):
367
    """Base InterRepository that copies revisions from a Git into a non-Git 
368
    repository."""
369
0.200.247 by Jelmer Vernooij
Fix git-import.
370
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False, 
371
              mapping=None, fetch_spec=None):
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
372
        if mapping is None:
373
            mapping = self.source.get_mapping()
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
374
        if revision_id is not None:
375
            interesting_heads = [revision_id]
376
        elif fetch_spec is not None:
377
            interesting_heads = fetch_spec.heads
378
        else:
379
            interesting_heads = None
0.200.247 by Jelmer Vernooij
Fix git-import.
380
        self._refs = {}
381
        def determine_wants(refs):
382
            self._refs = refs
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
383
            if interesting_heads is None:
0.200.247 by Jelmer Vernooij
Fix git-import.
384
                ret = [sha for (ref, sha) in refs.iteritems() if not ref.endswith("^{}")]
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
385
            else:
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
386
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
387
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
0.200.247 by Jelmer Vernooij
Fix git-import.
388
        self.fetch_objects(determine_wants, mapping, pb)
389
        return self._refs
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
390
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
391
392
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
393
    """InterRepository that copies revisions from a remote Git into a non-Git 
394
    repository."""
395
396
    def fetch_objects(self, determine_wants, mapping, pb=None):
397
        def progress(text):
398
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
0.200.466 by Jelmer Vernooij
Fix finding of heads for fetch_objects.
399
        store = BazaarObjectStore(self.target, mapping)
0.200.484 by Jelmer Vernooij
Cope with kind changes.
400
        self.target.lock_write()
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
401
        try:
402
            heads = self.target.get_graph().heads(self.target.all_revision_ids())
0.200.484 by Jelmer Vernooij
Cope with kind changes.
403
            graph_walker = store.get_graph_walker(
404
                    [store._lookup_revision_sha1(head) for head in heads])
405
            recorded_wants = []
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
406
0.200.484 by Jelmer Vernooij
Cope with kind changes.
407
            def record_determine_wants(heads):
408
                wants = determine_wants(heads)
409
                recorded_wants.extend(wants)
410
                return wants
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
411
        
0.200.484 by Jelmer Vernooij
Cope with kind changes.
412
            create_pb = None
413
            if pb is None:
414
                create_pb = pb = ui.ui_factory.nested_progress_bar()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
415
            try:
416
                self.target.start_write_group()
417
                try:
418
                    objects_iter = self.source.fetch_objects(
0.200.469 by Jelmer Vernooij
Fix fetch when revisions are already present locally, just only mapped.
419
                                record_determine_wants, graph_walker, 
420
                                store.get_raw, progress)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
421
                    import_git_objects(self.target, mapping, objects_iter, 
0.200.466 by Jelmer Vernooij
Fix finding of heads for fetch_objects.
422
                            store, recorded_wants, pb)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
423
                finally:
424
                    self.target.commit_write_group()
425
            finally:
0.200.484 by Jelmer Vernooij
Cope with kind changes.
426
                if create_pb:
427
                    create_pb.finished()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
428
        finally:
0.200.484 by Jelmer Vernooij
Cope with kind changes.
429
            self.target.unlock()
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
430
431
    @staticmethod
432
    def is_compatible(source, target):
433
        """Be compatible with GitRepository."""
434
        # FIXME: Also check target uses VersionedFile
435
        return (isinstance(source, RemoteGitRepository) and 
436
                target.supports_rich_root() and
437
                not isinstance(target, GitRepository))
438
439
440
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
0.200.496 by Roland Mas
Fix missing import.
441
    """InterRepository that copies revisions from a local Git into a non-Git 
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
442
    repository."""
443
444
    def fetch_objects(self, determine_wants, mapping, pb=None):
445
        wants = determine_wants(self.source._git.get_refs())
446
        create_pb = None
447
        if pb is None:
448
            create_pb = pb = ui.ui_factory.nested_progress_bar()
0.200.320 by Jelmer Vernooij
Handle lightweight checkouts.
449
        target_git_object_retriever = BazaarObjectStore(self.target, mapping)
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
450
        try:
451
            self.target.lock_write()
452
            try:
453
                self.target.start_write_group()
454
                try:
455
                    import_git_objects(self.target, mapping, 
456
                            self.source._git.object_store, 
457
                            target_git_object_retriever, wants, pb)
458
                finally:
459
                    self.target.commit_write_group()
460
            finally:
461
                self.target.unlock()
462
        finally:
463
            if create_pb:
464
                create_pb.finished()
465
466
    @staticmethod
467
    def is_compatible(source, target):
468
        """Be compatible with GitRepository."""
469
        # FIXME: Also check target uses VersionedFile
470
        return (isinstance(source, LocalGitRepository) and 
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
471
                target.supports_rich_root() and
472
                not isinstance(target, GitRepository))
473
474
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
475
class InterGitGitRepository(InterGitRepository):
0.200.291 by Jelmer Vernooij
Print proper error about not supporting push.
476
    """InterRepository that copies between Git repositories."""
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
477
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
478
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False, 
479
              mapping=None, fetch_spec=None, branches=None):
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
480
        if mapping is None:
481
            mapping = self.source.get_mapping()
482
        def progress(text):
0.200.261 by Jelmer Vernooij
More formatting fixes.
483
            trace.info("git: %s", text)
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
484
        r = self.target._git
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
485
        if revision_id is not None:
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
486
            args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
487
        elif fetch_spec is not None:
488
            args = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in fetch_spec.heads]
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
489
        if branches is not None:
490
            determine_wants = lambda x: [x[y] for y in branches if not x[y] in r.object_store]
491
        elif fetch_spec is None and revision_id is None:
0.200.247 by Jelmer Vernooij
Fix git-import.
492
            determine_wants = r.object_store.determine_wants_all
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
493
        else:
0.200.247 by Jelmer Vernooij
Fix git-import.
494
            determine_wants = lambda x: [y for y in args if not y in r.object_store]
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
495
0.200.463 by Jelmer Vernooij
Support remote dpush (except for references).
496
        graphwalker = r.get_graph_walker()
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
497
        f, commit = r.object_store.add_thin_pack()
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
498
        try:
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
499
            refs = self.source.fetch_pack(determine_wants, graphwalker,
500
                                          f.write, progress)
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
501
            commit()
0.200.456 by Jelmer Vernooij
Fix git -> git fetching.
502
            return refs
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
503
        except:
504
            f.close()
505
            raise
506
507
    @staticmethod
508
    def is_compatible(source, target):
509
        """Be compatible with GitRepository."""
510
        return (isinstance(source, GitRepository) and 
511
                isinstance(target, GitRepository))