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 ProtocolFile(object):
24 Some network ops are like file ops. The file ops expect to operate on
25 file objects, so provide them with a dummy file.
28 def __init__(self, read, write):
39 class Protocol(object):
41 def __init__(self, read, write):
45 def read_pkt_line(self):
47 Reads a 'pkt line' from the remote git process
49 :return: The next string from the stream
51 sizestr = self.read(4)
54 size = int(sizestr, 16)
57 return self.read(size-4)
59 def read_pkt_seq(self):
60 pkt = self.read_pkt_line()
63 pkt = self.read_pkt_line()
65 def write_pkt_line(self, line):
67 Sends a 'pkt line' to the remote git process
69 :param line: A string containing the data to send
74 self.write("%04x%s" % (len(line)+4, line))
76 def write_sideband(self, channel, blob):
78 Write data to the sideband (a git multiplexing method)
80 :param channel: int specifying which channel to write to
81 :param blob: a blob of data (as a string) to send on this channel
83 # a pktline can be a max of 65535. a sideband line can therefore be
85 # WTF: Why have the len in ASCII, but the channel in binary.
87 self.write_pkt_line("%s%s" % (chr(channel), blob[:65530]))
90 def send_cmd(self, cmd, *args):
92 Send a command and some arguments to a git server
96 :param cmd: The remote service to access
97 :param args: List of arguments to send to remove service
99 self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
103 Read a command and some arguments from the git client
107 :return: A tuple of (command, [list of arguments])
109 line = self.read_pkt_line()
110 splice_at = line.find(" ")
111 cmd, args = line[:splice_at], line[splice_at+1:]
112 return cmd, args.split(chr(0))
115 def extract_capabilities(text):
118 capabilities = text.split("\0")
119 return (capabilities[0], capabilities[1:])