<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Remote repositories — dulwich 0.9.0 documentation</title> <link rel="stylesheet" href="../_static/nature.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '0.9.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="top" title="dulwich 0.9.0 documentation" href="../index.html" /> <link rel="up" title="Tutorial" href="index.html" /> <link rel="next" title="Tagging" href="tag.html" /> <link rel="prev" title="The object store" href="object-store.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="tag.html" title="Tagging" accesskey="N">next</a> |</li> <li class="right" > <a href="object-store.html" title="The object store" accesskey="P">previous</a> |</li> <li><a href="../index.html">dulwich 0.9.0 documentation</a> »</li> <li><a href="index.html" accesskey="U">Tutorial</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <p id="tutorial-remote">Most of the tests in this file require a Dulwich server, so let’s start one:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dulwich.repo</span> <span class="kn">import</span> <span class="n">Repo</span> <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dulwich.server</span> <span class="kn">import</span> <span class="n">DictBackend</span><span class="p">,</span> <span class="n">TCPGitServer</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">threading</span> <span class="gp">>>> </span><span class="n">repo</span> <span class="o">=</span> <span class="n">Repo</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="s">"remote"</span><span class="p">,</span> <span class="n">mkdir</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">cid</span> <span class="o">=</span> <span class="n">repo</span><span class="o">.</span><span class="n">do_commit</span><span class="p">(</span><span class="s">"message"</span><span class="p">,</span> <span class="n">committer</span><span class="o">=</span><span class="s">"Jelmer <jelmer@samba.org>"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">backend</span> <span class="o">=</span> <span class="n">DictBackend</span><span class="p">({</span><span class="s">'/'</span><span class="p">:</span> <span class="n">repo</span><span class="p">})</span> <span class="gp">>>> </span><span class="n">dul_server</span> <span class="o">=</span> <span class="n">TCPGitServer</span><span class="p">(</span><span class="n">backend</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">dul_server</span><span class="o">.</span><span class="n">serve</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">server_address</span><span class="p">,</span> <span class="n">server_port</span> <span class="o">=</span> <span class="n">dul_server</span><span class="o">.</span><span class="n">socket</span><span class="o">.</span><span class="n">getsockname</span><span class="p">()</span> </pre></div> </div> <div class="section" id="remote-repositories"> <h1>Remote repositories<a class="headerlink" href="#remote-repositories" title="Permalink to this headline">¶</a></h1> <p>The interface for remote Git repositories is different from that for local repositories.</p> <p>The Git smart server protocol provides three basic operations:</p> <blockquote> <div><ul class="simple"> <li>upload-pack - provides a pack with objects requested by the client</li> <li>receive-pack - imports a pack with objects provided by the client</li> <li>upload-archive - provides a tarball with the contents of a specific revision</li> </ul> </div></blockquote> <p>The smart server protocol can be accessed over either plain TCP (git://), SSH (git+ssh://) or tunneled over HTTP (http://).</p> <p>Dulwich provides support for accessing remote repositories in <tt class="docutils literal"><span class="pre">dulwich.client</span></tt>. To create a new client, you can either construct one manually:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dulwich.client</span> <span class="kn">import</span> <span class="n">TCPGitClient</span> <span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">TCPGitClient</span><span class="p">(</span><span class="n">server_address</span><span class="p">,</span> <span class="n">server_port</span><span class="p">)</span> </pre></div> </div> <div class="section" id="retrieving-raw-pack-files"> <h2>Retrieving raw pack files<a class="headerlink" href="#retrieving-raw-pack-files" title="Permalink to this headline">¶</a></h2> <p>The client object can then be used to retrieve a pack. The <tt class="docutils literal"><span class="pre">fetch_pack</span></tt> method takes a <tt class="docutils literal"><span class="pre">determine_wants</span></tt> callback argument, which allows the client to determine which objects it wants to end up with:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">determine_wants</span><span class="p">(</span><span class="n">refs</span><span class="p">):</span> <span class="gp">... </span> <span class="c"># retrieve all objects</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">refs</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> </pre></div> </div> <p>Another required object is a “graph walker”, which is used to determine which objects that the client already has should not be sent again by the server. Here in the tutorial we’ll just use a dummy graph walker which claims that the client doesn’t have any objects:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">DummyGraphWalker</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">ack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sha</span><span class="p">):</span> <span class="k">pass</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> </pre></div> </div> <p>With the determine_wants function in place, we can now fetch a pack, which we will write to a <tt class="docutils literal"><span class="pre">StringIO</span></tt> object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">cStringIO</span> <span class="kn">import</span> <span class="n">StringIO</span> <span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">remote_refs</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">fetch_pack</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span> <span class="n">determine_wants</span><span class="p">,</span> <span class="gp">... </span> <span class="n">DummyGraphWalker</span><span class="p">(),</span> <span class="n">pack_data</span><span class="o">=</span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">)</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">f</span></tt> will now contain a full pack file:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">f</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()[:</span><span class="mi">4</span><span class="p">]</span> <span class="go">'PACK'</span> </pre></div> </div> </div> <div class="section" id="fetching-objects-into-a-local-repository"> <h2>Fetching objects into a local repository<a class="headerlink" href="#fetching-objects-into-a-local-repository" title="Permalink to this headline">¶</a></h2> <p>It also possible to fetch from a remote repository into a local repository, in which case dulwich takes care of providing the right graph walker, and importing the received pack file into the local repository:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dulwich.repo</span> <span class="kn">import</span> <span class="n">Repo</span> <span class="gp">>>> </span><span class="n">local</span> <span class="o">=</span> <span class="n">Repo</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="s">"local"</span><span class="p">,</span> <span class="n">mkdir</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">remote_refs</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span> <span class="n">local</span><span class="p">)</span> </pre></div> </div> <p>Let’s show down the server now that all tests have been run:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">dul_server</span><span class="o">.</span><span class="n">shutdown</span><span class="p">()</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Remote repositories</a><ul> <li><a class="reference internal" href="#retrieving-raw-pack-files">Retrieving raw pack files</a></li> <li><a class="reference internal" href="#fetching-objects-into-a-local-repository">Fetching objects into a local repository</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="object-store.html" title="previous chapter">The object store</a></p> <h4>Next topic</h4> <p class="topless"><a href="tag.html" title="next chapter">Tagging</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/tutorial/remote.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="tag.html" title="Tagging" >next</a> |</li> <li class="right" > <a href="object-store.html" title="The object store" >previous</a> |</li> <li><a href="../index.html">dulwich 0.9.0 documentation</a> »</li> <li><a href="index.html" >Tutorial</a> »</li> </ul> </div> <div class="footer"> © Copyright 2011, Jelmer Vernooij. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>