1 # client.py -- Implementation of the server side git protocols
2 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3 # Copyright (C) 2008 John Carr
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # or (at your option) a later version of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 """Client side support for the Git protocol.
22 The Dulwich client supports the following capabilities:
32 Known capabilities that are not supported:
39 __docformat__ = 'restructuredText'
41 from cStringIO import StringIO
48 from dulwich.errors import (
54 from dulwich.protocol import (
62 from dulwich.pack import (
67 # Python 2.6.6 included these in urlparse.uses_netloc upstream. Do
68 # monkeypatching to enable similar behaviour in earlier Pythons:
69 for scheme in ('git', 'git+ssh'):
70 if scheme not in urlparse.uses_netloc:
71 urlparse.uses_netloc.append(scheme)
73 def _fileno_can_read(fileno):
74 """Check if a file descriptor is readable."""
75 return len(select.select([fileno], [], [], 0)[0]) > 0
77 COMMON_CAPABILITIES = ['ofs-delta', 'side-band-64k']
78 FETCH_CAPABILITIES = ['thin-pack', 'multi_ack', 'multi_ack_detailed'] + COMMON_CAPABILITIES
79 SEND_CAPABILITIES = ['report-status'] + COMMON_CAPABILITIES
82 class ReportStatusParser(object):
83 """Handle status as reported by servers with the 'report-status' capability.
88 self._pack_status = None
89 self._ref_status_ok = True
90 self._ref_statuses = []
93 """Check if there were any errors and, if so, raise exceptions.
95 :raise SendPackError: Raised when the server could not unpack
96 :raise UpdateRefsError: Raised when refs could not be updated
98 if self._pack_status not in ('unpack ok', None):
99 raise SendPackError(self._pack_status)
100 if not self._ref_status_ok:
103 for status in self._ref_statuses:
104 if ' ' not in status:
105 # malformed response, move on to the next one
107 status, ref = status.split(' ', 1)
111 ref, status = ref.split(' ', 1)
114 ref_status[ref] = status
115 raise UpdateRefsError('%s failed to update' %
116 ', '.join([ref for ref in ref_status
118 ref_status=ref_status)
120 def handle_packet(self, pkt):
123 :raise GitProtocolError: Raised when packets are received after a
127 raise GitProtocolError("received more data after status report")
131 if self._pack_status is None:
132 self._pack_status = pkt.strip()
134 ref_status = pkt.strip()
135 self._ref_statuses.append(ref_status)
136 if not ref_status.startswith('ok '):
137 self._ref_status_ok = False
140 # TODO(durin42): this doesn't correctly degrade if the server doesn't
141 # support some capabilities. This should work properly with servers
142 # that don't support multi_ack.
143 class GitClient(object):
144 """Git smart server client.
148 def __init__(self, thin_packs=True, report_activity=None):
149 """Create a new GitClient instance.
151 :param thin_packs: Whether or not thin packs should be retrieved
152 :param report_activity: Optional callback for reporting transport
155 self._report_activity = report_activity
156 self._report_status_parser = None
157 self._fetch_capabilities = set(FETCH_CAPABILITIES)
158 self._send_capabilities = set(SEND_CAPABILITIES)
160 self._fetch_capabilities.remove('thin-pack')
162 def _read_refs(self, proto):
163 server_capabilities = None
165 # Receive refs from server
166 for pkt in proto.read_pkt_seq():
167 (sha, ref) = pkt.rstrip('\n').split(' ', 1)
169 raise GitProtocolError(ref)
170 if server_capabilities is None:
171 (ref, server_capabilities) = extract_capabilities(ref)
176 return refs, set(server_capabilities)
178 def send_pack(self, path, determine_wants, generate_pack_contents,
180 """Upload a pack to a remote repository.
182 :param path: Repository path
183 :param generate_pack_contents: Function that can return a sequence of the
184 shas of the objects to upload.
185 :param progress: Optional progress function
187 :raises SendPackError: if server rejects the pack data
188 :raises UpdateRefsError: if the server supports report-status
189 and rejects ref updates
191 raise NotImplementedError(self.send_pack)
193 def fetch(self, path, target, determine_wants=None, progress=None):
194 """Fetch into a target repository.
196 :param path: Path to fetch from
197 :param target: Target repository to fetch into
198 :param determine_wants: Optional function to determine what refs
200 :param progress: Optional progress function
201 :return: remote refs as dictionary
203 if determine_wants is None:
204 determine_wants = target.object_store.determine_wants_all
205 f, commit = target.object_store.add_pack()
206 result = self.fetch_pack(path, determine_wants,
207 target.get_graph_walker(), f.write, progress)
211 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
213 """Retrieve a pack from a git smart server.
215 :param determine_wants: Callback that returns list of commits to fetch
216 :param graph_walker: Object with next() and ack().
217 :param pack_data: Callback called for each bit of data in the pack
218 :param progress: Callback for progress reports (strings)
220 raise NotImplementedError(self.fetch_pack)
222 def _parse_status_report(self, proto):
223 unpack = proto.read_pkt_line().strip()
224 if unpack != 'unpack ok':
226 # flush remaining error data
227 while st is not None:
228 st = proto.read_pkt_line()
229 raise SendPackError(unpack)
232 ref_status = proto.read_pkt_line()
234 ref_status = ref_status.strip()
235 statuses.append(ref_status)
236 if not ref_status.startswith('ok '):
238 ref_status = proto.read_pkt_line()
243 for status in statuses:
244 if ' ' not in status:
245 # malformed response, move on to the next one
247 status, ref = status.split(' ', 1)
251 ref, status = ref.split(' ', 1)
254 ref_status[ref] = status
255 raise UpdateRefsError('%s failed to update' %
256 ', '.join([ref for ref in ref_status
258 ref_status=ref_status)
260 def _read_side_band64k_data(self, proto, channel_callbacks):
261 """Read per-channel data.
263 This requires the side-band-64k capability.
265 :param proto: Protocol object to read from
266 :param channel_callbacks: Dictionary mapping channels to packet
267 handlers to use. None for a callback discards channel data.
269 for pkt in proto.read_pkt_seq():
270 channel = ord(pkt[0])
273 cb = channel_callbacks[channel]
275 raise AssertionError('Invalid sideband channel %d' % channel)
280 def _handle_receive_pack_head(self, proto, capabilities, old_refs, new_refs):
281 """Handle the head of a 'git-receive-pack' request.
283 :param proto: Protocol object to read from
284 :param capabilities: List of negotiated capabilities
285 :param old_refs: Old refs, as received from the server
286 :param new_refs: New refs
287 :return: (have, want) tuple
290 have = [x for x in old_refs.values() if not x == ZERO_SHA]
291 sent_capabilities = False
293 for refname in set(new_refs.keys() + old_refs.keys()):
294 old_sha1 = old_refs.get(refname, ZERO_SHA)
295 new_sha1 = new_refs.get(refname, ZERO_SHA)
297 if old_sha1 != new_sha1:
298 if sent_capabilities:
299 proto.write_pkt_line('%s %s %s' % (old_sha1, new_sha1,
302 proto.write_pkt_line(
303 '%s %s %s\0%s' % (old_sha1, new_sha1, refname,
304 ' '.join(capabilities)))
305 sent_capabilities = True
306 if new_sha1 not in have and new_sha1 != ZERO_SHA:
307 want.append(new_sha1)
308 proto.write_pkt_line(None)
311 def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
312 """Handle the tail of a 'git-receive-pack' request.
314 :param proto: Protocol object to read from
315 :param capabilities: List of negotiated capabilities
316 :param progress: Optional progress reporting function
318 if "side-band-64k" in capabilities:
320 progress = lambda x: None
321 channel_callbacks = { 2: progress }
322 if 'report-status' in capabilities:
323 channel_callbacks[1] = PktLineParser(
324 self._report_status_parser.handle_packet).parse
325 self._read_side_band64k_data(proto, channel_callbacks)
327 if 'report-status' in capabilities:
328 for pkt in proto.read_pkt_seq():
329 self._report_status_parser.handle_packet(pkt)
330 if self._report_status_parser is not None:
331 self._report_status_parser.check()
332 # wait for EOF before returning
335 raise SendPackError('Unexpected response %r' % data)
337 def _handle_upload_pack_head(self, proto, capabilities, graph_walker,
339 """Handle the head of a 'git-upload-pack' request.
341 :param proto: Protocol object to read from
342 :param capabilities: List of negotiated capabilities
343 :param graph_walker: GraphWalker instance to call .ack() on
344 :param wants: List of commits to fetch
345 :param can_read: function that returns a boolean that indicates
346 whether there is extra graph data to read on proto
348 assert isinstance(wants, list) and type(wants[0]) == str
349 proto.write_pkt_line('want %s %s\n' % (
350 wants[0], ' '.join(capabilities)))
351 for want in wants[1:]:
352 proto.write_pkt_line('want %s\n' % want)
353 proto.write_pkt_line(None)
354 have = graph_walker.next()
356 proto.write_pkt_line('have %s\n' % have)
358 pkt = proto.read_pkt_line()
359 parts = pkt.rstrip('\n').split(' ')
360 if parts[0] == 'ACK':
361 graph_walker.ack(parts[1])
362 if parts[2] in ('continue', 'common'):
364 elif parts[2] == 'ready':
367 raise AssertionError(
368 "%s not in ('continue', 'ready', 'common)" %
370 have = graph_walker.next()
371 proto.write_pkt_line('done\n')
373 def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
374 pack_data, progress=None, rbufsize=_RBUFSIZE):
375 """Handle the tail of a 'git-upload-pack' request.
377 :param proto: Protocol object to read from
378 :param capabilities: List of negotiated capabilities
379 :param graph_walker: GraphWalker instance to call .ack() on
380 :param pack_data: Function to call with pack data
381 :param progress: Optional progress reporting function
382 :param rbufsize: Read buffer size
384 pkt = proto.read_pkt_line()
386 parts = pkt.rstrip('\n').split(' ')
387 if parts[0] == 'ACK':
388 graph_walker.ack(pkt.split(' ')[1])
389 if len(parts) < 3 or parts[2] not in (
390 'ready', 'continue', 'common'):
392 pkt = proto.read_pkt_line()
393 if "side-band-64k" in capabilities:
395 # Just ignore progress data
396 progress = lambda x: None
397 self._read_side_band64k_data(proto, {1: pack_data, 2: progress})
398 # wait for EOF before returning
401 raise Exception('Unexpected response %r' % data)
404 data = proto.read(rbufsize)
410 class TraditionalGitClient(GitClient):
411 """Traditional Git client."""
413 def _connect(self, cmd, path):
414 """Create a connection to the server.
416 This method is abstract - concrete implementations should
417 implement their own variant which connects to the server and
418 returns an initialized Protocol object with the service ready
419 for use and a can_read function which may be used to see if
422 :param cmd: The git service name to which we should connect.
423 :param path: The path we should pass to the service.
425 raise NotImplementedError()
427 def send_pack(self, path, determine_wants, generate_pack_contents,
429 """Upload a pack to a remote repository.
431 :param path: Repository path
432 :param generate_pack_contents: Function that can return a sequence of the
433 shas of the objects to upload.
434 :param progress: Optional callback called with progress updates
436 :raises SendPackError: if server rejects the pack data
437 :raises UpdateRefsError: if the server supports report-status
438 and rejects ref updates
440 proto, unused_can_read = self._connect('receive-pack', path)
441 old_refs, server_capabilities = self._read_refs(proto)
442 negotiated_capabilities = self._send_capabilities & server_capabilities
444 if 'report-status' in negotiated_capabilities:
445 self._report_status_parser = ReportStatusParser()
446 report_status_parser = self._report_status_parser
449 new_refs = orig_new_refs = determine_wants(dict(old_refs))
451 proto.write_pkt_line(None)
454 if not 'delete-refs' in server_capabilities:
455 # Server does not support deletions. Fail later.
456 def remove_del(pair):
457 if pair[1] == ZERO_SHA:
458 if 'report-status' in negotiated_capabilities:
459 report_status_parser._ref_statuses.append(
460 'ng %s remote does not support deleting refs'
462 report_status_parser._ref_status_ok = False
470 [(ref, sha) for ref, sha in new_refs.iteritems()]))
473 proto.write_pkt_line(None)
476 if len(new_refs) == 0 and len(orig_new_refs):
477 # NOOP - Original new refs filtered out by policy
478 proto.write_pkt_line(None)
479 if self._report_status_parser is not None:
480 self._report_status_parser.check()
483 (have, want) = self._handle_receive_pack_head(proto,
484 negotiated_capabilities, old_refs, new_refs)
485 if not want and old_refs == new_refs:
487 objects = generate_pack_contents(have, want)
489 entries, sha = write_pack_objects(proto.write_file(), objects)
490 elif len(set(new_refs.values()) - set([ZERO_SHA])) > 0:
491 # Check for valid create/update refs
492 filtered_new_refs = \
493 dict([(ref, sha) for ref, sha in new_refs.iteritems()
495 if len(set(filtered_new_refs.iteritems()) -
496 set(old_refs.iteritems())) > 0:
497 entries, sha = write_pack_objects(proto.write_file(), objects)
499 self._handle_receive_pack_tail(proto, negotiated_capabilities,
503 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
505 """Retrieve a pack from a git smart server.
507 :param determine_wants: Callback that returns list of commits to fetch
508 :param graph_walker: Object with next() and ack().
509 :param pack_data: Callback called for each bit of data in the pack
510 :param progress: Callback for progress reports (strings)
512 proto, can_read = self._connect('upload-pack', path)
513 refs, server_capabilities = self._read_refs(proto)
514 negotiated_capabilities = self._fetch_capabilities & server_capabilities
517 proto.write_pkt_line(None)
521 wants = determine_wants(refs)
523 proto.write_pkt_line(None)
525 if wants is not None:
526 wants = [cid for cid in wants if cid != ZERO_SHA]
528 proto.write_pkt_line(None)
530 self._handle_upload_pack_head(proto, negotiated_capabilities,
531 graph_walker, wants, can_read)
532 self._handle_upload_pack_tail(proto, negotiated_capabilities,
533 graph_walker, pack_data, progress)
536 def archive(self, path, committish, write_data, progress=None):
537 proto, can_read = self._connect('upload-archive', path)
538 proto.write_pkt_line("argument %s" % committish)
539 proto.write_pkt_line(None)
540 pkt = proto.read_pkt_line()
545 elif pkt.startswith("ERR "):
546 raise GitProtocolError(pkt[4:].rstrip("\n"))
548 raise AssertionError("invalid response %r" % pkt)
549 ret = proto.read_pkt_line()
551 raise AssertionError("expected pkt tail")
552 self._read_side_band64k_data(proto, {1: write_data, 2: progress})
555 class TCPGitClient(TraditionalGitClient):
556 """A Git Client that works over TCP directly (i.e. git://)."""
558 def __init__(self, host, port=None, *args, **kwargs):
563 TraditionalGitClient.__init__(self, *args, **kwargs)
565 def _connect(self, cmd, path):
566 sockaddrs = socket.getaddrinfo(self._host, self._port,
567 socket.AF_UNSPEC, socket.SOCK_STREAM)
569 err = socket.error("no address found for %s" % self._host)
570 for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
571 s = socket.socket(family, socktype, proto)
572 s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
576 except socket.error, err:
582 # -1 means system default buffering
583 rfile = s.makefile('rb', -1)
585 wfile = s.makefile('wb', 0)
586 proto = Protocol(rfile.read, wfile.write,
587 report_activity=self._report_activity)
588 if path.startswith("/~"):
590 proto.send_cmd('git-%s' % cmd, path, 'host=%s' % self._host)
591 return proto, lambda: _fileno_can_read(s)
594 class SubprocessWrapper(object):
595 """A socket-like object that talks to a subprocess via pipes."""
597 def __init__(self, proc):
599 self.read = proc.stdout.read
600 self.write = proc.stdin.write
603 if subprocess.mswindows:
604 from msvcrt import get_osfhandle
605 from win32pipe import PeekNamedPipe
606 handle = get_osfhandle(self.proc.stdout.fileno())
607 return PeekNamedPipe(handle, 0)[2] != 0
609 return _fileno_can_read(self.proc.stdout.fileno())
612 self.proc.stdin.close()
613 self.proc.stdout.close()
617 class SubprocessGitClient(TraditionalGitClient):
618 """Git client that talks to a server using a subprocess."""
620 def __init__(self, *args, **kwargs):
621 self._connection = None
623 self._stderr = kwargs.get('stderr')
624 if 'stderr' in kwargs:
626 TraditionalGitClient.__init__(self, *args, **kwargs)
628 def _connect(self, service, path):
630 argv = ['git', service, path]
631 p = SubprocessWrapper(
632 subprocess.Popen(argv, bufsize=0, stdin=subprocess.PIPE,
633 stdout=subprocess.PIPE,
634 stderr=self._stderr))
635 return Protocol(p.read, p.write,
636 report_activity=self._report_activity), p.can_read
639 class SSHVendor(object):
640 """A client side SSH implementation."""
642 def run_command(self, host, command, username=None, port=None):
643 """Connect to an SSH server.
645 Run a command remotely and return a file-like object for interaction
646 with the remote command.
648 :param host: Host name
649 :param command: Command to run
650 :param username: Optional ame of user to log in as
651 :param port: Optional SSH port to use
653 raise NotImplementedError(self.run_command)
656 class SubprocessSSHVendor(SSHVendor):
657 """SSH vendor that shells out to the local 'ssh' command."""
659 def run_command(self, host, command, username=None, port=None):
661 #FIXME: This has no way to deal with passwords..
664 args.extend(['-p', str(port)])
665 if username is not None:
666 host = '%s@%s' % (username, host)
668 proc = subprocess.Popen(args + command,
669 stdin=subprocess.PIPE,
670 stdout=subprocess.PIPE)
671 return SubprocessWrapper(proc)
681 class ParamikoWrapper(object):
682 STDERR_READ_N = 2048 # 2k
684 def __init__(self, client, channel, progress_stderr=None):
686 self.channel = channel
687 self.progress_stderr = progress_stderr
688 self.should_monitor = bool(progress_stderr) or True
689 self.monitor_thread = None
693 self.channel.setblocking(True)
696 if self.should_monitor:
697 self.monitor_thread = threading.Thread(target=self.monitor_stderr)
698 self.monitor_thread.start()
700 def monitor_stderr(self):
701 while self.should_monitor:
703 data = self.read_stderr(self.STDERR_READ_N)
707 self.should_monitor = False
711 if self.progress_stderr:
712 self.progress_stderr(data)
717 def stop_monitoring(self):
719 if self.should_monitor:
720 self.should_monitor = False
721 self.monitor_thread.join()
724 data = self.channel.in_stderr_buffer.empty()
728 return self.channel.recv_ready()
730 def write(self, data):
731 return self.channel.sendall(data)
733 def read_stderr(self, n):
734 return self.channel.recv_stderr(n)
736 def read(self, n=None):
737 data = self.channel.recv(n)
744 # Read more if needed
745 if n and data_len < n:
746 diff_len = n - data_len
747 return data + self.read(diff_len)
752 self.stop_monitoring()
757 class ParamikoSSHVendor(object):
759 def run_command(self, host, command, username=None, port=None,
760 progress_stderr=None, **kwargs):
761 client = paramiko.SSHClient()
763 policy = paramiko.client.MissingHostKeyPolicy()
764 client.set_missing_host_key_policy(policy)
765 client.connect(host, username=username, port=port, **kwargs)
768 channel = client.get_transport().open_session()
771 apply(channel.exec_command, command)
773 return ParamikoWrapper(client, channel,
774 progress_stderr=progress_stderr)
777 # Can be overridden by users
778 get_ssh_vendor = SubprocessSSHVendor
781 class SSHGitClient(TraditionalGitClient):
783 def __init__(self, host, port=None, username=None, *args, **kwargs):
786 self.username = username
787 TraditionalGitClient.__init__(self, *args, **kwargs)
788 self.alternative_paths = {}
790 def _get_cmd_path(self, cmd):
791 return self.alternative_paths.get(cmd, 'git-%s' % cmd)
793 def _connect(self, cmd, path):
794 if path.startswith("/~"):
796 con = get_ssh_vendor().run_command(
797 self.host, ["%s '%s'" % (self._get_cmd_path(cmd), path)],
798 port=self.port, username=self.username)
799 return (Protocol(con.read, con.write, report_activity=self._report_activity),
803 class HttpGitClient(GitClient):
805 def __init__(self, base_url, dumb=None, *args, **kwargs):
806 self.base_url = base_url.rstrip("/") + "/"
808 GitClient.__init__(self, *args, **kwargs)
810 def _get_url(self, path):
811 return urlparse.urljoin(self.base_url, path).rstrip("/") + "/"
813 def _http_request(self, url, headers={}, data=None):
814 req = urllib2.Request(url, headers=headers, data=data)
816 resp = self._perform(req)
817 except urllib2.HTTPError as e:
819 raise NotGitRepository()
821 raise GitProtocolError("unexpected http response %d" % e.code)
824 def _perform(self, req):
825 """Perform a HTTP request.
827 This is provided so subclasses can provide their own version.
829 :param req: urllib2.Request instance
830 :return: matching response
832 return urllib2.urlopen(req)
834 def _discover_references(self, service, url):
835 assert url[-1] == "/"
836 url = urlparse.urljoin(url, "info/refs")
838 if self.dumb != False:
839 url += "?service=%s" % service
840 headers["Content-Type"] = "application/x-%s-request" % service
841 resp = self._http_request(url, headers)
842 self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
843 proto = Protocol(resp.read, None)
845 # The first line should mention the service
846 pkts = list(proto.read_pkt_seq())
847 if pkts != [('# service=%s\n' % service)]:
848 raise GitProtocolError(
849 "unexpected first line %r from smart server" % pkts)
850 return self._read_refs(proto)
852 def _smart_request(self, service, url, data):
853 assert url[-1] == "/"
854 url = urlparse.urljoin(url, service)
855 headers = {"Content-Type": "application/x-%s-request" % service}
856 resp = self._http_request(url, headers, data)
857 if resp.info().gettype() != ("application/x-%s-result" % service):
858 raise GitProtocolError("Invalid content-type from server: %s"
859 % resp.info().gettype())
862 def send_pack(self, path, determine_wants, generate_pack_contents,
864 """Upload a pack to a remote repository.
866 :param path: Repository path
867 :param generate_pack_contents: Function that can return a sequence of the
868 shas of the objects to upload.
869 :param progress: Optional progress function
871 :raises SendPackError: if server rejects the pack data
872 :raises UpdateRefsError: if the server supports report-status
873 and rejects ref updates
875 url = self._get_url(path)
876 old_refs, server_capabilities = self._discover_references(
877 "git-receive-pack", url)
878 negotiated_capabilities = self._send_capabilities & server_capabilities
880 if 'report-status' in negotiated_capabilities:
881 self._report_status_parser = ReportStatusParser()
883 new_refs = determine_wants(dict(old_refs))
887 raise NotImplementedError(self.fetch_pack)
888 req_data = StringIO()
889 req_proto = Protocol(None, req_data.write)
890 (have, want) = self._handle_receive_pack_head(
891 req_proto, negotiated_capabilities, old_refs, new_refs)
892 if not want and old_refs == new_refs:
894 objects = generate_pack_contents(have, want)
896 entries, sha = write_pack_objects(req_proto.write_file(), objects)
897 resp = self._smart_request("git-receive-pack", url,
898 data=req_data.getvalue())
899 resp_proto = Protocol(resp.read, None)
900 self._handle_receive_pack_tail(resp_proto, negotiated_capabilities,
904 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
906 """Retrieve a pack from a git smart server.
908 :param determine_wants: Callback that returns list of commits to fetch
909 :param graph_walker: Object with next() and ack().
910 :param pack_data: Callback called for each bit of data in the pack
911 :param progress: Callback for progress reports (strings)
912 :return: Dictionary with the refs of the remote repository
914 url = self._get_url(path)
915 refs, server_capabilities = self._discover_references(
916 "git-upload-pack", url)
917 negotiated_capabilities = self._fetch_capabilities & server_capabilities
918 wants = determine_wants(refs)
919 if wants is not None:
920 wants = [cid for cid in wants if cid != ZERO_SHA]
924 raise NotImplementedError(self.send_pack)
925 req_data = StringIO()
926 req_proto = Protocol(None, req_data.write)
927 self._handle_upload_pack_head(req_proto,
928 negotiated_capabilities, graph_walker, wants,
930 resp = self._smart_request("git-upload-pack", url,
931 data=req_data.getvalue())
932 resp_proto = Protocol(resp.read, None)
933 self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,
934 graph_walker, pack_data, progress)
938 def get_transport_and_path(uri, **kwargs):
939 """Obtain a git client from a URI or path.
941 :param uri: URI or path
942 :param thin_packs: Whether or not thin packs should be retrieved
943 :param report_activity: Optional callback for reporting transport
945 :return: Tuple with client instance and relative path.
947 parsed = urlparse.urlparse(uri)
948 if parsed.scheme == 'git':
949 return (TCPGitClient(parsed.hostname, port=parsed.port, **kwargs),
951 elif parsed.scheme == 'git+ssh':
953 if path.startswith('/'):
954 path = parsed.path[1:]
955 return SSHGitClient(parsed.hostname, port=parsed.port,
956 username=parsed.username, **kwargs), path
957 elif parsed.scheme in ('http', 'https'):
958 return HttpGitClient(urlparse.urlunparse(parsed), **kwargs), parsed.path
960 if parsed.scheme and not parsed.netloc:
961 # SSH with no user@, zero or one leading slash.
962 return SSHGitClient(parsed.scheme, **kwargs), parsed.path
964 raise ValueError('Unknown git protocol scheme: %s' % parsed.scheme)
965 elif '@' in parsed.path and ':' in parsed.path:
966 # SSH with user@host:foo.
967 user_host, path = parsed.path.split(':')
968 user, host = user_host.rsplit('@')
969 return SSHGitClient(host, username=user, **kwargs), path
971 # Otherwise, assume it's a local path.
972 return SubprocessGitClient(**kwargs), uri