client.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. want = []
  67. have = []
  68. for changed_ref in changed_refs[:]:
  69. self.proto.write_pkt_line("%s %s %s" % changed_refs)
  70. want.append(changed_refs[1])
  71. if changed_refs[0] != "0"*40:
  72. have.append(changed_refs[0])
  73. self.proto.write_pkt_line(None)
  74. # FIXME: This is implementation specific
  75. # shas = generate_pack_contents(want, have, None)
  76. # write_pack_data(self.write, shas, len(shas))
  77. def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
  78. """Retrieve a pack from a git smart server.
  79. :param determine_wants: Callback that returns list of commits to fetch
  80. :param graph_walker: Object with next() and ack().
  81. :param pack_data: Callback called for each bit of data in the pack
  82. :param progress: Callback for progress reports (strings)
  83. """
  84. (refs, server_capabilities) = self.read_refs()
  85. wants = determine_wants(refs)
  86. if not wants:
  87. self.proto.write_pkt_line(None)
  88. return
  89. self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
  90. for want in wants[1:]:
  91. self.proto.write_pkt_line("want %s\n" % want)
  92. self.proto.write_pkt_line(None)
  93. have = graph_walker.next()
  94. while have:
  95. self.proto.write_pkt_line("have %s\n" % have)
  96. if len(select.select([self.fileno], [], [], 0)[0]) > 0:
  97. pkt = self.proto.read_pkt_line()
  98. parts = pkt.rstrip("\n").split(" ")
  99. if parts[0] == "ACK":
  100. graph_walker.ack(parts[1])
  101. assert parts[2] == "continue"
  102. have = graph_walker.next()
  103. self.proto.write_pkt_line("done\n")
  104. pkt = self.proto.read_pkt_line()
  105. while pkt:
  106. parts = pkt.rstrip("\n").split(" ")
  107. if parts[0] == "ACK":
  108. graph_walker.ack(pkt.split(" ")[1])
  109. if len(parts) < 3 or parts[2] != "continue":
  110. break
  111. pkt = self.proto.read_pkt_line()
  112. for pkt in self.proto.read_pkt_seq():
  113. channel = ord(pkt[0])
  114. pkt = pkt[1:]
  115. if channel == 1:
  116. pack_data(pkt)
  117. elif channel == 2:
  118. progress(pkt)
  119. else:
  120. raise AssertionError("Invalid sideband channel %d" % channel)
  121. class TCPGitClient(GitClient):
  122. def __init__(self, host, port=TCP_GIT_PORT):
  123. self._socket = socket.socket()
  124. self._socket.connect((host, port))
  125. self.rfile = self._socket.makefile('rb', -1)
  126. self.wfile = self._socket.makefile('wb', 0)
  127. self.host = host
  128. super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write)
  129. def send_pack(self, path):
  130. self.proto.send_cmd("git-receive-pack", path, "host=%s" % self.host)
  131. super(TCPGitClient, self).send_pack(path)
  132. def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
  133. self.proto.send_cmd("git-upload-pack", path, "host=%s" % self.host)
  134. super(TCPGitClient, self).fetch_pack(path, determine_wants, graph_walker, pack_data, progress)