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 (
35 MULTI_ACK_DETAILED = 2
37 class ProtocolFile(object):
39 Some network ops are like file ops. The file ops expect to operate on
40 file objects, so provide them with a dummy file.
43 def __init__(self, read, write):
54 class Protocol(object):
56 def __init__(self, read, write, report_activity=None):
59 self.report_activity = report_activity
61 def read_pkt_line(self):
63 Reads a 'pkt line' from the remote git process
65 :return: The next string from the stream
68 sizestr = self.read(4)
70 raise HangupException()
71 size = int(sizestr, 16)
73 if self.report_activity:
74 self.report_activity(4, 'read')
76 if self.report_activity:
77 self.report_activity(size, 'read')
78 return self.read(size-4)
79 except socket.error, e:
80 raise GitProtocolError(e)
82 def read_pkt_seq(self):
83 pkt = self.read_pkt_line()
86 pkt = self.read_pkt_line()
88 def write_pkt_line(self, line):
90 Sends a 'pkt line' to the remote git process
92 :param line: A string containing the data to send
97 if self.report_activity:
98 self.report_activity(4, 'write')
100 self.write("%04x%s" % (len(line)+4, line))
101 if self.report_activity:
102 self.report_activity(4+len(line), 'write')
103 except socket.error, e:
104 raise GitProtocolError(e)
106 def write_file(self):
107 class ProtocolFile(object):
109 def __init__(self, proto):
113 def write(self, data):
114 self._proto.write(data)
115 self._offset += len(data)
123 return ProtocolFile(self)
125 def write_sideband(self, channel, blob):
127 Write data to the sideband (a git multiplexing method)
129 :param channel: int specifying which channel to write to
130 :param blob: a blob of data (as a string) to send on this channel
132 # a pktline can be a max of 65520. a sideband line can therefore be
134 # WTF: Why have the len in ASCII, but the channel in binary.
136 self.write_pkt_line("%s%s" % (chr(channel), blob[:65515]))
139 def send_cmd(self, cmd, *args):
141 Send a command and some arguments to a git server
145 :param cmd: The remote service to access
146 :param args: List of arguments to send to remove service
148 self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
152 Read a command and some arguments from the git client
156 :return: A tuple of (command, [list of arguments])
158 line = self.read_pkt_line()
159 splice_at = line.find(" ")
160 cmd, args = line[:splice_at], line[splice_at+1:]
161 assert args[-1] == "\x00"
162 return cmd, args[:-1].split(chr(0))
165 def extract_capabilities(text):
166 """Extract a capabilities list from a string, if present.
168 :param text: String to extract from
169 :return: Tuple with text with capabilities removed and list of capabilities
173 text, capabilities = text.rstrip().split("\0")
174 return (text, capabilities.split(" "))
177 def extract_want_line_capabilities(text):
178 """Extract a capabilities list from a want line, if present.
180 Note that want lines have capabilities separated from the rest of the line
181 by a space instead of a null byte. Thus want lines have the form:
183 want obj-id cap1 cap2 ...
185 :param text: Want line to extract from
186 :return: Tuple with text with capabilities removed and list of capabilities
188 split_text = text.rstrip().split(" ")
189 if len(split_text) < 3:
191 return (" ".join(split_text[:2]), split_text[2:])
194 def ack_type(capabilities):
195 """Extract the ack type from a capabilities list."""
196 if 'multi_ack_detailed' in capabilities:
197 return MULTI_ACK_DETAILED
198 elif 'multi_ack' in capabilities: