client.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  21. def extract_capabilities(text):
  22. if not "\0" in text:
  23. return text
  24. capabilities = text.split("\0")
  25. return (capabilities[0], capabilities[1:])
  26. class SimpleFetchGraphWalker(object):
  27. def __init__(self, local_heads, get_parents):
  28. self.heads = set(local_heads)
  29. self.get_parents = get_parents
  30. self.parents = {}
  31. def ack(self, ref):
  32. if ref in self.heads:
  33. self.heads.remove(ref)
  34. if ref in self.parents:
  35. for p in self.parents[ref]:
  36. self.ack(p)
  37. def next(self):
  38. if self.heads:
  39. ret = self.heads.pop()
  40. ps = self.get_parents(ret)
  41. self.parents[ret] = ps
  42. self.heads.update(ps)
  43. return ret
  44. return None
  45. class GitClient(object):
  46. """Git smart server client.
  47. """
  48. def __init__(self, fileno, read, write, host):
  49. self.proto = Protocol(read, write)
  50. self.fileno = fileno
  51. self.host = host
  52. def send_cmd(self, name, *args):
  53. self.proto.write_pkt_line("%s %s" % (name, "".join(["%s\0" % a for a in args])))
  54. def capabilities(self):
  55. return "multi_ack side-band-64k thin-pack ofs-delta"
  56. def read_refs(self):
  57. server_capabilities = None
  58. refs = {}
  59. # Receive refs from server
  60. for pkt in self.proto.read_pkt_seq():
  61. (sha, ref) = pkt.rstrip("\n").split(" ", 1)
  62. if server_capabilities is None:
  63. (ref, server_capabilities) = extract_capabilities(ref)
  64. if not (ref == "capabilities^{}" and sha == "0" * 40):
  65. refs[ref] = sha
  66. return refs, server_capabilities
  67. def send_pack(self, path):
  68. self.send_cmd("git-receive-pack", path, "host=%s" % self.host)
  69. refs, server_capabilities = self.read_refs()
  70. changed_refs = [] # FIXME
  71. if not changed_refs:
  72. self.proto.write_pkt_line(None)
  73. return
  74. self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
  75. for changed_ref in changed_refs[:]:
  76. self.proto.write_pkt_line("%s %s %s" % changed_refs)
  77. self.proto.write_pkt_line(None)
  78. # FIXME: Send pack
  79. def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
  80. """Retrieve a pack from a git smart server.
  81. :param determine_wants: Callback that returns list of commits to fetch
  82. :param graph_walker: Object with next() and ack().
  83. :param pack_data: Callback called for each bit of data in the pack
  84. :param progress: Callback for progress reports (strings)
  85. """
  86. self.send_cmd("git-upload-pack", path, "host=%s" % self.host)
  87. (refs, server_capabilities) = self.read_refs()
  88. wants = determine_wants(refs)
  89. if not wants:
  90. self.proto.write_pkt_line(None)
  91. return
  92. self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
  93. for want in wants[1:]:
  94. self.proto.write_pkt_line("want %s\n" % want)
  95. self.proto.write_pkt_line(None)
  96. have = graph_walker.next()
  97. while have:
  98. self.proto.write_pkt_line("have %s\n" % have)
  99. if len(select.select([self.fileno], [], [], 0)[0]) > 0:
  100. pkt = self.proto.read_pkt_line()
  101. parts = pkt.rstrip("\n").split(" ")
  102. if parts[0] == "ACK":
  103. graph_walker.ack(parts[1])
  104. assert parts[2] == "continue"
  105. have = graph_walker.next()
  106. self.proto.write_pkt_line("done\n")
  107. pkt = self.proto.read_pkt_line()
  108. while pkt:
  109. parts = pkt.rstrip("\n").split(" ")
  110. if parts[0] == "ACK":
  111. graph_walker.ack(pkt.split(" ")[1])
  112. if len(parts) < 3 or parts[2] != "continue":
  113. break
  114. pkt = self.proto.read_pkt_line()
  115. for pkt in self.proto.read_pkt_seq():
  116. channel = ord(pkt[0])
  117. pkt = pkt[1:]
  118. if channel == 1:
  119. pack_data(pkt)
  120. elif channel == 2:
  121. progress(pkt)
  122. else:
  123. raise AssertionError("Invalid sideband channel %d" % channel)
  124. class TCPGitClient(GitClient):
  125. def __init__(self, host, port=9418):
  126. self._socket = socket.socket()
  127. self._socket.connect((host, port))
  128. self.rfile = self._socket.makefile('rb', -1)
  129. self.wfile = self._socket.makefile('wb', 0)
  130. super(TCPGitClient, self).__init__(self._socket.fileno(), self.rfile.read, self.wfile.write, host)