protocol.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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>
  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; version 2
  8. # or (at your option) any 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. """Generic functions for talking the git smart server protocol."""
  20. import socket
  21. from dulwich.errors import (
  22. HangupException,
  23. GitProtocolError,
  24. )
  25. TCP_GIT_PORT = 9418
  26. ZERO_SHA = "0" * 40
  27. SINGLE_ACK = 0
  28. MULTI_ACK = 1
  29. MULTI_ACK_DETAILED = 2
  30. class ProtocolFile(object):
  31. """
  32. Some network ops are like file ops. The file ops expect to operate on
  33. file objects, so provide them with a dummy file.
  34. """
  35. def __init__(self, read, write):
  36. self.read = read
  37. self.write = write
  38. def tell(self):
  39. pass
  40. def close(self):
  41. pass
  42. class Protocol(object):
  43. def __init__(self, read, write, report_activity=None):
  44. self.read = read
  45. self.write = write
  46. self.report_activity = report_activity
  47. def read_pkt_line(self):
  48. """
  49. Reads a 'pkt line' from the remote git process
  50. :return: The next string from the stream
  51. """
  52. try:
  53. sizestr = self.read(4)
  54. if not sizestr:
  55. raise HangupException()
  56. size = int(sizestr, 16)
  57. if size == 0:
  58. if self.report_activity:
  59. self.report_activity(4, 'read')
  60. return None
  61. if self.report_activity:
  62. self.report_activity(size, 'read')
  63. return self.read(size-4)
  64. except socket.error, e:
  65. raise GitProtocolError(e)
  66. def read_pkt_seq(self):
  67. pkt = self.read_pkt_line()
  68. while pkt:
  69. yield pkt
  70. pkt = self.read_pkt_line()
  71. def write_pkt_line(self, line):
  72. """
  73. Sends a 'pkt line' to the remote git process
  74. :param line: A string containing the data to send
  75. """
  76. try:
  77. if line is None:
  78. self.write("0000")
  79. if self.report_activity:
  80. self.report_activity(4, 'write')
  81. else:
  82. self.write("%04x%s" % (len(line)+4, line))
  83. if self.report_activity:
  84. self.report_activity(4+len(line), 'write')
  85. except socket.error, e:
  86. raise GitProtocolError(e)
  87. def write_file(self):
  88. class ProtocolFile(object):
  89. def __init__(self, proto):
  90. self._proto = proto
  91. self._offset = 0
  92. def write(self, data):
  93. self._proto.write(data)
  94. self._offset += len(data)
  95. def tell(self):
  96. return self._offset
  97. def close(self):
  98. pass
  99. return ProtocolFile(self)
  100. def write_sideband(self, channel, blob):
  101. """
  102. Write data to the sideband (a git multiplexing method)
  103. :param channel: int specifying which channel to write to
  104. :param blob: a blob of data (as a string) to send on this channel
  105. """
  106. # a pktline can be a max of 65520. a sideband line can therefore be
  107. # 65520-5 = 65515
  108. # WTF: Why have the len in ASCII, but the channel in binary.
  109. while blob:
  110. self.write_pkt_line("%s%s" % (chr(channel), blob[:65515]))
  111. blob = blob[65515:]
  112. def send_cmd(self, cmd, *args):
  113. """
  114. Send a command and some arguments to a git server
  115. Only used for git://
  116. :param cmd: The remote service to access
  117. :param args: List of arguments to send to remove service
  118. """
  119. self.write_pkt_line("%s %s" % (cmd, "".join(["%s\0" % a for a in args])))
  120. def read_cmd(self):
  121. """
  122. Read a command and some arguments from the git client
  123. Only used for git://
  124. :return: A tuple of (command, [list of arguments])
  125. """
  126. line = self.read_pkt_line()
  127. splice_at = line.find(" ")
  128. cmd, args = line[:splice_at], line[splice_at+1:]
  129. assert args[-1] == "\x00"
  130. return cmd, args[:-1].split(chr(0))
  131. def extract_capabilities(text):
  132. """Extract a capabilities list from a string, if present.
  133. :param text: String to extract from
  134. :return: Tuple with text with capabilities removed and list of capabilities
  135. """
  136. if not "\0" in text:
  137. return text, []
  138. text, capabilities = text.rstrip().split("\0")
  139. return (text, capabilities.split(" "))
  140. def extract_want_line_capabilities(text):
  141. """Extract a capabilities list from a want line, if present.
  142. Note that want lines have capabilities separated from the rest of the line
  143. by a space instead of a null byte. Thus want lines have the form:
  144. want obj-id cap1 cap2 ...
  145. :param text: Want line to extract from
  146. :return: Tuple with text with capabilities removed and list of capabilities
  147. """
  148. split_text = text.rstrip().split(" ")
  149. if len(split_text) < 3:
  150. return text, []
  151. return (" ".join(split_text[:2]), split_text[2:])
  152. def ack_type(capabilities):
  153. """Extract the ack type from a capabilities list."""
  154. if 'multi_ack_detailed' in capabilities:
  155. return MULTI_ACK_DETAILED
  156. elif 'multi_ack' in capabilities:
  157. return MULTI_ACK
  158. return SINGLE_ACK