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

Move push code to push.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
 
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
"""Push implementation that simply prints message saying push is not supported."""
 
18
 
 
19
from bzrlib import (
 
20
    ui,
 
21
    )
 
22
from bzrlib.repository import (
 
23
    InterRepository,
 
24
    )
 
25
 
 
26
from bzrlib.plugins.git.errors import (
 
27
    NoPushSupport,
 
28
    )
 
29
from bzrlib.plugins.git.mapping import (
 
30
    inventory_to_tree_and_blobs,
 
31
    revision_to_commit,
 
32
    )
 
33
from bzrlib.plugins.git.repository import (
 
34
    GitRepository,
 
35
    GitRepositoryFormat,
 
36
    )
 
37
 
 
38
class InterToGitRepository(InterRepository):
 
39
    """InterRepository that copies into a Git repository."""
 
40
 
 
41
    _matching_repo_format = GitRepositoryFormat()
 
42
 
 
43
    @staticmethod
 
44
    def _get_repo_format_to_test():
 
45
        return None
 
46
 
 
47
    def copy_content(self, revision_id=None, pb=None):
 
48
        """See InterRepository.copy_content."""
 
49
        self.fetch(revision_id, pb, find_ghosts=False)
 
50
 
 
51
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
 
52
            fetch_spec=None):
 
53
        raise NoPushSupport()
 
54
 
 
55
    def import_revision_gist(self, revid, parent_lookup):
 
56
        """Import the gist of a revision into this Git repository.
 
57
 
 
58
        """
 
59
        objects = []
 
60
        rev = self.source.get_revision(revid)
 
61
        for sha, object, path in inventory_to_tree_and_blobs(
 
62
                self.source.get_inventory(revid), self.source.texts, None):
 
63
            if path == "":
 
64
                tree_sha = sha
 
65
            objects.append((object, path))
 
66
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
 
67
        objects.append((commit, None))
 
68
        self.target._git.object_store.add_objects(objects)
 
69
        return commit.sha().hexdigest()
 
70
 
 
71
    def missing_revisions(self, stop_revision=None, ghosts=False):
 
72
        if stop_revision is not None:
 
73
            ancestry = [x for x in self.source.get_ancestry(stop_revision) if x is not None]
 
74
        else:
 
75
            ancestry = self.source.all_revision_ids()
 
76
        missing = []
 
77
        graph = self.source.get_graph()
 
78
        for revid in graph.iter_topo_order(ancestry):
 
79
            if not self.target.has_revision(revid):
 
80
                missing.append(revid)
 
81
            elif not ghosts:
 
82
                break
 
83
        return missing
 
84
 
 
85
    def dfetch(self, stop_revision=None, fetch_ghosts=False):
 
86
        """Import the gist of the ancestry of a particular revision."""
 
87
        revidmap = {}
 
88
        gitidmap = {}
 
89
        def parent_lookup(revid):
 
90
            try:
 
91
                return gitidmap[revid]
 
92
            except KeyError:
 
93
                return self.target.lookup_git_revid(revid)[0]
 
94
        mapping = self.target.get_mapping()
 
95
        self.source.lock_write()
 
96
        try:
 
97
            todo = self.missing_revisions(stop_revision, ghosts=fetch_ghosts)
 
98
            pb = ui.ui_factory.nested_progress_bar()
 
99
            try:
 
100
                for i, revid in enumerate(todo):
 
101
                    pb.update("pushing revisions", i, len(todo))
 
102
                    git_commit = self.import_revision_gist(revid, parent_lookup)
 
103
                    gitidmap[revid] = git_commit
 
104
                    git_revid = mapping.revision_id_foreign_to_bzr(git_commit)
 
105
                    revidmap[revid] = git_revid
 
106
            finally:
 
107
                pb.finished()
 
108
            if revidmap != {}:
 
109
                self.source.fetch(self.target, 
 
110
                        revision_id=revidmap[stop_revision])
 
111
        finally:
 
112
            self.source.unlock()
 
113
        return revidmap
 
114
 
 
115
    @staticmethod
 
116
    def is_compatible(source, target):
 
117
        """Be compatible with GitRepository."""
 
118
        return (not isinstance(source, GitRepository) and 
 
119
                isinstance(target, GitRepository))