remote.txt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, depth=None):
  33. ... # retrieve all objects
  34. ... return refs.values()
  35. Note that the ``depth`` keyword argument will contain an optional requested
  36. shallow fetch depth.
  37. Another required object is a "graph walker", which is used to determine
  38. which objects that the client already has should not be sent again
  39. by the server. Here in the tutorial we'll just use a dummy graph walker
  40. which claims that the client doesn't have any objects::
  41. >>> class DummyGraphWalker(object):
  42. ... def ack(self, sha): pass
  43. ... def next(self): pass
  44. ... def __next__(self): pass
  45. With the ``determine_wants`` function in place, we can now fetch a pack,
  46. which we will write to a ``BytesIO`` object::
  47. >>> from io import BytesIO
  48. >>> f = BytesIO()
  49. >>> result = client.fetch_pack(b"/", determine_wants,
  50. ... DummyGraphWalker(), pack_data=f.write)
  51. ``f`` will now contain a full pack file::
  52. >>> print(f.getvalue()[:4].decode('ascii'))
  53. PACK
  54. Fetching objects into a local repository
  55. ----------------------------------------
  56. It is also possible to fetch from a remote repository into a local repository,
  57. in which case Dulwich takes care of providing the right graph walker, and
  58. importing the received pack file into the local repository::
  59. >>> from dulwich.repo import Repo
  60. >>> local = Repo.init("local", mkdir=True)
  61. >>> remote_refs = client.fetch(b"/", local)
  62. Let's shut down the server now that all tests have been run::
  63. >>> dul_server.shutdown()