server.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # server.py -- Implementation of the server side git protocols
  2. # Copryight (C) 2008 John Carr <john.carr@unrouted.co.uk>
  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 SocketServer
  19. from dulwich.protocol import Protocol, ProtocolFile, TCP_GIT_PORT, extract_capabilities
  20. from dulwich.repo import Repo
  21. from dulwich.pack import PackData, Pack, write_pack_data
  22. import os, sha, tempfile
  23. class Backend(object):
  24. def get_refs(self):
  25. """
  26. Get all the refs in the repository
  27. :return: list of tuple(name, sha)
  28. """
  29. raise NotImplementedError
  30. def has_revision(self, sha):
  31. """
  32. Is a given sha in this repository?
  33. :return: True or False
  34. """
  35. raise NotImplementedError
  36. def apply_pack(self, refs, read):
  37. """ Import a set of changes into a repository and update the refs
  38. :param refs: list of tuple(name, sha)
  39. :param read: callback to read from the incoming pack
  40. """
  41. raise NotImplementedError
  42. def generate_pack(self, want, have, write, progress):
  43. """
  44. Generate a pack containing all commits a client is missing
  45. :param want: is a list of sha's the client desires
  46. :param have: is a list of sha's the client has (allowing us to send the minimal pack)
  47. :param write: is a callback to write pack data to the client
  48. :param progress: is a callback to send progress messages to the client
  49. """
  50. raise NotImplementedError
  51. class GitBackend(Backend):
  52. def __init__(self, gitdir=None):
  53. self.gitdir = gitdir
  54. if not self.gitdir:
  55. self.gitdir = tempfile.mkdtemp()
  56. Repo.create(self.gitdir)
  57. self.repo = Repo(self.gitdir)
  58. def get_refs(self):
  59. refs = []
  60. if self.repo.head():
  61. refs.append(('HEAD', self.repo.head()))
  62. for ref, sha in self.repo.heads().items():
  63. refs.append(('refs/heads/'+ref,sha))
  64. return refs
  65. def has_revision(self, sha):
  66. return self.repo.get_object(sha) != None
  67. def apply_pack(self, refs, read):
  68. # store the incoming pack in the repository
  69. fd, name = tempfile.mkstemp(suffix='.pack', prefix='pack-', dir=self.repo.pack_dir())
  70. os.write(fd, read())
  71. os.close(fd)
  72. # strip '.pack' off our filename
  73. basename = name[:-5]
  74. # generate an index for it
  75. pd = PackData(name)
  76. pd.create_index_v2(basename+".idx")
  77. for oldsha, sha, ref in refs:
  78. if ref == "0" * 40:
  79. self.repo.remove_ref(ref)
  80. else:
  81. self.repo.set_ref(ref, sha)
  82. print "pack applied"
  83. def generate_pack(self, want, have, write, progress):
  84. progress("dul-daemon says what\n")
  85. sha_queue = []
  86. commits_to_send = want[:]
  87. for sha in commits_to_send:
  88. if sha in sha_queue:
  89. continue
  90. sha_queue.append(sha)
  91. c = self.repo.commit(sha)
  92. for p in c.parents:
  93. if not p in commits_to_send:
  94. commits_to_send.append(p)
  95. def parse_tree(tree, sha_queue):
  96. for mode, name, x in tree.entries():
  97. if not x in sha_queue:
  98. try:
  99. t = self.repo.tree(x)
  100. sha_queue.append(x)
  101. parse_tree(t, sha_queue)
  102. except:
  103. sha_queue.append(x)
  104. treesha = c.tree
  105. if treesha not in sha_queue:
  106. sha_queue.append(treesha)
  107. t = self.repo.tree(treesha)
  108. parse_tree(t, sha_queue)
  109. progress("counting objects: %d\r" % len(sha_queue))
  110. progress("counting objects: %d, done.\n" % len(sha_queue))
  111. write_pack_data(ProtocolFile(None, write), (self.repo.get_object(sha) for sha in sha_queue), len(sha_queue))
  112. progress("how was that, then?\n")
  113. class Handler(object):
  114. def __init__(self, backend, read, write):
  115. self.backend = backend
  116. self.proto = Protocol(read, write)
  117. def capabilities(self):
  118. return " ".join(self.default_capabilities())
  119. class UploadPackHandler(Handler):
  120. def default_capabilities(self):
  121. return ("multi_ack", "side-band-64k", "thin-pack", "ofs-delta")
  122. def handle(self):
  123. refs = self.backend.get_refs()
  124. if refs:
  125. self.proto.write_pkt_line("%s %s\x00%s\n" % (refs[0][1], refs[0][0], self.capabilities()))
  126. for i in range(1, len(refs)):
  127. ref = refs[i]
  128. self.proto.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
  129. # i'm done..
  130. self.proto.write("0000")
  131. # Now client will either send "0000", meaning that it doesnt want to pull.
  132. # or it will start sending want want want commands
  133. want = self.proto.read_pkt_line()
  134. if want == None:
  135. return
  136. want, client_capabilities = extract_capabilities(want)
  137. # Keep reading the list of demands until we hit another "0000"
  138. want_revs = []
  139. while want and want[:4] == 'want':
  140. want_rev = want[5:45]
  141. # FIXME: This check probably isnt needed?
  142. if self.backend.has_revision(want_rev):
  143. want_revs.append(want_rev)
  144. want = self.proto.read_pkt_line()
  145. # Client will now tell us which commits it already has - if we have them we ACK them
  146. # this allows client to stop looking at that commits parents (main reason why git pull is fast)
  147. last_sha = None
  148. have_revs = []
  149. have = self.proto.read_pkt_line()
  150. while have and have[:4] == 'have':
  151. have_ref = have[5:45]
  152. if self.backend.has_revision(have_ref):
  153. self.proto.write_pkt_line("ACK %s continue\n" % have_ref)
  154. last_sha = have_ref
  155. have_revs.append(have_ref)
  156. have = self.proto.read_pkt_line()
  157. # At some point client will stop sending commits and will tell us it is done
  158. assert(have[:4] == "done")
  159. # Oddness: Git seems to resend the last ACK, without the "continue" statement
  160. if last_sha:
  161. self.proto.write_pkt_line("ACK %s\n" % last_sha)
  162. # The exchange finishes with a NAK
  163. self.proto.write_pkt_line("NAK\n")
  164. self.backend.generate_pack(want_revs, have_revs, lambda x: self.proto.write_sideband(1, x), lambda x: self.proto.write_sideband(2, x))
  165. # we are done
  166. self.proto.write("0000")
  167. class ReceivePackHandler(Handler):
  168. def default_capabilities(self):
  169. return ("report-status", "delete-refs")
  170. def handle(self):
  171. refs = self.backend.get_refs()
  172. if refs:
  173. self.proto.write_pkt_line("%s %s\x00%s\n" % (refs[0][1], refs[0][0], self.capabilities()))
  174. for i in range(1, len(refs)):
  175. ref = refs[i]
  176. self.proto.write_pkt_line("%s %s\n" % (ref[1], ref[0]))
  177. else:
  178. self.proto.write_pkt_line("0000000000000000000000000000000000000000 capabilities^{} %s" % self.capabilities())
  179. self.proto.write("0000")
  180. client_refs = []
  181. ref = self.proto.read_pkt_line()
  182. # if ref is none then client doesnt want to send us anything..
  183. if ref is None:
  184. return
  185. ref, client_capabilities = extract_capabilities(ref)
  186. # client will now send us a list of (oldsha, newsha, ref)
  187. while ref:
  188. client_refs.append(ref.split())
  189. ref = self.proto.read_pkt_line()
  190. # backend can now deal with this refs and read a pack using self.read
  191. self.backend.apply_pack(client_refs, self.proto.read)
  192. # when we have read all the pack from the client, it assumes everything worked OK
  193. # there is NO ack from the server before it reports victory.
  194. class TCPGitRequestHandler(SocketServer.StreamRequestHandler):
  195. def handle(self):
  196. proto = Protocol(self.rfile.read, self.wfile.write)
  197. command, args = proto.read_cmd()
  198. # switch case to handle the specific git command
  199. if command == 'git-upload-pack':
  200. cls = UploadPackHandler
  201. elif command == 'git-receive-pack':
  202. cls = ReceivePackHandler
  203. else:
  204. return
  205. h = cls(self.server.backend, self.rfile.read, self.wfile.write)
  206. h.handle()
  207. class TCPGitServer(SocketServer.TCPServer):
  208. allow_reuse_address = True
  209. serve = SocketServer.TCPServer.serve_forever
  210. def __init__(self, backend, listen_addr, port=TCP_GIT_PORT):
  211. self.backend = backend
  212. SocketServer.TCPServer.__init__(self, (listen_addr, port), TCPGitRequestHandler)