memoryrepo.py 1014 B

1234567891011121314151617181920212223242526272829303132
  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. fetch_result = porcelain.fetch(local_repo, sys.argv[1])
  15. local_repo.refs[b"refs/heads/master"] = fetch_result.refs[b"refs/heads/master"]
  16. print(local_repo.refs.as_dict())
  17. last_tree = local_repo[local_repo[b"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'", ref=b"refs/heads/master", tree=last_tree.id
  24. )
  25. porcelain.push(local_repo, sys.argv[1], "master")