/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 push.py

Reimplement dpush, but more efficient and only writing a single pack file rather than one per revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    InterRepository,
24
24
    )
25
25
 
 
26
from bzrlib.plugins.git.converter import (
 
27
    BazaarObjectStore,
 
28
    )
26
29
from bzrlib.plugins.git.errors import (
27
30
    NoPushSupport,
28
31
    )
35
38
    GitRepositoryFormat,
36
39
    )
37
40
 
 
41
 
 
42
class MissingObjectsIterator(object):
 
43
    """Iterate over git objects that are missing from a target repository.
 
44
 
 
45
    """
 
46
 
 
47
    def __init__(self, source, mapping):
 
48
        """Create a new missing objects iterator.
 
49
 
 
50
        """
 
51
        self.source = source
 
52
        self._object_store = BazaarObjectStore(self.source, mapping)
 
53
        self._revids = set()
 
54
        self._sent_shas = set()
 
55
        self._pending = []
 
56
 
 
57
    def import_revisions(self, revids):
 
58
        self._revids.update(revids)
 
59
        pb = ui.ui_factory.nested_progress_bar()
 
60
        try:
 
61
            for i, revid in enumerate(revids):
 
62
                pb.update("pushing revisions", i, len(revids))
 
63
                git_commit = self.import_revision(revid)
 
64
                yield (revid, git_commit)
 
65
        finally:
 
66
            pb.finished()
 
67
 
 
68
    def need_sha(self, sha):
 
69
        if sha in self._sent_shas:
 
70
            return False
 
71
        (type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
 
72
        assert type in ("blob", "tree")
 
73
        if revid in self._revids:
 
74
            # Not sent yet, and part of the set of revisions to send
 
75
            return True
 
76
        # Not changed in the revisions to send, so either not necessary
 
77
        # or already present remotely (as git doesn't do ghosts)
 
78
        return False
 
79
 
 
80
    def queue(self, sha, obj, path, ie=None, inv=None):
 
81
        if obj is None:
 
82
            obj = (ie, inv)
 
83
        self._pending.append((obj, path))
 
84
        self._sent_shas.add(sha)
 
85
 
 
86
    def import_revision(self, revid):
 
87
        """Import the gist of a revision into this Git repository.
 
88
 
 
89
        """
 
90
        inv = self.source.get_inventory(revid)
 
91
        todo = [inv.root]
 
92
        tree_sha = None
 
93
        while todo:
 
94
            ie = todo.pop()
 
95
            (sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
 
96
            if ie.parent_id is None:
 
97
                tree_sha = sha
 
98
            if not self.need_sha(sha):
 
99
                continue
 
100
            self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
 
101
            if ie.kind == "directory":
 
102
                todo.extend(ie.children.values())
 
103
        assert tree_sha is not None
 
104
        commit = self._object_store._get_commit(revid, tree_sha)
 
105
        self.queue(commit.id, commit, None)
 
106
        return commit.id
 
107
 
 
108
    def __len__(self):
 
109
        return len(self._pending)
 
110
 
 
111
    def __iter__(self):
 
112
        for (object, path) in self._pending:
 
113
            if isinstance(object, tuple):
 
114
                object = self._object_store._get_ie_object(*object)
 
115
            yield (object, path)   
 
116
 
 
117
 
38
118
class InterToGitRepository(InterRepository):
39
119
    """InterRepository that copies into a Git repository."""
40
120
 
52
132
            fetch_spec=None):
53
133
        raise NoPushSupport()
54
134
 
55
 
    def import_revision_gist(self, revid, parent_lookup):
56
 
        """Import the gist of a revision into this Git repository.
57
 
 
58
 
        """
59
 
        objects = []
60
 
        rev = self.source.get_revision(revid)
61
 
        for sha, object, path in inventory_to_tree_and_blobs(
62
 
                self.source.get_inventory(revid), self.source.texts, None):
63
 
            if path == "":
64
 
                tree_sha = sha
65
 
            objects.append((object, path))
66
 
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
67
 
        objects.append((commit, None))
68
 
        self.target._git.object_store.add_objects(objects)
69
 
        return commit.sha().hexdigest()
70
 
 
71
135
    def missing_revisions(self, stop_revision=None):
72
136
        if stop_revision is not None:
73
137
            ancestry = [x for x in self.source.get_ancestry(stop_revision) if x is not None]
83
147
    def dfetch(self, stop_revision=None):
84
148
        """Import the gist of the ancestry of a particular revision."""
85
149
        revidmap = {}
86
 
        gitidmap = {}
87
 
        def parent_lookup(revid):
88
 
            try:
89
 
                return gitidmap[revid]
90
 
            except KeyError:
91
 
                return self.target.lookup_git_revid(revid)[0]
92
150
        mapping = self.target.get_mapping()
93
151
        self.source.lock_write()
94
152
        try:
95
 
            todo = self.missing_revisions(stop_revision, ghosts=fetch_ghosts)
96
 
            pb = ui.ui_factory.nested_progress_bar()
97
 
            try:
98
 
                for i, revid in enumerate(todo):
99
 
                    pb.update("pushing revisions", i, len(todo))
100
 
                    git_commit = self.import_revision_gist(revid, parent_lookup)
101
 
                    gitidmap[revid] = git_commit
102
 
                    git_revid = mapping.revision_id_foreign_to_bzr(git_commit)
103
 
                    revidmap[revid] = git_revid
104
 
            finally:
105
 
                pb.finished()
 
153
            todo = self.missing_revisions(stop_revision)
 
154
            object_generator = MissingObjectsIterator(self.source, mapping)
 
155
            for old_bzr_revid, git_commit in object_generator.import_revisions(
 
156
                todo):
 
157
                new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
 
158
                revidmap[old_bzr_revid] = new_bzr_revid
 
159
            self.target._git.object_store.add_objects(object_generator) 
106
160
            if revidmap != {}:
107
161
                self.source.fetch(self.target, 
108
162
                        revision_id=revidmap[stop_revision])