/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Push implementation that simply prints message saying push is not supported."""

from bzrlib import (
    ui,
    )
from bzrlib.repository import (
    InterRepository,
    )
from bzrlib.revision import (
    NULL_REVISION,
    )

from bzrlib.plugins.git.converter import (
    BazaarObjectStore,
    )
from bzrlib.plugins.git.errors import (
    NoPushSupport,
    )
from bzrlib.plugins.git.mapping import (
    inventory_to_tree_and_blobs,
    revision_to_commit,
    )
from bzrlib.plugins.git.repository import (
    GitRepository,
    GitRepositoryFormat,
    )


class MissingObjectsIterator(object):
    """Iterate over git objects that are missing from a target repository.

    """

    def __init__(self, source, mapping, pb=None):
        """Create a new missing objects iterator.

        """
        self.source = source
        self._object_store = BazaarObjectStore(self.source, mapping)
        self._revids = set()
        self._sent_shas = set()
        self._pending = []
        self.pb = pb

    def import_revisions(self, revids):
        self._revids.update(revids)
        for i, revid in enumerate(revids):
            if self.pb:
                self.pb.update("pushing revisions", i, len(revids))
            git_commit = self.import_revision(revid)
            yield (revid, git_commit)

    def need_sha(self, sha):
        if sha in self._sent_shas:
            return False
        (type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
        assert type in ("blob", "tree")
        if revid in self._revids:
            # Not sent yet, and part of the set of revisions to send
            return True
        # Not changed in the revisions to send, so either not necessary
        # or already present remotely (as git doesn't do ghosts)
        return False

    def queue(self, sha, obj, path, ie=None, inv=None):
        if obj is None:
            obj = (ie, inv)
        self._pending.append((obj, path))
        self._sent_shas.add(sha)

    def import_revision(self, revid):
        """Import the gist of a revision into this Git repository.

        """
        inv = self.source.get_inventory(revid)
        todo = [inv.root]
        tree_sha = None
        while todo:
            ie = todo.pop()
            (sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
            if ie.parent_id is None:
                tree_sha = sha
            if not self.need_sha(sha):
                continue
            self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
            if ie.kind == "directory":
                todo.extend(ie.children.values())
        assert tree_sha is not None
        commit = self._object_store._get_commit(revid, tree_sha)
        self.queue(commit.id, commit, None)
        return commit.id

    def __len__(self):
        return len(self._pending)

    def __iter__(self):
        for i, (object, path) in enumerate(self._pending):
            if self.pb:
                self.pb.update("writing pack objects", i, len(self))
            if isinstance(object, tuple):
                object = self._object_store._get_ie_object(*object)
            yield (object, path)   


class InterToGitRepository(InterRepository):
    """InterRepository that copies into a Git repository."""

    _matching_repo_format = GitRepositoryFormat()

    @staticmethod
    def _get_repo_format_to_test():
        return None

    def copy_content(self, revision_id=None, pb=None):
        """See InterRepository.copy_content."""
        self.fetch(revision_id, pb, find_ghosts=False)

    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
            fetch_spec=None):
        raise NoPushSupport()

    def missing_revisions(self, stop_revision):
        if stop_revision is None:
            raise NotImplementedError
        missing = []
        pb = ui.ui_factory.nested_progress_bar()
        try:
            graph = self.source.get_graph()
            for revid, _ in graph.iter_ancestry([stop_revision]):
                pb.update("determining revisions to fetch", len(missing))
                if not self.target.has_revision(revid):
                    missing.append(revid)
            return graph.iter_topo_order(missing)
        finally:
            pb.finished()

    def dfetch(self, stop_revision=None):
        """Import the gist of the ancestry of a particular revision."""
        revidmap = {}
        mapping = self.target.get_mapping()
        self.source.lock_read()
        try:
            todo = [revid for revid in self.missing_revisions(stop_revision) if revid != NULL_REVISION]
            pb = ui.ui_factory.nested_progress_bar()
            try:
                object_generator = MissingObjectsIterator(self.source, mapping, pb)
                for old_bzr_revid, git_commit in object_generator.import_revisions(
                    todo):
                    new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
                    revidmap[old_bzr_revid] = new_bzr_revid
                self.target._git.object_store.add_objects(object_generator) 
            finally:
                pb.finished()
        finally:
            self.source.unlock()
        return revidmap

    @staticmethod
    def is_compatible(source, target):
        """Be compatible with GitRepository."""
        return (not isinstance(source, GitRepository) and 
                isinstance(target, GitRepository))