memoryrepo.py 1.0 KB

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