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

Print proper error about not supporting push.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
16
17
 
17
18
"""An adapter between a Git Repository and a Bazaar Branch"""
18
19
 
19
 
import os
20
 
import time
21
 
 
22
20
import bzrlib
23
21
from bzrlib import (
24
 
    deprecated_graph,
25
22
    errors,
26
23
    graph,
27
24
    inventory,
29
26
    repository,
30
27
    revision,
31
28
    revisiontree,
 
29
    ui,
32
30
    urlutils,
33
 
    versionedfile,
34
31
    )
35
32
from bzrlib.foreign import (
36
33
        ForeignRepository,
41
38
from bzrlib.plugins.git.foreign import (
42
39
    versionedfiles,
43
40
    )
44
 
from bzrlib.plugins.git.mapping import default_mapping
 
41
from bzrlib.plugins.git.mapping import (
 
42
    default_mapping,
 
43
    inventory_to_tree_and_blobs,
 
44
    mapping_registry,
 
45
    revision_to_commit,
 
46
    )
 
47
from bzrlib.plugins.git.versionedfiles import GitTexts
45
48
 
46
 
from bzrlib.plugins.git import git
 
49
import dulwich as git
 
50
import os
 
51
import time
47
52
 
48
53
 
49
54
class GitTags(object):
61
66
    _serializer = None
62
67
 
63
68
    def __init__(self, gitdir, lockfiles):
64
 
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
65
 
        from bzrlib.plugins.git import fetch
66
 
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
 
69
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
 
70
            lockfiles)
 
71
        from bzrlib.plugins.git import fetch, push
 
72
        for optimiser in [fetch.InterGitRepository, 
 
73
                          fetch.InterGitNonGitRepository,
 
74
                          push.InterToGitRepository]:
 
75
            repository.InterRepository.register_optimiser(optimiser)
67
76
 
68
77
    def is_shared(self):
69
78
        return True
83
92
 
84
93
 
85
94
class LocalGitRepository(GitRepository):
 
95
    """Git repository on the file system."""
86
96
 
87
97
    def __init__(self, gitdir, lockfiles):
88
98
        # FIXME: This also caches negatives. Need to be more careful 
94
104
        self.texts = None
95
105
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
96
106
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
 
107
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
108
        self.texts = GitTexts(self)
97
109
        self.tags = GitTags(self._git.get_tags())
98
110
 
99
111
    def all_revision_ids(self):
100
112
        ret = set([revision.NULL_REVISION])
101
 
        if self._git.heads() == []:
 
113
        heads = self._git.heads()
 
114
        if heads == {}:
102
115
            return ret
103
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in self._git.heads()]
 
116
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
104
117
        ret = set(bzr_heads)
105
118
        graph = self.get_graph()
106
119
        for rev, parents in graph.iter_ancestry(bzr_heads):
124
137
            if revision_id == revision.NULL_REVISION:
125
138
                parent_map[revision_id] = ()
126
139
                continue
127
 
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
 
140
            hexsha, mapping = self.lookup_git_revid(revision_id)
128
141
            commit  = self._git.commit(hexsha)
129
142
            if commit is None:
130
143
                continue
131
144
            else:
132
 
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
 
145
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
133
146
        return parent_map
134
147
 
135
148
    def get_ancestry(self, revision_id, topo_sorted=True):
136
149
        """See Repository.get_ancestry().
137
150
        """
138
151
        if revision_id is None:
139
 
            return self._all_revision_ids()
 
152
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
140
153
        assert isinstance(revision_id, str)
141
154
        ancestry = []
142
155
        graph = self.get_graph()
143
156
        for rev, parents in graph.iter_ancestry([revision_id]):
144
 
            if rev == revision.NULL_REVISION:
145
 
                rev = None
146
157
            ancestry.append(rev)
147
158
        ancestry.reverse()
148
 
        return ancestry
 
159
        return [None] + ancestry
 
160
 
 
161
    def import_revision_gist(self, source, revid, parent_lookup):
 
162
        """Import the gist of a revision into this Git repository.
 
163
 
 
164
        """
 
165
        objects = []
 
166
        rev = source.get_revision(revid)
 
167
        for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
 
168
            if path == "":
 
169
                tree_sha = sha
 
170
            objects.append((object, path))
 
171
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
 
172
        objects.append((commit, None))
 
173
        self._git.object_store.add_objects(objects)
 
174
        return commit.sha().hexdigest()
 
175
 
 
176
    def dfetch(self, source, stop_revision):
 
177
        """Import the gist of the ancestry of a particular revision."""
 
178
        if stop_revision is None:
 
179
            raise NotImplementedError
 
180
        revidmap = {}
 
181
        gitidmap = {}
 
182
        def parent_lookup(revid):
 
183
            try:
 
184
                return gitidmap[revid]
 
185
            except KeyError:
 
186
                return self.lookup_git_revid(revid)[0]
 
187
        todo = []
 
188
        source.lock_write()
 
189
        try:
 
190
            graph = source.get_graph()
 
191
            ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
 
192
            for revid in graph.iter_topo_order(ancestry):
 
193
                if not self.has_revision(revid):
 
194
                    todo.append(revid)
 
195
            pb = ui.ui_factory.nested_progress_bar()
 
196
            try:
 
197
                for i, revid in enumerate(todo):
 
198
                    pb.update("pushing revisions", i, len(todo))
 
199
                    git_commit = self.import_revision_gist(source, revid,
 
200
                        parent_lookup)
 
201
                    gitidmap[revid] = git_commit
 
202
                    git_revid = self.get_mapping().revision_id_foreign_to_bzr(
 
203
                        git_commit)
 
204
                    revidmap[revid] = git_revid
 
205
            finally:
 
206
                pb.finished()
 
207
            source.fetch(self, revision_id=revidmap[stop_revision])
 
208
        finally:
 
209
            source.unlock()
 
210
        return revidmap
149
211
 
150
212
    def get_signature_text(self, revision_id):
151
213
        raise errors.NoSuchRevision(self, revision_id)
162
224
    def has_signature_for_revision_id(self, revision_id):
163
225
        return False
164
226
 
165
 
    def lookup_git_revid(self, bzr_revid, mapping):
 
227
    def lookup_git_revid(self, bzr_revid):
166
228
        try:
167
 
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
 
229
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
168
230
        except errors.InvalidRevisionId:
169
231
            raise errors.NoSuchRevision(self, bzr_revid)
170
232
 
171
233
    def get_revision(self, revision_id):
172
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
234
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
173
235
        try:
174
236
            commit = self._git.commit(git_commit_id)
175
237
        except KeyError:
176
238
            raise errors.NoSuchRevision(self, revision_id)
177
239
        # print "fetched revision:", git_commit_id
178
 
        revision = self.get_mapping().import_commit(commit)
 
240
        revision = mapping.import_commit(commit)
179
241
        assert revision is not None
180
242
        return revision
181
243
 
196
258
 
197
259
    def revision_tree(self, revision_id):
198
260
        revision_id = revision.ensure_null(revision_id)
199
 
 
200
261
        if revision_id == revision.NULL_REVISION:
201
262
            inv = inventory.Inventory(root_id=None)
202
263
            inv.revision_id = revision_id
203
264
            return revisiontree.RevisionTree(self, inv, revision_id)
204
 
 
205
 
        return GitRevisionTree(self, self.get_mapping(), revision_id)
 
265
        return GitRevisionTree(self, revision_id)
206
266
 
207
267
    def get_inventory(self, revision_id):
208
268
        assert revision_id != None
211
271
    def set_make_working_trees(self, trees):
212
272
        pass
213
273
 
214
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
274
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
275
        progress=None):
215
276
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
216
277
 
217
278
 
218
279
class GitRevisionTree(revisiontree.RevisionTree):
219
280
 
220
 
    def __init__(self, repository, mapping, revision_id):
 
281
    def __init__(self, repository, revision_id):
221
282
        self._repository = repository
222
283
        self.revision_id = revision_id
223
284
        assert isinstance(revision_id, str)
224
 
        self.mapping = mapping
225
 
        git_id = repository.lookup_git_revid(revision_id, self.mapping)
 
285
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
226
286
        try:
227
287
            commit = repository._git.commit(git_id)
228
288
        except KeyError, r:
278
338
                self._build_inventory(hexsha, child_ie, child_path)
279
339
 
280
340
 
281
 
class GitFormat(object):
 
341
class GitRepositoryFormat(repository.RepositoryFormat):
282
342
 
283
343
    supports_tree_reference = False
284
344
    rich_root_data = True