2
0

memoryrepo.py 1002 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/python
  2. # This script creates a clone of a remote repository in local memory,
  3. # then adds a single file and pushes the result back.
  4. #
  5. # Example usage:
  6. # python examples/memoryrepo.py git+ssh://github.com/jelmer/testrepo
  7. import stat
  8. import sys
  9. from dulwich import porcelain
  10. from dulwich.objects import Blob
  11. from dulwich.repo import MemoryRepo
  12. local_repo = MemoryRepo()
  13. local_repo.refs.set_symbolic_ref(b'HEAD', b'refs/heads/master')
  14. print(local_repo.refs.as_dict())
  15. porcelain.fetch(local_repo, sys.argv[1])
  16. local_repo['refs/heads/master'] = local_repo['refs/remotes/origin/master']
  17. last_tree = local_repo[local_repo['HEAD'].tree]
  18. new_blob = Blob.from_string(b'Some contents')
  19. local_repo.object_store.add_object(new_blob)
  20. last_tree.add(b'test', stat.S_IFREG, new_blob.id)
  21. local_repo.object_store.add_object(last_tree)
  22. local_repo.do_commit(
  23. message=b'Add a file called \'test\'',
  24. ref=b'refs/heads/master',
  25. tree=last_tree.id)
  26. porcelain.push(local_repo, sys.argv[1], 'master')