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
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,
22 class Protocol(object):
24 def __init__(self, read, write):
28 def read_pkt_line(self):
30 Reads a 'pkt line' from the remote git process
32 :return: The next string from the stream
34 sizestr = self.read(4)
37 size = int(sizestr, 16)
40 return self.read(size-4)
42 def read_pkt_seq(self):
43 pkt = self.read_pkt_line()
46 pkt = self.read_pkt_line()
48 def write_pkt_line(self, line):
50 Sends a 'pkt line' to the remote git process
52 :param line: A string containing the data to send
57 self.write("%04x%s" % (len(line)+4, line))
59 def write_sideband(self, channel, blob):
61 Write data to the sideband (a git multiplexing method)
63 :param channel: int specifying which channel to write to
64 :param blob: a blob of data (as a string) to send on this channel
66 # a pktline can be a max of 65535. a sideband line can therefore be
68 # WTF: Why have the len in ASCII, but the channel in binary.
70 self.write_pkt_line("%s%s" % (chr(channel), blob[:65530]))
73 def send_cmd(self, cmd, *args):
75 Send a command and some arguments to a git server
79 :param cmd: The remote service to access
80 :param args: List of arguments to send to remove service
82 self.proto.write_pkt_line("%s %s" % (name, "".join(["%s\0" % a for a in args])))
86 Read a command and some arguments from the git client
90 :return: A tuple of (command, [list of arguments])
92 line = self.read_pkt_line()
93 splice_at = line.find(" ")
94 cmd, args = line[:splice_at], line[splice_at+1:]
95 return cmd, args.split(chr(0))
98 def extract_capabilities(text):
101 capabilities = text.split("\0")
102 return (capabilities[0], capabilities[1:])