/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
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
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
17
import bzrlib
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
18
from bzrlib import (
19
    branch,
20
    tag,
21
    urlutils,
22
    )
23
from bzrlib.bzrdir import (
24
    BzrDir,
25
    BzrDirFormat,
26
    )
27
from bzrlib.errors import (
28
    BzrError,
29
    NoSuchFile,
30
    NotLocalUrl,
31
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
32
from bzrlib.lockable_files import TransportLock
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
33
from bzrlib.repository import Repository
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
34
from bzrlib.trace import info
35
from bzrlib.transport import Transport
36
0.200.200 by Jelmer Vernooij
Register lazily where possible.
37
from bzrlib.plugins.git import lazy_check_versions
38
lazy_check_versions()
39
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
40
from bzrlib.plugins.git.branch import GitBranch
0.200.165 by Jelmer Vernooij
Give a proper error when the ref can not be found.
41
from bzrlib.plugins.git.errors import NoSuchRef
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
42
from bzrlib.plugins.git.dir import GitDir
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
43
from bzrlib.plugins.git.foreign import ForeignBranch
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
44
from bzrlib.plugins.git.repository import (
45
    GitRepositoryFormat,
46
    GitRepository,
47
    )
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
48
0.200.289 by Jelmer Vernooij
Cope with new member variables in RepositoryFormat.
49
import dulwich as git
50
from dulwich.errors import GitProtocolError
51
from dulwich.pack import (
52
    Pack,
53
    PackData,
54
    PackIndex,
55
    )
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
56
import os
57
import tempfile
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
58
import urllib
59
import urlparse
60
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
61
0.200.181 by Jelmer Vernooij
Support setting tags.
62
# Don't run any tests on GitSmartTransport as it is not intended to be 
63
# a full implementation of Transport
64
def get_test_permutations():
65
    return []
66
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
67
68
class GitSmartTransport(Transport):
69
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
70
    def __init__(self, url, _client=None):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
71
        Transport.__init__(self, url)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
72
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
73
        assert scheme == "git"
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
74
        hostport, self._path = urllib.splithost(loc)
75
        (self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
0.200.166 by Jelmer Vernooij
don't reuse client objects.
76
        self._client = _client
77
0.200.238 by Jelmer Vernooij
Import Transport.has().
78
    def has(self, relpath):
79
        return False
80
0.200.166 by Jelmer Vernooij
don't reuse client objects.
81
    def _get_client(self):
82
        if self._client is not None:
83
            ret = self._client
84
            self._client = None
85
            return ret
0.200.241 by Jelmer Vernooij
Use new thin packs argument.
86
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
87
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
88
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
89
        if progress is None:
90
            def progress(text):
91
                info("git: %s" % text)
0.200.240 by Jelmer Vernooij
Wrap socket errors.
92
        client = self._get_client()
93
        try:
94
            client.fetch_pack(self._path, determine_wants, 
95
                graph_walker, pack_data, progress)
96
        except GitProtocolError, e:
97
            raise BzrError(e)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
98
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
99
    def get(self, path):
100
        raise NoSuchFile(path)
101
0.200.160 by Jelmer Vernooij
Implement abspath.
102
    def abspath(self, relpath):
103
        return urlutils.join(self.base, relpath)
104
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
105
    def clone(self, offset=None):
106
        """See Transport.clone()."""
107
        if offset is None:
108
            newurl = self.base
109
        else:
110
            newurl = urlutils.join(self.base, offset)
111
112
        return GitSmartTransport(newurl, self._client)
113
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
114
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
115
class RemoteGitDir(GitDir):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
116
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
117
    def __init__(self, transport, lockfiles, format):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
118
        self._format = format
119
        self.root_transport = transport
120
        self.transport = transport
121
        self._lockfiles = lockfiles
122
123
    def open_repository(self):
124
        return RemoteGitRepository(self, self._lockfiles)
125
0.200.174 by Jelmer Vernooij
Merge John.
126
    def open_branch(self, _unsupported=False):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
127
        repo = self.open_repository()
128
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
129
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
130
131
    def open_workingtree(self):
132
        raise NotLocalUrl(self.transport.base)
133
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
134
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
135
class EmptyObjectStoreIterator(dict):
136
137
    def iterobjects(self):
138
        return []
139
140
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
141
class TemporaryPackIterator(Pack):
142
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
143
    def __init__(self, path, resolve_ext_ref):
144
        self.resolve_ext_ref = resolve_ext_ref
145
        super(TemporaryPackIterator, self).__init__(path)
146
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
147
    @property
148
    def idx(self):
149
        if self._idx is None:
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
150
            self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
151
            self._idx = PackIndex(self._idx_path)
152
        return self._idx
0.200.205 by Jelmer Vernooij
Fix remote fetching.
153
154
    def __del__(self):
0.200.218 by Jelmer Vernooij
Simplify TemporaryPack implementation.
155
        os.remove(self._data_path)
156
        os.remove(self._idx_path)
0.200.205 by Jelmer Vernooij
Fix remote fetching.
157
158
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
159
class RemoteGitRepository(GitRepository):
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
160
161
    def __init__(self, gitdir, lockfiles):
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
162
        GitRepository.__init__(self, gitdir, lockfiles)
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
163
        self._refs = None
164
165
    def get_refs(self):
166
        if self._refs is not None:
167
            return self._refs
168
        def determine_wants(heads):
169
            self._refs = heads
170
            return []
171
        self.bzrdir.root_transport.fetch_pack(determine_wants, None, 
172
            lambda x: None, lambda x: mutter("git: %s" % x))
173
        return self._refs
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
174
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
175
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
176
                   progress=None):
177
        self._transport.fetch_pack(determine_wants, graph_walker, pack_data, 
178
            progress)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
179
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
180
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
181
        fd, path = tempfile.mkstemp(suffix=".pack")
182
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
183
        os.close(fd)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
184
        if os.path.getsize(path) == 0:
0.225.2 by Jelmer Vernooij
Handle situation when repository is already up to date during pull.
185
            return EmptyObjectStoreIterator()
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
186
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
0.200.167 by Jelmer Vernooij
Implement fetch_objects properly.
187
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
188
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
189
class RemoteGitTagDict(tag.BasicTags):
190
191
    def __init__(self, branch):
192
        self.branch = branch
193
        self.repository = branch.repository
194
195
    def get_tag_dict(self):
196
        ret = {}
197
        refs = self.repository.get_refs()
198
        for k,v in refs.iteritems():
199
            if k.startswith("refs/tags/") and not k.endswith("^{}"):
200
                v = refs.get(k+"^{}", v)
0.228.4 by Jelmer Vernooij
Strip ref directory name from tag names.
201
                ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
202
        return ret
203
204
    def set_tag(self, name, revid):
205
        # FIXME: Not supported yet, should do a push of a new ref
206
        raise NotImplementedError(self.set_tag)
207
208
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
209
class RemoteGitBranch(GitBranch):
210
211
    def __init__(self, bzrdir, repository, name, lockfiles):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
212
        heads = repository.get_refs()
213
        if not name in heads:
214
            raise NoSuchRef(name)
215
        self._ref = heads[name]
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
216
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
217
218
    def last_revision(self):
0.200.140 by Jelmer Vernooij
Support negotiating with remote git repository and receiving pack.
219
        return self.mapping.revision_id_foreign_to_bzr(self._ref)
0.200.141 by Jelmer Vernooij
Separate out local and remote fetching.
220
0.200.169 by Jelmer Vernooij
Fix branch cloning.
221
    def _synchronize_history(self, destination, revision_id):
222
        """See Branch._synchronize_history()."""
223
        destination.generate_revision_history(self.last_revision())
224