dul-daemon 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. from dulwich.pack import write_pack_object
  25. class PackWriteWrapper(object):
  26. def __init__(self, write):
  27. self.writefn = write
  28. self.sha = sha.sha()
  29. def write(self, blob):
  30. self.sha.update(blob)
  31. self.writefn(blob)
  32. def tell(self):
  33. pass
  34. @property
  35. def digest(self):
  36. return self.sha.digest()
  37. class GitBackend(Backend):
  38. def __init__(self, gitdir=None):
  39. self.gitdir = gitdir
  40. if not self.gitdir:
  41. self.gitdir = tempfile.mkdtemp()
  42. Repo.create(self.gitdir)
  43. self.repo = Repo(self.gitdir)
  44. def get_refs(self):
  45. refs = []
  46. if self.repo.head():
  47. refs.append(('HEAD', self.repo.head()))
  48. for ref, sha in self.repo.heads().items():
  49. refs.append(('refs/heads/'+ref,sha))
  50. return refs
  51. def has_revision(self, sha):
  52. return self.repo.get_object(sha) != None
  53. def apply_pack(self, refs, read):
  54. # store the incoming pack in the repository
  55. fd, name = tempfile.mkstemp(suffix='.pack', prefix='', dir=self.repo.pack_dir())
  56. os.write(fd, read())
  57. os.close(fd)
  58. # strip '.pack' off our filename
  59. basename = name[:-5]
  60. # generate an index for it
  61. pd = PackData(name)
  62. pd.create_index_v2(basename+".idx")
  63. for oldsha, sha, ref in refs:
  64. if ref == "0" * 40:
  65. self.repo.remove_ref(ref)
  66. else:
  67. self.repo.set_ref(ref, sha)
  68. print "pack applied"
  69. def generate_pack(self, want, have, write, progress):
  70. progress("dul-daemon says what\n")
  71. sha_queue = []
  72. commits_to_send = want[:]
  73. for sha in commits_to_send:
  74. sha_queue.append((1,sha))
  75. c = self.repo.commit(sha)
  76. for p in c.parents():
  77. commits_to_send.append(p)
  78. progress("counting objects: %d\r" % len(sha_queue))
  79. progress("counting objects: %d, done.\n" % len(sha_queue))
  80. w = PackWriteWrapper(write)
  81. w.write("PACK")
  82. w.write(struct.pack(">L", 2))
  83. w.write(struct.pack(">L", len(sha_queue)))
  84. for type, sha in sha_queue:
  85. obj = self.repo.get_object(sha)
  86. write_pack_object(w, type, object)
  87. # send sha1 of pack
  88. write(w.digest())
  89. if __name__ == "__main__":
  90. gitdir = None
  91. if len(sys.argv) > 1:
  92. gitdir = sys.argv[1]
  93. backend = GitBackend(gitdir)
  94. server = TCPGitServer(backend, ('localhost', 9418))
  95. server.serve_forever()