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