/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to fetch.py

  • Committer: Jelmer Vernooij
  • Date: 2009-09-10 13:13:15 UTC
  • mto: (0.200.602 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090910131315-6890xg58pl2jseml
Allow serving remote URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from cStringIO import (
18
 
    StringIO,
19
 
    )
20
 
import dulwich as git
21
17
from dulwich.objects import (
22
18
    Commit,
23
19
    Tag,
26
22
from dulwich.object_store import (
27
23
    tree_lookup_path,
28
24
    )
 
25
import re
29
26
import stat
30
27
 
31
28
from bzrlib import (
36
33
    urlutils,
37
34
    )
38
35
from bzrlib.errors import (
39
 
    InvalidRevisionId,
 
36
    BzrError,
40
37
    NoSuchId,
41
 
    NoSuchRevision,
42
38
    )
43
39
from bzrlib.inventory import (
44
40
    Inventory,
67
63
    inventory_to_tree_and_blobs,
68
64
    mode_is_executable,
69
65
    squash_revision,
70
 
    text_to_blob,
71
66
    warn_unusual_mode,
72
67
    )
73
68
from bzrlib.plugins.git.object_store import (
83
78
    )
84
79
 
85
80
 
86
 
def import_git_blob(texts, mapping, path, hexsha, base_inv, parent_id, 
 
81
def import_git_blob(texts, mapping, path, hexsha, base_inv, base_ie, parent_id, 
87
82
    revision_id, parent_invs, shagitmap, lookup_object, executable, symlink):
88
83
    """Import a git blob object into a bzr repository.
89
84
 
101
96
    ie = cls(file_id, urlutils.basename(path).decode("utf-8"), parent_id)
102
97
    ie.executable = executable
103
98
    # See if this has changed at all
104
 
    try:
105
 
        base_ie = base_inv[file_id]
106
 
    except NoSuchId:
107
 
        base_ie = None
 
99
    if base_ie is None:
108
100
        base_sha = None
109
101
    else:
110
102
        try:
160
152
        shamap = [(hexsha, "blob", (ie.file_id, ie.revision))]
161
153
    else:
162
154
        shamap = []
163
 
    if file_id in base_inv:
 
155
    invdelta = []
 
156
    if base_ie is not None: 
164
157
        old_path = base_inv.id2path(file_id)
 
158
        if base_ie.kind == "directory":
 
159
            invdelta.extend(remove_disappeared_children(old_path, base_ie.children, []))
165
160
    else:
166
161
        old_path = None
167
 
    invdelta = [(old_path, path, file_id, ie)]
168
 
    invdelta.extend(remove_disappeared_children(base_inv, base_ie, []))
 
162
    invdelta.append((old_path, path, file_id, ie))
169
163
    return (invdelta, shamap)
170
164
 
171
165
 
172
 
def import_git_submodule(texts, mapping, path, hexsha, base_inv, parent_id, 
173
 
    revision_id, parent_invs, shagitmap, lookup_object):
174
 
    raise NotImplementedError(import_git_submodule)
175
 
 
176
 
 
177
 
def remove_disappeared_children(base_inv, base_ie, existing_children):
178
 
    if base_ie is None or base_ie.kind != 'directory':
179
 
        return []
 
166
class SubmodulesNotSupported(BzrError):
 
167
 
 
168
    _fmt = """Submodules can not yet be imported (requires nested tree support in Bazaar)."""
 
169
    internal = False
 
170
 
 
171
 
 
172
def import_git_submodule(texts, mapping, path, hexsha, base_inv, base_ie, 
 
173
    parent_id, revision_id, parent_invs, shagitmap, lookup_object):
 
174
    raise SubmodulesNotSupported()
 
175
 
 
176
 
 
177
def remove_disappeared_children(path, base_children, existing_children):
180
178
    ret = []
181
 
    deletable = [v for k,v in base_ie.children.iteritems() if k not in existing_children]
 
179
    deletable = [(osutils.pathjoin(path, k), v) for k,v in base_children.iteritems() if k not in existing_children]
182
180
    while deletable:
183
 
        ie = deletable.pop()
184
 
        ret.append((base_inv.id2path(ie.file_id), None, ie.file_id, None))
 
181
        (path, ie) = deletable.pop()
 
182
        ret.append((path, None, ie.file_id, None))
185
183
        if ie.kind == "directory":
186
 
            deletable.extend(ie.children.values())
 
184
            for name, child_ie in ie.children.iteritems():
 
185
                deletable.append((osutils.pathjoin(path, name), child_ie))
187
186
    return ret
188
187
 
189
188
 
190
 
def import_git_tree(texts, mapping, path, hexsha, base_inv, parent_id, 
 
189
def import_git_tree(texts, mapping, path, hexsha, base_inv, base_ie, parent_id, 
191
190
    revision_id, parent_invs, shagitmap, lookup_object):
192
191
    """Import a git tree object into a bzr repository.
193
192
 
202
201
    # We just have to hope this is indeed utf-8:
203
202
    ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")), 
204
203
        parent_id)
205
 
    try:
206
 
        base_ie = base_inv[file_id]
207
 
    except NoSuchId:
 
204
    if base_ie is None:
208
205
        # Newly appeared here
209
 
        base_ie = None
210
206
        ie.revision = revision_id
211
 
        texts.add_lines((file_id, ie.revision), (), [])
 
207
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), (), None, "")])
212
208
        invdelta.append((None, path, file_id, ie))
213
209
    else:
214
210
        # See if this has changed at all
222
218
                return [], {}, []
223
219
        if base_ie.kind != "directory":
224
220
            ie.revision = revision_id
225
 
            texts.add_lines((ie.file_id, ie.revision), (), [])
 
221
            texts.insert_record_stream([FulltextContentFactory((ie.file_id, ie.revision), (), None, "")])
226
222
            invdelta.append((base_inv.id2path(ie.file_id), path, ie.file_id, ie))
 
223
    if base_ie is not None and base_ie.kind == "directory":
 
224
        base_children = base_ie.children
 
225
    else:
 
226
        base_children = {}
227
227
    # Remember for next time
228
228
    existing_children = set()
229
229
    child_modes = {}
236
236
        if stat.S_ISDIR(mode):
237
237
            subinvdelta, grandchildmodes, subshamap = import_git_tree(
238
238
                    texts, mapping, child_path, child_hexsha, base_inv, 
239
 
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
 
239
                    base_children.get(basename), file_id, revision_id, parent_invs, shagitmap,
 
240
                    lookup_object)
240
241
            invdelta.extend(subinvdelta)
241
242
            child_modes.update(grandchildmodes)
242
243
            shamap.extend(subshamap)
243
244
        elif S_ISGITLINK(mode): # submodule
244
245
            subinvdelta, grandchildmodes, subshamap = import_git_submodule(
245
 
                    texts, mapping, child_path, child_hexsha, base_inv,
 
246
                    texts, mapping, child_path, child_hexsha, base_inv, base_children.get(basename),
246
247
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
247
248
            invdelta.extend(subinvdelta)
248
249
            child_modes.update(grandchildmodes)
249
250
            shamap.extend(subshamap)
250
251
        else:
251
252
            subinvdelta, subshamap = import_git_blob(texts, mapping, 
252
 
                    child_path, child_hexsha, base_inv, file_id, revision_id, 
253
 
                    parent_invs, shagitmap, lookup_object, 
 
253
                    child_path, child_hexsha, base_inv, base_children.get(basename), file_id,
 
254
                    revision_id, parent_invs, shagitmap, lookup_object, 
254
255
                    mode_is_executable(mode), stat.S_ISLNK(mode))
255
256
            invdelta.extend(subinvdelta)
256
257
            shamap.extend(subshamap)
258
259
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0111):
259
260
            child_modes[child_path] = mode
260
261
    # Remove any children that have disappeared
261
 
    invdelta.extend(remove_disappeared_children(base_inv, base_ie, existing_children))
 
262
    if base_ie is not None and base_ie.kind == "directory":
 
263
        invdelta.extend(remove_disappeared_children(base_inv.id2path(file_id), 
 
264
            base_children, existing_children))
262
265
    shamap.append((hexsha, "tree", (file_id, revision_id)))
263
266
    return invdelta, child_modes, shamap
264
267
 
292
295
        try:
293
296
            o = lookup_object(head)
294
297
        except KeyError:
 
298
            trace.mutter('missing head %s', head)
295
299
            continue
296
300
        if isinstance(o, Commit):
297
301
            rev = mapping.import_commit(o)
328
332
                parent_invs_cache[parent_id] = parent_inv
329
333
        if parent_invs == []:
330
334
            base_inv = Inventory(root_id=None)
 
335
            base_ie = None
331
336
        else:
332
337
            base_inv = parent_invs[0]
 
338
            base_ie = base_inv.root
333
339
        inv_delta, unusual_modes, shamap = import_git_tree(repo.texts, 
334
 
                mapping, "", root_trees[revid], base_inv, None, revid, 
 
340
                mapping, "", root_trees[revid], base_inv, base_ie, None, revid, 
335
341
                parent_invs, target_git_object_retriever._idmap, lookup_object)
336
342
        target_git_object_retriever._idmap.add_entries(shamap)
337
343
        if unusual_modes != {}:
400
406
            else:
401
407
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
402
408
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
403
 
        self.fetch_objects(determine_wants, mapping, pb)
 
409
        pack_hint = self.fetch_objects(determine_wants, mapping, pb)
 
410
        if pack_hint is not None and self.target._format.pack_compresses:
 
411
            self.target.pack(hint=pack_hint)
 
412
        if interesting_heads is not None:
 
413
            present_interesting_heads = self.target.has_revisions(interesting_heads)
 
414
            missing_interesting_heads = set(interesting_heads) - present_interesting_heads
 
415
            if missing_interesting_heads:
 
416
                raise AssertionError("Missing interesting heads: %r" % missing_interesting_heads)
404
417
        return self._refs
405
418
 
406
419
 
 
420
_GIT_PROGRESS_RE = re.compile(r"(.*?): +(\d+)% \((\d+)/(\d+)\)")
 
421
def report_git_progress(pb, text):
 
422
    text = text.rstrip("\r\n")
 
423
    g = _GIT_PROGRESS_RE.match(text)
 
424
    if g is not None:
 
425
        (text, pct, current, total) = g.groups()
 
426
        pb.update(text, int(current), int(total))
 
427
    else:
 
428
        pb.update(text, 0, 0)
 
429
 
 
430
 
407
431
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
408
432
    """InterRepository that copies revisions from a remote Git into a non-Git 
409
433
    repository."""
410
434
 
 
435
    def get_target_heads(self):
 
436
        # FIXME: This should be more efficient
 
437
        all_revs = self.target.all_revision_ids()
 
438
        parent_map = self.target.get_parent_map(all_revs)
 
439
        all_parents = set()
 
440
        map(all_parents.update, parent_map.itervalues())
 
441
        return set(all_revs) - all_parents
 
442
 
411
443
    def fetch_objects(self, determine_wants, mapping, pb=None):
412
444
        def progress(text):
413
 
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
 
445
            report_git_progress(pb, text)
414
446
        store = BazaarObjectStore(self.target, mapping)
415
447
        self.target.lock_write()
416
448
        try:
417
 
            heads = self.target.get_graph().heads(self.target.all_revision_ids())
 
449
            heads = self.get_target_heads()
418
450
            graph_walker = store.get_graph_walker(
419
451
                    [store._lookup_revision_sha1(head) for head in heads])
420
452
            recorded_wants = []
436
468
                    import_git_objects(self.target, mapping, objects_iter, 
437
469
                            store, recorded_wants, pb)
438
470
                finally:
439
 
                    self.target.commit_write_group()
 
471
                    pack_hint = self.target.commit_write_group()
 
472
                return pack_hint
440
473
            finally:
441
474
                if create_pb:
442
475
                    create_pb.finished()
471
504
                            self.source._git.object_store, 
472
505
                            target_git_object_retriever, wants, pb)
473
506
                finally:
474
 
                    self.target.commit_write_group()
 
507
                    pack_hint = self.target.commit_write_group()
 
508
                return pack_hint
475
509
            finally:
476
510
                self.target.unlock()
477
511
        finally: