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, host):
  44. self.proto = Protocol(read, write)
  45. self.fileno = fileno
  46. self.host = host
  47. def capabilities(self):
  48. return "multi_ack side-band-64k thin-pack ofs-delta"
  49. def read_refs(self):
  50. server_capabilities = None
  51. refs = {}
  52. # Receive refs from server
  53. for pkt in self.proto.read_pkt_seq():
  54. (sha, ref) = pkt.rstrip("\n").split(" ", 1)
  55. if server_capabilities is None:
  56. (ref, server_capabilities) = extract_capabilities(ref)
  57. if not (ref == "capabilities^{}" and sha == "0" * 40):
  58. refs[ref] = sha
  59. return refs, server_capabilities
  60. def send_pack(self, path):
  61. refs, server_capabilities = self.read_refs()
  62. changed_refs = [] # FIXME
  63. if not changed_refs:
  64. self.proto.write_pkt_line(None)
  65. return
  66. self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
  67. for changed_ref in changed_refs[:]:
  68. self.proto.write_pkt_line("%s %s %s" % changed_refs)
  69. self.proto.write_pkt_line(None)
  70. # FIXME: Send pack
  71. def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
  72. """Retrieve a pack from a git smart server.
  73. :param determine_wants: Callback that returns list of commits to fetch
  74. :param graph_walker: Object with next() and ack().
  75. :param pack_data: Callback called for each bit of data in the pack
  76. :param progress: Callback for progress reports (strings)
  77. """
  78. (refs, server_capabilities) = self.read_refs()
  79. wants = determine_wants(refs)
  80. if not wants:
  81. self.proto.write_pkt_line(None)
  82. return
  83. self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
  84. for want in wants[1:]:
  85. self.proto.write_pkt_line("want %s\n" % want)
  86. self.proto.write_pkt_line(None)
  87. have = graph_walker.next()
  88. while have:
  89. self.proto.write_pkt_line("have %s\n" % have)
  90. if len(select.select([self.fileno], [], [], 0)[0]) > 0:
  91. pkt = self.proto.read_pkt_line()
  92. parts = pkt.rstrip("\n").split(" ")
  93. if parts[0] == "ACK":
  94. graph_walker.ack(parts[1])
  95. assert parts[2] == "continue"
  96. have = graph_walker.next()
  97. self.proto.write_pkt_line("done\n")
  98. pkt = self.proto.read_pkt_line()
  99. while pkt:
  100. parts = pkt.rstrip("\n").split(" ")
  101. if parts[0] == "ACK":
  102. graph_walker.ack(pkt.split(" ")[1])
  103. if len(parts) < 3 or parts[2] != "continue":
  104. break
  105. pkt = self.proto.read_pkt_line()
  106. for pkt in self.proto.read_pkt_seq():
  107. channel = ord(pkt[0])
  108. pkt = pkt[1:]
  109. if channel == 1:
  110. pack_data(pkt)
  111. elif channel == 2:
  112. progress(pkt)
  113. else:
  114. raise AssertionError("Invalid sideband channel %d" % channel)
  115. class TCPGitClient(GitClient):
  116. def __init__(self, host, port=TCP_GIT_PORT):
  117. self._socket = socket.socket()
  118. self._socket.connect((host, port))
  119. self.rfile = self._socket.makefile('rb', -1)
  120. self.wfile = self._socket.makefile('wb', 0)
  121. super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write, host)
  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)