/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

update copyright years

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""An adapter between a Git Repository and a Bazaar Branch"""
19
19
 
20
 
import bzrlib
21
20
from bzrlib import (
22
21
    errors,
23
22
    graph,
24
23
    inventory,
25
 
    osutils,
26
24
    repository,
27
25
    revision,
28
26
    revisiontree,
29
 
    ui,
30
 
    urlutils,
31
27
    )
32
28
from bzrlib.foreign import (
33
29
    ForeignRepository,
34
30
    )
35
 
from bzrlib.trace import (
36
 
    mutter,
37
 
    )
38
 
from bzrlib.transport import (
39
 
    get_transport,
40
 
    )
41
31
 
42
32
from bzrlib.plugins.git.commit import (
43
33
    GitCommitBuilder,
44
34
    )
45
 
from bzrlib.plugins.git.foreign import (
46
 
    versionedfiles,
47
 
    )
48
 
from bzrlib.plugins.git.inventory import (
49
 
    GitInventory,
50
 
    )
51
35
from bzrlib.plugins.git.mapping import (
52
36
    default_mapping,
53
37
    foreign_git,
54
38
    mapping_registry,
55
39
    )
 
40
from bzrlib.plugins.git.tree import (
 
41
    GitRevisionTree,
 
42
    )
56
43
from bzrlib.plugins.git.versionedfiles import (
 
44
    GitRevisions,
57
45
    GitTexts,
58
46
    )
59
47
 
60
 
import dulwich as git
61
 
import os
62
 
import time
63
 
 
64
 
 
65
 
class GitTags(object):
66
 
 
67
 
    def __init__(self, tags):
68
 
        self._tags = tags
69
 
 
70
 
    def __iter__(self):
71
 
        return iter(self._tags)
72
 
 
73
48
 
74
49
class GitRepository(ForeignRepository):
75
50
    """An adapter to git repositories for bzr."""
79
54
    vcs = foreign_git
80
55
 
81
56
    def __init__(self, gitdir, lockfiles):
82
 
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
 
57
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
83
58
            lockfiles)
84
59
        from bzrlib.plugins.git import fetch, push
85
 
        for optimiser in [fetch.InterRemoteGitNonGitRepository, 
 
60
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
86
61
                          fetch.InterLocalGitNonGitRepository,
87
 
                          fetch.InterGitRepository,
88
 
                          push.InterToGitRepository]:
 
62
                          fetch.InterGitGitRepository,
 
63
                          push.InterToLocalGitRepository,
 
64
                          push.InterToRemoteGitRepository]:
89
65
            repository.InterRepository.register_optimiser(optimiser)
90
66
 
91
67
    def is_shared(self):
92
 
        return True
 
68
        return False
93
69
 
94
70
    def supports_rich_root(self):
95
71
        return True
96
72
 
97
 
    def _warn_if_deprecated(self):
 
73
    def _warn_if_deprecated(self, branch=None):
98
74
        # This class isn't deprecated
99
75
        pass
100
76
 
104
80
    def make_working_trees(self):
105
81
        return True
106
82
 
 
83
    def revision_graph_can_have_wrong_parents(self):
 
84
        return False
 
85
 
 
86
    def dfetch(self, source, stop_revision):
 
87
        interrepo = repository.InterRepository.get(source, self)
 
88
        return interrepo.dfetch(stop_revision)
 
89
 
 
90
    def dfetch_refs(self, source, stop_revision):
 
91
        interrepo = repository.InterRepository.get(source, self)
 
92
        return interrepo.dfetch_refs(stop_revision)
 
93
 
107
94
 
108
95
class LocalGitRepository(GitRepository):
109
96
    """Git repository on the file system."""
110
97
 
111
98
    def __init__(self, gitdir, lockfiles):
112
 
        # FIXME: This also caches negatives. Need to be more careful 
113
 
        # about this once we start writing to git
114
 
        self._parents_provider = graph.CachingParentsProvider(self)
115
99
        GitRepository.__init__(self, gitdir, lockfiles)
116
100
        self.base = gitdir.root_transport.base
117
101
        self._git = gitdir._git
118
 
        self.texts = None
119
 
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
120
 
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
121
 
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
102
        self.signatures = None
 
103
        self.revisions = GitRevisions(self, self._git.object_store)
 
104
        self.inventories = None
122
105
        self.texts = GitTexts(self)
123
 
        self.tags = GitTags(self._git.get_tags())
124
106
 
125
107
    def all_revision_ids(self):
126
 
        ret = set([revision.NULL_REVISION])
127
 
        heads = self._git.heads()
 
108
        ret = set([])
 
109
        heads = self._git.refs.as_dict('refs/heads')
128
110
        if heads == {}:
129
111
            return ret
130
112
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
134
116
            ret.add(rev)
135
117
        return ret
136
118
 
137
 
    #def get_revision_delta(self, revision_id):
138
 
    #    parent_revid = self.get_revision(revision_id).parent_ids[0]
139
 
    #    diff = self._git.diff(ids.convert_revision_id_bzr_to_git(parent_revid),
140
 
    #                   ids.convert_revision_id_bzr_to_git(revision_id))
141
 
 
142
 
    def _make_parents_provider(self):
143
 
        """See Repository._make_parents_provider()."""
144
 
        return self._parents_provider
145
 
 
146
119
    def get_parent_map(self, revids):
147
120
        parent_map = {}
148
121
        for revision_id in revids:
150
123
            if revision_id == revision.NULL_REVISION:
151
124
                parent_map[revision_id] = ()
152
125
                continue
153
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
154
 
            commit  = self._git.commit(hexsha)
155
 
            if commit is None:
 
126
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
 
127
            try:
 
128
                commit = self._git[hexsha]
 
129
            except KeyError:
156
130
                continue
157
 
            else:
158
 
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
 
131
            parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
159
132
        return parent_map
160
133
 
161
134
    def get_ancestry(self, revision_id, topo_sorted=True):
171
144
        ancestry.reverse()
172
145
        return [None] + ancestry
173
146
 
174
 
    def dfetch(self, source, stop_revision):
175
 
        interrepo = repository.InterRepository.get(source, self)
176
 
        return interrepo.dfetch(stop_revision)
177
 
 
178
147
    def get_signature_text(self, revision_id):
179
148
        raise errors.NoSuchRevision(self, revision_id)
180
149
 
181
 
    def lookup_revision_id(self, revid):
 
150
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
182
151
        """Lookup a revision id.
183
 
        
184
 
        :param revid: Bazaar revision id.
185
 
        :return: Tuple with git revisionid and mapping.
 
152
 
186
153
        """
187
 
        # Yes, this doesn't really work, but good enough as a stub
188
 
        return osutils.sha(rev_id).hexdigest(), self.get_mapping()
 
154
        if mapping is None:
 
155
            mapping = self.get_mapping()
 
156
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
189
157
 
190
158
    def has_signature_for_revision_id(self, revision_id):
191
159
        return False
192
160
 
193
 
    def lookup_git_revid(self, bzr_revid):
 
161
    def lookup_bzr_revision_id(self, bzr_revid):
194
162
        try:
195
163
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
196
164
        except errors.InvalidRevisionId:
197
165
            raise errors.NoSuchRevision(self, bzr_revid)
198
166
 
199
167
    def get_revision(self, revision_id):
200
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
168
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
201
169
        try:
202
 
            commit = self._git.commit(git_commit_id)
 
170
            commit = self._git[git_commit_id]
203
171
        except KeyError:
204
172
            raise errors.NoSuchRevision(self, revision_id)
205
173
        # print "fetched revision:", git_commit_id
209
177
 
210
178
    def has_revision(self, revision_id):
211
179
        try:
212
 
            self.get_revision(revision_id)
 
180
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
213
181
        except errors.NoSuchRevision:
214
182
            return False
215
 
        else:
216
 
            return True
 
183
        return (git_commit_id in self._git)
 
184
 
 
185
    def has_revisions(self, revision_ids):
 
186
        ret = set()
 
187
        for revid in revision_ids:
 
188
            if self.has_revision(revid):
 
189
                ret.add(revid)
 
190
        return ret
217
191
 
218
192
    def get_revisions(self, revids):
219
193
        return [self.get_revision(r) for r in revids]
241
215
        progress=None):
242
216
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
243
217
 
244
 
 
245
 
class GitRevisionTree(revisiontree.RevisionTree):
246
 
 
247
 
    def __init__(self, repository, revision_id):
248
 
        self._repository = repository
249
 
        self._revision_id = revision_id
250
 
        assert isinstance(revision_id, str)
251
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
252
 
        try:
253
 
            commit = repository._git.commit(git_id)
254
 
        except KeyError, r:
255
 
            raise errors.NoSuchRevision(repository, revision_id)
256
 
        self.tree = commit.tree
257
 
        self._inventory = GitInventory(self.tree, self.mapping, repository._git.object_store, revision_id)
258
 
 
259
 
    def get_revision_id(self):
260
 
        return self._revision_id
261
 
 
262
 
    def get_file_text(self, file_id):
263
 
        entry = self._inventory[file_id]
264
 
        if entry.kind == 'directory': return ""
265
 
        return entry.object.data
 
218
    def _get_versioned_file_checker(self, text_key_references=None,
 
219
                        ancestors=None):
 
220
        return GitVersionedFileChecker(self,
 
221
            text_key_references=text_key_references, ancestors=ancestors)
 
222
    
 
223
 
 
224
class GitVersionedFileChecker(repository._VersionedFileChecker):
 
225
 
 
226
    file_ids = []
 
227
 
 
228
    def _check_file_version_parents(self, texts, progress_bar):
 
229
        return {}, []
 
230
 
266
231
 
267
232
class GitRepositoryFormat(repository.RepositoryFormat):
 
233
    """Git repository format."""
268
234
 
269
235
    supports_tree_reference = False
270
236
    rich_root_data = True
273
239
        return "Git Repository"
274
240
 
275
241
    def initialize(self, url, shared=False, _internal=False):
276
 
        raise bzr_errors.UninitializableFormat(self)
 
242
        raise errors.UninitializableFormat(self)
277
243
 
278
244
    def check_conversion_target(self, target_repo_format):
279
245
        return target_repo_format.rich_root_data
 
246
 
 
247
    def get_foreign_tests_repository_factory(self):
 
248
        from bzrlib.plugins.git.tests.test_repository import (
 
249
            ForeignTestsRepositoryFactory,
 
250
            )
 
251
        return ForeignTestsRepositoryFactory()
 
252
 
 
253
    def network_name(self):
 
254
        return "git"