/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

  • Committer: Jelmer Vernooij
  • Date: 2018-03-31 19:47:40 UTC
  • mto: (0.200.1910 work)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180331194740-q1lhux2ao2etsblk
Use same logic for interpreting progress reports everywhere.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Remote dirs, repositories and branches."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
import re
 
22
 
 
23
from ... import (
 
24
    config,
 
25
    debug,
 
26
    errors,
 
27
    trace,
 
28
    ui,
 
29
    urlutils,
 
30
    )
 
31
from ...push import (
 
32
    PushResult,
 
33
    )
 
34
from ...errors import (
 
35
    AlreadyBranchError,
 
36
    BzrError,
 
37
    DivergedBranches,
 
38
    InProcessTransport,
 
39
    InvalidRevisionId,
 
40
    NoSuchFile,
 
41
    NoSuchRevision,
 
42
    NoSuchTag,
 
43
    NotBranchError,
 
44
    NotLocalUrl,
 
45
    NoWorkingTree,
 
46
    UninitializableFormat,
 
47
    )
 
48
from ...transport import (
 
49
    Transport,
 
50
    )
 
51
 
 
52
from . import (
 
53
    lazy_check_versions,
 
54
    )
 
55
lazy_check_versions()
 
56
 
 
57
from .branch import (
 
58
    GitBranch,
 
59
    GitBranchFormat,
 
60
    GitBranchPushResult,
 
61
    GitTags,
 
62
    )
 
63
from .dir import (
 
64
    GitControlDirFormat,
 
65
    GitDir,
 
66
    BareLocalGitControlDirFormat,
 
67
    )
 
68
from .errors import (
 
69
    GitSmartRemoteNotSupported,
 
70
    NoSuchRef,
 
71
    )
 
72
from .mapping import (
 
73
    mapping_registry,
 
74
    )
 
75
from .object_store import (
 
76
    get_object_store,
 
77
    )
 
78
from .push import (
 
79
    remote_divergence,
 
80
    )
 
81
from .repository import (
 
82
    GitRepository,
 
83
    )
 
84
from .refs import (
 
85
    branch_name_to_ref,
 
86
    is_peeled,
 
87
    ref_to_tag_name,
 
88
    tag_name_to_ref,
 
89
    )
 
90
 
 
91
import dulwich
 
92
import dulwich.client
 
93
from dulwich.errors import (
 
94
    GitProtocolError,
 
95
    )
 
96
from dulwich.pack import (
 
97
    Pack,
 
98
    pack_objects_to_data,
 
99
    )
 
100
from dulwich.protocol import ZERO_SHA
 
101
from dulwich.refs import SYMREF
 
102
from dulwich.repo import DictRefsContainer
 
103
import os
 
104
import select
 
105
import tempfile
 
106
import urllib
 
107
import urlparse
 
108
 
 
109
# urlparse only supports a limited number of schemes by default
 
110
 
 
111
urlparse.uses_netloc.extend(['git', 'git+ssh'])
 
112
 
 
113
from dulwich.pack import load_pack_index
 
114
 
 
115
 
 
116
# Don't run any tests on GitSmartTransport as it is not intended to be
 
117
# a full implementation of Transport
 
118
def get_test_permutations():
 
119
    return []
 
120
 
 
121
 
 
122
def split_git_url(url):
 
123
    """Split a Git URL.
 
124
 
 
125
    :param url: Git URL
 
126
    :return: Tuple with host, port, username, path.
 
127
    """
 
128
    (scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
 
129
    path = urllib.unquote(loc)
 
130
    if path.startswith("/~"):
 
131
        path = path[1:]
 
132
    (username, hostport) = urllib.splituser(netloc)
 
133
    (host, port) = urllib.splitnport(hostport, None)
 
134
    return (host, port, username, path)
 
135
 
 
136
 
 
137
class RemoteGitError(BzrError):
 
138
 
 
139
    _fmt = "Remote server error: %(msg)s"
 
140
 
 
141
 
 
142
def parse_git_error(url, message):
 
143
    """Parse a remote git server error and return a bzr exception.
 
144
 
 
145
    :param url: URL of the remote repository
 
146
    :param message: Message sent by the remote git server
 
147
    """
 
148
    message = str(message).strip()
 
149
    if message.startswith("Could not find Repository "):
 
150
        return NotBranchError(url, message)
 
151
    if message == "HEAD failed to update":
 
152
        base_url, _ = urlutils.split_segment_parameters(url)
 
153
        raise BzrError(
 
154
            ("Unable to update remote HEAD branch. To update the master "
 
155
             "branch, specify the URL %s,branch=master.") % base_url)
 
156
    # Don't know, just return it to the user as-is
 
157
    return RemoteGitError(message)
 
158
 
 
159
 
 
160
class GitSmartTransport(Transport):
 
161
 
 
162
    def __init__(self, url, _client=None):
 
163
        Transport.__init__(self, url)
 
164
        (self._host, self._port, self._username, self._path) = \
 
165
            split_git_url(url)
 
166
        if 'transport' in debug.debug_flags:
 
167
            trace.mutter('host: %r, user: %r, port: %r, path: %r',
 
168
                         self._host, self._username, self._port, self._path)
 
169
        self._client = _client
 
170
        self._stripped_path = self._path.rsplit(",", 1)[0]
 
171
 
 
172
    def external_url(self):
 
173
        return self.base
 
174
 
 
175
    def has(self, relpath):
 
176
        return False
 
177
 
 
178
    def _get_client(self):
 
179
        raise NotImplementedError(self._get_client)
 
180
 
 
181
    def _get_path(self):
 
182
        return self._stripped_path
 
183
 
 
184
    def get(self, path):
 
185
        raise NoSuchFile(path)
 
186
 
 
187
    def abspath(self, relpath):
 
188
        return urlutils.join(self.base, relpath)
 
189
 
 
190
    def clone(self, offset=None):
 
191
        """See Transport.clone()."""
 
192
        if offset is None:
 
193
            newurl = self.base
 
194
        else:
 
195
            newurl = urlutils.join(self.base, offset)
 
196
 
 
197
        return self.__class__(newurl, self._client)
 
198
 
 
199
 
 
200
class TCPGitSmartTransport(GitSmartTransport):
 
201
 
 
202
    _scheme = 'git'
 
203
 
 
204
    def _get_client(self):
 
205
        if self._client is not None:
 
206
            ret = self._client
 
207
            self._client = None
 
208
            return ret
 
209
        if self._host == '':
 
210
            # return dulwich.client.LocalGitClient()
 
211
            return dulwich.client.SubprocessGitClient()
 
212
        return dulwich.client.TCPGitClient(self._host, self._port,
 
213
            report_activity=self._report_activity)
 
214
 
 
215
 
 
216
class SSHSocketWrapper(object):
 
217
 
 
218
    def __init__(self, sock):
 
219
        self.sock = sock
 
220
 
 
221
    def read(self, len=None):
 
222
        return self.sock.recv(len)
 
223
 
 
224
    def write(self, data):
 
225
        return self.sock.write(data)
 
226
 
 
227
    def can_read(self):
 
228
        return len(select.select([self.sock.fileno()], [], [], 0)[0]) > 0
 
229
 
 
230
 
 
231
class DulwichSSHVendor(dulwich.client.SSHVendor):
 
232
 
 
233
    def __init__(self):
 
234
        from ...transport import ssh
 
235
        self.bzr_ssh_vendor = ssh._get_ssh_vendor()
 
236
 
 
237
    def run_command(self, host, command, username=None, port=None):
 
238
        connection = self.bzr_ssh_vendor.connect_ssh(username=username,
 
239
            password=None, port=port, host=host, command=command)
 
240
        (kind, io_object) = connection.get_sock_or_pipes()
 
241
        if kind == 'socket':
 
242
            return SSHSocketWrapper(io_object)
 
243
        else:
 
244
            raise AssertionError("Unknown io object kind %r'" % kind)
 
245
 
 
246
 
 
247
#dulwich.client.get_ssh_vendor = DulwichSSHVendor
 
248
 
 
249
 
 
250
class SSHGitSmartTransport(GitSmartTransport):
 
251
 
 
252
    _scheme = 'git+ssh'
 
253
 
 
254
    def _get_path(self):
 
255
        path = self._stripped_path
 
256
        if path.startswith("/~/"):
 
257
            return path[3:]
 
258
        return path
 
259
 
 
260
    def _get_client(self):
 
261
        if self._client is not None:
 
262
            ret = self._client
 
263
            self._client = None
 
264
            return ret
 
265
        location_config = config.LocationConfig(self.base)
 
266
        client = dulwich.client.SSHGitClient(self._host, self._port, self._username,
 
267
            report_activity=self._report_activity)
 
268
        # Set up alternate pack program paths
 
269
        upload_pack = location_config.get_user_option('git_upload_pack')
 
270
        if upload_pack:
 
271
            client.alternative_paths["upload-pack"] = upload_pack
 
272
        receive_pack = location_config.get_user_option('git_receive_pack')
 
273
        if receive_pack:
 
274
            client.alternative_paths["receive-pack"] = receive_pack
 
275
        return client
 
276
 
 
277
 
 
278
class RemoteGitBranchFormat(GitBranchFormat):
 
279
 
 
280
    def get_format_description(self):
 
281
        return 'Remote Git Branch'
 
282
 
 
283
    @property
 
284
    def _matchingcontroldir(self):
 
285
        return RemoteGitControlDirFormat()
 
286
 
 
287
    def initialize(self, a_controldir, name=None, repository=None,
 
288
                   append_revisions_only=None):
 
289
        raise UninitializableFormat(self)
 
290
 
 
291
 
 
292
_GIT_PROGRESS_PARTIAL_RE = re.compile(r"(.*?): +(\d+)% \((\d+)/(\d+)\)")
 
293
_GIT_PROGRESS_TOTAL_RE = re.compile(r"(.*?): (\d+)")
 
294
def default_report_progress(pb, text):
 
295
    text = text.rstrip("\r\n")
 
296
    if text.startswith('error: '):
 
297
        trace.show_error('git: %s', text[len('error: '):])
 
298
    else:
 
299
        trace.mutter("git: %s", text)
 
300
        g = _GIT_PROGRESS_PARTIAL_RE.match(text)
 
301
        if g is not None:
 
302
            (text, pct, current, total) = g.groups()
 
303
            pb.update(text, int(current), int(total))
 
304
        else:
 
305
            g = _GIT_PROGRESS_TOTAL_RE.match(text)
 
306
            if g is not None:
 
307
                (text, total) = g.groups()
 
308
                pb.update(text, None, int(total))
 
309
            else:
 
310
                pb.update(text, None, None)
 
311
 
 
312
 
 
313
class RemoteGitDir(GitDir):
 
314
 
 
315
    def __init__(self, transport, format, client, client_path):
 
316
        self._format = format
 
317
        self.root_transport = transport
 
318
        self.transport = transport
 
319
        self._mode_check_done = None
 
320
        self._client = client
 
321
        self._client_path = client_path
 
322
        self.base = self.root_transport.base
 
323
        self._refs = None
 
324
 
 
325
    @property
 
326
    def _gitrepository_class(self):
 
327
        return RemoteGitRepository
 
328
 
 
329
    def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
 
330
        if progress is None:
 
331
            progress = lambda text: default_report_progress(pb, text)
 
332
            pb = ui.ui_factory.nested_progress_bar()
 
333
        else:
 
334
            pb = None
 
335
        try:
 
336
            result = self._client.fetch_pack(self._client_path, determine_wants,
 
337
                graph_walker, pack_data, progress)
 
338
            if result.refs is None:
 
339
                result.refs = {}
 
340
            self._refs = remote_refs_dict_to_container(result.refs, result.symrefs)
 
341
            return result
 
342
        except GitProtocolError, e:
 
343
            raise parse_git_error(self.transport.external_url(), e)
 
344
        finally:
 
345
            if pb is not None:
 
346
                pb.finished()
 
347
 
 
348
    def send_pack(self, get_changed_refs, generate_pack_data, progress=None):
 
349
        if progress is None:
 
350
            progress = lambda text: default_report_progress(pb, text)
 
351
            pb = ui.ui_factory.nested_progress_bar()
 
352
        else:
 
353
            pb = None
 
354
        try:
 
355
            return self._client.send_pack(self._client_path, get_changed_refs,
 
356
                generate_pack_data, progress)
 
357
        except GitProtocolError, e:
 
358
            raise parse_git_error(self.transport.external_url(), e)
 
359
        finally:
 
360
            if pb is not None:
 
361
                pb.finished()
 
362
 
 
363
    def create_branch(self, name=None, repository=None,
 
364
                      append_revisions_only=None, ref=None):
 
365
        refname = self._get_selected_ref(name, ref)
 
366
        if refname != b'HEAD' and refname in self.get_refs_container():
 
367
            raise AlreadyBranchError(self.user_url)
 
368
        if refname in self.get_refs_container():
 
369
            ref_chain, unused_sha = self.get_refs_container().follow(self._get_selected_ref(None))
 
370
            if ref_chain[0] == b'HEAD':
 
371
                refname = ref_chain[1]
 
372
        repo = self.open_repository()
 
373
        return RemoteGitBranch(self, repo, refname)
 
374
 
 
375
    def destroy_branch(self, name=None):
 
376
        refname = self._get_selected_ref(name)
 
377
        def get_changed_refs(old_refs):
 
378
            ret = dict(old_refs)
 
379
            if not refname in ret:
 
380
                raise NotBranchError(self.user_url)
 
381
            ret[refname] = dulwich.client.ZERO_SHA
 
382
            return ret
 
383
        def generate_pack_data(have, want, ofs_delta=False):
 
384
            return pack_objects_to_data([])
 
385
        self.send_pack(get_changed_refs, generate_pack_data)
 
386
 
 
387
    @property
 
388
    def user_url(self):
 
389
        return self.control_url
 
390
 
 
391
    @property
 
392
    def user_transport(self):
 
393
        return self.root_transport
 
394
 
 
395
    @property
 
396
    def control_url(self):
 
397
        return self.control_transport.base
 
398
 
 
399
    @property
 
400
    def control_transport(self):
 
401
        return self.root_transport
 
402
 
 
403
    def open_repository(self):
 
404
        return RemoteGitRepository(self)
 
405
 
 
406
    def open_branch(self, name=None, unsupported=False,
 
407
            ignore_fallbacks=False, ref=None, possible_transports=None,
 
408
            nascent_ok=False):
 
409
        repo = self.open_repository()
 
410
        ref = self._get_selected_ref(name, ref)
 
411
        if not nascent_ok and ref not in self.get_refs_container():
 
412
            raise NotBranchError(self.root_transport.base,
 
413
                    controldir=self)
 
414
        ref_chain, unused_sha = self.get_refs_container().follow(ref)
 
415
        return RemoteGitBranch(self, repo, ref_chain[-1])
 
416
 
 
417
    def open_workingtree(self, recommend_upgrade=False):
 
418
        raise NotLocalUrl(self.transport.base)
 
419
 
 
420
    def has_workingtree(self):
 
421
        return False
 
422
 
 
423
    def get_peeled(self, name):
 
424
        return self.get_refs_container().get_peeled(name)
 
425
 
 
426
    def get_refs_container(self):
 
427
        if self._refs is not None:
 
428
            return self._refs
 
429
        result = self.fetch_pack(lambda x: None, None,
 
430
            lambda x: None, lambda x: trace.mutter("git: %s" % x))
 
431
        self._refs = remote_refs_dict_to_container(
 
432
                result.refs, result.symrefs)
 
433
        return self._refs
 
434
 
 
435
    def push_branch(self, source, revision_id=None, overwrite=False,
 
436
                    remember=False, create_prefix=False, lossy=False,
 
437
                    name=None):
 
438
        """Push the source branch into this ControlDir."""
 
439
        if revision_id is None:
 
440
            # No revision supplied by the user, default to the branch
 
441
            # revision
 
442
            revision_id = source.last_revision()
 
443
 
 
444
        push_result = PushResult()
 
445
        push_result.workingtree_updated = None
 
446
        push_result.master_branch = None
 
447
        push_result.source_branch = source
 
448
        push_result.stacked_on = None
 
449
        push_result.branch_push_result = None
 
450
        repo = self.find_repository()
 
451
        refname = self._get_selected_ref(name)
 
452
        source_store = get_object_store(source.repository)
 
453
        with source_store.lock_read():
 
454
            def get_changed_refs(refs):
 
455
                self._refs = remote_refs_dict_to_container(refs)
 
456
                ret = dict(refs)
 
457
                # TODO(jelmer): Unpeel if necessary
 
458
                if lossy:
 
459
                    new_sha = source_store._lookup_revision_sha1(revision_id)
 
460
                else:
 
461
                    new_sha = repo.lookup_bzr_revision_id(revision_id)[0]
 
462
                if not overwrite:
 
463
                    if remote_divergence(ret.get(refname), new_sha, source_store):
 
464
                        raise DivergedBranches(
 
465
                                source, self.open_branch(name, nascent_ok=True))
 
466
                ret[refname] = new_sha
 
467
                return ret
 
468
            if lossy:
 
469
                generate_pack_data = source_store.generate_lossy_pack_data
 
470
            else:
 
471
                generate_pack_data = source_store.generate_pack_data
 
472
            new_refs = self.send_pack(get_changed_refs, generate_pack_data)
 
473
        push_result.new_revid = repo.lookup_foreign_revision_id(
 
474
                new_refs[refname])
 
475
        try:
 
476
            old_remote = self._refs[refname]
 
477
        except KeyError:
 
478
            old_remote = ZERO_SHA
 
479
        push_result.old_revid = repo.lookup_foreign_revision_id(old_remote)
 
480
        self._refs = remote_refs_dict_to_container(new_refs)
 
481
        push_result.old_revno = None
 
482
        push_result.target_branch = self.open_branch(name)
 
483
        if old_remote != ZERO_SHA:
 
484
            push_result.branch_push_result = GitBranchPushResult()
 
485
            push_result.branch_push_result.source_branch = source
 
486
            push_result.branch_push_result.target_branch = push_result.target_branch
 
487
            push_result.branch_push_result.local_branch = None
 
488
            push_result.branch_push_result.master_branch = push_result.target_branch
 
489
            push_result.branch_push_result.old_revid = push_result.old_revid
 
490
            push_result.branch_push_result.new_revid = push_result.new_revid
 
491
        if source.get_push_location() is None or remember:
 
492
            source.set_push_location(push_result.target_branch.base)
 
493
        return push_result
 
494
 
 
495
 
 
496
class EmptyObjectStoreIterator(dict):
 
497
 
 
498
    def iterobjects(self):
 
499
        return []
 
500
 
 
501
 
 
502
class TemporaryPackIterator(Pack):
 
503
 
 
504
    def __init__(self, path, resolve_ext_ref):
 
505
        super(TemporaryPackIterator, self).__init__(
 
506
            path, resolve_ext_ref=resolve_ext_ref)
 
507
        self._idx_load = lambda: self._idx_load_or_generate(self._idx_path)
 
508
 
 
509
    def _idx_load_or_generate(self, path):
 
510
        if not os.path.exists(path):
 
511
            pb = ui.ui_factory.nested_progress_bar()
 
512
            try:
 
513
                def report_progress(cur, total):
 
514
                    pb.update("generating index", cur, total)
 
515
                self.data.create_index(path,
 
516
                    progress=report_progress)
 
517
            finally:
 
518
                pb.finished()
 
519
        return load_pack_index(path)
 
520
 
 
521
    def __del__(self):
 
522
        if self._idx is not None:
 
523
            self._idx.close()
 
524
            os.remove(self._idx_path)
 
525
        if self._data is not None:
 
526
            self._data.close()
 
527
            os.remove(self._data_path)
 
528
 
 
529
 
 
530
class BzrGitHttpClient(dulwich.client.HttpGitClient):
 
531
 
 
532
    def __init__(self, transport, *args, **kwargs):
 
533
        self.transport = transport
 
534
        super(BzrGitHttpClient, self).__init__(transport.external_url(), *args, **kwargs)
 
535
        import urllib2
 
536
        self._http_perform = getattr(self.transport, "_perform", urllib2.urlopen)
 
537
 
 
538
    def _perform(self, req):
 
539
        req.accepted_errors = (200, 404)
 
540
        req.follow_redirections = True
 
541
        req.redirected_to = None
 
542
        return self._http_perform(req)
 
543
 
 
544
 
 
545
class RemoteGitControlDirFormat(GitControlDirFormat):
 
546
    """The .git directory control format."""
 
547
 
 
548
    supports_workingtrees = False
 
549
 
 
550
    @classmethod
 
551
    def _known_formats(self):
 
552
        return set([RemoteGitControlDirFormat()])
 
553
 
 
554
    def get_branch_format(self):
 
555
        return RemoteGitBranchFormat()
 
556
 
 
557
    def is_initializable(self):
 
558
        return False
 
559
 
 
560
    def is_supported(self):
 
561
        return True
 
562
 
 
563
    def open(self, transport, _found=None):
 
564
        """Open this directory.
 
565
 
 
566
        """
 
567
        # we dont grok readonly - git isn't integrated with transport.
 
568
        url = transport.base
 
569
        if url.startswith('readonly+'):
 
570
            url = url[len('readonly+'):]
 
571
        scheme = urlparse.urlsplit(transport.external_url())[0]
 
572
        if isinstance(transport, GitSmartTransport):
 
573
            client = transport._get_client()
 
574
            client_path = transport._get_path()
 
575
        elif scheme in ("http", "https"):
 
576
            client = BzrGitHttpClient(transport)
 
577
            client_path, _ = urlutils.split_segment_parameters(transport._path)
 
578
        elif scheme == 'file':
 
579
            client = dulwich.client.LocalGitClient()
 
580
            client_path = transport.local_abspath('.')
 
581
        else:
 
582
            raise NotBranchError(transport.base)
 
583
        if not _found:
 
584
            pass # TODO(jelmer): Actually probe for something
 
585
        return RemoteGitDir(transport, self, client, client_path)
 
586
 
 
587
    def get_format_description(self):
 
588
        return "Remote Git Repository"
 
589
 
 
590
    def initialize_on_transport(self, transport):
 
591
        raise UninitializableFormat(self)
 
592
 
 
593
    def supports_transport(self, transport):
 
594
        try:
 
595
            external_url = transport.external_url()
 
596
        except InProcessTransport:
 
597
            raise NotBranchError(path=transport.base)
 
598
        return (external_url.startswith("http:") or
 
599
                external_url.startswith("https:") or
 
600
                external_url.startswith("git+") or
 
601
                external_url.startswith("git:"))
 
602
 
 
603
 
 
604
class RemoteGitRepository(GitRepository):
 
605
 
 
606
    @property
 
607
    def user_url(self):
 
608
        return self.control_url
 
609
 
 
610
    def get_parent_map(self, revids):
 
611
        raise GitSmartRemoteNotSupported(self.get_parent_map, self)
 
612
 
 
613
    def fetch_pack(self, determine_wants, graph_walker, pack_data,
 
614
                   progress=None):
 
615
        return self.controldir.fetch_pack(determine_wants, graph_walker,
 
616
                                          pack_data, progress)
 
617
 
 
618
    def send_pack(self, get_changed_refs, generate_pack_data):
 
619
        return self.controldir.send_pack(get_changed_refs, generate_pack_data)
 
620
 
 
621
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
622
                      progress=None):
 
623
        fd, path = tempfile.mkstemp(suffix=".pack")
 
624
        try:
 
625
            self.fetch_pack(determine_wants, graph_walker,
 
626
                lambda x: os.write(fd, x), progress)
 
627
        finally:
 
628
            os.close(fd)
 
629
        if os.path.getsize(path) == 0:
 
630
            return EmptyObjectStoreIterator()
 
631
        return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
 
632
 
 
633
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
 
634
        # This won't work for any round-tripped bzr revisions, but it's a start..
 
635
        try:
 
636
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
637
        except InvalidRevisionId:
 
638
            raise NoSuchRevision(self, bzr_revid)
 
639
 
 
640
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
 
641
        """Lookup a revision id.
 
642
 
 
643
        """
 
644
        if mapping is None:
 
645
            mapping = self.get_mapping()
 
646
        # Not really an easy way to parse foreign revids here..
 
647
        return mapping.revision_id_foreign_to_bzr(foreign_revid)
 
648
 
 
649
    def revision_tree(self, revid):
 
650
        raise GitSmartRemoteNotSupported(self.revision_tree, self)
 
651
 
 
652
    def get_revisions(self, revids):
 
653
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
 
654
 
 
655
    def has_revisions(self, revids):
 
656
        raise GitSmartRemoteNotSupported(self.get_revisions, self)
 
657
 
 
658
 
 
659
class RemoteGitTagDict(GitTags):
 
660
 
 
661
    def set_tag(self, name, revid):
 
662
        sha = self.branch.lookup_bzr_revision_id(revid)[0]
 
663
        self._set_ref(name, sha)
 
664
 
 
665
    def delete_tag(self, name):
 
666
        self._set_ref(name, dulwich.client.ZERO_SHA)
 
667
 
 
668
    def _set_ref(self, name, sha):
 
669
        ref = tag_name_to_ref(name)
 
670
        def get_changed_refs(old_refs):
 
671
            ret = dict(old_refs)
 
672
            if sha == dulwich.client.ZERO_SHA and ref not in ret:
 
673
                raise NoSuchTag(name)
 
674
            ret[ref] = sha
 
675
            return ret
 
676
        def generate_pack_data(have, want, ofs_delta=False):
 
677
            return pack_objects_to_data([])
 
678
        self.repository.send_pack(get_changed_refs, generate_pack_data)
 
679
 
 
680
 
 
681
class RemoteGitBranch(GitBranch):
 
682
 
 
683
    def __init__(self, controldir, repository, name):
 
684
        self._sha = None
 
685
        super(RemoteGitBranch, self).__init__(controldir, repository, name,
 
686
                RemoteGitBranchFormat())
 
687
 
 
688
    def last_revision_info(self):
 
689
        raise GitSmartRemoteNotSupported(self.last_revision_info, self)
 
690
 
 
691
    @property
 
692
    def user_url(self):
 
693
        return self.control_url
 
694
 
 
695
    @property
 
696
    def control_url(self):
 
697
        return self.base
 
698
 
 
699
    def revision_id_to_revno(self, revision_id):
 
700
        raise GitSmartRemoteNotSupported(self.revision_id_to_revno, self)
 
701
 
 
702
    def last_revision(self):
 
703
        return self.lookup_foreign_revision_id(self.head)
 
704
 
 
705
    @property
 
706
    def head(self):
 
707
        if self._sha is not None:
 
708
            return self._sha
 
709
        refs = self.controldir.get_refs_container()
 
710
        name = branch_name_to_ref(self.name)
 
711
        try:
 
712
            self._sha = refs[name]
 
713
        except KeyError:
 
714
            raise NoSuchRef(name, self.repository.user_url, refs)
 
715
        return self._sha
 
716
 
 
717
    def _synchronize_history(self, destination, revision_id):
 
718
        """See Branch._synchronize_history()."""
 
719
        destination.generate_revision_history(self.last_revision())
 
720
 
 
721
    def _get_parent_location(self):
 
722
        return None
 
723
 
 
724
    def get_push_location(self):
 
725
        return None
 
726
 
 
727
    def set_push_location(self, url):
 
728
        pass
 
729
 
 
730
    def _iter_tag_refs(self):
 
731
        """Iterate over the tag refs.
 
732
 
 
733
        :param refs: Refs dictionary (name -> git sha1)
 
734
        :return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
 
735
        """
 
736
        refs = self.controldir.get_refs_container()
 
737
        for ref_name, unpeeled in refs.as_dict().iteritems():
 
738
            try:
 
739
                tag_name = ref_to_tag_name(ref_name)
 
740
            except (ValueError, UnicodeDecodeError):
 
741
                continue
 
742
            peeled = refs.get_peeled(ref_name)
 
743
            if peeled is None:
 
744
                try:
 
745
                    peeled = refs.peel_sha(unpeeled).id
 
746
                except KeyError:
 
747
                    # Let's just hope it's a commit
 
748
                    peeled = unpeeled
 
749
            if type(tag_name) is not unicode:
 
750
                raise TypeError(tag_name)
 
751
            yield (ref_name, tag_name, peeled, unpeeled)
 
752
 
 
753
 
 
754
def remote_refs_dict_to_container(refs_dict, symrefs_dict={}):
 
755
    base = {}
 
756
    peeled = {}
 
757
    for k, v in refs_dict.iteritems():
 
758
        if is_peeled(k):
 
759
            peeled[k[:-3]] = v
 
760
        else:
 
761
            base[k] = v
 
762
            peeled[k] = v
 
763
    for name, target in symrefs_dict.iteritems():
 
764
        base[name] = SYMREF + target
 
765
    ret = DictRefsContainer(base)
 
766
    ret._peeled = peeled
 
767
    return ret