1
 
# Copyright (C) 2008 Canonical Ltd
 
3
 
# This program is free software; you can redistribute it and/or modify
 
4
 
# it under the terms of the GNU General Public License as published by
 
5
 
# the Free Software Foundation; either version 2 of the License, or
 
6
 
# (at your option) any later version.
 
8
 
# This program is distributed in the hope that it will be useful,
 
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 
# GNU General Public License for more details.
 
13
 
# You should have received a copy of the GNU General Public License
 
14
 
# along with this program; if not, write to the Free Software
 
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
from bzrlib import osutils
 
18
 
from bzrlib.errors import InvalidRevisionId
 
19
 
from bzrlib.inventory import Inventory
 
20
 
from bzrlib.repository import InterRepository
 
21
 
from bzrlib.trace import info
 
23
 
from bzrlib.plugins.git import git
 
24
 
from bzrlib.plugins.git.repository import LocalGitRepository, GitRepository, GitFormat
 
25
 
from bzrlib.plugins.git.remote import RemoteGitRepository
 
27
 
from dulwich.objects import Commit
 
29
 
from cStringIO import StringIO
 
32
 
class BzrFetchGraphWalker(object):
 
34
 
    def __init__(self, repository, mapping):
 
35
 
        self.repository = repository
 
36
 
        self.mapping = mapping
 
38
 
        self.heads = set(repository.all_revision_ids())
 
42
 
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
 
45
 
    def remove(self, revid):
 
48
 
            self.heads.remove(revid)
 
49
 
        if revid in self.parents:
 
50
 
            for p in self.parents[revid]:
 
55
 
            ret = self.heads.pop()
 
56
 
            ps = self.repository.get_parent_map([ret])[ret]
 
57
 
            self.parents[ret] = ps
 
58
 
            self.heads.update([p for p in ps if not p in self.done])
 
61
 
                return self.mapping.revision_id_bzr_to_foreign(ret)
 
62
 
            except InvalidRevisionId:
 
67
 
def import_git_blob(repo, mapping, path, blob):
 
68
 
    """Import a git blob object into a bzr repository.
 
70
 
    :param repo: bzr repository
 
71
 
    :param path: Path in the tree
 
72
 
    :param blob: A git blob
 
74
 
    file_id = mapping.generate_file_id(path)
 
75
 
    repo.texts.add_lines((file_id, blob.id),
 
77
 
        osutils.split_lines(blob.data))
 
78
 
    inv.add_path(path, "file", file_id)
 
81
 
def import_git_tree(repo, mapping, path, tree, inv, lookup_object):
 
82
 
    """Import a git tree object into a bzr repository.
 
84
 
    :param repo: A Bzr repository object
 
85
 
    :param path: Path in the tree
 
86
 
    :param tree: A git tree object
 
87
 
    :param inv: Inventory object
 
89
 
    file_id = mapping.generate_file_id(path)
 
90
 
    repo.texts.add_lines((file_id, tree.id),
 
93
 
    inv.add_path(path, "directory", file_id)
 
94
 
    for mode, name, hexsha in tree.entries():
 
95
 
        entry_kind = (mode & 0700000) / 0100000
 
96
 
        basename = name.decode("utf-8")
 
100
 
            child_path = urlutils.join(path, name)
 
102
 
            import_git_tree(repo, mapping, child_path, lookup_object, inv)
 
103
 
        elif entry_kind == 1:
 
104
 
            import_git_blob(repo, mapping, child_path, lookup_object, inv)
 
106
 
            raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
 
109
 
def import_git_objects(repo, mapping, object_iter):
 
110
 
    """Import a set of git objects into a bzr repository.
 
112
 
    :param repo: Bazaar repository
 
113
 
    :param mapping: Mapping to use
 
114
 
    :param object_iter: Iterator over Git objects.
 
116
 
    # TODO: a more (memory-)efficient implementation of this
 
118
 
    for o in object_iter:
 
121
 
    # Find and convert commit objects
 
122
 
    for o in objects.iterkeys():
 
123
 
        if isinstance(o, Commit):
 
124
 
            rev = mapping.import_commit(o)
 
125
 
            root_trees[rev] = objects[o.tree_sha]
 
126
 
    # Create the inventory objects
 
127
 
    for rev, root_tree in root_trees.iteritems():
 
128
 
        # We have to do this here, since we have to walk the tree and 
 
129
 
        # we need to make sure to import the blobs / trees with the riht 
 
130
 
        # path; this may involve adding them more than once.
 
132
 
        def lookup_object(sha):
 
135
 
            return reconstruct_git_object(repo, mapping, sha)
 
136
 
        import_git_tree(repo, mapping, "", tree, inv, lookup_object)
 
137
 
        repo.add_revision(rev.revision_id, rev, inv)
 
140
 
def reconstruct_git_commit(repo, rev):
 
141
 
    raise NotImplementedError(self.reconstruct_git_commit)
 
144
 
def reconstruct_git_object(repo, mapping, sha):
 
146
 
    revid = mapping.revision_id_foreign_to_bzr(sha)
 
148
 
        rev = repo.get_revision(revid)
 
149
 
    except NoSuchRevision:
 
152
 
        return reconstruct_git_commit(rev)
 
156
 
    raise KeyError("No such object %s" % sha)
 
159
 
class InterGitRepository(InterRepository):
 
161
 
    _matching_repo_format = GitFormat()
 
164
 
    def _get_repo_format_to_test():
 
167
 
    def copy_content(self, revision_id=None, pb=None):
 
168
 
        """See InterRepository.copy_content."""
 
169
 
        self.fetch(revision_id, pb, find_ghosts=False)
 
171
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
 
174
 
            mapping = self.source.get_mapping()
 
177
 
                pb.note("git: %s" % text)
 
179
 
                info("git: %s" % text)
 
180
 
        def determine_wants(heads):
 
181
 
            if revision_id is None:
 
184
 
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
 
185
 
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
 
186
 
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
 
187
 
        self.target.lock_write()
 
189
 
            import_git_objects(self.target, mapping,
 
190
 
                self.source.fetch_objects(determine_wants, graph_walker, 
 
196
 
    def is_compatible(source, target):
 
197
 
        """Be compatible with GitRepository."""
 
198
 
        # FIXME: Also check target uses VersionedFile
 
199
 
        return (isinstance(source, LocalGitRepository) and 
 
200
 
                target.supports_rich_root())