/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

Support bzr.dev.

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
 
 
20
import dulwich as git
19
21
import os
20
22
import time
21
23
 
22
24
import bzrlib
23
25
from bzrlib import (
24
 
    deprecated_graph,
25
26
    errors,
26
27
    graph,
27
28
    inventory,
29
30
    repository,
30
31
    revision,
31
32
    revisiontree,
 
33
    ui,
32
34
    urlutils,
33
 
    versionedfile,
34
35
    )
35
36
from bzrlib.foreign import (
36
37
        ForeignRepository,
41
42
from bzrlib.plugins.git.foreign import (
42
43
    versionedfiles,
43
44
    )
44
 
from bzrlib.plugins.git.mapping import default_mapping
45
 
 
46
 
from bzrlib.plugins.git import git
 
45
from bzrlib.plugins.git.mapping import (
 
46
    default_mapping,
 
47
    inventory_to_tree_and_blobs,
 
48
    mapping_registry,
 
49
    revision_to_commit,
 
50
    )
 
51
from bzrlib.plugins.git.versionedfiles import GitTexts
47
52
 
48
53
 
49
54
class GitTags(object):
63
68
    def __init__(self, gitdir, lockfiles):
64
69
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
65
70
        from bzrlib.plugins.git import fetch
66
 
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
 
71
        for optimiser in [fetch.InterGitRepository, 
 
72
                          fetch.InterGitNonGitRepository]:
 
73
            repository.InterRepository.register_optimiser(optimiser)
67
74
 
68
75
    def is_shared(self):
69
76
        return True
83
90
 
84
91
 
85
92
class LocalGitRepository(GitRepository):
 
93
    """Git repository on the file system."""
86
94
 
87
95
    def __init__(self, gitdir, lockfiles):
88
96
        # FIXME: This also caches negatives. Need to be more careful 
94
102
        self.texts = None
95
103
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
96
104
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
 
105
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
106
        self.texts = GitTexts(self)
97
107
        self.tags = GitTags(self._git.get_tags())
98
108
 
99
109
    def all_revision_ids(self):
100
110
        ret = set([revision.NULL_REVISION])
101
 
        if self._git.heads() == []:
 
111
        heads = self._git.heads()
 
112
        if heads == {}:
102
113
            return ret
103
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in self._git.heads()]
 
114
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
104
115
        ret = set(bzr_heads)
105
116
        graph = self.get_graph()
106
117
        for rev, parents in graph.iter_ancestry(bzr_heads):
124
135
            if revision_id == revision.NULL_REVISION:
125
136
                parent_map[revision_id] = ()
126
137
                continue
127
 
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
 
138
            hexsha, mapping = self.lookup_git_revid(revision_id)
128
139
            commit  = self._git.commit(hexsha)
129
140
            if commit is None:
130
141
                continue
131
142
            else:
132
 
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
 
143
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
133
144
        return parent_map
134
145
 
135
146
    def get_ancestry(self, revision_id, topo_sorted=True):
136
147
        """See Repository.get_ancestry().
137
148
        """
138
149
        if revision_id is None:
139
 
            return self._all_revision_ids()
 
150
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
140
151
        assert isinstance(revision_id, str)
141
152
        ancestry = []
142
153
        graph = self.get_graph()
143
154
        for rev, parents in graph.iter_ancestry([revision_id]):
144
 
            if rev == revision.NULL_REVISION:
145
 
                rev = None
146
155
            ancestry.append(rev)
147
156
        ancestry.reverse()
148
 
        return ancestry
 
157
        return [None] + ancestry
 
158
 
 
159
    def import_revision_gist(self, source, revid, parent_lookup):
 
160
        """Import the gist of a revision into this Git repository.
 
161
 
 
162
        """
 
163
        objects = []
 
164
        rev = source.get_revision(revid)
 
165
        for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
 
166
            if path == "":
 
167
                tree_sha = sha
 
168
            objects.append((object, path))
 
169
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
 
170
        objects.append((commit, None))
 
171
        self._git.object_store.add_objects(objects)
 
172
        return commit.sha().hexdigest()
 
173
 
 
174
    def dfetch(self, source, stop_revision):
 
175
        """Import the gist of the ancestry of a particular revision."""
 
176
        if stop_revision is None:
 
177
            raise NotImplementedError
 
178
        revidmap = {}
 
179
        gitidmap = {}
 
180
        todo = []
 
181
        source.lock_write()
 
182
        try:
 
183
            graph = source.get_graph()
 
184
            ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
 
185
            for revid in graph.iter_topo_order(ancestry):
 
186
                if not self.has_revision(revid):
 
187
                    todo.append(revid)
 
188
            pb = ui.ui_factory.nested_progress_bar()
 
189
            try:
 
190
                for i, revid in enumerate(todo):
 
191
                    pb.update("pushing revisions", i, len(todo))
 
192
                    git_commit = self.import_revision_gist(source, revid,
 
193
                        gitidmap.__getitem__)
 
194
                    gitidmap[revid] = git_commit
 
195
                    git_revid = self.get_mapping().revision_id_foreign_to_bzr(
 
196
                        git_commit)
 
197
                    revidmap[revid] = git_revid
 
198
            finally:
 
199
                pb.finished()
 
200
            source.fetch(self, revision_id=revidmap[stop_revision])
 
201
        finally:
 
202
            source.unlock()
 
203
        return revidmap
149
204
 
150
205
    def get_signature_text(self, revision_id):
151
206
        raise errors.NoSuchRevision(self, revision_id)
162
217
    def has_signature_for_revision_id(self, revision_id):
163
218
        return False
164
219
 
165
 
    def lookup_git_revid(self, bzr_revid, mapping):
 
220
    def lookup_git_revid(self, bzr_revid):
166
221
        try:
167
 
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
 
222
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
168
223
        except errors.InvalidRevisionId:
169
224
            raise errors.NoSuchRevision(self, bzr_revid)
170
225
 
171
226
    def get_revision(self, revision_id):
172
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
227
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
173
228
        try:
174
229
            commit = self._git.commit(git_commit_id)
175
230
        except KeyError:
176
231
            raise errors.NoSuchRevision(self, revision_id)
177
232
        # print "fetched revision:", git_commit_id
178
 
        revision = self.get_mapping().import_commit(commit)
 
233
        revision = mapping.import_commit(commit)
179
234
        assert revision is not None
180
235
        return revision
181
236
 
196
251
 
197
252
    def revision_tree(self, revision_id):
198
253
        revision_id = revision.ensure_null(revision_id)
199
 
 
200
254
        if revision_id == revision.NULL_REVISION:
201
255
            inv = inventory.Inventory(root_id=None)
202
256
            inv.revision_id = revision_id
203
257
            return revisiontree.RevisionTree(self, inv, revision_id)
204
 
 
205
 
        return GitRevisionTree(self, self.get_mapping(), revision_id)
 
258
        return GitRevisionTree(self, revision_id)
206
259
 
207
260
    def get_inventory(self, revision_id):
208
261
        assert revision_id != None
211
264
    def set_make_working_trees(self, trees):
212
265
        pass
213
266
 
214
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
267
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
268
        progress=None):
215
269
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
216
270
 
217
271
 
218
272
class GitRevisionTree(revisiontree.RevisionTree):
219
273
 
220
 
    def __init__(self, repository, mapping, revision_id):
 
274
    def __init__(self, repository, revision_id):
221
275
        self._repository = repository
222
276
        self.revision_id = revision_id
223
277
        assert isinstance(revision_id, str)
224
 
        self.mapping = mapping
225
 
        git_id = repository.lookup_git_revid(revision_id, self.mapping)
 
278
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
226
279
        try:
227
280
            commit = repository._git.commit(git_id)
228
281
        except KeyError, r: