dul-daemon 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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
  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 refsha in self.repo.heads().items():
  35. refs.append(refsha)
  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")
  57. commits_to_send = want[:]
  58. count = 0
  59. if __name__ == "__main__":
  60. gitdir = None
  61. if len(sys.argv) > 1:
  62. gitdir = sys.argv[1]
  63. backend = GitBackend(gitdir)
  64. server = TCPGitServer(backend, ('localhost', 9418))
  65. server.serve_forever()