dul-daemon 3.0 KB

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