client.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # server.py -- Implementation of the server side git protocols
  2. # Copryight (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. import select
  19. import socket
  20. from dulwich.protocol import Protocol, TCP_GIT_PORT, extract_capabilities
  21. class SimpleFetchGraphWalker(object):
  22. def __init__(self, local_heads, get_parents):
  23. self.heads = set(local_heads)
  24. self.get_parents = get_parents
  25. self.parents = {}
  26. def ack(self, ref):
  27. if ref in self.heads:
  28. self.heads.remove(ref)
  29. if ref in self.parents:
  30. for p in self.parents[ref]:
  31. self.ack(p)
  32. def next(self):
  33. if self.heads:
  34. ret = self.heads.pop()
  35. ps = self.get_parents(ret)
  36. self.parents[ret] = ps
  37. self.heads.update(ps)
  38. return ret
  39. return None
  40. class GitClient(object):
  41. """Git smart server client.
  42. """
  43. def __init__(self, fileno, read, write):
  44. self.proto = Protocol(read, write)
  45. self.fileno = fileno
  46. def capabilities(self):
  47. return "multi_ack side-band-64k thin-pack ofs-delta"
  48. def read_refs(self):
  49. server_capabilities = None
  50. refs = {}
  51. # Receive refs from server
  52. for pkt in self.proto.read_pkt_seq():
  53. (sha, ref) = pkt.rstrip("\n").split(" ", 1)
  54. if server_capabilities is None:
  55. (ref, server_capabilities) = extract_capabilities(ref)
  56. if not (ref == "capabilities^{}" and sha == "0" * 40):
  57. refs[ref] = sha
  58. return refs, server_capabilities
  59. def send_pack(self, path):
  60. refs, server_capabilities = self.read_refs()
  61. changed_refs = [] # FIXME
  62. if not changed_refs:
  63. self.proto.write_pkt_line(None)
  64. return
  65. self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
  66. for changed_ref in changed_refs[:]:
  67. self.proto.write_pkt_line("%s %s %s" % changed_refs)
  68. self.proto.write_pkt_line(None)
  69. # FIXME: Send pack
  70. def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
  71. """Retrieve a pack from a git smart server.
  72. :param determine_wants: Callback that returns list of commits to fetch
  73. :param graph_walker: Object with next() and ack().
  74. :param pack_data: Callback called for each bit of data in the pack
  75. :param progress: Callback for progress reports (strings)
  76. """
  77. (refs, server_capabilities) = self.read_refs()
  78. wants = determine_wants(refs)
  79. if not wants:
  80. self.proto.write_pkt_line(None)
  81. return
  82. self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
  83. for want in wants[1:]:
  84. self.proto.write_pkt_line("want %s\n" % want)
  85. self.proto.write_pkt_line(None)
  86. have = graph_walker.next()
  87. while have:
  88. self.proto.write_pkt_line("have %s\n" % have)
  89. if len(select.select([self.fileno], [], [], 0)[0]) > 0:
  90. pkt = self.proto.read_pkt_line()
  91. parts = pkt.rstrip("\n").split(" ")
  92. if parts[0] == "ACK":
  93. graph_walker.ack(parts[1])
  94. assert parts[2] == "continue"
  95. have = graph_walker.next()
  96. self.proto.write_pkt_line("done\n")
  97. pkt = self.proto.read_pkt_line()
  98. while pkt:
  99. parts = pkt.rstrip("\n").split(" ")
  100. if parts[0] == "ACK":
  101. graph_walker.ack(pkt.split(" ")[1])
  102. if len(parts) < 3 or parts[2] != "continue":
  103. break
  104. pkt = self.proto.read_pkt_line()
  105. for pkt in self.proto.read_pkt_seq():
  106. channel = ord(pkt[0])
  107. pkt = pkt[1:]
  108. if channel == 1:
  109. pack_data(pkt)
  110. elif channel == 2:
  111. progress(pkt)
  112. else:
  113. raise AssertionError("Invalid sideband channel %d" % channel)
  114. class TCPGitClient(GitClient):
  115. def __init__(self, host, port=TCP_GIT_PORT):
  116. self._socket = socket.socket(type=socket.SOCK_STREAM)
  117. self._socket.connect((host, port))
  118. self.rfile = self._socket.makefile('rb', -1)
  119. self.wfile = self._socket.makefile('wb', 0)
  120. self.host = host
  121. super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write)
  122. def send_pack(self, path):
  123. self.proto.send_cmd("git-receive-pack", path, "host=%s" % self.host)
  124. super(TCPGitClient, self).send_pack(path)
  125. def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
  126. self.proto.send_cmd("git-upload-pack", path, "host=%s" % self.host)
  127. super(TCPGitClient, self).fetch_pack(path, determine_wants, graph_walker, pack_data, progress)