~jelmer/dulwich/lp-pqm

« back to all changes in this revision

Viewing changes to docs/tutorial/remote.txt

  • Committer: Jelmer Vernooij
  • Date: 2012-02-01 22:13:51 UTC
  • mfrom: (413.11.554)
  • Revision ID: jelmer@samba.org-20120201221351-b3n2p9zttzh62dwu
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. _tutorial-remote:
 
2
 
 
3
Most of the tests in this file require a Dulwich server, so let's start one:
 
4
 
 
5
    >>> from dulwich.repo import Repo
 
6
    >>> from dulwich.server import DictBackend, TCPGitServer
 
7
    >>> import threading
 
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()
 
14
 
 
15
Remote repositories
 
16
===================
 
17
 
 
18
The interface for remote Git repositories is different from that
 
19
for local repositories.
 
20
 
 
21
The Git smart server protocol provides three basic operations:
 
22
 
 
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
 
26
 
 
27
The smart server protocol can be accessed over either plain TCP (git://),
 
28
SSH (git+ssh://) or tunneled over HTTP (http://).
 
29
 
 
30
Dulwich provides support for accessing remote repositories in
 
31
``dulwich.client``. To create a new client, you can either construct
 
32
one manually::
 
33
 
 
34
   >>> from dulwich.client import TCPGitClient
 
35
   >>> client = TCPGitClient(server_address, server_port)
 
36
 
 
37
Retrieving raw pack files
 
38
-------------------------
 
39
 
 
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::
 
43
 
 
44
   >>> def determine_wants(refs):
 
45
   ...    # retrieve all objects
 
46
   ...    return refs.values()
 
47
 
 
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::
 
52
 
 
53
   >>> class DummyGraphWalker(object):
 
54
   ...     def ack(self, sha): pass
 
55
   ...     def next(self): pass
 
56
 
 
57
With the determine_wants function in place, we can now fetch a pack,
 
58
which we will write to a ``StringIO`` object::
 
59
 
 
60
   >>> from cStringIO import StringIO
 
61
   >>> f = StringIO()
 
62
   >>> remote_refs = client.fetch_pack("/", determine_wants,
 
63
   ...    DummyGraphWalker(), pack_data=f.write)
 
64
 
 
65
``f`` will now contain a full pack file::
 
66
 
 
67
   >>> f.getvalue()[:4]
 
68
   'PACK'
 
69
 
 
70
Fetching objects into a local repository
 
71
----------------------------------------
 
72
 
 
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::
 
76
 
 
77
   >>> from dulwich.repo import Repo
 
78
   >>> local = Repo.init("local", mkdir=True)
 
79
   >>> remote_refs = client.fetch("/", local)
 
80
 
 
81
Let's show down the server now that all tests have been run::
 
82
 
 
83
   >>> dul_server.shutdown()