Merge branch 'http-fetch-capa' of git://github.com/wgrant/dulwich
[jelmer/dulwich.git] / dulwich / client.py
1 # client.py -- Implementation of the server side git protocols
2 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3 # Copyright (C) 2008 John Carr
4 #
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.
9 #
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.
14 #
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,
18 # MA  02110-1301, USA.
19
20 """Client side support for the Git protocol.
21
22 The Dulwich client supports the following capabilities:
23
24  * thin-pack
25  * multi_ack_detailed
26  * multi_ack
27  * side-band-64k
28  * ofs-delta
29  * report-status
30  * delete-refs
31
32 Known capabilities that are not supported:
33
34  * shallow
35  * no-progress
36  * include-tag
37 """
38
39 __docformat__ = 'restructuredText'
40
41 from cStringIO import StringIO
42 import select
43 import socket
44 import subprocess
45 import urllib2
46 import urlparse
47
48 from dulwich.errors import (
49     GitProtocolError,
50     NotGitRepository,
51     SendPackError,
52     UpdateRefsError,
53     )
54 from dulwich.protocol import (
55     _RBUFSIZE,
56     PktLineParser,
57     Protocol,
58     TCP_GIT_PORT,
59     ZERO_SHA,
60     extract_capabilities,
61     )
62 from dulwich.pack import (
63     write_pack_objects,
64     )
65
66
67 # Python 2.6.6 included these in urlparse.uses_netloc upstream. Do
68 # monkeypatching to enable similar behaviour in earlier Pythons:
69 for scheme in ('git', 'git+ssh'):
70     if scheme not in urlparse.uses_netloc:
71         urlparse.uses_netloc.append(scheme)
72
73 def _fileno_can_read(fileno):
74     """Check if a file descriptor is readable."""
75     return len(select.select([fileno], [], [], 0)[0]) > 0
76
77 COMMON_CAPABILITIES = ['ofs-delta', 'side-band-64k']
78 FETCH_CAPABILITIES = ['thin-pack', 'multi_ack', 'multi_ack_detailed'] + COMMON_CAPABILITIES
79 SEND_CAPABILITIES = ['report-status'] + COMMON_CAPABILITIES
80
81
82 class ReportStatusParser(object):
83     """Handle status as reported by servers with the 'report-status' capability.
84     """
85
86     def __init__(self):
87         self._done = False
88         self._pack_status = None
89         self._ref_status_ok = True
90         self._ref_statuses = []
91
92     def check(self):
93         """Check if there were any errors and, if so, raise exceptions.
94
95         :raise SendPackError: Raised when the server could not unpack
96         :raise UpdateRefsError: Raised when refs could not be updated
97         """
98         if self._pack_status not in ('unpack ok', None):
99             raise SendPackError(self._pack_status)
100         if not self._ref_status_ok:
101             ref_status = {}
102             ok = set()
103             for status in self._ref_statuses:
104                 if ' ' not in status:
105                     # malformed response, move on to the next one
106                     continue
107                 status, ref = status.split(' ', 1)
108
109                 if status == 'ng':
110                     if ' ' in ref:
111                         ref, status = ref.split(' ', 1)
112                 else:
113                     ok.add(ref)
114                 ref_status[ref] = status
115             raise UpdateRefsError('%s failed to update' %
116                                   ', '.join([ref for ref in ref_status
117                                              if ref not in ok]),
118                                   ref_status=ref_status)
119
120     def handle_packet(self, pkt):
121         """Handle a packet.
122
123         :raise GitProtocolError: Raised when packets are received after a
124             flush packet.
125         """
126         if self._done:
127             raise GitProtocolError("received more data after status report")
128         if pkt is None:
129             self._done = True
130             return
131         if self._pack_status is None:
132             self._pack_status = pkt.strip()
133         else:
134             ref_status = pkt.strip()
135             self._ref_statuses.append(ref_status)
136             if not ref_status.startswith('ok '):
137                 self._ref_status_ok = False
138
139
140 # TODO(durin42): this doesn't correctly degrade if the server doesn't
141 # support some capabilities. This should work properly with servers
142 # that don't support multi_ack.
143 class GitClient(object):
144     """Git smart server client.
145
146     """
147
148     def __init__(self, thin_packs=True, report_activity=None):
149         """Create a new GitClient instance.
150
151         :param thin_packs: Whether or not thin packs should be retrieved
152         :param report_activity: Optional callback for reporting transport
153             activity.
154         """
155         self._report_activity = report_activity
156         self._report_status_parser = None
157         self._fetch_capabilities = set(FETCH_CAPABILITIES)
158         self._send_capabilities = set(SEND_CAPABILITIES)
159         if not thin_packs:
160             self._fetch_capabilities.remove('thin-pack')
161
162     def _read_refs(self, proto):
163         server_capabilities = None
164         refs = {}
165         # Receive refs from server
166         for pkt in proto.read_pkt_seq():
167             (sha, ref) = pkt.rstrip('\n').split(' ', 1)
168             if sha == 'ERR':
169                 raise GitProtocolError(ref)
170             if server_capabilities is None:
171                 (ref, server_capabilities) = extract_capabilities(ref)
172             refs[ref] = sha
173
174         if len(refs) == 0:
175             return None, set([])
176         return refs, set(server_capabilities)
177
178     def send_pack(self, path, determine_wants, generate_pack_contents,
179                   progress=None):
180         """Upload a pack to a remote repository.
181
182         :param path: Repository path
183         :param generate_pack_contents: Function that can return a sequence of the
184             shas of the objects to upload.
185         :param progress: Optional progress function
186
187         :raises SendPackError: if server rejects the pack data
188         :raises UpdateRefsError: if the server supports report-status
189                                  and rejects ref updates
190         """
191         raise NotImplementedError(self.send_pack)
192
193     def fetch(self, path, target, determine_wants=None, progress=None):
194         """Fetch into a target repository.
195
196         :param path: Path to fetch from
197         :param target: Target repository to fetch into
198         :param determine_wants: Optional function to determine what refs
199             to fetch
200         :param progress: Optional progress function
201         :return: remote refs as dictionary
202         """
203         if determine_wants is None:
204             determine_wants = target.object_store.determine_wants_all
205         f, commit = target.object_store.add_pack()
206         result = self.fetch_pack(path, determine_wants,
207                 target.get_graph_walker(), f.write, progress)
208         commit()
209         return result
210
211     def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
212                    progress=None):
213         """Retrieve a pack from a git smart server.
214
215         :param determine_wants: Callback that returns list of commits to fetch
216         :param graph_walker: Object with next() and ack().
217         :param pack_data: Callback called for each bit of data in the pack
218         :param progress: Callback for progress reports (strings)
219         """
220         raise NotImplementedError(self.fetch_pack)
221
222     def _parse_status_report(self, proto):
223         unpack = proto.read_pkt_line().strip()
224         if unpack != 'unpack ok':
225             st = True
226             # flush remaining error data
227             while st is not None:
228                 st = proto.read_pkt_line()
229             raise SendPackError(unpack)
230         statuses = []
231         errs = False
232         ref_status = proto.read_pkt_line()
233         while ref_status:
234             ref_status = ref_status.strip()
235             statuses.append(ref_status)
236             if not ref_status.startswith('ok '):
237                 errs = True
238             ref_status = proto.read_pkt_line()
239
240         if errs:
241             ref_status = {}
242             ok = set()
243             for status in statuses:
244                 if ' ' not in status:
245                     # malformed response, move on to the next one
246                     continue
247                 status, ref = status.split(' ', 1)
248
249                 if status == 'ng':
250                     if ' ' in ref:
251                         ref, status = ref.split(' ', 1)
252                 else:
253                     ok.add(ref)
254                 ref_status[ref] = status
255             raise UpdateRefsError('%s failed to update' %
256                                   ', '.join([ref for ref in ref_status
257                                              if ref not in ok]),
258                                   ref_status=ref_status)
259
260     def _read_side_band64k_data(self, proto, channel_callbacks):
261         """Read per-channel data.
262
263         This requires the side-band-64k capability.
264
265         :param proto: Protocol object to read from
266         :param channel_callbacks: Dictionary mapping channels to packet
267             handlers to use. None for a callback discards channel data.
268         """
269         for pkt in proto.read_pkt_seq():
270             channel = ord(pkt[0])
271             pkt = pkt[1:]
272             try:
273                 cb = channel_callbacks[channel]
274             except KeyError:
275                 raise AssertionError('Invalid sideband channel %d' % channel)
276             else:
277                 if cb is not None:
278                     cb(pkt)
279
280     def _handle_receive_pack_head(self, proto, capabilities, old_refs, new_refs):
281         """Handle the head of a 'git-receive-pack' request.
282
283         :param proto: Protocol object to read from
284         :param capabilities: List of negotiated capabilities
285         :param old_refs: Old refs, as received from the server
286         :param new_refs: New refs
287         :return: (have, want) tuple
288         """
289         want = []
290         have = [x for x in old_refs.values() if not x == ZERO_SHA]
291         sent_capabilities = False
292
293         for refname in set(new_refs.keys() + old_refs.keys()):
294             old_sha1 = old_refs.get(refname, ZERO_SHA)
295             new_sha1 = new_refs.get(refname, ZERO_SHA)
296
297             if old_sha1 != new_sha1:
298                 if sent_capabilities:
299                     proto.write_pkt_line('%s %s %s' % (old_sha1, new_sha1,
300                                                             refname))
301                 else:
302                     proto.write_pkt_line(
303                       '%s %s %s\0%s' % (old_sha1, new_sha1, refname,
304                                         ' '.join(capabilities)))
305                     sent_capabilities = True
306             if new_sha1 not in have and new_sha1 != ZERO_SHA:
307                 want.append(new_sha1)
308         proto.write_pkt_line(None)
309         return (have, want)
310
311     def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
312         """Handle the tail of a 'git-receive-pack' request.
313
314         :param proto: Protocol object to read from
315         :param capabilities: List of negotiated capabilities
316         :param progress: Optional progress reporting function
317         """
318         if "side-band-64k" in capabilities:
319             if progress is None:
320                 progress = lambda x: None
321             channel_callbacks = { 2: progress }
322             if 'report-status' in capabilities:
323                 channel_callbacks[1] = PktLineParser(
324                     self._report_status_parser.handle_packet).parse
325             self._read_side_band64k_data(proto, channel_callbacks)
326         else:
327             if 'report-status' in capabilities:
328                 for pkt in proto.read_pkt_seq():
329                     self._report_status_parser.handle_packet(pkt)
330         if self._report_status_parser is not None:
331             self._report_status_parser.check()
332         # wait for EOF before returning
333         data = proto.read()
334         if data:
335             raise SendPackError('Unexpected response %r' % data)
336
337     def _handle_upload_pack_head(self, proto, capabilities, graph_walker,
338                                  wants, can_read):
339         """Handle the head of a 'git-upload-pack' request.
340
341         :param proto: Protocol object to read from
342         :param capabilities: List of negotiated capabilities
343         :param graph_walker: GraphWalker instance to call .ack() on
344         :param wants: List of commits to fetch
345         :param can_read: function that returns a boolean that indicates
346             whether there is extra graph data to read on proto
347         """
348         assert isinstance(wants, list) and type(wants[0]) == str
349         proto.write_pkt_line('want %s %s\n' % (
350             wants[0], ' '.join(capabilities)))
351         for want in wants[1:]:
352             proto.write_pkt_line('want %s\n' % want)
353         proto.write_pkt_line(None)
354         have = graph_walker.next()
355         while have:
356             proto.write_pkt_line('have %s\n' % have)
357             if can_read():
358                 pkt = proto.read_pkt_line()
359                 parts = pkt.rstrip('\n').split(' ')
360                 if parts[0] == 'ACK':
361                     graph_walker.ack(parts[1])
362                     if parts[2] in ('continue', 'common'):
363                         pass
364                     elif parts[2] == 'ready':
365                         break
366                     else:
367                         raise AssertionError(
368                             "%s not in ('continue', 'ready', 'common)" %
369                             parts[2])
370             have = graph_walker.next()
371         proto.write_pkt_line('done\n')
372
373     def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
374                                  pack_data, progress=None, rbufsize=_RBUFSIZE):
375         """Handle the tail of a 'git-upload-pack' request.
376
377         :param proto: Protocol object to read from
378         :param capabilities: List of negotiated capabilities
379         :param graph_walker: GraphWalker instance to call .ack() on
380         :param pack_data: Function to call with pack data
381         :param progress: Optional progress reporting function
382         :param rbufsize: Read buffer size
383         """
384         pkt = proto.read_pkt_line()
385         while pkt:
386             parts = pkt.rstrip('\n').split(' ')
387             if parts[0] == 'ACK':
388                 graph_walker.ack(pkt.split(' ')[1])
389             if len(parts) < 3 or parts[2] not in (
390                     'ready', 'continue', 'common'):
391                 break
392             pkt = proto.read_pkt_line()
393         if "side-band-64k" in capabilities:
394             if progress is None:
395                 # Just ignore progress data
396                 progress = lambda x: None
397             self._read_side_band64k_data(proto, {1: pack_data, 2: progress})
398             # wait for EOF before returning
399             data = proto.read()
400             if data:
401                 raise Exception('Unexpected response %r' % data)
402         else:
403             while True:
404                 data = proto.read(rbufsize)
405                 if data == "":
406                     break
407                 pack_data(data)
408
409
410 class TraditionalGitClient(GitClient):
411     """Traditional Git client."""
412
413     def _connect(self, cmd, path):
414         """Create a connection to the server.
415
416         This method is abstract - concrete implementations should
417         implement their own variant which connects to the server and
418         returns an initialized Protocol object with the service ready
419         for use and a can_read function which may be used to see if
420         reads would block.
421
422         :param cmd: The git service name to which we should connect.
423         :param path: The path we should pass to the service.
424         """
425         raise NotImplementedError()
426
427     def send_pack(self, path, determine_wants, generate_pack_contents,
428                   progress=None):
429         """Upload a pack to a remote repository.
430
431         :param path: Repository path
432         :param generate_pack_contents: Function that can return a sequence of the
433             shas of the objects to upload.
434         :param progress: Optional callback called with progress updates
435
436         :raises SendPackError: if server rejects the pack data
437         :raises UpdateRefsError: if the server supports report-status
438                                  and rejects ref updates
439         """
440         proto, unused_can_read = self._connect('receive-pack', path)
441         old_refs, server_capabilities = self._read_refs(proto)
442         negotiated_capabilities = self._send_capabilities & server_capabilities
443
444         if 'report-status' in negotiated_capabilities:
445             self._report_status_parser = ReportStatusParser()
446         report_status_parser = self._report_status_parser
447
448         try:
449             new_refs = orig_new_refs = determine_wants(dict(old_refs))
450         except:
451             proto.write_pkt_line(None)
452             raise
453
454         if not 'delete-refs' in server_capabilities:
455             # Server does not support deletions. Fail later.
456             def remove_del(pair):
457                 if pair[1] == ZERO_SHA:
458                     if 'report-status' in negotiated_capabilities:
459                         report_status_parser._ref_statuses.append(
460                             'ng %s remote does not support deleting refs'
461                             % pair[1])
462                         report_status_parser._ref_status_ok = False
463                     return False
464                 else:
465                     return True
466
467             new_refs = dict(
468                 filter(
469                     remove_del,
470                     [(ref, sha) for ref, sha in new_refs.iteritems()]))
471
472         if new_refs is None:
473             proto.write_pkt_line(None)
474             return old_refs
475
476         if len(new_refs) == 0 and len(orig_new_refs):
477             # NOOP - Original new refs filtered out by policy
478             proto.write_pkt_line(None)
479             if self._report_status_parser is not None:
480                 self._report_status_parser.check()
481             return old_refs
482
483         (have, want) = self._handle_receive_pack_head(proto,
484             negotiated_capabilities, old_refs, new_refs)
485         if not want and old_refs == new_refs:
486             return new_refs
487         objects = generate_pack_contents(have, want)
488         if len(objects) > 0:
489             entries, sha = write_pack_objects(proto.write_file(), objects)
490         elif len(set(new_refs.values()) - set([ZERO_SHA])) > 0:
491             # Check for valid create/update refs
492             filtered_new_refs = \
493                 dict([(ref, sha) for ref, sha in new_refs.iteritems()
494                      if sha != ZERO_SHA])
495             if len(set(filtered_new_refs.iteritems()) -
496                     set(old_refs.iteritems())) > 0:
497                 entries, sha = write_pack_objects(proto.write_file(), objects)
498
499         self._handle_receive_pack_tail(proto, negotiated_capabilities,
500             progress)
501         return new_refs
502
503     def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
504                    progress=None):
505         """Retrieve a pack from a git smart server.
506
507         :param determine_wants: Callback that returns list of commits to fetch
508         :param graph_walker: Object with next() and ack().
509         :param pack_data: Callback called for each bit of data in the pack
510         :param progress: Callback for progress reports (strings)
511         """
512         proto, can_read = self._connect('upload-pack', path)
513         refs, server_capabilities = self._read_refs(proto)
514         negotiated_capabilities = self._fetch_capabilities & server_capabilities
515
516         if refs is None:
517             proto.write_pkt_line(None)
518             return refs
519
520         try:
521             wants = determine_wants(refs)
522         except:
523             proto.write_pkt_line(None)
524             raise
525         if wants is not None:
526             wants = [cid for cid in wants if cid != ZERO_SHA]
527         if not wants:
528             proto.write_pkt_line(None)
529             return refs
530         self._handle_upload_pack_head(proto, negotiated_capabilities,
531             graph_walker, wants, can_read)
532         self._handle_upload_pack_tail(proto, negotiated_capabilities,
533             graph_walker, pack_data, progress)
534         return refs
535
536     def archive(self, path, committish, write_data, progress=None):
537         proto, can_read = self._connect('upload-archive', path)
538         proto.write_pkt_line("argument %s" % committish)
539         proto.write_pkt_line(None)
540         pkt = proto.read_pkt_line()
541         if pkt == "NACK\n":
542             return
543         elif pkt == "ACK\n":
544             pass
545         elif pkt.startswith("ERR "):
546             raise GitProtocolError(pkt[4:].rstrip("\n"))
547         else:
548             raise AssertionError("invalid response %r" % pkt)
549         ret = proto.read_pkt_line()
550         if ret is not None:
551             raise AssertionError("expected pkt tail")
552         self._read_side_band64k_data(proto, {1: write_data, 2: progress})
553
554
555 class TCPGitClient(TraditionalGitClient):
556     """A Git Client that works over TCP directly (i.e. git://)."""
557
558     def __init__(self, host, port=None, *args, **kwargs):
559         if port is None:
560             port = TCP_GIT_PORT
561         self._host = host
562         self._port = port
563         TraditionalGitClient.__init__(self, *args, **kwargs)
564
565     def _connect(self, cmd, path):
566         sockaddrs = socket.getaddrinfo(self._host, self._port,
567             socket.AF_UNSPEC, socket.SOCK_STREAM)
568         s = None
569         err = socket.error("no address found for %s" % self._host)
570         for (family, socktype, proto, canonname, sockaddr) in sockaddrs:
571             s = socket.socket(family, socktype, proto)
572             s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
573             try:
574                 s.connect(sockaddr)
575                 break
576             except socket.error, err:
577                 if s is not None:
578                     s.close()
579                 s = None
580         if s is None:
581             raise err
582         # -1 means system default buffering
583         rfile = s.makefile('rb', -1)
584         # 0 means unbuffered
585         wfile = s.makefile('wb', 0)
586         proto = Protocol(rfile.read, wfile.write,
587                          report_activity=self._report_activity)
588         if path.startswith("/~"):
589             path = path[1:]
590         proto.send_cmd('git-%s' % cmd, path, 'host=%s' % self._host)
591         return proto, lambda: _fileno_can_read(s)
592
593
594 class SubprocessWrapper(object):
595     """A socket-like object that talks to a subprocess via pipes."""
596
597     def __init__(self, proc):
598         self.proc = proc
599         self.read = proc.stdout.read
600         self.write = proc.stdin.write
601
602     def can_read(self):
603         if subprocess.mswindows:
604             from msvcrt import get_osfhandle
605             from win32pipe import PeekNamedPipe
606             handle = get_osfhandle(self.proc.stdout.fileno())
607             return PeekNamedPipe(handle, 0)[2] != 0
608         else:
609             return _fileno_can_read(self.proc.stdout.fileno())
610
611     def close(self):
612         self.proc.stdin.close()
613         self.proc.stdout.close()
614         self.proc.wait()
615
616
617 class SubprocessGitClient(TraditionalGitClient):
618     """Git client that talks to a server using a subprocess."""
619
620     def __init__(self, *args, **kwargs):
621         self._connection = None
622         self._stderr = None
623         self._stderr = kwargs.get('stderr')
624         if 'stderr' in kwargs:
625             del kwargs['stderr']
626         TraditionalGitClient.__init__(self, *args, **kwargs)
627
628     def _connect(self, service, path):
629         import subprocess
630         argv = ['git', service, path]
631         p = SubprocessWrapper(
632             subprocess.Popen(argv, bufsize=0, stdin=subprocess.PIPE,
633                              stdout=subprocess.PIPE,
634                              stderr=self._stderr))
635         return Protocol(p.read, p.write,
636                         report_activity=self._report_activity), p.can_read
637
638
639 class SSHVendor(object):
640     """A client side SSH implementation."""
641
642     def connect_ssh(self, host, command, username=None, port=None):
643         """Connect to an SSH server.
644
645         Run a command remotely and return a file-like object for interaction
646         with the remote command.
647
648         :param host: Host name
649         :param command: Command to run
650         :param username: Optional ame of user to log in as
651         :param port: Optional SSH port to use
652         """
653         raise NotImplementedError(self.connect_ssh)
654
655
656 class SubprocessSSHVendor(SSHVendor):
657     """SSH vendor that shells out to the local 'ssh' command."""
658
659     def connect_ssh(self, host, command, username=None, port=None):
660         import subprocess
661         #FIXME: This has no way to deal with passwords..
662         args = ['ssh', '-x']
663         if port is not None:
664             args.extend(['-p', str(port)])
665         if username is not None:
666             host = '%s@%s' % (username, host)
667         args.append(host)
668         proc = subprocess.Popen(args + command,
669                                 stdin=subprocess.PIPE,
670                                 stdout=subprocess.PIPE)
671         return SubprocessWrapper(proc)
672
673
674 # Can be overridden by users
675 get_ssh_vendor = SubprocessSSHVendor
676
677
678 class SSHGitClient(TraditionalGitClient):
679
680     def __init__(self, host, port=None, username=None, *args, **kwargs):
681         self.host = host
682         self.port = port
683         self.username = username
684         TraditionalGitClient.__init__(self, *args, **kwargs)
685         self.alternative_paths = {}
686
687     def _get_cmd_path(self, cmd):
688         return self.alternative_paths.get(cmd, 'git-%s' % cmd)
689
690     def _connect(self, cmd, path):
691         if path.startswith("/~"):
692             path = path[1:]
693         con = get_ssh_vendor().connect_ssh(
694             self.host, ["%s '%s'" % (self._get_cmd_path(cmd), path)],
695             port=self.port, username=self.username)
696         return (Protocol(con.read, con.write, report_activity=self._report_activity),
697                 con.can_read)
698
699
700 class HttpGitClient(GitClient):
701
702     def __init__(self, base_url, dumb=None, *args, **kwargs):
703         self.base_url = base_url.rstrip("/") + "/"
704         self.dumb = dumb
705         GitClient.__init__(self, *args, **kwargs)
706
707     def _get_url(self, path):
708         return urlparse.urljoin(self.base_url, path).rstrip("/") + "/"
709
710     def _http_request(self, url, headers={}, data=None):
711         req = urllib2.Request(url, headers=headers, data=data)
712         try:
713             resp = self._perform(req)
714         except urllib2.HTTPError as e:
715             if e.code == 404:
716                 raise NotGitRepository()
717             if e.code != 200:
718                 raise GitProtocolError("unexpected http response %d" % e.code)
719         return resp
720
721     def _perform(self, req):
722         """Perform a HTTP request.
723
724         This is provided so subclasses can provide their own version.
725
726         :param req: urllib2.Request instance
727         :return: matching response
728         """
729         return urllib2.urlopen(req)
730
731     def _discover_references(self, service, url):
732         assert url[-1] == "/"
733         url = urlparse.urljoin(url, "info/refs")
734         headers = {}
735         if self.dumb != False:
736             url += "?service=%s" % service
737             headers["Content-Type"] = "application/x-%s-request" % service
738         resp = self._http_request(url, headers)
739         self.dumb = (not resp.info().gettype().startswith("application/x-git-"))
740         proto = Protocol(resp.read, None)
741         if not self.dumb:
742             # The first line should mention the service
743             pkts = list(proto.read_pkt_seq())
744             if pkts != [('# service=%s\n' % service)]:
745                 raise GitProtocolError(
746                     "unexpected first line %r from smart server" % pkts)
747         return self._read_refs(proto)
748
749     def _smart_request(self, service, url, data):
750         assert url[-1] == "/"
751         url = urlparse.urljoin(url, service)
752         headers = {"Content-Type": "application/x-%s-request" % service}
753         resp = self._http_request(url, headers, data)
754         if resp.info().gettype() != ("application/x-%s-result" % service):
755             raise GitProtocolError("Invalid content-type from server: %s"
756                 % resp.info().gettype())
757         return resp
758
759     def send_pack(self, path, determine_wants, generate_pack_contents,
760                   progress=None):
761         """Upload a pack to a remote repository.
762
763         :param path: Repository path
764         :param generate_pack_contents: Function that can return a sequence of the
765             shas of the objects to upload.
766         :param progress: Optional progress function
767
768         :raises SendPackError: if server rejects the pack data
769         :raises UpdateRefsError: if the server supports report-status
770                                  and rejects ref updates
771         """
772         url = self._get_url(path)
773         old_refs, server_capabilities = self._discover_references(
774             "git-receive-pack", url)
775         negotiated_capabilities = self._send_capabilities & server_capabilities
776
777         if 'report-status' in negotiated_capabilities:
778             self._report_status_parser = ReportStatusParser()
779
780         new_refs = determine_wants(dict(old_refs))
781         if new_refs is None:
782             return old_refs
783         if self.dumb:
784             raise NotImplementedError(self.fetch_pack)
785         req_data = StringIO()
786         req_proto = Protocol(None, req_data.write)
787         (have, want) = self._handle_receive_pack_head(
788             req_proto, negotiated_capabilities, old_refs, new_refs)
789         if not want and old_refs == new_refs:
790             return new_refs
791         objects = generate_pack_contents(have, want)
792         if len(objects) > 0:
793             entries, sha = write_pack_objects(req_proto.write_file(), objects)
794         resp = self._smart_request("git-receive-pack", url,
795             data=req_data.getvalue())
796         resp_proto = Protocol(resp.read, None)
797         self._handle_receive_pack_tail(resp_proto, negotiated_capabilities,
798             progress)
799         return new_refs
800
801     def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
802                    progress=None):
803         """Retrieve a pack from a git smart server.
804
805         :param determine_wants: Callback that returns list of commits to fetch
806         :param graph_walker: Object with next() and ack().
807         :param pack_data: Callback called for each bit of data in the pack
808         :param progress: Callback for progress reports (strings)
809         :return: Dictionary with the refs of the remote repository
810         """
811         url = self._get_url(path)
812         refs, server_capabilities = self._discover_references(
813             "git-upload-pack", url)
814         negotiated_capabilities = self._fetch_capabilities & server_capabilities
815         wants = determine_wants(refs)
816         if wants is not None:
817             wants = [cid for cid in wants if cid != ZERO_SHA]
818         if not wants:
819             return refs
820         if self.dumb:
821             raise NotImplementedError(self.send_pack)
822         req_data = StringIO()
823         req_proto = Protocol(None, req_data.write)
824         self._handle_upload_pack_head(req_proto,
825             negotiated_capabilities, graph_walker, wants,
826             lambda: False)
827         resp = self._smart_request("git-upload-pack", url,
828             data=req_data.getvalue())
829         resp_proto = Protocol(resp.read, None)
830         self._handle_upload_pack_tail(resp_proto, negotiated_capabilities,
831             graph_walker, pack_data, progress)
832         return refs
833
834
835 def get_transport_and_path(uri, **kwargs):
836     """Obtain a git client from a URI or path.
837
838     :param uri: URI or path
839     :param thin_packs: Whether or not thin packs should be retrieved
840     :param report_activity: Optional callback for reporting transport
841         activity.
842     :return: Tuple with client instance and relative path.
843     """
844     parsed = urlparse.urlparse(uri)
845     if parsed.scheme == 'git':
846         return (TCPGitClient(parsed.hostname, port=parsed.port, **kwargs),
847                 parsed.path)
848     elif parsed.scheme == 'git+ssh':
849         path = parsed.path
850         if path.startswith('/'):
851             path = parsed.path[1:]
852         return SSHGitClient(parsed.hostname, port=parsed.port,
853                             username=parsed.username, **kwargs), path
854     elif parsed.scheme in ('http', 'https'):
855         return HttpGitClient(urlparse.urlunparse(parsed), **kwargs), parsed.path
856
857     if parsed.scheme and not parsed.netloc:
858         # SSH with no user@, zero or one leading slash.
859         return SSHGitClient(parsed.scheme, **kwargs), parsed.path
860     elif parsed.scheme:
861         raise ValueError('Unknown git protocol scheme: %s' % parsed.scheme)
862     elif '@' in parsed.path and ':' in parsed.path:
863         # SSH with user@host:foo.
864         user_host, path = parsed.path.split(':')
865         user, host = user_host.rsplit('@')
866         return SSHGitClient(host, username=user, **kwargs), path
867
868     # Otherwise, assume it's a local path.
869     return SubprocessGitClient(**kwargs), uri