Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > 259bb81ad79746e82330cefe2537996c > files > 175

python-dulwich-0.10.0-1.mga4.x86_64.rpm


<!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>The repository &mdash; dulwich 0.10.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.10.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.10.0 documentation" href="../index.html" />
    <link rel="up" title="Tutorial" href="index.html" />
    <link rel="next" title="The object store" href="object-store.html" />
    <link rel="prev" title="Git File format" href="file-format.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="object-store.html" title="The object store"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="file-format.html" title="Git File format"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">dulwich 0.10.0 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">Tutorial</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="the-repository">
<span id="tutorial-repo"></span><h1>The repository<a class="headerlink" href="#the-repository" title="Permalink to this headline">¶</a></h1>
<p>After this introduction, let&#8217;s start directly with code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">dulwich.repo</span> <span class="kn">import</span> <span class="n">Repo</span>
</pre></div>
</div>
<p>The access to a repository is through the Repo object. You can open an
existing repository or you can create a new one. There are two types of Git
repositories:</p>
<blockquote>
<div><p>Regular Repositories &#8211; They are the ones you create using <tt class="docutils literal"><span class="pre">git</span> <span class="pre">init</span></tt> and
you daily use. They contain a <tt class="docutils literal"><span class="pre">.git</span></tt> folder.</p>
<p>Bare Repositories &#8211; There is no &#8221;.git&#8221; folder. The top-level folder
contains itself the &#8220;branches&#8221;, &#8220;hooks&#8221;... folders. These are used for
published repositories (mirrors). They do not have a working tree.</p>
</div></blockquote>
<div class="section" id="creating-a-repository">
<h2>Creating a repository<a class="headerlink" href="#creating-a-repository" title="Permalink to this headline">¶</a></h2>
<p>Let&#8217;s create a folder and turn it into a repository, like <tt class="docutils literal"><span class="pre">git</span> <span class="pre">init</span></tt> would:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">os</span> <span class="kn">import</span> <span class="n">mkdir</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mkdir</span><span class="p">(</span><span class="s">&quot;myrepo&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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">&quot;myrepo&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">repo</span>
<span class="go">&lt;Repo at &#39;myrepo&#39;&gt;</span>
</pre></div>
</div>
<p>You can already look a the structure of the &#8220;myrepo/.git&#8221; folder, though it
is mostly empty for now.</p>
</div>
<div class="section" id="opening-an-existing-repository">
<h2>Opening an existing repository<a class="headerlink" href="#opening-an-existing-repository" title="Permalink to this headline">¶</a></h2>
<p>To reopen an existing repository, simply pass its path to the constructor
of <tt class="docutils literal"><span class="pre">Repo</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">repo</span> <span class="o">=</span> <span class="n">Repo</span><span class="p">(</span><span class="s">&quot;myrepo&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">repo</span>
<span class="go">&lt;Repo at &#39;myrepo&#39;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="opening-the-index">
<h2>Opening the index<a class="headerlink" href="#opening-the-index" title="Permalink to this headline">¶</a></h2>
<p>The index is used as a staging area. Once you do a commit,
the files tracked in the index will be recorded as the contents of the new
commit. As mentioned earlier, only non-bare repositories have a working tree,
so only non-bare repositories will have an index, too. To open the index, simply
call:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">index</span> <span class="o">=</span> <span class="n">repo</span><span class="o">.</span><span class="n">open_index</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">repr</span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\\\</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;/&#39;</span><span class="p">)</span>
<span class="go">&quot;Index(&#39;myrepo/.git/index&#39;)&quot;</span>
</pre></div>
</div>
<p>Since the repository was just created, the index will be empty:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
<span class="go">[]</span>
</pre></div>
</div>
</div>
<div class="section" id="staging-new-files">
<h2>Staging new files<a class="headerlink" href="#staging-new-files" title="Permalink to this headline">¶</a></h2>
<p>The repository allows &#8220;staging&#8221; files. Only files can be staged - directories
aren&#8217;t tracked explicitly by git. Let&#8217;s create a simple text file and stage it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;myrepo/foo&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;monty&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">repo</span><span class="o">.</span><span class="n">stage</span><span class="p">([</span><span class="s">&quot;foo&quot;</span><span class="p">])</span>
</pre></div>
</div>
<p>It will now show up in the index:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">repo</span><span class="o">.</span><span class="n">open_index</span><span class="p">())</span>
<span class="go">[&#39;foo&#39;]</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-new-commits">
<h2>Creating new commits<a class="headerlink" href="#creating-new-commits" title="Permalink to this headline">¶</a></h2>
<p>Now that we have staged a change, we can commit it. The easiest way to
do this is by using <tt class="docutils literal"><span class="pre">Repo.do_commit</span></tt>. It is also possible to manipulate
the lower-level objects involved in this, but we&#8217;ll leave that for a
separate chapter of the tutorial.</p>
<p>To create a simple commit on the current branch, it is only necessary
to specify the message. The committer and author will be retrieved from the
repository configuration or global configuration if they are not specified:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">commit_id</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="gp">... </span>    <span class="s">&quot;The first commit&quot;</span><span class="p">,</span> <span class="n">committer</span><span class="o">=</span><span class="s">&quot;Jelmer Vernooij &lt;jelmer@samba.org&gt;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">do_commit</span></tt> returns the SHA1 of the commit. Since the commit was to the
default branch, the repository&#8217;s head will now be set to that commit:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">repo</span><span class="o">.</span><span class="n">head</span><span class="p">()</span> <span class="o">==</span> <span class="n">commit_id</span>
<span class="go">True</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="#">The repository</a><ul>
<li><a class="reference internal" href="#creating-a-repository">Creating a repository</a></li>
<li><a class="reference internal" href="#opening-an-existing-repository">Opening an existing repository</a></li>
<li><a class="reference internal" href="#opening-the-index">Opening the index</a></li>
<li><a class="reference internal" href="#staging-new-files">Staging new files</a></li>
<li><a class="reference internal" href="#creating-new-commits">Creating new commits</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="file-format.html"
                        title="previous chapter">Git File format</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="object-store.html"
                        title="next chapter">The object store</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/tutorial/repo.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="object-store.html" title="The object store"
             >next</a> |</li>
        <li class="right" >
          <a href="file-format.html" title="Git File format"
             >previous</a> |</li>
        <li><a href="../index.html">dulwich 0.10.0 documentation</a> &raquo;</li>
          <li><a href="index.html" >Tutorial</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2011, Jelmer Vernooij.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>