remote.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. .. _tutorial-remote:
  2. Most of the tests in this file require a Dulwich server, so let's start one:
  3. >>> from dulwich.repo import Repo
  4. >>> from dulwich.server import DictBackend, TCPGitServer
  5. >>> import threading
  6. >>> repo = Repo.init("remote", mkdir=True)
  7. >>> cid = repo.do_commit(b"message", committer=b"Jelmer <jelmer@samba.org>")
  8. >>> backend = DictBackend({b'/': repo})
  9. >>> dul_server = TCPGitServer(backend, b'localhost', 0)
  10. >>> threading.Thread(target=dul_server.serve).start()
  11. >>> server_address, server_port=dul_server.socket.getsockname()
  12. Remote repositories
  13. ===================
  14. The interface for remote Git repositories is different from that
  15. for local repositories.
  16. The Git smart server protocol provides three basic operations:
  17. * upload-pack - provides a pack with objects requested by the client
  18. * receive-pack - imports a pack with objects provided by the client
  19. * upload-archive - provides a tarball with the contents of a specific revision
  20. The smart server protocol can be accessed over either plain TCP (git://),
  21. SSH (git+ssh://) or tunneled over HTTP (http://).
  22. Dulwich provides support for accessing remote repositories in
  23. ``dulwich.client``. To create a new client, you can construct
  24. one manually::
  25. >>> from dulwich.client import TCPGitClient
  26. >>> client = TCPGitClient(server_address, server_port)
  27. Retrieving raw pack files
  28. -------------------------
  29. The client object can then be used to retrieve a pack. The ``fetch_pack``
  30. method takes a ``determine_wants`` callback argument, which allows the
  31. client to determine which objects it wants to end up with::
  32. >>> def determine_wants(refs):
  33. ... # retrieve all objects
  34. ... return refs.values()
  35. Another required object is a "graph walker", which is used to determine
  36. which objects that the client already has should not be sent again
  37. by the server. Here in the tutorial we'll just use a dummy graph walker
  38. which claims that the client doesn't have any objects::
  39. >>> class DummyGraphWalker(object):
  40. ... def ack(self, sha): pass
  41. ... def next(self): pass
  42. ... def __next__(self): pass
  43. With the ``determine_wants`` function in place, we can now fetch a pack,
  44. which we will write to a ``BytesIO`` object::
  45. >>> from io import BytesIO
  46. >>> f = BytesIO()
  47. >>> result = client.fetch_pack(b"/", determine_wants,
  48. ... DummyGraphWalker(), pack_data=f.write)
  49. ``f`` will now contain a full pack file::
  50. >>> print(f.getvalue()[:4].decode('ascii'))
  51. PACK
  52. Fetching objects into a local repository
  53. ----------------------------------------
  54. It is also possible to fetch from a remote repository into a local repository,
  55. in which case Dulwich takes care of providing the right graph walker, and
  56. importing the received pack file into the local repository::
  57. >>> from dulwich.repo import Repo
  58. >>> local = Repo.init("local", mkdir=True)
  59. >>> remote_refs = client.fetch(b"/", local)
  60. Let's shut down the server now that all tests have been run::
  61. >>> dul_server.shutdown()