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 cStringIO import StringIO
48 from dulwich.errors import (
54 from dulwich.protocol import (
63 from dulwich.pack import (
66 from dulwich.refs import (
71 # Python 2.6.6 included these in urlparse.uses_netloc upstream. Do
72 # monkeypatching to enable similar behaviour in earlier Pythons:
73 for scheme in ('git', 'git+ssh'):
74 if scheme not in urlparse.uses_netloc:
75 urlparse.uses_netloc.append(scheme)
77 def _fileno_can_read(fileno):
78 """Check if a file descriptor is readable."""
79 return len(select.select([fileno], [], [], 0)[0]) > 0
81 COMMON_CAPABILITIES = ['ofs-delta', 'side-band-64k']
82 FETCH_CAPABILITIES = ['thin-pack', 'multi_ack', 'multi_ack_detailed'] + COMMON_CAPABILITIES
83 SEND_CAPABILITIES = ['report-status'] + COMMON_CAPABILITIES
86 class ReportStatusParser(object):
87 """Handle status as reported by servers with the 'report-status' capability.
92 self._pack_status = None
93 self._ref_status_ok = True
94 self._ref_statuses = []
97 """Check if there were any errors and, if so, raise exceptions.
99 :raise SendPackError: Raised when the server could not unpack
100 :raise UpdateRefsError: Raised when refs could not be updated
102 if self._pack_status not in ('unpack ok', None):
103 raise SendPackError(self._pack_status)
104 if not self._ref_status_ok:
107 for status in self._ref_statuses:
108 if ' ' not in status:
109 # malformed response, move on to the next one
111 status, ref = status.split(' ', 1)
115 ref, status = ref.split(' ', 1)
118 ref_status[ref] = status
119 raise UpdateRefsError('%s failed to update' %
120 ', '.join([ref for ref in ref_status
122 ref_status=ref_status)
124 def handle_packet(self, pkt):
127 :raise GitProtocolError: Raised when packets are received after a
131 raise GitProtocolError("received more data after status report")
135 if self._pack_status is None:
136 self._pack_status = pkt.strip()
138 ref_status = pkt.strip()
139 self._ref_statuses.append(ref_status)
140 if not ref_status.startswith('ok '):
141 self._ref_status_ok = False
144 def read_pkt_refs(proto):
145 server_capabilities = None
147 # Receive refs from server
148 for pkt in proto.read_pkt_seq():
149 (sha, ref) = pkt.rstrip('\n').split(None, 1)
151 raise GitProtocolError(ref)
152 if server_capabilities is None:
153 (ref, server_capabilities) = extract_capabilities(ref)
158 return refs, set(server_capabilities)
161 # TODO(durin42): this doesn't correctly degrade if the server doesn't
162 # support some capabilities. This should work properly with servers
163 # that don't support multi_ack.
164 class GitClient(object):
165 """Git smart server client.
169 def __init__(self, thin_packs=True, report_activity=None):
170 """Create a new GitClient instance.
172 :param thin_packs: Whether or not thin packs should be retrieved
173 :param report_activity: Optional callback for reporting transport
176 self._report_activity = report_activity
177 self._report_status_parser = None
178 self._fetch_capabilities = set(FETCH_CAPABILITIES)
179 self._send_capabilities = set(SEND_CAPABILITIES)
181 self._fetch_capabilities.remove('thin-pack')
183 def send_pack(self, path, determine_wants, generate_pack_contents,
185 """Upload a pack to a remote repository.
187 :param path: Repository path
188 :param generate_pack_contents: Function that can return a sequence of the
189 shas of the objects to upload.
190 :param progress: Optional progress function
192 :raises SendPackError: if server rejects the pack data
193 :raises UpdateRefsError: if the server supports report-status
194 and rejects ref updates
196 raise NotImplementedError(self.send_pack)
198 def fetch(self, path, target, determine_wants=None, progress=None):
199 """Fetch into a target repository.
201 :param path: Path to fetch from
202 :param target: Target repository to fetch into
203 :param determine_wants: Optional function to determine what refs
205 :param progress: Optional progress function
206 :return: remote refs as dictionary
208 if determine_wants is None:
209 determine_wants = target.object_store.determine_wants_all
210 f, commit, abort = target.object_store.add_pack()
212 result = self.fetch_pack(path, determine_wants,
213 target.get_graph_walker(), f.write, progress)
221 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
223 """Retrieve a pack from a git smart server.
225 :param determine_wants: Callback that returns list of commits to fetch
226 :param graph_walker: Object with next() and ack().
227 :param pack_data: Callback called for each bit of data in the pack
228 :param progress: Callback for progress reports (strings)
230 raise NotImplementedError(self.fetch_pack)
232 def _parse_status_report(self, proto):
233 unpack = proto.read_pkt_line().strip()
234 if unpack != 'unpack ok':
236 # flush remaining error data
237 while st is not None:
238 st = proto.read_pkt_line()
239 raise SendPackError(unpack)
242 ref_status = proto.read_pkt_line()
244 ref_status = ref_status.strip()
245 statuses.append(ref_status)
246 if not ref_status.startswith('ok '):
248 ref_status = proto.read_pkt_line()
253 for status in statuses:
254 if ' ' not in status:
255 # malformed response, move on to the next one
257 status, ref = status.split(' ', 1)
261 ref, status = ref.split(' ', 1)
264 ref_status[ref] = status
265 raise UpdateRefsError('%s failed to update' %
266 ', '.join([ref for ref in ref_status
268 ref_status=ref_status)
270 def _read_side_band64k_data(self, proto, channel_callbacks):
271 """Read per-channel data.
273 This requires the side-band-64k capability.
275 :param proto: Protocol object to read from
276 :param channel_callbacks: Dictionary mapping channels to packet
277 handlers to use. None for a callback discards channel data.
279 for pkt in proto.read_pkt_seq():
280 channel = ord(pkt[0])
283 cb = channel_callbacks[channel]
285 raise AssertionError('Invalid sideband channel %d' % channel)
290 def _handle_receive_pack_head(self, proto, capabilities, old_refs, new_refs):
291 """Handle the head of a 'git-receive-pack' request.
293 :param proto: Protocol object to read from
294 :param capabilities: List of negotiated capabilities
295 :param old_refs: Old refs, as received from the server
296 :param new_refs: New refs
297 :return: (have, want) tuple
300 have = [x for x in old_refs.values() if not x == ZERO_SHA]
301 sent_capabilities = False
303 for refname in set(new_refs.keys() + old_refs.keys()):
304 old_sha1 = old_refs.get(refname, ZERO_SHA)
305 new_sha1 = new_refs.get(refname, ZERO_SHA)
307 if old_sha1 != new_sha1:
308 if sent_capabilities:
309 proto.write_pkt_line('%s %s %s' % (old_sha1, new_sha1,
312 proto.write_pkt_line(
313 '%s %s %s\0%s' % (old_sha1, new_sha1, refname,
314 ' '.join(capabilities)))
315 sent_capabilities = True
316 if new_sha1 not in have and new_sha1 != ZERO_SHA:
317 want.append(new_sha1)
318 proto.write_pkt_line(None)
321 def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
322 """Handle the tail of a 'git-receive-pack' request.
324 :param proto: Protocol object to read from
325 :param capabilities: List of negotiated capabilities
326 :param progress: Optional progress reporting function
328 if "side-band-64k" in capabilities:
330 progress = lambda x: None
331 channel_callbacks = { 2: progress }
332 if 'report-status' in capabilities:
333 channel_callbacks[1] = PktLineParser(
334 self._report_status_parser.handle_packet).parse
335 self._read_side_band64k_data(proto, channel_callbacks)
337 if 'report-status' in capabilities:
338 for pkt in proto.read_pkt_seq():
339 self._report_status_parser.handle_packet(pkt)
340 if self._report_status_parser is not None:
341 self._report_status_parser.check()
342 # wait for EOF before returning
345 raise SendPackError('Unexpected response %r' % data)
347 def _handle_upload_pack_head(self, proto, capabilities, graph_walker,
349 """Handle the head of a 'git-upload-pack' request.
351 :param proto: Protocol object to read from
352 :param capabilities: List of negotiated capabilities
353 :param graph_walker: GraphWalker instance to call .ack() on
354 :param wants: List of commits to fetch
355 :param can_read: function that returns a boolean that indicates
356 whether there is extra graph data to read on proto
358 assert isinstance(wants, list) and type(wants[0]) == str
359 proto.write_pkt_line('want %s %s\n' % (
360 wants[0], ' '.join(capabilities)))
361 for want in wants[1:]:
362 proto.write_pkt_line('want %s\n' % want)
363 proto.write_pkt_line(None)
364 have = graph_walker.next()
366 proto.write_pkt_line('have %s\n' % have)
368 pkt = proto.read_pkt_line()
369 parts = pkt.rstrip('\n').split(' ')
370 if parts[0] == 'ACK':
371 graph_walker.ack(parts[1])
372 if parts[2] in ('continue', 'common'):
374 elif parts[2] == 'ready':
377 raise AssertionError(
378 "%s not in ('continue', 'ready', 'common)" %
380 have = graph_walker.next()
381 proto.write_pkt_line('done\n')
383 def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
384 pack_data, progress=None, rbufsize=_RBUFSIZE):
385 """Handle the tail of a 'git-upload-pack' request.
387 :param proto: Protocol object to read from
388 :param capabilities: List of negotiated capabilities
389 :param graph_walker: GraphWalker instance to call .ack() on
390 :param pack_data: Function to call with pack data
391 :param progress: Optional progress reporting function
392 :param rbufsize: Read buffer size
394 pkt = proto.read_pkt_line()
396 parts = pkt.rstrip('\n').split(' ')
397 if parts[0] == 'ACK':
398 graph_walker.ack(pkt.split(' ')[1])
399 if len(parts) < 3 or parts[2] not in (
400 'ready', 'continue', 'common'):
402 pkt = proto.read_pkt_line()
403 if "side-band-64k" in capabilities:
405 # Just ignore progress data
406 progress = lambda x: None
407 self._read_side_band64k_data(proto, {1: pack_data, 2: progress})
408 # wait for EOF before returning
411 raise Exception('Unexpected response %r' % data)
414 data = proto.read(rbufsize)
420 class TraditionalGitClient(GitClient):
421 """Traditional Git client."""
423 def _connect(self, cmd, path):
424 """Create a connection to the server.
426 This method is abstract - concrete implementations should
427 implement their own variant which connects to the server and
428 returns an initialized Protocol object with the service ready
429 for use and a can_read function which may be used to see if
432 :param cmd: The git service name to which we should connect.
433 :param path: The path we should pass to the service.
435 raise NotImplementedError()
437 def send_pack(self, path, determine_wants, generate_pack_contents,
439 """Upload a pack to a remote repository.
441 :param path: Repository path
442 :param generate_pack_contents: Function that can return a sequence of the
443 shas of the objects to upload.
444 :param progress: Optional callback called with progress updates
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)
451 old_refs, server_capabilities = read_pkt_refs(proto)
452 negotiated_capabilities = self._send_capabilities & server_capabilities
454 if 'report-status' in negotiated_capabilities:
455 self._report_status_parser = ReportStatusParser()
456 report_status_parser = self._report_status_parser
459 new_refs = orig_new_refs = determine_wants(dict(old_refs))
461 proto.write_pkt_line(None)
464 if not 'delete-refs' in server_capabilities:
465 # Server does not support deletions. Fail later.
466 def remove_del(pair):
467 if pair[1] == ZERO_SHA:
468 if 'report-status' in negotiated_capabilities:
469 report_status_parser._ref_statuses.append(
470 'ng %s remote does not support deleting refs'
472 report_status_parser._ref_status_ok = False
480 [(ref, sha) for ref, sha in new_refs.iteritems()]))
483 proto.write_pkt_line(None)
486 if len(new_refs) == 0 and len(orig_new_refs):
487 # NOOP - Original new refs filtered out by policy
488 proto.write_pkt_line(None)
489 if self._report_status_parser is not None:
490 self._report_status_parser.check()
493 (have, want) = self._handle_receive_pack_head(proto,
494 negotiated_capabilities, old_refs, new_refs)
495 if not want and old_refs == new_refs:
497 objects = generate_pack_contents(have, want)
499 entries, sha = write_pack_objects(proto.write_file(), objects)
500 elif len(set(new_refs.values()) - set([ZERO_SHA])) > 0:
501 # Check for valid create/update refs
502 filtered_new_refs = \
503 dict([(ref, sha) for ref, sha in new_refs.iteritems()
505 if len(set(filtered_new_refs.iteritems()) -
506 set(old_refs.iteritems())) > 0:
507 entries, sha = write_pack_objects(proto.write_file(), objects)
509 self._handle_receive_pack_tail(proto, negotiated_capabilities,
513 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
515 """Retrieve a pack from a git smart server.
517 :param determine_wants: Callback that returns list of commits to fetch
518 :param graph_walker: Object with next() and ack().
519 :param pack_data: Callback called for each bit of data in the pack
520 :param progress: Callback for progress reports (strings)
522 proto, can_read = self._connect('upload-pack', path)
523 refs, server_capabilities = read_pkt_refs(proto)
524 negotiated_capabilities = self._fetch_capabilities & server_capabilities
527 proto.write_pkt_line(None)
531 wants = determine_wants(refs)
533 proto.write_pkt_line(None)
535 if wants is not None:
536 wants = [cid for cid in wants if cid != ZERO_SHA]
538 proto.write_pkt_line(None)
540 self._handle_upload_pack_head(proto, negotiated_capabilities,
541 graph_walker, wants, can_read)
542 self._handle_upload_pack_tail(proto, negotiated_capabilities,
543 graph_walker, pack_data, progress)
546 def archive(self, path, committish, write_data, progress=None):
547 proto, can_read = self._connect('upload-archive', path)
548 proto.write_pkt_line("argument %s" % committish)
549 proto.write_pkt_line(None)
550 pkt = proto.read_pkt_line()
555 elif pkt.startswith("ERR "):
556 raise GitProtocolError(pkt[4:].rstrip("\n"))
558 raise AssertionError("invalid response %r" % pkt)
559 ret = proto.read_pkt_line()
561 raise AssertionError("expected pkt tail")
562 self._read_side_band64k_data(proto, {1: write_data, 2: progress})
565 class TCPGitClient(TraditionalGitClient):
566 """A Git Client that works over TCP directly (i.e. git://)."""
568 def __init__(self, host, port=None, *args, **kwargs):
573 TraditionalGitClient.__init__(self, *args, **kwargs)
575 def _connect(self, cmd, path):
576 sockaddrs = socket.getaddrinfo(self._host, self._port,
577 socket.AF_UNSPEC, socket.SOCK_STREAM)
579 err = socket.error("no address found for %s" % self._host)
580 for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
581 s = socket.socket(family, socktype, proto)
582 s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
586 except socket.error as err:
592 # -1 means system default buffering
593 rfile = s.makefile('rb', -1)
595 wfile = s.makefile('wb', 0)
596 proto = Protocol(rfile.read, wfile.write,
597 report_activity=self._report_activity)
598 if path.startswith("/~"):
600 proto.send_cmd('git-%s' % cmd, path, 'host=%s' % self._host)
601 return proto, lambda: _fileno_can_read(s)
604 class SubprocessWrapper(object):
605 """A socket-like object that talks to a subprocess via pipes."""
607 def __init__(self, proc):
609 self.read = proc.stdout.read
610 self.write = proc.stdin.write
613 if subprocess.mswindows:
614 from msvcrt import get_osfhandle
615 from win32pipe import PeekNamedPipe
616 handle = get_osfhandle(self.proc.stdout.fileno())
617 return PeekNamedPipe(handle, 0)[2] != 0
619 return _fileno_can_read(self.proc.stdout.fileno())
622 self.proc.stdin.close()
623 self.proc.stdout.close()
627 class SubprocessGitClient(TraditionalGitClient):
628 """Git client that talks to a server using a subprocess."""
630 def __init__(self, *args, **kwargs):
631 self._connection = None
633 self._stderr = kwargs.get('stderr')
634 if 'stderr' in kwargs:
636 TraditionalGitClient.__init__(self, *args, **kwargs)
638 def _connect(self, service, path):
640 argv = ['git', service, path]
641 p = SubprocessWrapper(
642 subprocess.Popen(argv, bufsize=0, stdin=subprocess.PIPE,
643 stdout=subprocess.PIPE,
644 stderr=self._stderr))
645 return Protocol(p.read, p.write,
646 report_activity=self._report_activity), p.can_read
649 class LocalGitClient(GitClient):
650 """Git Client that just uses a local Repo."""
652 def __init__(self, thin_packs=True, report_activity=None):
653 """Create a new LocalGitClient instance.
655 :param path: Path to the local repository
656 :param thin_packs: Whether or not thin packs should be retrieved
657 :param report_activity: Optional callback for reporting transport
660 self._report_activity = report_activity
661 # Ignore the thin_packs argument
663 def send_pack(self, path, determine_wants, generate_pack_contents,
665 """Upload a pack to a remote repository.
667 :param path: Repository path
668 :param generate_pack_contents: Function that can return a sequence of the
669 shas of the objects to upload.
670 :param progress: Optional progress function
672 :raises SendPackError: if server rejects the pack data
673 :raises UpdateRefsError: if the server supports report-status
674 and rejects ref updates
676 raise NotImplementedError(self.send_pack)
678 def fetch(self, path, target, determine_wants=None, progress=None):
679 """Fetch into a target repository.
681 :param path: Path to fetch from
682 :param target: Target repository to fetch into
683 :param determine_wants: Optional function to determine what refs
685 :param progress: Optional progress function
686 :return: remote refs as dictionary
688 from dulwich.repo import Repo
690 return r.fetch(target, determine_wants=determine_wants, progress=progress)
692 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
694 """Retrieve a pack from a git smart server.
696 :param determine_wants: Callback that returns list of commits to fetch
697 :param graph_walker: Object with next() and ack().
698 :param pack_data: Callback called for each bit of data in the pack
699 :param progress: Callback for progress reports (strings)
701 from dulwich.repo import Repo
703 objects_iter = r.fetch_objects(determine_wants, graph_walker, progress)
705 # Did the process short-circuit (e.g. in a stateless RPC call)? Note
706 # that the client still expects a 0-object pack in most cases.
707 if objects_iter is None:
709 write_pack_objects(ProtocolFile(None, pack_data), objects_iter)
712 # What Git client to use for local access
713 default_local_git_client_cls = SubprocessGitClient
715 class SSHVendor(object):
716 """A client side SSH implementation."""
718 def connect_ssh(self, host, command, username=None, port=None):
721 "SSHVendor.connect_ssh has been renamed to SSHVendor.run_command",
723 return self.run_command(host, command, username=username, port=port)
725 def run_command(self, host, command, username=None, port=None):
726 """Connect to an SSH server.
728 Run a command remotely and return a file-like object for interaction
729 with the remote command.
731 :param host: Host name
732 :param command: Command to run
733 :param username: Optional ame of user to log in as
734 :param port: Optional SSH port to use
736 raise NotImplementedError(self.run_command)
739 class SubprocessSSHVendor(SSHVendor):
740 """SSH vendor that shells out to the local 'ssh' command."""
742 def run_command(self, host, command, username=None, port=None):
744 #FIXME: This has no way to deal with passwords..
747 args.extend(['-p', str(port)])
748 if username is not None:
749 host = '%s@%s' % (username, host)
751 proc = subprocess.Popen(args + command,
752 stdin=subprocess.PIPE,
753 stdout=subprocess.PIPE)
754 return SubprocessWrapper(proc)
764 class ParamikoWrapper(object):
765 STDERR_READ_N = 2048 # 2k
767 def __init__(self, client, channel, progress_stderr=None):
769 self.channel = channel
770 self.progress_stderr = progress_stderr
771 self.should_monitor = bool(progress_stderr) or True
772 self.monitor_thread = None
776 self.channel.setblocking(True)
779 if self.should_monitor:
780 self.monitor_thread = threading.Thread(target=self.monitor_stderr)
781 self.monitor_thread.start()
783 def monitor_stderr(self):
784 while self.should_monitor:
786 data = self.read_stderr(self.STDERR_READ_N)
790 self.should_monitor = False
794 if self.progress_stderr:
795 self.progress_stderr(data)
800 def stop_monitoring(self):
802 if self.should_monitor:
803 self.should_monitor = False
804 self.monitor_thread.join()
807 data = self.channel.in_stderr_buffer.empty()
811 return self.channel.recv_ready()
813 def write(self, data):
814 return self.channel.sendall(data)
816 def read_stderr(self, n):
817 return self.channel.recv_stderr(n)
819 def read(self, n=None):
820 data = self.channel.recv(n)
827 # Read more if needed
828 if n and data_len < n:
829 diff_len = n - data_len
830 return data + self.read(diff_len)
835 self.stop_monitoring()
840 class ParamikoSSHVendor(object):
845 def run_command(self, host, command, username=None, port=None,
846 progress_stderr=None):
848 # Paramiko needs an explicit port. None is not valid
852 client = paramiko.SSHClient()
854 policy = paramiko.client.MissingHostKeyPolicy()
855 client.set_missing_host_key_policy(policy)
856 client.connect(host, username=username, port=port,
860 channel = client.get_transport().open_session()
863 apply(channel.exec_command, command)
865 return ParamikoWrapper(client, channel,
866 progress_stderr=progress_stderr)
869 # Can be overridden by users
870 get_ssh_vendor = SubprocessSSHVendor
873 class SSHGitClient(TraditionalGitClient):
875 def __init__(self, host, port=None, username=None, *args, **kwargs):
878 self.username = username
879 TraditionalGitClient.__init__(self, *args, **kwargs)
880 self.alternative_paths = {}
882 def _get_cmd_path(self, cmd):
883 return self.alternative_paths.get(cmd, 'git-%s' % cmd)
885 def _connect(self, cmd, path):
886 if path.startswith("/~"):
888 con = get_ssh_vendor().run_command(
889 self.host, ["%s '%s'" % (self._get_cmd_path(cmd), path)],
890 port=self.port, username=self.username)
891 return (Protocol(con.read, con.write, report_activity=self._report_activity),
895 class HttpGitClient(GitClient):
897 def __init__(self, base_url, dumb=None, handlers=None, *args, **kwargs):
898 self.base_url = base_url.rstrip("/") + "/"
900 self.handlers = handlers or []
901 GitClient.__init__(self, *args, **kwargs)
903 def _get_url(self, path):
904 return urlparse.urljoin(self.base_url, path).rstrip("/") + "/"
906 def _http_request(self, url, headers={}, data=None):
907 opener = urllib2.build_opener(*self.handlers)
908 urllib2.install_opener(opener)
909 req = urllib2.Request(url, headers=headers, data=data)
911 resp = self._perform(req)
912 except urllib2.HTTPError as e:
914 raise NotGitRepository()
916 raise GitProtocolError("unexpected http response %d" % e.code)
919 def _perform(self, req):
920 """Perform a HTTP request.
922 This is provided so subclasses can provide their own version.
924 :param req: urllib2.Request instance
925 :return: matching response
927 return urllib2.urlopen(req)
929 def _discover_references(self, service, url):
930 assert url[-1] == "/"
931 url = urlparse.urljoin(url, "info/refs")
933 if self.dumb != False:
934 url += "?service=%s" % service
935 headers["Content-Type"] = "application/x-%s-request" % service
936 resp = self._http_request(url, headers)
937 self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
939 proto = Protocol(resp.read, None)
940 # The first line should mention the service
941 pkts = list(proto.read_pkt_seq())
942 if pkts != [('# service=%s\n' % service)]:
943 raise GitProtocolError(
944 "unexpected first line %r from smart server" % pkts)
945 return read_pkt_refs(proto)
947 return read_info_refs(resp), set()
949 def _smart_request(self, service, url, data):
950 assert url[-1] == "/"
951 url = urlparse.urljoin(url, service)
952 headers = {"Content-Type": "application/x-%s-request" % service}
953 resp = self._http_request(url, headers, data)
954 if resp.info().gettype() != ("application/x-%s-result" % service):
955 raise GitProtocolError("Invalid content-type from server: %s"
956 % resp.info().gettype())
959 def send_pack(self, path, determine_wants, generate_pack_contents,
961 """Upload a pack to a remote repository.
963 :param path: Repository path
964 :param generate_pack_contents: Function that can return a sequence of the
965 shas of the objects to upload.
966 :param progress: Optional progress function
968 :raises SendPackError: if server rejects the pack data
969 :raises UpdateRefsError: if the server supports report-status
970 and rejects ref updates
972 url = self._get_url(path)
973 old_refs, server_capabilities = self._discover_references(
974 "git-receive-pack", url)
975 negotiated_capabilities = self._send_capabilities & server_capabilities
977 if 'report-status' in negotiated_capabilities:
978 self._report_status_parser = ReportStatusParser()
980 new_refs = determine_wants(dict(old_refs))
984 raise NotImplementedError(self.fetch_pack)
985 req_data = StringIO()
986 req_proto = Protocol(None, req_data.write)
987 (have, want) = self._handle_receive_pack_head(
988 req_proto, negotiated_capabilities, old_refs, new_refs)
989 if not want and old_refs == new_refs:
991 objects = generate_pack_contents(have, want)
993 entries, sha = write_pack_objects(req_proto.write_file(), objects)
994 resp = self._smart_request("git-receive-pack", url,
995 data=req_data.getvalue())
996 resp_proto = Protocol(resp.read, None)
997 self._handle_receive_pack_tail(resp_proto, negotiated_capabilities,
1001 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
1003 """Retrieve a pack from a git smart server.
1005 :param determine_wants: Callback that returns list of commits to fetch
1006 :param graph_walker: Object with next() and ack().
1007 :param pack_data: Callback called for each bit of data in the pack
1008 :param progress: Callback for progress reports (strings)
1009 :return: Dictionary with the refs of the remote repository
1011 url = self._get_url(path)
1012 refs, server_capabilities = self._discover_references(
1013 "git-upload-pack", url)
1014 negotiated_capabilities = self._fetch_capabilities & server_capabilities
1015 wants = determine_wants(refs)
1016 if wants is not None:
1017 wants = [cid for cid in wants if cid != ZERO_SHA]
1021 raise NotImplementedError(self.send_pack)
1022 req_data = StringIO()
1023 req_proto = Protocol(None, req_data.write)
1024 self._handle_upload_pack_head(req_proto,
1025 negotiated_capabilities, graph_walker, wants,
1027 resp = self._smart_request("git-upload-pack", url,
1028 data=req_data.getvalue())
1029 resp_proto = Protocol(resp.read, None)
1030 self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,
1031 graph_walker, pack_data, progress)
1035 def get_transport_and_path_from_url(url, **kwargs):
1036 """Obtain a git client from a URL.
1038 :param url: URL to open
1039 :param thin_packs: Whether or not thin packs should be retrieved
1040 :param report_activity: Optional callback for reporting transport
1042 :return: Tuple with client instance and relative path.
1044 parsed = urlparse.urlparse(url)
1045 if parsed.scheme == 'git':
1046 return (TCPGitClient(parsed.hostname, port=parsed.port, **kwargs),
1048 elif parsed.scheme == 'git+ssh':
1050 if path.startswith('/'):
1051 path = parsed.path[1:]
1052 return SSHGitClient(parsed.hostname, port=parsed.port,
1053 username=parsed.username, **kwargs), path
1054 elif parsed.scheme in ('http', 'https'):
1055 return HttpGitClient(urlparse.urlunparse(parsed), **kwargs), parsed.path
1056 elif parsed.scheme == 'file':
1057 return default_local_git_client_cls(**kwargs), parsed.path
1059 raise ValueError("unknown scheme '%s'" % parsed.scheme)
1062 def get_transport_and_path(location, **kwargs):
1063 """Obtain a git client from a URL.
1065 :param location: URL or path
1066 :param thin_packs: Whether or not thin packs should be retrieved
1067 :param report_activity: Optional callback for reporting transport
1069 :return: Tuple with client instance and relative path.
1071 # First, try to parse it as a URL
1073 return get_transport_and_path_from_url(location, **kwargs)
1077 if ':' in location and not '@' in location:
1078 # SSH with no user@, zero or one leading slash.
1079 (hostname, path) = location.split(':')
1080 return SSHGitClient(hostname, **kwargs), path
1081 elif '@' in location and ':' in location:
1082 # SSH with user@host:foo.
1083 user_host, path = location.split(':')
1084 user, host = user_host.rsplit('@')
1085 return SSHGitClient(host, username=user, **kwargs), path
1087 # Otherwise, assume it's a local path.
1088 return default_local_git_client_cls(**kwargs), location