2
0

remote.txt 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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("message", committer="Jelmer <jelmer@samba.org>")
  8. >>> backend = DictBackend({'/': repo})
  9. >>> dul_server = TCPGitServer(backend, '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 either 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. With the ``determine_wants`` function in place, we can now fetch a pack,
  43. which we will write to a ``BytesIO`` object::
  44. >>> from io import BytesIO
  45. >>> f = BytesIO()
  46. >>> remote_refs = client.fetch_pack("/", determine_wants,
  47. ... DummyGraphWalker(), pack_data=f.write)
  48. ``f`` will now contain a full pack file::
  49. >>> f.getvalue()[:4]
  50. 'PACK'
  51. Fetching objects into a local repository
  52. ----------------------------------------
  53. It is also possible to fetch from a remote repository into a local repository,
  54. in which case Dulwich takes care of providing the right graph walker, and
  55. importing the received pack file into the local repository::
  56. >>> from dulwich.repo import Repo
  57. >>> local = Repo.init("local", mkdir=True)
  58. >>> remote_refs = client.fetch("/", local)
  59. Let's shut down the server now that all tests have been run::
  60. >>> dul_server.shutdown()