dul-daemon 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/python
  2. # dul-daemon - Simple git-daemon a like
  3. # Copyright (C) 2008 John Carr <john.carr@unrouted.co.uk>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. import os, sys, tempfile, struct
  20. from dulwich.server import Backend, TCPGitServer
  21. from dulwich.repo import Repo
  22. from dulwich.pack import PackData, Pack
  23. import sha
  24. class PackWriteWrapper(object):
  25. def __init__(self, write):
  26. self.writefn = write
  27. self.sha = sha.sha()
  28. def write(self, blob):
  29. self.sha.update(blob)
  30. self.writefn(blob)
  31. def tell(self):
  32. pass
  33. @property
  34. def digest(self):
  35. return self.sha.digest()
  36. class GitBackend(Backend):
  37. def __init__(self, gitdir=None):
  38. self.gitdir = gitdir
  39. if not self.gitdir:
  40. self.gitdir = tempfile.mkdtemp()
  41. Repo.create(self.gitdir)
  42. self.repo = Repo(self.gitdir)
  43. def get_refs(self):
  44. refs = []
  45. if self.repo.head():
  46. refs.append(('HEAD', self.repo.head()))
  47. for ref, sha in self.repo.heads().items():
  48. refs.append(('refs/heads/'+ref,sha))
  49. return refs
  50. def has_revision(self, sha):
  51. return self.repo.get_object(sha) != None
  52. def apply_pack(self, refs, read):
  53. # store the incoming pack in the repository
  54. fd, name = tempfile.mkstemp(suffix='.pack', prefix='', dir=self.repo.pack_dir())
  55. os.write(fd, read())
  56. os.close(fd)
  57. # strip '.pack' off our filename
  58. basename = name[:-5]
  59. # generate an index for it
  60. pd = PackData(name)
  61. pd.create_index_v2(basename+".idx")
  62. for oldsha, sha, ref in refs:
  63. if ref == "0" * 40:
  64. self.repo.remove_ref(ref)
  65. else:
  66. self.repo.set_ref(ref, sha)
  67. print "pack applied"
  68. def generate_pack(self, want, have, write, progress):
  69. progress("dul-daemon says what\n")
  70. sha_queue = []
  71. commits_to_send = want[:]
  72. for sha in commits_to_send:
  73. sha_queue.append(sha)
  74. c = self.repo.commit(sha)
  75. for p in c.parents():
  76. commits_to_send.append(p)
  77. progress("counting objects: %d\r" % len(sha_queue))
  78. progress("counting objects: %d, done.\n" % len(sha_queue))
  79. w = PackWriteWrapper(write)
  80. w.write("PACK")
  81. w.write(struct.pack(">L", 2)
  82. w.write(struct.pack(">L", len(sha_queue))
  83. for type, sha in sha_queue:
  84. obj = self.repo.get_object(sha)
  85. write_pack_object(w, type, object)
  86. # send sha1 of pack
  87. write(w.digest())
  88. if __name__ == "__main__":
  89. gitdir = None
  90. if len(sys.argv) > 1:
  91. gitdir = sys.argv[1]
  92. backend = GitBackend(gitdir)
  93. server = TCPGitServer(backend, ('localhost', 9418))
  94. server.serve_forever()