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, BufferedReader
52 import urllib.request as urllib2
53 import urllib.parse as urlparse
55 from dulwich.errors import (
61 from dulwich.protocol import (
63 CAPABILITY_DELETE_REFS,
65 CAPABILITY_MULTI_ACK_DETAILED,
67 CAPABILITY_REPORT_STATUS,
68 CAPABILITY_SIDE_BAND_64K,
73 SIDE_BAND_CHANNEL_DATA,
74 SIDE_BAND_CHANNEL_PROGRESS,
75 SIDE_BAND_CHANNEL_FATAL,
83 from dulwich.pack import (
86 from dulwich.refs import (
91 def _fileno_can_read(fileno):
92 """Check if a file descriptor is readable."""
93 return len(select.select([fileno], [], [], 0)[0]) > 0
95 COMMON_CAPABILITIES = [CAPABILITY_OFS_DELTA, CAPABILITY_SIDE_BAND_64K]
96 FETCH_CAPABILITIES = ([CAPABILITY_THIN_PACK, CAPABILITY_MULTI_ACK,
97 CAPABILITY_MULTI_ACK_DETAILED] +
99 SEND_CAPABILITIES = [CAPABILITY_REPORT_STATUS] + COMMON_CAPABILITIES
102 class ReportStatusParser(object):
103 """Handle status as reported by servers with 'report-status' capability.
108 self._pack_status = None
109 self._ref_status_ok = True
110 self._ref_statuses = []
113 """Check if there were any errors and, if so, raise exceptions.
115 :raise SendPackError: Raised when the server could not unpack
116 :raise UpdateRefsError: Raised when refs could not be updated
118 if self._pack_status not in (b'unpack ok', None):
119 raise SendPackError(self._pack_status)
120 if not self._ref_status_ok:
123 for status in self._ref_statuses:
124 if b' ' not in status:
125 # malformed response, move on to the next one
127 status, ref = status.split(b' ', 1)
131 ref, status = ref.split(b' ', 1)
134 ref_status[ref] = status
135 # TODO(jelmer): don't assume encoding of refs is ascii.
136 raise UpdateRefsError(', '.join([
137 ref.decode('ascii') for ref in ref_status if ref not in ok]) +
138 ' failed to update', ref_status=ref_status)
140 def handle_packet(self, pkt):
143 :raise GitProtocolError: Raised when packets are received after a
147 raise GitProtocolError("received more data after status report")
151 if self._pack_status is None:
152 self._pack_status = pkt.strip()
154 ref_status = pkt.strip()
155 self._ref_statuses.append(ref_status)
156 if not ref_status.startswith(b'ok '):
157 self._ref_status_ok = False
160 def read_pkt_refs(proto):
161 server_capabilities = None
163 # Receive refs from server
164 for pkt in proto.read_pkt_seq():
165 (sha, ref) = pkt.rstrip(b'\n').split(None, 1)
167 raise GitProtocolError(ref)
168 if server_capabilities is None:
169 (ref, server_capabilities) = extract_capabilities(ref)
174 return refs, set(server_capabilities)
177 # TODO(durin42): this doesn't correctly degrade if the server doesn't
178 # support some capabilities. This should work properly with servers
179 # that don't support multi_ack.
180 class GitClient(object):
181 """Git smart server client.
185 def __init__(self, thin_packs=True, report_activity=None):
186 """Create a new GitClient instance.
188 :param thin_packs: Whether or not thin packs should be retrieved
189 :param report_activity: Optional callback for reporting transport
192 self._report_activity = report_activity
193 self._report_status_parser = None
194 self._fetch_capabilities = set(FETCH_CAPABILITIES)
195 self._send_capabilities = set(SEND_CAPABILITIES)
197 self._fetch_capabilities.remove(CAPABILITY_THIN_PACK)
199 def send_pack(self, path, determine_wants, generate_pack_contents,
200 progress=None, write_pack=write_pack_objects):
201 """Upload a pack to a remote repository.
203 :param path: Repository path
204 :param generate_pack_contents: Function that can return a sequence of
205 the shas of the objects to upload.
206 :param progress: Optional progress function
207 :param write_pack: Function called with (file, iterable of objects) to
208 write the objects returned by generate_pack_contents to the server.
210 :raises SendPackError: if server rejects the pack data
211 :raises UpdateRefsError: if the server supports report-status
212 and rejects ref updates
214 raise NotImplementedError(self.send_pack)
216 def fetch(self, path, target, determine_wants=None, progress=None):
217 """Fetch into a target repository.
219 :param path: Path to fetch from
220 :param target: Target repository to fetch into
221 :param determine_wants: Optional function to determine what refs
223 :param progress: Optional progress function
224 :return: remote refs as dictionary
226 if determine_wants is None:
227 determine_wants = target.object_store.determine_wants_all
228 f, commit, abort = target.object_store.add_pack()
230 result = self.fetch_pack(
231 path, determine_wants, target.get_graph_walker(), f.write,
240 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
242 """Retrieve a pack from a git smart server.
244 :param determine_wants: Callback that returns list of commits to fetch
245 :param graph_walker: Object with next() and ack().
246 :param pack_data: Callback called for each bit of data in the pack
247 :param progress: Callback for progress reports (strings)
249 raise NotImplementedError(self.fetch_pack)
251 def _parse_status_report(self, proto):
252 unpack = proto.read_pkt_line().strip()
253 if unpack != b'unpack ok':
255 # flush remaining error data
256 while st is not None:
257 st = proto.read_pkt_line()
258 raise SendPackError(unpack)
261 ref_status = proto.read_pkt_line()
263 ref_status = ref_status.strip()
264 statuses.append(ref_status)
265 if not ref_status.startswith(b'ok '):
267 ref_status = proto.read_pkt_line()
272 for status in statuses:
273 if b' ' not in status:
274 # malformed response, move on to the next one
276 status, ref = status.split(b' ', 1)
280 ref, status = ref.split(b' ', 1)
283 ref_status[ref] = status
284 raise UpdateRefsError(', '.join([ref for ref in ref_status
286 b' failed to update',
287 ref_status=ref_status)
289 def _read_side_band64k_data(self, proto, channel_callbacks):
290 """Read per-channel data.
292 This requires the side-band-64k capability.
294 :param proto: Protocol object to read from
295 :param channel_callbacks: Dictionary mapping channels to packet
296 handlers to use. None for a callback discards channel data.
298 for pkt in proto.read_pkt_seq():
299 channel = ord(pkt[:1])
302 cb = channel_callbacks[channel]
304 raise AssertionError('Invalid sideband channel %d' % channel)
309 def _handle_receive_pack_head(self, proto, capabilities, old_refs,
311 """Handle the head of a 'git-receive-pack' request.
313 :param proto: Protocol object to read from
314 :param capabilities: List of negotiated capabilities
315 :param old_refs: Old refs, as received from the server
316 :param new_refs: New refs
317 :return: (have, want) tuple
320 have = [x for x in old_refs.values() if not x == ZERO_SHA]
321 sent_capabilities = False
323 all_refs = set(new_refs.keys()).union(set(old_refs.keys()))
324 for refname in all_refs:
325 old_sha1 = old_refs.get(refname, ZERO_SHA)
326 new_sha1 = new_refs.get(refname, ZERO_SHA)
328 if old_sha1 != new_sha1:
329 if sent_capabilities:
330 proto.write_pkt_line(old_sha1 + b' ' + new_sha1 + b' ' + refname)
332 proto.write_pkt_line(
333 old_sha1 + b' ' + new_sha1 + b' ' + refname + b'\0' +
334 b' '.join(capabilities))
335 sent_capabilities = True
336 if new_sha1 not in have and new_sha1 != ZERO_SHA:
337 want.append(new_sha1)
338 proto.write_pkt_line(None)
341 def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
342 """Handle the tail of a 'git-receive-pack' request.
344 :param proto: Protocol object to read from
345 :param capabilities: List of negotiated capabilities
346 :param progress: Optional progress reporting function
348 if b"side-band-64k" in capabilities:
350 progress = lambda x: None
351 channel_callbacks = {2: progress}
352 if CAPABILITY_REPORT_STATUS in capabilities:
353 channel_callbacks[1] = PktLineParser(
354 self._report_status_parser.handle_packet).parse
355 self._read_side_band64k_data(proto, channel_callbacks)
357 if CAPABILITY_REPORT_STATUS in capabilities:
358 for pkt in proto.read_pkt_seq():
359 self._report_status_parser.handle_packet(pkt)
360 if self._report_status_parser is not None:
361 self._report_status_parser.check()
363 def _handle_upload_pack_head(self, proto, capabilities, graph_walker,
365 """Handle the head of a 'git-upload-pack' request.
367 :param proto: Protocol object to read from
368 :param capabilities: List of negotiated capabilities
369 :param graph_walker: GraphWalker instance to call .ack() on
370 :param wants: List of commits to fetch
371 :param can_read: function that returns a boolean that indicates
372 whether there is extra graph data to read on proto
374 assert isinstance(wants, list) and isinstance(wants[0], bytes)
375 proto.write_pkt_line(COMMAND_WANT + b' ' + wants[0] + b' ' + b' '.join(capabilities) + b'\n')
376 for want in wants[1:]:
377 proto.write_pkt_line(COMMAND_WANT + b' ' + want + b'\n')
378 proto.write_pkt_line(None)
379 have = next(graph_walker)
381 proto.write_pkt_line(COMMAND_HAVE + b' ' + have + b'\n')
383 pkt = proto.read_pkt_line()
384 parts = pkt.rstrip(b'\n').split(b' ')
385 if parts[0] == b'ACK':
386 graph_walker.ack(parts[1])
387 if parts[2] in (b'continue', b'common'):
389 elif parts[2] == b'ready':
392 raise AssertionError(
393 "%s not in ('continue', 'ready', 'common)" %
395 have = next(graph_walker)
396 proto.write_pkt_line(COMMAND_DONE + b'\n')
398 def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
399 pack_data, progress=None, rbufsize=_RBUFSIZE):
400 """Handle the tail of a 'git-upload-pack' request.
402 :param proto: Protocol object to read from
403 :param capabilities: List of negotiated capabilities
404 :param graph_walker: GraphWalker instance to call .ack() on
405 :param pack_data: Function to call with pack data
406 :param progress: Optional progress reporting function
407 :param rbufsize: Read buffer size
409 pkt = proto.read_pkt_line()
411 parts = pkt.rstrip(b'\n').split(b' ')
412 if parts[0] == b'ACK':
413 graph_walker.ack(parts[1])
414 if len(parts) < 3 or parts[2] not in (
415 b'ready', b'continue', b'common'):
417 pkt = proto.read_pkt_line()
418 if CAPABILITY_SIDE_BAND_64K in capabilities:
420 # Just ignore progress data
421 progress = lambda x: None
422 self._read_side_band64k_data(proto, {
423 SIDE_BAND_CHANNEL_DATA: pack_data,
424 SIDE_BAND_CHANNEL_PROGRESS: progress}
428 data = proto.read(rbufsize)
434 class TraditionalGitClient(GitClient):
435 """Traditional Git client."""
437 def _connect(self, cmd, path):
438 """Create a connection to the server.
440 This method is abstract - concrete implementations should
441 implement their own variant which connects to the server and
442 returns an initialized Protocol object with the service ready
443 for use and a can_read function which may be used to see if
446 :param cmd: The git service name to which we should connect.
447 :param path: The path we should pass to the service.
449 raise NotImplementedError()
451 def send_pack(self, path, determine_wants, generate_pack_contents,
452 progress=None, write_pack=write_pack_objects):
453 """Upload a pack to a remote repository.
455 :param path: Repository path
456 :param generate_pack_contents: Function that can return a sequence of
457 the shas of the objects to upload.
458 :param progress: Optional callback called with progress updates
459 :param write_pack: Function called with (file, iterable of objects) to
460 write the objects returned by generate_pack_contents to the server.
462 :raises SendPackError: if server rejects the pack data
463 :raises UpdateRefsError: if the server supports report-status
464 and rejects ref updates
466 proto, unused_can_read = self._connect(b'receive-pack', path)
468 old_refs, server_capabilities = read_pkt_refs(proto)
469 negotiated_capabilities = self._send_capabilities & server_capabilities
471 if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
472 self._report_status_parser = ReportStatusParser()
473 report_status_parser = self._report_status_parser
476 new_refs = orig_new_refs = determine_wants(dict(old_refs))
478 proto.write_pkt_line(None)
481 if not CAPABILITY_DELETE_REFS in server_capabilities:
482 # Server does not support deletions. Fail later.
483 new_refs = dict(orig_new_refs)
484 for ref, sha in orig_new_refs.items():
486 if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
487 report_status_parser._ref_statuses.append(
488 b'ng ' + sha + b' remote does not support deleting refs')
489 report_status_parser._ref_status_ok = False
493 proto.write_pkt_line(None)
496 if len(new_refs) == 0 and len(orig_new_refs):
497 # NOOP - Original new refs filtered out by policy
498 proto.write_pkt_line(None)
499 if report_status_parser is not None:
500 report_status_parser.check()
503 (have, want) = self._handle_receive_pack_head(
504 proto, negotiated_capabilities, old_refs, new_refs)
505 if not want and old_refs == new_refs:
507 objects = generate_pack_contents(have, want)
509 dowrite = len(objects) > 0
510 dowrite = dowrite or any(old_refs.get(ref) != sha
511 for (ref, sha) in new_refs.items()
514 write_pack(proto.write_file(), objects)
516 self._handle_receive_pack_tail(
517 proto, negotiated_capabilities, progress)
520 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
522 """Retrieve a pack from a git smart server.
524 :param determine_wants: Callback that returns list of commits to fetch
525 :param graph_walker: Object with next() and ack().
526 :param pack_data: Callback called for each bit of data in the pack
527 :param progress: Callback for progress reports (strings)
529 proto, can_read = self._connect(b'upload-pack', path)
531 refs, server_capabilities = read_pkt_refs(proto)
532 negotiated_capabilities = (
533 self._fetch_capabilities & server_capabilities)
536 proto.write_pkt_line(None)
540 wants = determine_wants(refs)
542 proto.write_pkt_line(None)
544 if wants is not None:
545 wants = [cid for cid in wants if cid != ZERO_SHA]
547 proto.write_pkt_line(None)
549 self._handle_upload_pack_head(
550 proto, negotiated_capabilities, graph_walker, wants, can_read)
551 self._handle_upload_pack_tail(
552 proto, negotiated_capabilities, graph_walker, pack_data, progress)
555 def archive(self, path, committish, write_data, progress=None,
557 proto, can_read = self._connect(b'upload-archive', path)
559 proto.write_pkt_line(b"argument " + committish)
560 proto.write_pkt_line(None)
561 pkt = proto.read_pkt_line()
564 elif pkt == b"ACK\n":
566 elif pkt.startswith(b"ERR "):
567 raise GitProtocolError(pkt[4:].rstrip(b"\n"))
569 raise AssertionError("invalid response %r" % pkt)
570 ret = proto.read_pkt_line()
572 raise AssertionError("expected pkt tail")
573 self._read_side_band64k_data(proto, {
574 SIDE_BAND_CHANNEL_DATA: write_data,
575 SIDE_BAND_CHANNEL_PROGRESS: progress,
576 SIDE_BAND_CHANNEL_FATAL: write_error})
579 class TCPGitClient(TraditionalGitClient):
580 """A Git Client that works over TCP directly (i.e. git://)."""
582 def __init__(self, host, port=None, *args, **kwargs):
587 TraditionalGitClient.__init__(self, *args, **kwargs)
589 def _connect(self, cmd, path):
590 sockaddrs = socket.getaddrinfo(
591 self._host, self._port, socket.AF_UNSPEC, socket.SOCK_STREAM)
593 err = socket.error("no address found for %s" % self._host)
594 for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
595 s = socket.socket(family, socktype, proto)
596 s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
600 except socket.error as err:
606 # -1 means system default buffering
607 rfile = s.makefile('rb', -1)
609 wfile = s.makefile('wb', 0)
615 proto = Protocol(rfile.read, wfile.write, close,
616 report_activity=self._report_activity)
617 if path.startswith(b"/~"):
619 proto.send_cmd(b'git-' + cmd, path, b'host=' + self._host)
620 return proto, lambda: _fileno_can_read(s)
623 class SubprocessWrapper(object):
624 """A socket-like object that talks to a subprocess via pipes."""
626 def __init__(self, proc):
628 if sys.version_info[0] == 2:
629 self.read = proc.stdout.read
631 self.read = BufferedReader(proc.stdout).read
632 self.write = proc.stdin.write
635 if subprocess.mswindows:
636 from msvcrt import get_osfhandle
637 from win32pipe import PeekNamedPipe
638 handle = get_osfhandle(self.proc.stdout.fileno())
639 data, total_bytes_avail, msg_bytes_left = PeekNamedPipe(handle, 0)
640 return total_bytes_avail != 0
642 return _fileno_can_read(self.proc.stdout.fileno())
645 self.proc.stdin.close()
646 self.proc.stdout.close()
648 self.proc.stderr.close()
652 class SubprocessGitClient(TraditionalGitClient):
653 """Git client that talks to a server using a subprocess."""
655 def __init__(self, *args, **kwargs):
656 self._connection = None
658 self._stderr = kwargs.get('stderr')
659 if 'stderr' in kwargs:
661 TraditionalGitClient.__init__(self, *args, **kwargs)
663 def _connect(self, service, path):
665 argv = ['git', service, path]
666 p = SubprocessWrapper(
667 subprocess.Popen(argv, bufsize=0, stdin=subprocess.PIPE,
668 stdout=subprocess.PIPE,
669 stderr=self._stderr))
670 return Protocol(p.read, p.write, p.close,
671 report_activity=self._report_activity), p.can_read
674 class LocalGitClient(GitClient):
675 """Git Client that just uses a local Repo."""
677 def __init__(self, thin_packs=True, report_activity=None):
678 """Create a new LocalGitClient instance.
680 :param path: Path to the local repository
681 :param thin_packs: Whether or not thin packs should be retrieved
682 :param report_activity: Optional callback for reporting transport
685 self._report_activity = report_activity
686 # Ignore the thin_packs argument
688 def send_pack(self, path, determine_wants, generate_pack_contents,
689 progress=None, write_pack=write_pack_objects):
690 """Upload a pack to a remote repository.
692 :param path: Repository path
693 :param generate_pack_contents: Function that can return a sequence of
694 the shas of the objects to upload.
695 :param progress: Optional progress function
696 :param write_pack: Function called with (file, iterable of objects) to
697 write the objects returned by generate_pack_contents to the server.
699 :raises SendPackError: if server rejects the pack data
700 :raises UpdateRefsError: if the server supports report-status
701 and rejects ref updates
703 from dulwich.repo import Repo
706 old_refs = target.get_refs()
707 new_refs = determine_wants(old_refs)
709 have = [sha1 for sha1 in old_refs.values() if sha1 != ZERO_SHA]
711 all_refs = set(new_refs.keys()).union(set(old_refs.keys()))
712 for refname in all_refs:
713 old_sha1 = old_refs.get(refname, ZERO_SHA)
714 new_sha1 = new_refs.get(refname, ZERO_SHA)
715 if new_sha1 not in have and new_sha1 != ZERO_SHA:
716 want.append(new_sha1)
718 if not want and old_refs == new_refs:
721 target.object_store.add_objects(generate_pack_contents(have, want))
723 for name, sha in new_refs.items():
724 target.refs[name] = sha
728 def fetch(self, path, target, determine_wants=None, progress=None):
729 """Fetch into a target repository.
731 :param path: Path to fetch from
732 :param target: Target repository to fetch into
733 :param determine_wants: Optional function to determine what refs
735 :param progress: Optional progress function
736 :return: remote refs as dictionary
738 from dulwich.repo import Repo
740 return r.fetch(target, determine_wants=determine_wants,
743 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
745 """Retrieve a pack from a git smart server.
747 :param determine_wants: Callback that returns list of commits to fetch
748 :param graph_walker: Object with next() and ack().
749 :param pack_data: Callback called for each bit of data in the pack
750 :param progress: Callback for progress reports (strings)
752 from dulwich.repo import Repo
754 objects_iter = r.fetch_objects(determine_wants, graph_walker, progress)
756 # Did the process short-circuit (e.g. in a stateless RPC call)? Note
757 # that the client still expects a 0-object pack in most cases.
758 if objects_iter is None:
760 write_pack_objects(ProtocolFile(None, pack_data), objects_iter)
763 # What Git client to use for local access
764 default_local_git_client_cls = SubprocessGitClient
767 class SSHVendor(object):
768 """A client side SSH implementation."""
770 def connect_ssh(self, host, command, username=None, port=None):
773 "SSHVendor.connect_ssh has been renamed to SSHVendor.run_command",
775 return self.run_command(host, command, username=username, port=port)
777 def run_command(self, host, command, username=None, port=None):
778 """Connect to an SSH server.
780 Run a command remotely and return a file-like object for interaction
781 with the remote command.
783 :param host: Host name
784 :param command: Command to run
785 :param username: Optional ame of user to log in as
786 :param port: Optional SSH port to use
788 raise NotImplementedError(self.run_command)
791 class SubprocessSSHVendor(SSHVendor):
792 """SSH vendor that shells out to the local 'ssh' command."""
794 def run_command(self, host, command, username=None, port=None):
796 #FIXME: This has no way to deal with passwords..
799 args.extend(['-p', str(port)])
800 if username is not None:
801 host = '%s@%s' % (username, host)
803 proc = subprocess.Popen(args + command,
804 stdin=subprocess.PIPE,
805 stdout=subprocess.PIPE)
806 return SubprocessWrapper(proc)
816 class ParamikoWrapper(object):
817 STDERR_READ_N = 2048 # 2k
819 def __init__(self, client, channel, progress_stderr=None):
821 self.channel = channel
822 self.progress_stderr = progress_stderr
823 self.should_monitor = bool(progress_stderr) or True
824 self.monitor_thread = None
828 self.channel.setblocking(True)
831 if self.should_monitor:
832 self.monitor_thread = threading.Thread(
833 target=self.monitor_stderr)
834 self.monitor_thread.start()
836 def monitor_stderr(self):
837 while self.should_monitor:
839 data = self.read_stderr(self.STDERR_READ_N)
843 self.should_monitor = False
847 if self.progress_stderr:
848 self.progress_stderr(data)
853 def stop_monitoring(self):
855 if self.should_monitor:
856 self.should_monitor = False
857 self.monitor_thread.join()
860 data = self.channel.in_stderr_buffer.empty()
864 return self.channel.recv_ready()
866 def write(self, data):
867 return self.channel.sendall(data)
869 def read_stderr(self, n):
870 return self.channel.recv_stderr(n)
872 def read(self, n=None):
873 data = self.channel.recv(n)
880 # Read more if needed
881 if n and data_len < n:
882 diff_len = n - data_len
883 return data + self.read(diff_len)
888 self.stop_monitoring()
890 class ParamikoSSHVendor(object):
895 def run_command(self, host, command, username=None, port=None,
896 progress_stderr=None):
898 # Paramiko needs an explicit port. None is not valid
902 client = paramiko.SSHClient()
904 policy = paramiko.client.MissingHostKeyPolicy()
905 client.set_missing_host_key_policy(policy)
906 client.connect(host, username=username, port=port,
910 channel = client.get_transport().open_session()
913 channel.exec_command(*command)
915 return ParamikoWrapper(
916 client, channel, progress_stderr=progress_stderr)
919 # Can be overridden by users
920 get_ssh_vendor = SubprocessSSHVendor
923 class SSHGitClient(TraditionalGitClient):
925 def __init__(self, host, port=None, username=None, *args, **kwargs):
928 self.username = username
929 TraditionalGitClient.__init__(self, *args, **kwargs)
930 self.alternative_paths = {}
932 def _get_cmd_path(self, cmd):
933 return self.alternative_paths.get(cmd, b'git-' + cmd)
935 def _connect(self, cmd, path):
936 if path.startswith(b"/~"):
938 con = get_ssh_vendor().run_command(
939 self.host, [self._get_cmd_path(cmd) + b" '" + path + b"'"],
940 port=self.port, username=self.username)
941 return (Protocol(con.read, con.write, con.close,
942 report_activity=self._report_activity),
946 def default_user_agent_string():
947 return "dulwich/%s" % ".".join([str(x) for x in dulwich.__version__])
950 def default_urllib2_opener(config):
951 if config is not None:
952 proxy_server = config.get("http", "proxy")
956 if proxy_server is not None:
957 handlers.append(urllib2.ProxyHandler({"http": proxy_server}))
958 opener = urllib2.build_opener(*handlers)
959 if config is not None:
960 user_agent = config.get("http", "useragent")
963 if user_agent is None:
964 user_agent = default_user_agent_string()
965 opener.addheaders = [('User-agent', user_agent)]
969 class HttpGitClient(GitClient):
971 def __init__(self, base_url, dumb=None, opener=None, config=None, *args,
973 self.base_url = base_url.rstrip("/") + "/"
976 self.opener = default_urllib2_opener(config)
979 GitClient.__init__(self, *args, **kwargs)
981 def _get_url(self, path):
982 return urlparse.urljoin(self.base_url, path).rstrip("/") + "/"
984 def _http_request(self, url, headers={}, data=None):
985 req = urllib2.Request(url, headers=headers, data=data)
987 resp = self.opener.open(req)
988 except urllib2.HTTPError as e:
990 raise NotGitRepository()
992 raise GitProtocolError("unexpected http response %d" % e.code)
995 def _discover_references(self, service, url):
996 assert url[-1] == "/"
997 url = urlparse.urljoin(url, "info/refs")
999 if self.dumb is not False:
1000 url += "?service=%s" % service
1001 headers["Content-Type"] = "application/x-%s-request" % service
1002 resp = self._http_request(url, headers)
1004 self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
1006 proto = Protocol(resp.read, None)
1007 # The first line should mention the service
1008 pkts = list(proto.read_pkt_seq())
1009 if pkts != [('# service=%s\n' % service)]:
1010 raise GitProtocolError(
1011 "unexpected first line %r from smart server" % pkts)
1012 return read_pkt_refs(proto)
1014 return read_info_refs(resp), set()
1018 def _smart_request(self, service, url, data):
1019 assert url[-1] == "/"
1020 url = urlparse.urljoin(url, service)
1021 headers = {"Content-Type": "application/x-%s-request" % service}
1022 resp = self._http_request(url, headers, data)
1023 if resp.info().gettype() != ("application/x-%s-result" % service):
1024 raise GitProtocolError("Invalid content-type from server: %s"
1025 % resp.info().gettype())
1028 def send_pack(self, path, determine_wants, generate_pack_contents,
1029 progress=None, write_pack=write_pack_objects):
1030 """Upload a pack to a remote repository.
1032 :param path: Repository path
1033 :param generate_pack_contents: Function that can return a sequence of
1034 the shas of the objects to upload.
1035 :param progress: Optional progress function
1036 :param write_pack: Function called with (file, iterable of objects) to
1037 write the objects returned by generate_pack_contents to the server.
1039 :raises SendPackError: if server rejects the pack data
1040 :raises UpdateRefsError: if the server supports report-status
1041 and rejects ref updates
1043 url = self._get_url(path)
1044 old_refs, server_capabilities = self._discover_references(
1045 b"git-receive-pack", url)
1046 negotiated_capabilities = self._send_capabilities & server_capabilities
1048 if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
1049 self._report_status_parser = ReportStatusParser()
1051 new_refs = determine_wants(dict(old_refs))
1052 if new_refs is None:
1055 raise NotImplementedError(self.fetch_pack)
1056 req_data = BytesIO()
1057 req_proto = Protocol(None, req_data.write)
1058 (have, want) = self._handle_receive_pack_head(
1059 req_proto, negotiated_capabilities, old_refs, new_refs)
1060 if not want and old_refs == new_refs:
1062 objects = generate_pack_contents(have, want)
1063 if len(objects) > 0:
1064 write_pack(req_proto.write_file(), objects)
1065 resp = self._smart_request(b"git-receive-pack", url,
1066 data=req_data.getvalue())
1068 resp_proto = Protocol(resp.read, None)
1069 self._handle_receive_pack_tail(resp_proto, negotiated_capabilities,
1076 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
1078 """Retrieve a pack from a git smart server.
1080 :param determine_wants: Callback that returns list of commits to fetch
1081 :param graph_walker: Object with next() and ack().
1082 :param pack_data: Callback called for each bit of data in the pack
1083 :param progress: Callback for progress reports (strings)
1084 :return: Dictionary with the refs of the remote repository
1086 url = self._get_url(path)
1087 refs, server_capabilities = self._discover_references(
1088 b"git-upload-pack", url)
1089 negotiated_capabilities = self._fetch_capabilities & server_capabilities
1090 wants = determine_wants(refs)
1091 if wants is not None:
1092 wants = [cid for cid in wants if cid != ZERO_SHA]
1096 raise NotImplementedError(self.send_pack)
1097 req_data = BytesIO()
1098 req_proto = Protocol(None, req_data.write)
1099 self._handle_upload_pack_head(
1100 req_proto, negotiated_capabilities, graph_walker, wants,
1102 resp = self._smart_request(
1103 b"git-upload-pack", url, data=req_data.getvalue())
1105 resp_proto = Protocol(resp.read, None)
1106 self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,
1107 graph_walker, pack_data, progress)
1113 def get_transport_and_path_from_url(url, config=None, **kwargs):
1114 """Obtain a git client from a URL.
1116 :param url: URL to open
1117 :param config: Optional config object
1118 :param thin_packs: Whether or not thin packs should be retrieved
1119 :param report_activity: Optional callback for reporting transport
1121 :return: Tuple with client instance and relative path.
1123 parsed = urlparse.urlparse(url)
1124 if parsed.scheme == 'git':
1125 return (TCPGitClient(parsed.hostname, port=parsed.port, **kwargs),
1127 elif parsed.scheme == 'git+ssh':
1129 if path.startswith('/'):
1130 path = parsed.path[1:]
1131 return SSHGitClient(parsed.hostname, port=parsed.port,
1132 username=parsed.username, **kwargs), path
1133 elif parsed.scheme in ('http', 'https'):
1134 return HttpGitClient(urlparse.urlunparse(parsed), config=config,
1135 **kwargs), parsed.path
1136 elif parsed.scheme == 'file':
1137 return default_local_git_client_cls(**kwargs), parsed.path
1139 raise ValueError("unknown scheme '%s'" % parsed.scheme)
1142 def get_transport_and_path(location, **kwargs):
1143 """Obtain a git client from a URL.
1145 :param location: URL or path
1146 :param config: Optional config object
1147 :param thin_packs: Whether or not thin packs should be retrieved
1148 :param report_activity: Optional callback for reporting transport
1150 :return: Tuple with client instance and relative path.
1152 # First, try to parse it as a URL
1154 return get_transport_and_path_from_url(location, **kwargs)
1158 if (sys.platform == 'win32' and
1159 location[0].isalpha() and location[1:3] == ':\\'):
1160 # Windows local path
1161 return default_local_git_client_cls(**kwargs), location
1163 if ':' in location and not '@' in location:
1164 # SSH with no user@, zero or one leading slash.
1165 (hostname, path) = location.split(':')
1166 return SSHGitClient(hostname, **kwargs), path
1167 elif '@' in location and ':' in location:
1168 # SSH with user@host:foo.
1169 user_host, path = location.split(':')
1170 user, host = user_host.rsplit('@')
1171 return SSHGitClient(host, username=user, **kwargs), path
1173 # Otherwise, assume it's a local path.
1174 return default_local_git_client_cls(**kwargs), location