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

Share more infrastructure between LocalGitDir and RemoteGitDir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
from bzrlib.errors import InvalidRevisionId
 
18
from bzrlib.repository import InterRepository
 
19
from bzrlib.trace import info
 
20
 
 
21
from bzrlib.plugins.git.repository import LocalGitRepository, GitRepository, GitFormat
 
22
from bzrlib.plugins.git.remote import RemoteGitRepository
 
23
 
 
24
from cStringIO import StringIO
 
25
 
 
26
 
 
27
class BzrFetchGraphWalker(object):
 
28
 
 
29
    def __init__(self, repository, mapping):
 
30
        self.repository = repository
 
31
        self.mapping = mapping
 
32
        self.done = set()
 
33
        self.heads = set(repository.all_revision_ids())
 
34
        self.parents = {}
 
35
 
 
36
    def ack(self, sha):
 
37
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
 
38
        self.remove(revid)
 
39
 
 
40
    def remove(self, revid):
 
41
        self.done.add(revid)
 
42
        if ref in self.heads:
 
43
            self.heads.remove(revid)
 
44
        if revid in self.parents:
 
45
            for p in self.parents[revid]:
 
46
                self.remove(p)
 
47
 
 
48
    def next(self):
 
49
        while self.heads:
 
50
            ret = self.heads.pop()
 
51
            ps = self.repository.get_parent_map([ret])[ret]
 
52
            self.parents[ret] = ps
 
53
            self.heads.update([p for p in ps if not p in self.done])
 
54
            try:
 
55
                self.done.add(ret)
 
56
                return self.mapping.revision_id_bzr_to_foreign(ret)
 
57
            except InvalidRevisionId:
 
58
                pass
 
59
        return None
 
60
 
 
61
 
 
62
def import_git_object(repo, object):
 
63
    raise NotImplementedError(import_git_object)
 
64
 
 
65
 
 
66
class InterGitRepository(InterRepository):
 
67
 
 
68
    _matching_repo_format = GitFormat()
 
69
 
 
70
    @staticmethod
 
71
    def _get_repo_format_to_test():
 
72
        return None
 
73
 
 
74
    def copy_content(self, revision_id=None, pb=None):
 
75
        """See InterRepository.copy_content."""
 
76
        self.fetch(revision_id, pb, find_ghosts=False)
 
77
 
 
78
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
 
79
              mapping=None):
 
80
        if mapping is None:
 
81
            mapping = self.source.get_mapping()
 
82
        def progress(text):
 
83
            if pb is not None:
 
84
                pb.note("git: %s" % text)
 
85
            else:
 
86
                info("git: %s" % text)
 
87
        def determine_wants(heads):
 
88
            if revision_id is None:
 
89
                ret = heads.values()
 
90
            else:
 
91
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
 
92
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
 
93
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
 
94
        self.target.lock_write()
 
95
        try:
 
96
            for o in self.source.fetch_objects(determine_wants, graph_walker, progress):
 
97
                import_git_object(o)
 
98
        finally:
 
99
            self.target.unlock()
 
100
 
 
101
    @staticmethod
 
102
    def is_compatible(source, target):
 
103
        """Be compatible with GitRepository."""
 
104
        # FIXME: Also check target uses VersionedFile
 
105
        return isinstance(source, LocalGitRepository) and target.supports_rich_root()