1 # client.py -- Implementation of the server side git protocols
2 # Copyright (C) 2008-2013 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 io import BytesIO
52 import urllib.request as urllib2
53 import urllib.parse as urlparse
55 from dulwich.errors import (
61 from dulwich.protocol import (
70 from dulwich.pack import (
73 from dulwich.refs import (
78 def _fileno_can_read(fileno):
79 """Check if a file descriptor is readable."""
80 return len(select.select([fileno], [], [], 0)[0]) > 0
82 COMMON_CAPABILITIES = ['ofs-delta', 'side-band-64k']
83 FETCH_CAPABILITIES = (['thin-pack', 'multi_ack', 'multi_ack_detailed'] +
85 SEND_CAPABILITIES = ['report-status'] + COMMON_CAPABILITIES
88 class ReportStatusParser(object):
89 """Handle status as reported by servers with 'report-status' capability.
94 self._pack_status = None
95 self._ref_status_ok = True
96 self._ref_statuses = []
99 """Check if there were any errors and, if so, raise exceptions.
101 :raise SendPackError: Raised when the server could not unpack
102 :raise UpdateRefsError: Raised when refs could not be updated
104 if self._pack_status not in ('unpack ok', None):
105 raise SendPackError(self._pack_status)
106 if not self._ref_status_ok:
109 for status in self._ref_statuses:
110 if ' ' not in status:
111 # malformed response, move on to the next one
113 status, ref = status.split(' ', 1)
117 ref, status = ref.split(' ', 1)
120 ref_status[ref] = status
121 raise UpdateRefsError('%s failed to update' %
122 ', '.join([ref for ref in ref_status
124 ref_status=ref_status)
126 def handle_packet(self, pkt):
129 :raise GitProtocolError: Raised when packets are received after a
133 raise GitProtocolError("received more data after status report")
137 if self._pack_status is None:
138 self._pack_status = pkt.strip()
140 ref_status = pkt.strip()
141 self._ref_statuses.append(ref_status)
142 if not ref_status.startswith('ok '):
143 self._ref_status_ok = False
146 def read_pkt_refs(proto):
147 server_capabilities = None
149 # Receive refs from server
150 for pkt in proto.read_pkt_seq():
151 (sha, ref) = pkt.rstrip('\n').split(None, 1)
153 raise GitProtocolError(ref)
154 if server_capabilities is None:
155 (ref, server_capabilities) = extract_capabilities(ref)
160 return refs, set(server_capabilities)
163 # TODO(durin42): this doesn't correctly degrade if the server doesn't
164 # support some capabilities. This should work properly with servers
165 # that don't support multi_ack.
166 class GitClient(object):
167 """Git smart server client.
171 def __init__(self, thin_packs=True, report_activity=None):
172 """Create a new GitClient instance.
174 :param thin_packs: Whether or not thin packs should be retrieved
175 :param report_activity: Optional callback for reporting transport
178 self._report_activity = report_activity
179 self._report_status_parser = None
180 self._fetch_capabilities = set(FETCH_CAPABILITIES)
181 self._send_capabilities = set(SEND_CAPABILITIES)
183 self._fetch_capabilities.remove('thin-pack')
185 def send_pack(self, path, determine_wants, generate_pack_contents,
186 progress=None, write_pack=write_pack_objects):
187 """Upload a pack to a remote repository.
189 :param path: Repository path
190 :param generate_pack_contents: Function that can return a sequence of
191 the shas of the objects to upload.
192 :param progress: Optional progress function
193 :param write_pack: Function called with (file, iterable of objects) to
194 write the objects returned by generate_pack_contents to the server.
196 :raises SendPackError: if server rejects the pack data
197 :raises UpdateRefsError: if the server supports report-status
198 and rejects ref updates
200 raise NotImplementedError(self.send_pack)
202 def fetch(self, path, target, determine_wants=None, progress=None):
203 """Fetch into a target repository.
205 :param path: Path to fetch from
206 :param target: Target repository to fetch into
207 :param determine_wants: Optional function to determine what refs
209 :param progress: Optional progress function
210 :return: remote refs as dictionary
212 if determine_wants is None:
213 determine_wants = target.object_store.determine_wants_all
214 f, commit, abort = target.object_store.add_pack()
216 result = self.fetch_pack(
217 path, determine_wants, target.get_graph_walker(), f.write,
226 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
228 """Retrieve a pack from a git smart server.
230 :param determine_wants: Callback that returns list of commits to fetch
231 :param graph_walker: Object with next() and ack().
232 :param pack_data: Callback called for each bit of data in the pack
233 :param progress: Callback for progress reports (strings)
235 raise NotImplementedError(self.fetch_pack)
237 def _parse_status_report(self, proto):
238 unpack = proto.read_pkt_line().strip()
239 if unpack != 'unpack ok':
241 # flush remaining error data
242 while st is not None:
243 st = proto.read_pkt_line()
244 raise SendPackError(unpack)
247 ref_status = proto.read_pkt_line()
249 ref_status = ref_status.strip()
250 statuses.append(ref_status)
251 if not ref_status.startswith('ok '):
253 ref_status = proto.read_pkt_line()
258 for status in statuses:
259 if ' ' not in status:
260 # malformed response, move on to the next one
262 status, ref = status.split(' ', 1)
266 ref, status = ref.split(' ', 1)
269 ref_status[ref] = status
270 raise UpdateRefsError('%s failed to update' %
271 ', '.join([ref for ref in ref_status
273 ref_status=ref_status)
275 def _read_side_band64k_data(self, proto, channel_callbacks):
276 """Read per-channel data.
278 This requires the side-band-64k capability.
280 :param proto: Protocol object to read from
281 :param channel_callbacks: Dictionary mapping channels to packet
282 handlers to use. None for a callback discards channel data.
284 for pkt in proto.read_pkt_seq():
285 channel = ord(pkt[0])
288 cb = channel_callbacks[channel]
290 raise AssertionError('Invalid sideband channel %d' % channel)
295 def _handle_receive_pack_head(self, proto, capabilities, old_refs,
297 """Handle the head of a 'git-receive-pack' request.
299 :param proto: Protocol object to read from
300 :param capabilities: List of negotiated capabilities
301 :param old_refs: Old refs, as received from the server
302 :param new_refs: New refs
303 :return: (have, want) tuple
306 have = [x for x in old_refs.values() if not x == ZERO_SHA]
307 sent_capabilities = False
309 for refname in set(new_refs.keys() + old_refs.keys()):
310 old_sha1 = old_refs.get(refname, ZERO_SHA)
311 new_sha1 = new_refs.get(refname, ZERO_SHA)
313 if old_sha1 != new_sha1:
314 if sent_capabilities:
315 proto.write_pkt_line('%s %s %s' % (
316 old_sha1, new_sha1, refname))
318 proto.write_pkt_line(
319 '%s %s %s\0%s' % (old_sha1, new_sha1, refname,
320 ' '.join(capabilities)))
321 sent_capabilities = True
322 if new_sha1 not in have and new_sha1 != ZERO_SHA:
323 want.append(new_sha1)
324 proto.write_pkt_line(None)
327 def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
328 """Handle the tail of a 'git-receive-pack' request.
330 :param proto: Protocol object to read from
331 :param capabilities: List of negotiated capabilities
332 :param progress: Optional progress reporting function
334 if "side-band-64k" in capabilities:
336 progress = lambda x: None
337 channel_callbacks = {2: progress}
338 if 'report-status' in capabilities:
339 channel_callbacks[1] = PktLineParser(
340 self._report_status_parser.handle_packet).parse
341 self._read_side_band64k_data(proto, channel_callbacks)
343 if 'report-status' in capabilities:
344 for pkt in proto.read_pkt_seq():
345 self._report_status_parser.handle_packet(pkt)
346 if self._report_status_parser is not None:
347 self._report_status_parser.check()
349 def _handle_upload_pack_head(self, proto, capabilities, graph_walker,
351 """Handle the head of a 'git-upload-pack' request.
353 :param proto: Protocol object to read from
354 :param capabilities: List of negotiated capabilities
355 :param graph_walker: GraphWalker instance to call .ack() on
356 :param wants: List of commits to fetch
357 :param can_read: function that returns a boolean that indicates
358 whether there is extra graph data to read on proto
360 assert isinstance(wants, list) and isinstance(wants[0], str)
361 proto.write_pkt_line('want %s %s\n' % (
362 wants[0], ' '.join(capabilities)))
363 for want in wants[1:]:
364 proto.write_pkt_line('want %s\n' % want)
365 proto.write_pkt_line(None)
366 have = next(graph_walker)
368 proto.write_pkt_line('have %s\n' % have)
370 pkt = proto.read_pkt_line()
371 parts = pkt.rstrip('\n').split(' ')
372 if parts[0] == 'ACK':
373 graph_walker.ack(parts[1])
374 if parts[2] in ('continue', 'common'):
376 elif parts[2] == 'ready':
379 raise AssertionError(
380 "%s not in ('continue', 'ready', 'common)" %
382 have = next(graph_walker)
383 proto.write_pkt_line('done\n')
385 def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
386 pack_data, progress=None, rbufsize=_RBUFSIZE):
387 """Handle the tail of a 'git-upload-pack' request.
389 :param proto: Protocol object to read from
390 :param capabilities: List of negotiated capabilities
391 :param graph_walker: GraphWalker instance to call .ack() on
392 :param pack_data: Function to call with pack data
393 :param progress: Optional progress reporting function
394 :param rbufsize: Read buffer size
396 pkt = proto.read_pkt_line()
398 parts = pkt.rstrip('\n').split(' ')
399 if parts[0] == 'ACK':
400 graph_walker.ack(parts[1])
401 if len(parts) < 3 or parts[2] not in (
402 'ready', 'continue', 'common'):
404 pkt = proto.read_pkt_line()
405 if "side-band-64k" in capabilities:
407 # Just ignore progress data
408 progress = lambda x: None
409 self._read_side_band64k_data(proto, {1: pack_data, 2: progress})
412 data = proto.read(rbufsize)
418 class TraditionalGitClient(GitClient):
419 """Traditional Git client."""
421 def _connect(self, cmd, path):
422 """Create a connection to the server.
424 This method is abstract - concrete implementations should
425 implement their own variant which connects to the server and
426 returns an initialized Protocol object with the service ready
427 for use and a can_read function which may be used to see if
430 :param cmd: The git service name to which we should connect.
431 :param path: The path we should pass to the service.
433 raise NotImplementedError()
435 def send_pack(self, path, determine_wants, generate_pack_contents,
436 progress=None, write_pack=write_pack_objects):
437 """Upload a pack to a remote repository.
439 :param path: Repository path
440 :param generate_pack_contents: Function that can return a sequence of
441 the shas of the objects to upload.
442 :param progress: Optional callback called with progress updates
443 :param write_pack: Function called with (file, iterable of objects) to
444 write the objects returned by generate_pack_contents to the server.
446 :raises SendPackError: if server rejects the pack data
447 :raises UpdateRefsError: if the server supports report-status
448 and rejects ref updates
450 proto, unused_can_read = self._connect('receive-pack', path)
452 old_refs, server_capabilities = read_pkt_refs(proto)
453 negotiated_capabilities = self._send_capabilities & server_capabilities
455 if 'report-status' in negotiated_capabilities:
456 self._report_status_parser = ReportStatusParser()
457 report_status_parser = self._report_status_parser
460 new_refs = orig_new_refs = determine_wants(dict(old_refs))
462 proto.write_pkt_line(None)
465 if not 'delete-refs' in server_capabilities:
466 # Server does not support deletions. Fail later.
467 new_refs = dict(orig_new_refs)
468 for ref, sha in orig_new_refs.iteritems():
470 if 'report-status' in negotiated_capabilities:
471 report_status_parser._ref_statuses.append(
472 'ng %s remote does not support deleting refs'
474 report_status_parser._ref_status_ok = False
478 proto.write_pkt_line(None)
481 if len(new_refs) == 0 and len(orig_new_refs):
482 # NOOP - Original new refs filtered out by policy
483 proto.write_pkt_line(None)
484 if report_status_parser is not None:
485 report_status_parser.check()
488 (have, want) = self._handle_receive_pack_head(
489 proto, negotiated_capabilities, old_refs, new_refs)
490 if not want and old_refs == new_refs:
492 objects = generate_pack_contents(have, want)
494 dowrite = len(objects) > 0
495 dowrite = dowrite or any(old_refs.get(ref) != sha
496 for (ref, sha) in new_refs.iteritems()
499 write_pack(proto.write_file(), objects)
501 self._handle_receive_pack_tail(
502 proto, negotiated_capabilities, progress)
505 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
507 """Retrieve a pack from a git smart server.
509 :param determine_wants: Callback that returns list of commits to fetch
510 :param graph_walker: Object with next() and ack().
511 :param pack_data: Callback called for each bit of data in the pack
512 :param progress: Callback for progress reports (strings)
514 proto, can_read = self._connect('upload-pack', path)
516 refs, server_capabilities = read_pkt_refs(proto)
517 negotiated_capabilities = (
518 self._fetch_capabilities & server_capabilities)
521 proto.write_pkt_line(None)
525 wants = determine_wants(refs)
527 proto.write_pkt_line(None)
529 if wants is not None:
530 wants = [cid for cid in wants if cid != ZERO_SHA]
532 proto.write_pkt_line(None)
534 self._handle_upload_pack_head(
535 proto, negotiated_capabilities, graph_walker, wants, can_read)
536 self._handle_upload_pack_tail(
537 proto, negotiated_capabilities, graph_walker, pack_data, progress)
540 def archive(self, path, committish, write_data, progress=None,
542 proto, can_read = self._connect(b'upload-archive', path)
544 proto.write_pkt_line("argument %s" % committish)
545 proto.write_pkt_line(None)
546 pkt = proto.read_pkt_line()
551 elif pkt.startswith("ERR "):
552 raise GitProtocolError(pkt[4:].rstrip("\n"))
554 raise AssertionError("invalid response %r" % pkt)
555 ret = proto.read_pkt_line()
557 raise AssertionError("expected pkt tail")
558 self._read_side_band64k_data(proto, {
559 1: write_data, 2: progress, 3: write_error})
562 class TCPGitClient(TraditionalGitClient):
563 """A Git Client that works over TCP directly (i.e. git://)."""
565 def __init__(self, host, port=None, *args, **kwargs):
570 TraditionalGitClient.__init__(self, *args, **kwargs)
572 def _connect(self, cmd, path):
573 sockaddrs = socket.getaddrinfo(
574 self._host, self._port, socket.AF_UNSPEC, socket.SOCK_STREAM)
576 err = socket.error("no address found for %s" % self._host)
577 for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
578 s = socket.socket(family, socktype, proto)
579 s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
583 except socket.error as err:
589 # -1 means system default buffering
590 rfile = s.makefile('rb', -1)
592 wfile = s.makefile('wb', 0)
598 proto = Protocol(rfile.read, wfile.write, close,
599 report_activity=self._report_activity)
600 if path.startswith("/~"):
602 proto.send_cmd('git-%s' % cmd, path, 'host=%s' % self._host)
603 return proto, lambda: _fileno_can_read(s)
606 class SubprocessWrapper(object):
607 """A socket-like object that talks to a subprocess via pipes."""
609 def __init__(self, proc):
611 self.read = proc.stdout.read
612 self.write = proc.stdin.write
615 if subprocess.mswindows:
616 from msvcrt import get_osfhandle
617 from win32pipe import PeekNamedPipe
618 handle = get_osfhandle(self.proc.stdout.fileno())
619 return PeekNamedPipe(handle, 0)[2] != 0
621 return _fileno_can_read(self.proc.stdout.fileno())
624 self.proc.stdin.close()
625 self.proc.stdout.close()
627 self.proc.stderr.close()
631 class SubprocessGitClient(TraditionalGitClient):
632 """Git client that talks to a server using a subprocess."""
634 def __init__(self, *args, **kwargs):
635 self._connection = None
637 self._stderr = kwargs.get('stderr')
638 if 'stderr' in kwargs:
640 TraditionalGitClient.__init__(self, *args, **kwargs)
642 def _connect(self, service, path):
644 argv = ['git', service, path]
645 p = SubprocessWrapper(
646 subprocess.Popen(argv, bufsize=0, stdin=subprocess.PIPE,
647 stdout=subprocess.PIPE,
648 stderr=self._stderr))
649 return Protocol(p.read, p.write, p.close,
650 report_activity=self._report_activity), p.can_read
653 class LocalGitClient(GitClient):
654 """Git Client that just uses a local Repo."""
656 def __init__(self, thin_packs=True, report_activity=None):
657 """Create a new LocalGitClient instance.
659 :param path: Path to the local repository
660 :param thin_packs: Whether or not thin packs should be retrieved
661 :param report_activity: Optional callback for reporting transport
664 self._report_activity = report_activity
665 # Ignore the thin_packs argument
667 def send_pack(self, path, determine_wants, generate_pack_contents,
668 progress=None, write_pack=write_pack_objects):
669 """Upload a pack to a remote repository.
671 :param path: Repository path
672 :param generate_pack_contents: Function that can return a sequence of
673 the shas of the objects to upload.
674 :param progress: Optional progress function
675 :param write_pack: Function called with (file, iterable of objects) to
676 write the objects returned by generate_pack_contents to the server.
678 :raises SendPackError: if server rejects the pack data
679 :raises UpdateRefsError: if the server supports report-status
680 and rejects ref updates
682 raise NotImplementedError(self.send_pack)
684 def fetch(self, path, target, determine_wants=None, progress=None):
685 """Fetch into a target repository.
687 :param path: Path to fetch from
688 :param target: Target repository to fetch into
689 :param determine_wants: Optional function to determine what refs
691 :param progress: Optional progress function
692 :return: remote refs as dictionary
694 from dulwich.repo import Repo
696 return r.fetch(target, determine_wants=determine_wants,
699 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
701 """Retrieve a pack from a git smart server.
703 :param determine_wants: Callback that returns list of commits to fetch
704 :param graph_walker: Object with next() and ack().
705 :param pack_data: Callback called for each bit of data in the pack
706 :param progress: Callback for progress reports (strings)
708 from dulwich.repo import Repo
710 objects_iter = r.fetch_objects(determine_wants, graph_walker, progress)
712 # Did the process short-circuit (e.g. in a stateless RPC call)? Note
713 # that the client still expects a 0-object pack in most cases.
714 if objects_iter is None:
716 write_pack_objects(ProtocolFile(None, pack_data), objects_iter)
719 # What Git client to use for local access
720 default_local_git_client_cls = SubprocessGitClient
723 class SSHVendor(object):
724 """A client side SSH implementation."""
726 def connect_ssh(self, host, command, username=None, port=None):
729 "SSHVendor.connect_ssh has been renamed to SSHVendor.run_command",
731 return self.run_command(host, command, username=username, port=port)
733 def run_command(self, host, command, username=None, port=None):
734 """Connect to an SSH server.
736 Run a command remotely and return a file-like object for interaction
737 with the remote command.
739 :param host: Host name
740 :param command: Command to run
741 :param username: Optional ame of user to log in as
742 :param port: Optional SSH port to use
744 raise NotImplementedError(self.run_command)
747 class SubprocessSSHVendor(SSHVendor):
748 """SSH vendor that shells out to the local 'ssh' command."""
750 def run_command(self, host, command, username=None, port=None):
752 #FIXME: This has no way to deal with passwords..
755 args.extend(['-p', str(port)])
756 if username is not None:
757 host = '%s@%s' % (username, host)
759 proc = subprocess.Popen(args + command,
760 stdin=subprocess.PIPE,
761 stdout=subprocess.PIPE)
762 return SubprocessWrapper(proc)
772 class ParamikoWrapper(object):
773 STDERR_READ_N = 2048 # 2k
775 def __init__(self, client, channel, progress_stderr=None):
777 self.channel = channel
778 self.progress_stderr = progress_stderr
779 self.should_monitor = bool(progress_stderr) or True
780 self.monitor_thread = None
784 self.channel.setblocking(True)
787 if self.should_monitor:
788 self.monitor_thread = threading.Thread(
789 target=self.monitor_stderr)
790 self.monitor_thread.start()
792 def monitor_stderr(self):
793 while self.should_monitor:
795 data = self.read_stderr(self.STDERR_READ_N)
799 self.should_monitor = False
803 if self.progress_stderr:
804 self.progress_stderr(data)
809 def stop_monitoring(self):
811 if self.should_monitor:
812 self.should_monitor = False
813 self.monitor_thread.join()
816 data = self.channel.in_stderr_buffer.empty()
820 return self.channel.recv_ready()
822 def write(self, data):
823 return self.channel.sendall(data)
825 def read_stderr(self, n):
826 return self.channel.recv_stderr(n)
828 def read(self, n=None):
829 data = self.channel.recv(n)
836 # Read more if needed
837 if n and data_len < n:
838 diff_len = n - data_len
839 return data + self.read(diff_len)
844 self.stop_monitoring()
846 class ParamikoSSHVendor(object):
851 def run_command(self, host, command, username=None, port=None,
852 progress_stderr=None):
854 # Paramiko needs an explicit port. None is not valid
858 client = paramiko.SSHClient()
860 policy = paramiko.client.MissingHostKeyPolicy()
861 client.set_missing_host_key_policy(policy)
862 client.connect(host, username=username, port=port,
866 channel = client.get_transport().open_session()
869 channel.exec_command(*command)
871 return ParamikoWrapper(
872 client, channel, progress_stderr=progress_stderr)
875 # Can be overridden by users
876 get_ssh_vendor = SubprocessSSHVendor
879 class SSHGitClient(TraditionalGitClient):
881 def __init__(self, host, port=None, username=None, *args, **kwargs):
884 self.username = username
885 TraditionalGitClient.__init__(self, *args, **kwargs)
886 self.alternative_paths = {}
888 def _get_cmd_path(self, cmd):
889 return self.alternative_paths.get(cmd, 'git-%s' % cmd)
891 def _connect(self, cmd, path):
892 if path.startswith("/~"):
894 con = get_ssh_vendor().run_command(
895 self.host, ["%s '%s'" % (self._get_cmd_path(cmd), path)],
896 port=self.port, username=self.username)
897 return (Protocol(con.read, con.write, con.close,
898 report_activity=self._report_activity),
902 def default_user_agent_string():
903 return "dulwich/%s" % ".".join([str(x) for x in dulwich.__version__])
906 def default_urllib2_opener(config):
907 if config is not None:
908 proxy_server = config.get("http", "proxy")
912 if proxy_server is not None:
913 handlers.append(urllib2.ProxyHandler({"http": proxy_server}))
914 opener = urllib2.build_opener(*handlers)
915 if config is not None:
916 user_agent = config.get("http", "useragent")
919 if user_agent is None:
920 user_agent = default_user_agent_string()
921 opener.addheaders = [('User-agent', user_agent)]
925 class HttpGitClient(GitClient):
927 def __init__(self, base_url, dumb=None, opener=None, config=None, *args,
929 self.base_url = base_url.rstrip("/") + "/"
932 self.opener = default_urllib2_opener(config)
935 GitClient.__init__(self, *args, **kwargs)
937 def _get_url(self, path):
938 return urlparse.urljoin(self.base_url, path).rstrip("/") + "/"
940 def _http_request(self, url, headers={}, data=None):
941 req = urllib2.Request(url, headers=headers, data=data)
943 resp = self.opener.open(req)
944 except urllib2.HTTPError as e:
946 raise NotGitRepository()
948 raise GitProtocolError("unexpected http response %d" % e.code)
951 def _discover_references(self, service, url):
952 assert url[-1] == "/"
953 url = urlparse.urljoin(url, "info/refs")
955 if self.dumb is not False:
956 url += "?service=%s" % service
957 headers["Content-Type"] = "application/x-%s-request" % service
958 resp = self._http_request(url, headers)
960 self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
962 proto = Protocol(resp.read, None)
963 # The first line should mention the service
964 pkts = list(proto.read_pkt_seq())
965 if pkts != [('# service=%s\n' % service)]:
966 raise GitProtocolError(
967 "unexpected first line %r from smart server" % pkts)
968 return read_pkt_refs(proto)
970 return read_info_refs(resp), set()
974 def _smart_request(self, service, url, data):
975 assert url[-1] == "/"
976 url = urlparse.urljoin(url, service)
977 headers = {"Content-Type": "application/x-%s-request" % service}
978 resp = self._http_request(url, headers, data)
979 if resp.info().gettype() != ("application/x-%s-result" % service):
980 raise GitProtocolError("Invalid content-type from server: %s"
981 % resp.info().gettype())
984 def send_pack(self, path, determine_wants, generate_pack_contents,
985 progress=None, write_pack=write_pack_objects):
986 """Upload a pack to a remote repository.
988 :param path: Repository path
989 :param generate_pack_contents: Function that can return a sequence of
990 the shas of the objects to upload.
991 :param progress: Optional progress function
992 :param write_pack: Function called with (file, iterable of objects) to
993 write the objects returned by generate_pack_contents to the server.
995 :raises SendPackError: if server rejects the pack data
996 :raises UpdateRefsError: if the server supports report-status
997 and rejects ref updates
999 url = self._get_url(path)
1000 old_refs, server_capabilities = self._discover_references(
1001 "git-receive-pack", url)
1002 negotiated_capabilities = self._send_capabilities & server_capabilities
1004 if 'report-status' in negotiated_capabilities:
1005 self._report_status_parser = ReportStatusParser()
1007 new_refs = determine_wants(dict(old_refs))
1008 if new_refs is None:
1011 raise NotImplementedError(self.fetch_pack)
1012 req_data = BytesIO()
1013 req_proto = Protocol(None, req_data.write)
1014 (have, want) = self._handle_receive_pack_head(
1015 req_proto, negotiated_capabilities, old_refs, new_refs)
1016 if not want and old_refs == new_refs:
1018 objects = generate_pack_contents(have, want)
1019 if len(objects) > 0:
1020 write_pack(req_proto.write_file(), objects)
1021 resp = self._smart_request("git-receive-pack", url,
1022 data=req_data.getvalue())
1024 resp_proto = Protocol(resp.read, None)
1025 self._handle_receive_pack_tail(resp_proto, negotiated_capabilities,
1032 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
1034 """Retrieve a pack from a git smart server.
1036 :param determine_wants: Callback that returns list of commits to fetch
1037 :param graph_walker: Object with next() and ack().
1038 :param pack_data: Callback called for each bit of data in the pack
1039 :param progress: Callback for progress reports (strings)
1040 :return: Dictionary with the refs of the remote repository
1042 url = self._get_url(path)
1043 refs, server_capabilities = self._discover_references(
1044 "git-upload-pack", url)
1045 negotiated_capabilities = self._fetch_capabilities & server_capabilities
1046 wants = determine_wants(refs)
1047 if wants is not None:
1048 wants = [cid for cid in wants if cid != ZERO_SHA]
1052 raise NotImplementedError(self.send_pack)
1053 req_data = BytesIO()
1054 req_proto = Protocol(None, req_data.write)
1055 self._handle_upload_pack_head(
1056 req_proto, negotiated_capabilities, graph_walker, wants,
1058 resp = self._smart_request(
1059 "git-upload-pack", url, data=req_data.getvalue())
1061 resp_proto = Protocol(resp.read, None)
1062 self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,
1063 graph_walker, pack_data, progress)
1069 def get_transport_and_path_from_url(url, config=None, **kwargs):
1070 """Obtain a git client from a URL.
1072 :param url: URL to open
1073 :param config: Optional config object
1074 :param thin_packs: Whether or not thin packs should be retrieved
1075 :param report_activity: Optional callback for reporting transport
1077 :return: Tuple with client instance and relative path.
1079 parsed = urlparse.urlparse(url)
1080 if parsed.scheme == 'git':
1081 return (TCPGitClient(parsed.hostname, port=parsed.port, **kwargs),
1083 elif parsed.scheme == 'git+ssh':
1085 if path.startswith('/'):
1086 path = parsed.path[1:]
1087 return SSHGitClient(parsed.hostname, port=parsed.port,
1088 username=parsed.username, **kwargs), path
1089 elif parsed.scheme in ('http', 'https'):
1090 return HttpGitClient(urlparse.urlunparse(parsed), config=config,
1091 **kwargs), parsed.path
1092 elif parsed.scheme == 'file':
1093 return default_local_git_client_cls(**kwargs), parsed.path
1095 raise ValueError("unknown scheme '%s'" % parsed.scheme)
1098 def get_transport_and_path(location, **kwargs):
1099 """Obtain a git client from a URL.
1101 :param location: URL or path
1102 :param config: Optional config object
1103 :param thin_packs: Whether or not thin packs should be retrieved
1104 :param report_activity: Optional callback for reporting transport
1106 :return: Tuple with client instance and relative path.
1108 # First, try to parse it as a URL
1110 return get_transport_and_path_from_url(location, **kwargs)
1114 if (sys.platform == 'win32' and
1115 location[0].isalpha() and location[1:2] == ':\\'):
1116 # Windows local path
1117 return default_local_git_client_cls(**kwargs), location
1119 if ':' in location and not '@' in location:
1120 # SSH with no user@, zero or one leading slash.
1121 (hostname, path) = location.split(':')
1122 return SSHGitClient(hostname, **kwargs), path
1123 elif '@' in location and ':' in location:
1124 # SSH with user@host:foo.
1125 user_host, path = location.split(':')
1126 user, host = user_host.rsplit('@')
1127 return SSHGitClient(host, username=user, **kwargs), path
1129 # Otherwise, assume it's a local path.
1130 return default_local_git_client_cls(**kwargs), location