/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

Add simple HACKING document.

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):
64
69
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
65
70
        from bzrlib.plugins.git import fetch
66
71
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
 
72
        repository.InterRepository.register_optimiser(fetch.InterGitNonGitRepository)
67
73
 
68
74
    def is_shared(self):
69
75
        return True
94
100
        self.texts = None
95
101
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
96
102
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
 
103
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
 
104
        self.texts = GitTexts(self)
97
105
        self.tags = GitTags(self._git.get_tags())
98
106
 
99
107
    def all_revision_ids(self):
100
108
        ret = set([revision.NULL_REVISION])
101
 
        if self._git.heads() == []:
 
109
        heads = self._git.heads()
 
110
        if heads == {}:
102
111
            return ret
103
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in self._git.heads()]
 
112
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
104
113
        ret = set(bzr_heads)
105
114
        graph = self.get_graph()
106
115
        for rev, parents in graph.iter_ancestry(bzr_heads):
124
133
            if revision_id == revision.NULL_REVISION:
125
134
                parent_map[revision_id] = ()
126
135
                continue
127
 
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
 
136
            hexsha, mapping = self.lookup_git_revid(revision_id)
128
137
            commit  = self._git.commit(hexsha)
129
138
            if commit is None:
130
139
                continue
131
140
            else:
132
 
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
 
141
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
133
142
        return parent_map
134
143
 
135
144
    def get_ancestry(self, revision_id, topo_sorted=True):
136
145
        """See Repository.get_ancestry().
137
146
        """
138
147
        if revision_id is None:
139
 
            return self._all_revision_ids()
 
148
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
140
149
        assert isinstance(revision_id, str)
141
150
        ancestry = []
142
151
        graph = self.get_graph()
143
152
        for rev, parents in graph.iter_ancestry([revision_id]):
144
 
            if rev == revision.NULL_REVISION:
145
 
                rev = None
146
153
            ancestry.append(rev)
147
154
        ancestry.reverse()
148
 
        return ancestry
 
155
        return [None] + ancestry
 
156
 
 
157
    def import_revision_gist(self, source, revid, parent_lookup):
 
158
        """Import the gist of a revision into this Git repository.
 
159
 
 
160
        """
 
161
        objects = []
 
162
        rev = source.get_revision(revid)
 
163
        for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
 
164
            if path == "":
 
165
                tree_sha = sha
 
166
            objects.append((object, path))
 
167
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
 
168
        objects.append((commit, None))
 
169
        self._git.object_store.add_objects(objects)
 
170
        return commit.sha().hexdigest()
 
171
 
 
172
    def dfetch(self, source, stop_revision):
 
173
        """Import the gist of the ancestry of a particular revision."""
 
174
        if stop_revision is None:
 
175
            raise NotImplementedError
 
176
        revidmap = {}
 
177
        gitidmap = {}
 
178
        todo = []
 
179
        source.lock_write()
 
180
        try:
 
181
            graph = source.get_graph()
 
182
            ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
 
183
            for revid in graph.iter_topo_order(ancestry):
 
184
                if not self.has_revision(revid):
 
185
                    todo.append(revid)
 
186
            pb = ui.ui_factory.nested_progress_bar()
 
187
            try:
 
188
                for i, revid in enumerate(todo):
 
189
                    pb.update("pushing revisions", i, len(todo))
 
190
                    git_commit = self.import_revision_gist(source, revid, gitidmap.__getitem__)
 
191
                    gitidmap[revid] = git_commit
 
192
                    git_revid = self.get_mapping().revision_id_foreign_to_bzr(git_commit)
 
193
                    revidmap[revid] = git_revid
 
194
            finally:
 
195
                pb.finished()
 
196
            source.fetch(self, revision_id=revidmap[stop_revision])
 
197
        finally:
 
198
            source.unlock()
 
199
        return revidmap
149
200
 
150
201
    def get_signature_text(self, revision_id):
151
202
        raise errors.NoSuchRevision(self, revision_id)
162
213
    def has_signature_for_revision_id(self, revision_id):
163
214
        return False
164
215
 
165
 
    def lookup_git_revid(self, bzr_revid, mapping):
 
216
    def lookup_git_revid(self, bzr_revid):
166
217
        try:
167
 
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
 
218
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
168
219
        except errors.InvalidRevisionId:
169
220
            raise errors.NoSuchRevision(self, bzr_revid)
170
221
 
171
222
    def get_revision(self, revision_id):
172
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
223
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
173
224
        try:
174
225
            commit = self._git.commit(git_commit_id)
175
226
        except KeyError:
176
227
            raise errors.NoSuchRevision(self, revision_id)
177
228
        # print "fetched revision:", git_commit_id
178
 
        revision = self.get_mapping().import_commit(commit)
 
229
        revision = mapping.import_commit(commit)
179
230
        assert revision is not None
180
231
        return revision
181
232
 
202
253
            inv.revision_id = revision_id
203
254
            return revisiontree.RevisionTree(self, inv, revision_id)
204
255
 
205
 
        return GitRevisionTree(self, self.get_mapping(), revision_id)
 
256
        return GitRevisionTree(self, revision_id)
206
257
 
207
258
    def get_inventory(self, revision_id):
208
259
        assert revision_id != None
211
262
    def set_make_working_trees(self, trees):
212
263
        pass
213
264
 
214
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
265
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
215
266
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
216
267
 
217
268
 
218
269
class GitRevisionTree(revisiontree.RevisionTree):
219
270
 
220
 
    def __init__(self, repository, mapping, revision_id):
 
271
    def __init__(self, repository, revision_id):
221
272
        self._repository = repository
222
273
        self.revision_id = revision_id
223
274
        assert isinstance(revision_id, str)
224
 
        self.mapping = mapping
225
 
        git_id = repository.lookup_git_revid(revision_id, self.mapping)
 
275
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
226
276
        try:
227
277
            commit = repository._git.commit(git_id)
228
278
        except KeyError, r: