1 # protocol.py -- Shared parts of the git protocols
2 # Copryight (C) 2008 John Carr <john.carr@unrouted.co.uk>
3 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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; version 2
8 # or (at your option) any 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 """Generic functions for talking the git smart server protocol."""
24 from dulwich.errors import (
31 class ProtocolFile(object):
33 Some network ops are like file ops. The file ops expect to operate on
34 file objects, so provide them with a dummy file.
37 def __init__(self, read, write):
48 class Protocol(object):
50 def __init__(self, read, write, report_activity=None):
53 self.report_activity = report_activity
55 def read_pkt_line(self):
57 Reads a 'pkt line' from the remote git process
59 :return: The next string from the stream
62 sizestr = self.read(4)
64 raise HangupException()
65 size = int(sizestr, 16)
67 self.report_activity(4, 'read')
69 if self.report_activity:
70 self.report_activity(size, 'read')
71 return self.read(size-4)
72 except socket.error, e:
73 raise GitProtocolError(e)
75 def read_pkt_seq(self):
76 pkt = self.read_pkt_line()
79 pkt = self.read_pkt_line()
81 def write_pkt_line(self, line):
83 Sends a 'pkt line' to the remote git process
85 :param line: A string containing the data to send
90 self.report_activity(4, 'write')
92 self.write("%04x%s" % (len(line)+4, line))
93 self.report_activity(4+len(line), 'write')
94 except socket.error, e:
95 raise GitProtocolError(e)
97 def write_sideband(self, channel, blob):
99 Write data to the sideband (a git multiplexing method)
101 :param channel: int specifying which channel to write to
102 :param blob: a blob of data (as a string) to send on this channel
104 # a pktline can be a max of 65520. a sideband line can therefore be
106 # WTF: Why have the len in ASCII, but the channel in binary.
108 self.write_pkt_line("%s%s" % (chr(channel), blob[:65515]))
111 def send_cmd(self, cmd, *args):
113 Send a command and some arguments to a git server
117 :param cmd: The remote service to access
118 :param args: List of arguments to send to remove service
120 self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
124 Read a command and some arguments from the git client
128 :return: A tuple of (command, [list of arguments])
130 line = self.read_pkt_line()
131 splice_at = line.find(" ")
132 cmd, args = line[:splice_at], line[splice_at+1:]
133 assert args[-1] == "\x00"
134 return cmd, args[:-1].split(chr(0))
137 def extract_capabilities(text):
138 """Extract a capabilities list from a string, if present.
140 :param text: String to extract from
141 :return: Tuple with text with capabilities removed and list of
142 capabilities or None (if no capabilities were present.
146 capabilities = text.split("\0")
147 return (capabilities[0], capabilities[1:])