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

Merge http support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import bzrlib
18
17
from bzrlib import (
19
 
    branch,
 
18
    config,
 
19
    debug,
20
20
    tag,
 
21
    trace,
21
22
    ui,
22
23
    urlutils,
23
24
    )
28
29
    NoSuchRevision,
29
30
    NotLocalUrl,
30
31
    )
31
 
from bzrlib.trace import (
32
 
    info,
33
 
    )
34
32
from bzrlib.transport import (
35
33
    Transport,
36
34
    )
55
53
    mapping_registry,
56
54
    )
57
55
from bzrlib.plugins.git.repository import (
58
 
    GitRepositoryFormat,
59
56
    GitRepository,
60
57
    )
61
58
 
72
69
import urllib
73
70
import urlparse
74
71
 
75
 
try:
76
 
    from dulwich.pack import load_pack_index
77
 
except ImportError:
78
 
    from dulwich.pack import PackIndex as load_pack_index
79
 
 
80
 
 
81
 
# Don't run any tests on GitSmartTransport as it is not intended to be 
 
72
from dulwich.pack import load_pack_index
 
73
 
 
74
 
 
75
# Don't run any tests on GitSmartTransport as it is not intended to be
82
76
# a full implementation of Transport
83
77
def get_test_permutations():
84
78
    return []
85
79
 
86
80
 
 
81
def split_git_url(url):
 
82
    """Split a Git URL.
 
83
 
 
84
    :param url: Git URL
 
85
    :return: Tuple with host, port, username, path.
 
86
    """
 
87
    (scheme, _, loc, _, _) = urlparse.urlsplit(url)
 
88
    hostport, escaped_path = urllib.splithost(loc)
 
89
    path = urllib.unquote(escaped_path)
 
90
    if path.startswith("/~"):
 
91
        path = path[1:]
 
92
    (username, hostport) = urllib.splituser(hostport)
 
93
    (host, port) = urllib.splitnport(hostport, None)
 
94
    return (host, port, username, path)
 
95
 
 
96
 
87
97
class GitSmartTransport(Transport):
88
98
 
89
99
    def __init__(self, url, _client=None):
90
100
        Transport.__init__(self, url)
91
 
        (scheme, _, loc, _, _) = urlparse.urlsplit(url)
92
 
        hostport, self._path = urllib.splithost(loc)
93
 
        (self._username, hostport) = urllib.splituser(hostport)
94
 
        (self._host, self._port) = urllib.splitnport(hostport, None)
 
101
        (self._host, self._port, self._username, self._path) = \
 
102
            split_git_url(url)
 
103
        if 'transport' in debug.debug_flags:
 
104
            trace.mutter('host: %r, user: %r, port: %r, path: %r',
 
105
                         self._host, self._username, self._port, self._path)
95
106
        self._client = _client
96
107
 
97
108
    def external_url(self):
100
111
    def has(self, relpath):
101
112
        return False
102
113
 
103
 
    def _get_client(self):
 
114
    def _get_client(self, thin_packs):
104
115
        raise NotImplementedError(self._get_client)
105
116
 
106
117
    def _get_path(self):
109
120
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
110
121
        if progress is None:
111
122
            def progress(text):
112
 
                info("git: %s" % text)
113
 
        client = self._get_client()
 
123
                trace.info("git: %s" % text)
 
124
        client = self._get_client(thin_packs=False)
114
125
        try:
115
 
            return client.fetch_pack(self._get_path(), determine_wants, 
 
126
            return client.fetch_pack(self._get_path(), determine_wants,
116
127
                graph_walker, pack_data, progress)
117
128
        except GitProtocolError, e:
118
129
            raise BzrError(e)
119
130
 
120
131
    def send_pack(self, get_changed_refs, generate_pack_contents):
121
 
        client = self._get_client()
 
132
        client = self._get_client(thin_packs=False)
122
133
        try:
123
 
            return client.send_pack(self._get_path(), get_changed_refs, 
 
134
            return client.send_pack(self._get_path(), get_changed_refs,
124
135
                generate_pack_contents)
125
136
        except GitProtocolError, e:
126
137
            raise BzrError(e)
145
156
 
146
157
    _scheme = 'git'
147
158
 
148
 
    def _get_client(self):
 
159
    def _get_client(self, thin_packs):
149
160
        if self._client is not None:
150
161
            ret = self._client
151
162
            self._client = None
152
163
            return ret
153
 
        return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
 
164
        return git.client.TCPGitClient(self._host, self._port, thin_packs=thin_packs,
154
165
            report_activity=self._report_activity)
155
166
 
156
167
 
163
174
            return self._path[3:]
164
175
        return self._path
165
176
 
166
 
    def _get_client(self):
 
177
    def _get_client(self, thin_packs):
167
178
        if self._client is not None:
168
179
            ret = self._client
169
180
            self._client = None
170
181
            return ret
171
182
        return git.client.SSHGitClient(self._host, self._port, self._username,
172
 
            thin_packs=False, report_activity=self._report_activity)
 
183
            thin_packs=thin_packs, report_activity=self._report_activity)
173
184
 
174
185
 
175
186
class RemoteGitDir(GitDir):
189
200
        # TODO: Support for multiple branches in one bzrdir in bzrlib!
190
201
        return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
191
202
 
192
 
    def open_workingtree(self):
 
203
    def open_workingtree(self, recommend_upgrade=False):
193
204
        raise NotLocalUrl(self.transport.base)
194
205
 
195
206
 
206
217
        self.resolve_ext_ref = resolve_ext_ref
207
218
 
208
219
    @property
 
220
    def data(self):
 
221
        if self._data is None:
 
222
            self._data = PackData(self._data_path)
 
223
        return self._data
 
224
 
 
225
    @property
209
226
    def index(self):
210
227
        if self._idx is None:
211
 
            pb = ui.ui_factory.nested_progress_bar()
212
 
            try:
213
 
                def report_progress(cur, total):
214
 
                    pb.update("generating index", cur, total)
215
 
                self.data.create_index(self._idx_path, self.resolve_ext_ref,
216
 
                    progress=report_progress)
217
 
            finally:
218
 
                pb.finished()
 
228
            if not os.path.exists(self._idx_path):
 
229
                pb = ui.ui_factory.nested_progress_bar()
 
230
                try:
 
231
                    def report_progress(cur, total):
 
232
                        pb.update("generating index", cur, total)
 
233
                    self.data.create_index(self._idx_path, self.resolve_ext_ref,
 
234
                        progress=report_progress)
 
235
                finally:
 
236
                    pb.finished()
219
237
            self._idx = load_pack_index(self._idx_path)
220
238
        return self._idx
221
239
 
222
240
    def __del__(self):
223
 
        os.remove(self._data_path)
224
 
        os.remove(self._idx_path)
 
241
        if self._idx is not None:
 
242
            self._idx.close()
 
243
            os.remove(self._idx_path)
 
244
        if self._data is not None:
 
245
            self._data.close()
 
246
            os.remove(self._data_path)
225
247
 
226
248
 
227
249
class RemoteGitRepository(GitRepository):
245
267
    def get_refs(self):
246
268
        if self._refs is not None:
247
269
            return self._refs
248
 
        self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None, 
249
 
            lambda x: None, lambda x: mutter("git: %s" % x))
 
270
        self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None,
 
271
            lambda x: None, lambda x: trace.mutter("git: %s" % x))
250
272
        return self._refs
251
273
 
252
 
    def fetch_pack(self, determine_wants, graph_walker, pack_data, 
 
274
    def fetch_pack(self, determine_wants, graph_walker, pack_data,
253
275
                   progress=None):
254
276
        return self._transport.fetch_pack(determine_wants, graph_walker,
255
277
                                          pack_data, progress)
257
279
    def send_pack(self, get_changed_refs, generate_pack_contents):
258
280
        return self._transport.send_pack(get_changed_refs, generate_pack_contents)
259
281
 
260
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
 
282
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
283
                      progress=None):
261
284
        fd, path = tempfile.mkstemp(suffix=".pack")
262
 
        self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
 
285
        self.fetch_pack(determine_wants, graph_walker,
 
286
            lambda x: os.write(fd, x), progress)
263
287
        os.close(fd)
264
288
        if os.path.getsize(path) == 0:
265
289
            return EmptyObjectStoreIterator()
266
290
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
267
291
 
268
 
    def lookup_git_revid(self, bzr_revid):
 
292
    def lookup_bzr_revision_id(self, bzr_revid):
269
293
        # This won't work for any round-tripped bzr revisions, but it's a start..
270
294
        try:
271
295
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
280
304
        self.repository = branch.repository
281
305
 
282
306
    def get_tag_dict(self):
283
 
        return extract_tags(self.repository.get_refs(), self.branch.mapping)
 
307
        tags = {}
 
308
        for k, v in extract_tags(self.repository.get_refs()).iteritems():
 
309
            tags[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
310
        return tags
284
311
 
285
312
    def set_tag(self, name, revid):
286
313
        # FIXME: Not supported yet, should do a push of a new ref
291
318
 
292
319
    def __init__(self, bzrdir, repository, name, lockfiles):
293
320
        self._ref = None
294
 
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name, 
 
321
        super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
295
322
                lockfiles)
296
323
 
297
324
    def revision_history(self):
300
327
    def last_revision(self):
301
328
        return self.mapping.revision_id_foreign_to_bzr(self.head)
302
329
 
 
330
    def _get_config(self):
 
331
        class EmptyConfig(object):
 
332
 
 
333
            def _get_configobj(self):
 
334
                return config.ConfigObj()
 
335
 
 
336
        return EmptyConfig()
 
337
 
303
338
    @property
304
339
    def head(self):
305
340
        if self._ref is not None:
306
341
            return self._ref
307
342
        heads = self.repository.get_refs()
308
343
        if not self.name in heads:
309
 
            raise NoSuchRef(name)
 
344
            raise NoSuchRef(self.name)
310
345
        self._ref = heads[self.name]
311
346
        return self._ref
312
347
 
313
348
    def _synchronize_history(self, destination, revision_id):
314
349
        """See Branch._synchronize_history()."""
315
350
        destination.generate_revision_history(self.last_revision())
316
 
 
 
351
 
317
352
    def get_push_location(self):
318
353
        return None
319
354