1
# Copyright (C) 2007-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
 
 
18
from bzrlib import urlutils
 
 
19
from bzrlib.bzrdir import BzrDir, BzrDirFormat
 
 
20
from bzrlib.errors import NoSuchFile, NotLocalUrl
 
 
21
from bzrlib.lockable_files import TransportLock
 
 
22
from bzrlib.repository import Repository
 
 
23
from bzrlib.trace import info
 
 
24
from bzrlib.transport import Transport
 
 
26
from bzrlib.plugins.git import lazy_check_versions
 
 
29
from bzrlib.plugins.git.branch import GitBranch
 
 
30
from bzrlib.plugins.git.errors import NoSuchRef
 
 
31
from bzrlib.plugins.git.dir import GitDir
 
 
32
from bzrlib.plugins.git.foreign import ForeignBranch
 
 
33
from bzrlib.plugins.git.repository import GitFormat, GitRepository
 
 
41
from dulwich.pack import PackData, Pack
 
 
43
# Don't run any tests on GitSmartTransport as it is not intended to be 
 
 
44
# a full implementation of Transport
 
 
45
def get_test_permutations():
 
 
49
class GitSmartTransport(Transport):
 
 
51
    def __init__(self, url, _client=None):
 
 
52
        Transport.__init__(self, url)
 
 
53
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
 
 
54
        assert scheme == "git"
 
 
55
        hostport, self._path = urllib.splithost(loc)
 
 
56
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
 
 
57
        self._client = _client
 
 
59
    def _get_client(self):
 
 
60
        if self._client is not None:
 
 
64
        return git.client.TCPGitClient(self._host, self._port)
 
 
66
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
 
 
69
                info("git: %s" % text)
 
 
70
        self._get_client().fetch_pack(self._path, determine_wants, 
 
 
71
            graph_walker, pack_data, progress)
 
 
74
        raise NoSuchFile(path)
 
 
76
    def abspath(self, relpath):
 
 
77
        return urlutils.join(self.base, relpath)
 
 
79
    def clone(self, offset=None):
 
 
80
        """See Transport.clone()."""
 
 
84
            newurl = urlutils.join(self.base, offset)
 
 
86
        return GitSmartTransport(newurl, self._client)
 
 
89
class RemoteGitDir(GitDir):
 
 
91
    def __init__(self, transport, lockfiles, format):
 
 
93
        self.root_transport = transport
 
 
94
        self.transport = transport
 
 
95
        self._lockfiles = lockfiles
 
 
97
    def open_repository(self):
 
 
98
        return RemoteGitRepository(self, self._lockfiles)
 
 
100
    def open_branch(self, _unsupported=False):
 
 
101
        repo = self.open_repository()
 
 
102
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
 
 
103
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
 
 
105
    def open_workingtree(self):
 
 
106
        raise NotLocalUrl(self.transport.base)
 
 
109
class RemoteGitRepository(GitRepository):
 
 
111
    def __init__(self, gitdir, lockfiles):
 
 
112
        GitRepository.__init__(self, gitdir, lockfiles)
 
 
114
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
 
 
116
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
 
 
119
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
 
 
120
        fd, path = tempfile.mkstemp(suffix=".pack")
 
 
121
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
 
 
123
        basename = path[:-len(".pack")]
 
 
125
        p.create_index_v2(basename+".idx")
 
 
126
        pack = Pack(basename)
 
 
128
        return (len(p), pack.iterobjects())
 
 
131
class RemoteGitBranch(GitBranch):
 
 
133
    def __init__(self, bzrdir, repository, name, lockfiles):
 
 
134
        def determine_wants(heads):
 
 
135
            if not name in heads:
 
 
136
                raise NoSuchRef(name)
 
 
137
            self._ref = heads[name]
 
 
138
        bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None, 
 
 
139
                             lambda x: mutter("git: %s" % x))
 
 
140
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
 
 
142
    def last_revision(self):
 
 
143
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
 
 
145
    def _synchronize_history(self, destination, revision_id):
 
 
146
        """See Branch._synchronize_history()."""
 
 
147
        destination.generate_revision_history(self.last_revision())