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 contextlib import closing
42 from io import BytesIO, BufferedReader
53 import urllib.request as urllib2
54 import urllib.parse as urlparse
56 from dulwich.errors import (
62 from dulwich.protocol import (
64 CAPABILITY_DELETE_REFS,
66 CAPABILITY_MULTI_ACK_DETAILED,
68 CAPABILITY_REPORT_STATUS,
69 CAPABILITY_SIDE_BAND_64K,
74 SIDE_BAND_CHANNEL_DATA,
75 SIDE_BAND_CHANNEL_PROGRESS,
76 SIDE_BAND_CHANNEL_FATAL,
84 from dulwich.pack import (
87 from dulwich.refs import (
92 def _fileno_can_read(fileno):
93 """Check if a file descriptor is readable."""
94 return len(select.select([fileno], [], [], 0)[0]) > 0
96 COMMON_CAPABILITIES = [CAPABILITY_OFS_DELTA, CAPABILITY_SIDE_BAND_64K]
97 FETCH_CAPABILITIES = ([CAPABILITY_THIN_PACK, CAPABILITY_MULTI_ACK,
98 CAPABILITY_MULTI_ACK_DETAILED] +
100 SEND_CAPABILITIES = [CAPABILITY_REPORT_STATUS] + COMMON_CAPABILITIES
103 class ReportStatusParser(object):
104 """Handle status as reported by servers with 'report-status' capability.
109 self._pack_status = None
110 self._ref_status_ok = True
111 self._ref_statuses = []
114 """Check if there were any errors and, if so, raise exceptions.
116 :raise SendPackError: Raised when the server could not unpack
117 :raise UpdateRefsError: Raised when refs could not be updated
119 if self._pack_status not in (b'unpack ok', None):
120 raise SendPackError(self._pack_status)
121 if not self._ref_status_ok:
124 for status in self._ref_statuses:
125 if b' ' not in status:
126 # malformed response, move on to the next one
128 status, ref = status.split(b' ', 1)
132 ref, status = ref.split(b' ', 1)
135 ref_status[ref] = status
136 # TODO(jelmer): don't assume encoding of refs is ascii.
137 raise UpdateRefsError(', '.join([
138 ref.decode('ascii') for ref in ref_status if ref not in ok]) +
139 ' failed to update', ref_status=ref_status)
141 def handle_packet(self, pkt):
144 :raise GitProtocolError: Raised when packets are received after a
148 raise GitProtocolError("received more data after status report")
152 if self._pack_status is None:
153 self._pack_status = pkt.strip()
155 ref_status = pkt.strip()
156 self._ref_statuses.append(ref_status)
157 if not ref_status.startswith(b'ok '):
158 self._ref_status_ok = False
161 def read_pkt_refs(proto):
162 server_capabilities = None
164 # Receive refs from server
165 for pkt in proto.read_pkt_seq():
166 (sha, ref) = pkt.rstrip(b'\n').split(None, 1)
168 raise GitProtocolError(ref)
169 if server_capabilities is None:
170 (ref, server_capabilities) = extract_capabilities(ref)
175 return refs, set(server_capabilities)
178 # TODO(durin42): this doesn't correctly degrade if the server doesn't
179 # support some capabilities. This should work properly with servers
180 # that don't support multi_ack.
181 class GitClient(object):
182 """Git smart server client.
186 def __init__(self, thin_packs=True, report_activity=None):
187 """Create a new GitClient instance.
189 :param thin_packs: Whether or not thin packs should be retrieved
190 :param report_activity: Optional callback for reporting transport
193 self._report_activity = report_activity
194 self._report_status_parser = None
195 self._fetch_capabilities = set(FETCH_CAPABILITIES)
196 self._send_capabilities = set(SEND_CAPABILITIES)
198 self._fetch_capabilities.remove(CAPABILITY_THIN_PACK)
200 def send_pack(self, path, determine_wants, generate_pack_contents,
201 progress=None, write_pack=write_pack_objects):
202 """Upload a pack to a remote repository.
204 :param path: Repository path
205 :param generate_pack_contents: Function that can return a sequence of
206 the shas of the objects to upload.
207 :param progress: Optional progress function
208 :param write_pack: Function called with (file, iterable of objects) to
209 write the objects returned by generate_pack_contents to the server.
211 :raises SendPackError: if server rejects the pack data
212 :raises UpdateRefsError: if the server supports report-status
213 and rejects ref updates
215 raise NotImplementedError(self.send_pack)
217 def fetch(self, path, target, determine_wants=None, progress=None):
218 """Fetch into a target repository.
220 :param path: Path to fetch from
221 :param target: Target repository to fetch into
222 :param determine_wants: Optional function to determine what refs
224 :param progress: Optional progress function
225 :return: remote refs as dictionary
227 if determine_wants is None:
228 determine_wants = target.object_store.determine_wants_all
229 f, commit, abort = target.object_store.add_pack()
231 result = self.fetch_pack(
232 path, determine_wants, target.get_graph_walker(), f.write,
241 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
243 """Retrieve a pack from a git smart server.
245 :param determine_wants: Callback that returns list of commits to fetch
246 :param graph_walker: Object with next() and ack().
247 :param pack_data: Callback called for each bit of data in the pack
248 :param progress: Callback for progress reports (strings)
250 raise NotImplementedError(self.fetch_pack)
252 def _parse_status_report(self, proto):
253 unpack = proto.read_pkt_line().strip()
254 if unpack != b'unpack ok':
256 # flush remaining error data
257 while st is not None:
258 st = proto.read_pkt_line()
259 raise SendPackError(unpack)
262 ref_status = proto.read_pkt_line()
264 ref_status = ref_status.strip()
265 statuses.append(ref_status)
266 if not ref_status.startswith(b'ok '):
268 ref_status = proto.read_pkt_line()
273 for status in statuses:
274 if b' ' not in status:
275 # malformed response, move on to the next one
277 status, ref = status.split(b' ', 1)
281 ref, status = ref.split(b' ', 1)
284 ref_status[ref] = status
285 raise UpdateRefsError(', '.join([ref for ref in ref_status
287 b' failed to update',
288 ref_status=ref_status)
290 def _read_side_band64k_data(self, proto, channel_callbacks):
291 """Read per-channel data.
293 This requires the side-band-64k capability.
295 :param proto: Protocol object to read from
296 :param channel_callbacks: Dictionary mapping channels to packet
297 handlers to use. None for a callback discards channel data.
299 for pkt in proto.read_pkt_seq():
300 channel = ord(pkt[:1])
303 cb = channel_callbacks[channel]
305 raise AssertionError('Invalid sideband channel %d' % channel)
310 def _handle_receive_pack_head(self, proto, capabilities, old_refs,
312 """Handle the head of a 'git-receive-pack' request.
314 :param proto: Protocol object to read from
315 :param capabilities: List of negotiated capabilities
316 :param old_refs: Old refs, as received from the server
317 :param new_refs: New refs
318 :return: (have, want) tuple
321 have = [x for x in old_refs.values() if not x == ZERO_SHA]
322 sent_capabilities = False
324 all_refs = set(new_refs.keys()).union(set(old_refs.keys()))
325 for refname in all_refs:
326 old_sha1 = old_refs.get(refname, ZERO_SHA)
327 new_sha1 = new_refs.get(refname, ZERO_SHA)
329 if old_sha1 != new_sha1:
330 if sent_capabilities:
331 proto.write_pkt_line(old_sha1 + b' ' + new_sha1 + b' ' + refname)
333 proto.write_pkt_line(
334 old_sha1 + b' ' + new_sha1 + b' ' + refname + b'\0' +
335 b' '.join(capabilities))
336 sent_capabilities = True
337 if new_sha1 not in have and new_sha1 != ZERO_SHA:
338 want.append(new_sha1)
339 proto.write_pkt_line(None)
342 def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
343 """Handle the tail of a 'git-receive-pack' request.
345 :param proto: Protocol object to read from
346 :param capabilities: List of negotiated capabilities
347 :param progress: Optional progress reporting function
349 if b"side-band-64k" in capabilities:
351 progress = lambda x: None
352 channel_callbacks = {2: progress}
353 if CAPABILITY_REPORT_STATUS in capabilities:
354 channel_callbacks[1] = PktLineParser(
355 self._report_status_parser.handle_packet).parse
356 self._read_side_band64k_data(proto, channel_callbacks)
358 if CAPABILITY_REPORT_STATUS in capabilities:
359 for pkt in proto.read_pkt_seq():
360 self._report_status_parser.handle_packet(pkt)
361 if self._report_status_parser is not None:
362 self._report_status_parser.check()
364 def _handle_upload_pack_head(self, proto, capabilities, graph_walker,
366 """Handle the head of a 'git-upload-pack' request.
368 :param proto: Protocol object to read from
369 :param capabilities: List of negotiated capabilities
370 :param graph_walker: GraphWalker instance to call .ack() on
371 :param wants: List of commits to fetch
372 :param can_read: function that returns a boolean that indicates
373 whether there is extra graph data to read on proto
375 assert isinstance(wants, list) and isinstance(wants[0], bytes)
376 proto.write_pkt_line(COMMAND_WANT + b' ' + wants[0] + b' ' + b' '.join(capabilities) + b'\n')
377 for want in wants[1:]:
378 proto.write_pkt_line(COMMAND_WANT + b' ' + want + b'\n')
379 proto.write_pkt_line(None)
380 have = next(graph_walker)
382 proto.write_pkt_line(COMMAND_HAVE + b' ' + have + b'\n')
384 pkt = proto.read_pkt_line()
385 parts = pkt.rstrip(b'\n').split(b' ')
386 if parts[0] == b'ACK':
387 graph_walker.ack(parts[1])
388 if parts[2] in (b'continue', b'common'):
390 elif parts[2] == b'ready':
393 raise AssertionError(
394 "%s not in ('continue', 'ready', 'common)" %
396 have = next(graph_walker)
397 proto.write_pkt_line(COMMAND_DONE + b'\n')
399 def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
400 pack_data, progress=None, rbufsize=_RBUFSIZE):
401 """Handle the tail of a 'git-upload-pack' request.
403 :param proto: Protocol object to read from
404 :param capabilities: List of negotiated capabilities
405 :param graph_walker: GraphWalker instance to call .ack() on
406 :param pack_data: Function to call with pack data
407 :param progress: Optional progress reporting function
408 :param rbufsize: Read buffer size
410 pkt = proto.read_pkt_line()
412 parts = pkt.rstrip(b'\n').split(b' ')
413 if parts[0] == b'ACK':
414 graph_walker.ack(parts[1])
415 if len(parts) < 3 or parts[2] not in (
416 b'ready', b'continue', b'common'):
418 pkt = proto.read_pkt_line()
419 if CAPABILITY_SIDE_BAND_64K in capabilities:
421 # Just ignore progress data
422 progress = lambda x: None
423 self._read_side_band64k_data(proto, {
424 SIDE_BAND_CHANNEL_DATA: pack_data,
425 SIDE_BAND_CHANNEL_PROGRESS: progress}
429 data = proto.read(rbufsize)
435 class TraditionalGitClient(GitClient):
436 """Traditional Git client."""
438 def _connect(self, cmd, path):
439 """Create a connection to the server.
441 This method is abstract - concrete implementations should
442 implement their own variant which connects to the server and
443 returns an initialized Protocol object with the service ready
444 for use and a can_read function which may be used to see if
447 :param cmd: The git service name to which we should connect.
448 :param path: The path we should pass to the service.
450 raise NotImplementedError()
452 def send_pack(self, path, determine_wants, generate_pack_contents,
453 progress=None, write_pack=write_pack_objects):
454 """Upload a pack to a remote repository.
456 :param path: Repository path
457 :param generate_pack_contents: Function that can return a sequence of
458 the shas of the objects to upload.
459 :param progress: Optional callback called with progress updates
460 :param write_pack: Function called with (file, iterable of objects) to
461 write the objects returned by generate_pack_contents to the server.
463 :raises SendPackError: if server rejects the pack data
464 :raises UpdateRefsError: if the server supports report-status
465 and rejects ref updates
467 proto, unused_can_read = self._connect(b'receive-pack', path)
469 old_refs, server_capabilities = read_pkt_refs(proto)
470 negotiated_capabilities = self._send_capabilities & server_capabilities
472 if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
473 self._report_status_parser = ReportStatusParser()
474 report_status_parser = self._report_status_parser
477 new_refs = orig_new_refs = determine_wants(dict(old_refs))
479 proto.write_pkt_line(None)
482 if not CAPABILITY_DELETE_REFS in server_capabilities:
483 # Server does not support deletions. Fail later.
484 new_refs = dict(orig_new_refs)
485 for ref, sha in orig_new_refs.items():
487 if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
488 report_status_parser._ref_statuses.append(
489 b'ng ' + sha + b' remote does not support deleting refs')
490 report_status_parser._ref_status_ok = False
494 proto.write_pkt_line(None)
497 if len(new_refs) == 0 and len(orig_new_refs):
498 # NOOP - Original new refs filtered out by policy
499 proto.write_pkt_line(None)
500 if report_status_parser is not None:
501 report_status_parser.check()
504 (have, want) = self._handle_receive_pack_head(
505 proto, negotiated_capabilities, old_refs, new_refs)
506 if not want and old_refs == new_refs:
508 objects = generate_pack_contents(have, want)
510 dowrite = len(objects) > 0
511 dowrite = dowrite or any(old_refs.get(ref) != sha
512 for (ref, sha) in new_refs.items()
515 write_pack(proto.write_file(), objects)
517 self._handle_receive_pack_tail(
518 proto, negotiated_capabilities, progress)
521 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
523 """Retrieve a pack from a git smart server.
525 :param determine_wants: Callback that returns list of commits to fetch
526 :param graph_walker: Object with next() and ack().
527 :param pack_data: Callback called for each bit of data in the pack
528 :param progress: Callback for progress reports (strings)
530 proto, can_read = self._connect(b'upload-pack', path)
532 refs, server_capabilities = read_pkt_refs(proto)
533 negotiated_capabilities = (
534 self._fetch_capabilities & server_capabilities)
537 proto.write_pkt_line(None)
541 wants = determine_wants(refs)
543 proto.write_pkt_line(None)
545 if wants is not None:
546 wants = [cid for cid in wants if cid != ZERO_SHA]
548 proto.write_pkt_line(None)
550 self._handle_upload_pack_head(
551 proto, negotiated_capabilities, graph_walker, wants, can_read)
552 self._handle_upload_pack_tail(
553 proto, negotiated_capabilities, graph_walker, pack_data, progress)
556 def archive(self, path, committish, write_data, progress=None,
558 proto, can_read = self._connect(b'upload-archive', path)
560 proto.write_pkt_line(b"argument " + committish)
561 proto.write_pkt_line(None)
562 pkt = proto.read_pkt_line()
565 elif pkt == b"ACK\n":
567 elif pkt.startswith(b"ERR "):
568 raise GitProtocolError(pkt[4:].rstrip(b"\n"))
570 raise AssertionError("invalid response %r" % pkt)
571 ret = proto.read_pkt_line()
573 raise AssertionError("expected pkt tail")
574 self._read_side_band64k_data(proto, {
575 SIDE_BAND_CHANNEL_DATA: write_data,
576 SIDE_BAND_CHANNEL_PROGRESS: progress,
577 SIDE_BAND_CHANNEL_FATAL: write_error})
580 class TCPGitClient(TraditionalGitClient):
581 """A Git Client that works over TCP directly (i.e. git://)."""
583 def __init__(self, host, port=None, *args, **kwargs):
588 TraditionalGitClient.__init__(self, *args, **kwargs)
590 def _connect(self, cmd, path):
591 sockaddrs = socket.getaddrinfo(
592 self._host, self._port, socket.AF_UNSPEC, socket.SOCK_STREAM)
594 err = socket.error("no address found for %s" % self._host)
595 for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
596 s = socket.socket(family, socktype, proto)
597 s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
601 except socket.error as err:
607 # -1 means system default buffering
608 rfile = s.makefile('rb', -1)
610 wfile = s.makefile('wb', 0)
616 proto = Protocol(rfile.read, wfile.write, close,
617 report_activity=self._report_activity)
618 if path.startswith(b"/~"):
620 proto.send_cmd(b'git-' + cmd, path, b'host=' + self._host)
621 return proto, lambda: _fileno_can_read(s)
624 class SubprocessWrapper(object):
625 """A socket-like object that talks to a subprocess via pipes."""
627 def __init__(self, proc):
629 if sys.version_info[0] == 2:
630 self.read = proc.stdout.read
632 self.read = BufferedReader(proc.stdout).read
633 self.write = proc.stdin.write
636 if subprocess.mswindows:
637 from msvcrt import get_osfhandle
638 from win32pipe import PeekNamedPipe
639 handle = get_osfhandle(self.proc.stdout.fileno())
640 data, total_bytes_avail, msg_bytes_left = PeekNamedPipe(handle, 0)
641 return total_bytes_avail != 0
643 return _fileno_can_read(self.proc.stdout.fileno())
646 self.proc.stdin.close()
647 self.proc.stdout.close()
649 self.proc.stderr.close()
653 def find_git_command():
654 """Find command to run for system Git (usually C Git).
656 if sys.platform == 'win32': # support .exe, .bat and .cmd
657 try: # to avoid overhead
659 except ImportError: # run through cmd.exe with some overhead
660 return ['cmd', '/c', 'git']
662 status, git = win32api.FindExecutable('git')
668 class SubprocessGitClient(TraditionalGitClient):
669 """Git client that talks to a server using a subprocess."""
671 def __init__(self, *args, **kwargs):
672 self._connection = None
674 self._stderr = kwargs.get('stderr')
675 if 'stderr' in kwargs:
677 TraditionalGitClient.__init__(self, *args, **kwargs)
681 def _connect(self, service, path):
683 if self.git_command is None:
684 git_command = find_git_command()
685 argv = git_command + [service, path]
686 argv = ['git', service.decode('ascii'), path]
687 p = SubprocessWrapper(
688 subprocess.Popen(argv, bufsize=0, stdin=subprocess.PIPE,
689 stdout=subprocess.PIPE,
690 stderr=self._stderr))
691 return Protocol(p.read, p.write, p.close,
692 report_activity=self._report_activity), p.can_read
695 class LocalGitClient(GitClient):
696 """Git Client that just uses a local Repo."""
698 def __init__(self, thin_packs=True, report_activity=None):
699 """Create a new LocalGitClient instance.
701 :param path: Path to the local repository
702 :param thin_packs: Whether or not thin packs should be retrieved
703 :param report_activity: Optional callback for reporting transport
706 self._report_activity = report_activity
707 # Ignore the thin_packs argument
709 def send_pack(self, path, determine_wants, generate_pack_contents,
710 progress=None, write_pack=write_pack_objects):
711 """Upload a pack to a remote repository.
713 :param path: Repository path
714 :param generate_pack_contents: Function that can return a sequence of
715 the shas of the objects to upload.
716 :param progress: Optional progress function
717 :param write_pack: Function called with (file, iterable of objects) to
718 write the objects returned by generate_pack_contents to the server.
720 :raises SendPackError: if server rejects the pack data
721 :raises UpdateRefsError: if the server supports report-status
722 and rejects ref updates
724 from dulwich.repo import Repo
726 with closing(Repo(path)) as target:
727 old_refs = target.get_refs()
728 new_refs = determine_wants(dict(old_refs))
730 have = [sha1 for sha1 in old_refs.values() if sha1 != ZERO_SHA]
732 all_refs = set(new_refs.keys()).union(set(old_refs.keys()))
733 for refname in all_refs:
734 old_sha1 = old_refs.get(refname, ZERO_SHA)
735 new_sha1 = new_refs.get(refname, ZERO_SHA)
736 if new_sha1 not in have and new_sha1 != ZERO_SHA:
737 want.append(new_sha1)
739 if not want and old_refs == new_refs:
742 target.object_store.add_objects(generate_pack_contents(have, want))
744 for name, sha in new_refs.items():
745 target.refs[name] = sha
749 def fetch(self, path, target, determine_wants=None, progress=None):
750 """Fetch into a target repository.
752 :param path: Path to fetch from
753 :param target: Target repository to fetch into
754 :param determine_wants: Optional function to determine what refs
756 :param progress: Optional progress function
757 :return: remote refs as dictionary
759 from dulwich.repo import Repo
760 with closing(Repo(path)) as r:
761 return r.fetch(target, determine_wants=determine_wants,
764 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
766 """Retrieve a pack from a git smart server.
768 :param determine_wants: Callback that returns list of commits to fetch
769 :param graph_walker: Object with next() and ack().
770 :param pack_data: Callback called for each bit of data in the pack
771 :param progress: Callback for progress reports (strings)
773 from dulwich.repo import Repo
774 with closing(Repo(path)) as r:
775 objects_iter = r.fetch_objects(determine_wants, graph_walker, progress)
777 # Did the process short-circuit (e.g. in a stateless RPC call)? Note
778 # that the client still expects a 0-object pack in most cases.
779 if objects_iter is None:
781 write_pack_objects(ProtocolFile(None, pack_data), objects_iter)
784 # What Git client to use for local access
785 default_local_git_client_cls = LocalGitClient
788 class SSHVendor(object):
789 """A client side SSH implementation."""
791 def connect_ssh(self, host, command, username=None, port=None):
794 "SSHVendor.connect_ssh has been renamed to SSHVendor.run_command",
796 return self.run_command(host, command, username=username, port=port)
798 def run_command(self, host, command, username=None, port=None):
799 """Connect to an SSH server.
801 Run a command remotely and return a file-like object for interaction
802 with the remote command.
804 :param host: Host name
805 :param command: Command to run
806 :param username: Optional ame of user to log in as
807 :param port: Optional SSH port to use
809 raise NotImplementedError(self.run_command)
812 class SubprocessSSHVendor(SSHVendor):
813 """SSH vendor that shells out to the local 'ssh' command."""
815 def run_command(self, host, command, username=None, port=None):
817 #FIXME: This has no way to deal with passwords..
820 args.extend(['-p', str(port)])
821 if username is not None:
822 host = '%s@%s' % (username, host)
824 proc = subprocess.Popen(args + command,
825 stdin=subprocess.PIPE,
826 stdout=subprocess.PIPE)
827 return SubprocessWrapper(proc)
837 class ParamikoWrapper(object):
838 STDERR_READ_N = 2048 # 2k
840 def __init__(self, client, channel, progress_stderr=None):
842 self.channel = channel
843 self.progress_stderr = progress_stderr
844 self.should_monitor = bool(progress_stderr) or True
845 self.monitor_thread = None
849 self.channel.setblocking(True)
852 if self.should_monitor:
853 self.monitor_thread = threading.Thread(
854 target=self.monitor_stderr)
855 self.monitor_thread.start()
857 def monitor_stderr(self):
858 while self.should_monitor:
860 data = self.read_stderr(self.STDERR_READ_N)
864 self.should_monitor = False
868 if self.progress_stderr:
869 self.progress_stderr(data)
874 def stop_monitoring(self):
876 if self.should_monitor:
877 self.should_monitor = False
878 self.monitor_thread.join()
881 data = self.channel.in_stderr_buffer.empty()
885 return self.channel.recv_ready()
887 def write(self, data):
888 return self.channel.sendall(data)
890 def read_stderr(self, n):
891 return self.channel.recv_stderr(n)
893 def read(self, n=None):
894 data = self.channel.recv(n)
901 # Read more if needed
902 if n and data_len < n:
903 diff_len = n - data_len
904 return data + self.read(diff_len)
909 self.stop_monitoring()
911 class ParamikoSSHVendor(object):
916 def run_command(self, host, command, username=None, port=None,
917 progress_stderr=None):
919 # Paramiko needs an explicit port. None is not valid
923 client = paramiko.SSHClient()
925 policy = paramiko.client.MissingHostKeyPolicy()
926 client.set_missing_host_key_policy(policy)
927 client.connect(host, username=username, port=port,
931 channel = client.get_transport().open_session()
934 channel.exec_command(*command)
936 return ParamikoWrapper(
937 client, channel, progress_stderr=progress_stderr)
940 # Can be overridden by users
941 get_ssh_vendor = SubprocessSSHVendor
944 class SSHGitClient(TraditionalGitClient):
946 def __init__(self, host, port=None, username=None, *args, **kwargs):
949 self.username = username
950 TraditionalGitClient.__init__(self, *args, **kwargs)
951 self.alternative_paths = {}
953 def _get_cmd_path(self, cmd):
954 cmd = cmd.decode('ascii')
955 return self.alternative_paths.get(cmd, 'git-' + cmd)
957 def _connect(self, cmd, path):
958 if path.startswith("/~"):
960 con = get_ssh_vendor().run_command(
961 self.host, [self._get_cmd_path(cmd), path],
962 port=self.port, username=self.username)
963 return (Protocol(con.read, con.write, con.close,
964 report_activity=self._report_activity),
968 def default_user_agent_string():
969 return "dulwich/%s" % ".".join([str(x) for x in dulwich.__version__])
972 def default_urllib2_opener(config):
973 if config is not None:
974 proxy_server = config.get("http", "proxy")
978 if proxy_server is not None:
979 handlers.append(urllib2.ProxyHandler({"http": proxy_server}))
980 opener = urllib2.build_opener(*handlers)
981 if config is not None:
982 user_agent = config.get("http", "useragent")
985 if user_agent is None:
986 user_agent = default_user_agent_string()
987 opener.addheaders = [('User-agent', user_agent)]
991 class HttpGitClient(GitClient):
993 def __init__(self, base_url, dumb=None, opener=None, config=None, *args,
995 self.base_url = base_url.rstrip("/") + "/"
998 self.opener = default_urllib2_opener(config)
1000 self.opener = opener
1001 GitClient.__init__(self, *args, **kwargs)
1004 return "%s(%r, dumb=%r)" % (type(self).__name__, self.base_url, self.dumb)
1006 def _get_url(self, path):
1007 return urlparse.urljoin(self.base_url, path).rstrip("/") + "/"
1009 def _http_request(self, url, headers={}, data=None):
1010 req = urllib2.Request(url, headers=headers, data=data)
1012 resp = self.opener.open(req)
1013 except urllib2.HTTPError as e:
1015 raise NotGitRepository()
1017 raise GitProtocolError("unexpected http response %d" % e.code)
1020 def _discover_references(self, service, url):
1021 assert url[-1] == "/"
1022 url = urlparse.urljoin(url, "info/refs")
1024 if self.dumb is not False:
1025 url += "?service=%s" % service
1026 headers["Content-Type"] = "application/x-%s-request" % service
1027 resp = self._http_request(url, headers)
1029 self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
1031 proto = Protocol(resp.read, None)
1032 # The first line should mention the service
1033 pkts = list(proto.read_pkt_seq())
1034 if pkts != [('# service=%s\n' % service)]:
1035 raise GitProtocolError(
1036 "unexpected first line %r from smart server" % pkts)
1037 return read_pkt_refs(proto)
1039 return read_info_refs(resp), set()
1043 def _smart_request(self, service, url, data):
1044 assert url[-1] == "/"
1045 url = urlparse.urljoin(url, service)
1046 headers = {"Content-Type": "application/x-%s-request" % service}
1047 resp = self._http_request(url, headers, data)
1048 if resp.info().gettype() != ("application/x-%s-result" % service):
1049 raise GitProtocolError("Invalid content-type from server: %s"
1050 % resp.info().gettype())
1053 def send_pack(self, path, determine_wants, generate_pack_contents,
1054 progress=None, write_pack=write_pack_objects):
1055 """Upload a pack to a remote repository.
1057 :param path: Repository path
1058 :param generate_pack_contents: Function that can return a sequence of
1059 the shas of the objects to upload.
1060 :param progress: Optional progress function
1061 :param write_pack: Function called with (file, iterable of objects) to
1062 write the objects returned by generate_pack_contents to the server.
1064 :raises SendPackError: if server rejects the pack data
1065 :raises UpdateRefsError: if the server supports report-status
1066 and rejects ref updates
1068 url = self._get_url(path)
1069 old_refs, server_capabilities = self._discover_references(
1070 b"git-receive-pack", url)
1071 negotiated_capabilities = self._send_capabilities & server_capabilities
1073 if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
1074 self._report_status_parser = ReportStatusParser()
1076 new_refs = determine_wants(dict(old_refs))
1077 if new_refs is None:
1080 raise NotImplementedError(self.fetch_pack)
1081 req_data = BytesIO()
1082 req_proto = Protocol(None, req_data.write)
1083 (have, want) = self._handle_receive_pack_head(
1084 req_proto, negotiated_capabilities, old_refs, new_refs)
1085 if not want and old_refs == new_refs:
1087 objects = generate_pack_contents(have, want)
1088 if len(objects) > 0:
1089 write_pack(req_proto.write_file(), objects)
1090 resp = self._smart_request(b"git-receive-pack", url,
1091 data=req_data.getvalue())
1093 resp_proto = Protocol(resp.read, None)
1094 self._handle_receive_pack_tail(resp_proto, negotiated_capabilities,
1101 def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
1103 """Retrieve a pack from a git smart server.
1105 :param determine_wants: Callback that returns list of commits to fetch
1106 :param graph_walker: Object with next() and ack().
1107 :param pack_data: Callback called for each bit of data in the pack
1108 :param progress: Callback for progress reports (strings)
1109 :return: Dictionary with the refs of the remote repository
1111 url = self._get_url(path)
1112 refs, server_capabilities = self._discover_references(
1113 b"git-upload-pack", url)
1114 negotiated_capabilities = self._fetch_capabilities & server_capabilities
1115 wants = determine_wants(refs)
1116 if wants is not None:
1117 wants = [cid for cid in wants if cid != ZERO_SHA]
1121 raise NotImplementedError(self.send_pack)
1122 req_data = BytesIO()
1123 req_proto = Protocol(None, req_data.write)
1124 self._handle_upload_pack_head(
1125 req_proto, negotiated_capabilities, graph_walker, wants,
1127 resp = self._smart_request(
1128 b"git-upload-pack", url, data=req_data.getvalue())
1130 resp_proto = Protocol(resp.read, None)
1131 self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,
1132 graph_walker, pack_data, progress)
1138 def get_transport_and_path_from_url(url, config=None, **kwargs):
1139 """Obtain a git client from a URL.
1141 :param url: URL to open
1142 :param config: Optional config object
1143 :param thin_packs: Whether or not thin packs should be retrieved
1144 :param report_activity: Optional callback for reporting transport
1146 :return: Tuple with client instance and relative path.
1148 parsed = urlparse.urlparse(url)
1149 if parsed.scheme == 'git':
1150 return (TCPGitClient(parsed.hostname, port=parsed.port, **kwargs),
1152 elif parsed.scheme == 'git+ssh':
1154 if path.startswith('/'):
1155 path = parsed.path[1:]
1156 return SSHGitClient(parsed.hostname, port=parsed.port,
1157 username=parsed.username, **kwargs), path
1158 elif parsed.scheme in ('http', 'https'):
1159 return HttpGitClient(urlparse.urlunparse(parsed), config=config,
1160 **kwargs), parsed.path
1161 elif parsed.scheme == 'file':
1162 return default_local_git_client_cls(**kwargs), parsed.path
1164 raise ValueError("unknown scheme '%s'" % parsed.scheme)
1167 def get_transport_and_path(location, **kwargs):
1168 """Obtain a git client from a URL.
1170 :param location: URL or path
1171 :param config: Optional config object
1172 :param thin_packs: Whether or not thin packs should be retrieved
1173 :param report_activity: Optional callback for reporting transport
1175 :return: Tuple with client instance and relative path.
1177 # First, try to parse it as a URL
1179 return get_transport_and_path_from_url(location, **kwargs)
1183 if (sys.platform == 'win32' and
1184 location[0].isalpha() and location[1:3] == ':\\'):
1185 # Windows local path
1186 return default_local_git_client_cls(**kwargs), location
1188 if ':' in location and not '@' in location:
1189 # SSH with no user@, zero or one leading slash.
1190 (hostname, path) = location.split(':')
1191 return SSHGitClient(hostname, **kwargs), path
1192 elif '@' in location and ':' in location:
1193 # SSH with user@host:foo.
1194 user_host, path = location.split(':')
1195 user, host = user_host.rsplit('@')
1196 return SSHGitClient(host, username=user, **kwargs), path
1198 # Otherwise, assume it's a local path.
1199 return default_local_git_client_cls(**kwargs), location