diff options
author | Documentation Generation <libgit2@users.noreply.github.com> | 2023-02-25 14:05:36 +0000 |
---|---|---|
committer | Documentation Generation <libgit2@users.noreply.github.com> | 2023-02-25 14:05:36 +0000 |
commit | 8de45cbb9eb6cc0b75faec8a81bc71f217e7f40f (patch) | |
tree | 23530932bdda039d85610c66300538032c5d6c01 /ex/v1.6.1/general.html | |
parent | 498feceb2d7b093b53e40bf31c7353d85481d572 (diff) | |
download | libgit2-8de45cbb9eb6cc0b75faec8a81bc71f217e7f40f.tar.gz |
generated docs
Diffstat (limited to 'ex/v1.6.1/general.html')
-rw-r--r-- | ex/v1.6.1/general.html | 1167 |
1 files changed, 1167 insertions, 0 deletions
diff --git a/ex/v1.6.1/general.html b/ex/v1.6.1/general.html new file mode 100644 index 000000000..ae2b2c017 --- /dev/null +++ b/ex/v1.6.1/general.html @@ -0,0 +1,1167 @@ +<!DOCTYPE html> +<html> +<head> + <meta http-equiv="content-type" content="text/html;charset=utf-8"> + <title>general.c</title> + <link rel="stylesheet" href="../css.css"> + <style type="text/css"> + a.fnlink {text-decoration: none} + a.fnlink:hover {text-decoration: underline} + </style> +</head> +<body> +<div id='container'> + <div id="background"></div> + <div id="jump_to"> + Jump To … + <div id="jump_wrapper"> + <div id="jump_page"> + <a class="source" href="../../#v1.6.1">API Docs</a> + <a class="source" href="add.html">add.c</a> + <a class="source" href="args.html">args.c</a> + <a class="source" href="blame.html">blame.c</a> + <a class="source" href="cat-file.html">cat-file.c</a> + <a class="source" href="checkout.html">checkout.c</a> + <a class="source" href="clone.html">clone.c</a> + <a class="source" href="commit.html">commit.c</a> + <a class="source" href="common.html">common.c</a> + <a class="source" href="config.html">config.c</a> + <a class="source" href="describe.html">describe.c</a> + <a class="source" href="diff.html">diff.c</a> + <a class="source" href="fetch.html">fetch.c</a> + <a class="source" href="for-each-ref.html">for-each-ref.c</a> + <a class="source" href="general.html">general.c</a> + <a class="source" href="index-pack.html">index-pack.c</a> + <a class="source" href="init.html">init.c</a> + <a class="source" href="lg2.html">lg2.c</a> + <a class="source" href="log.html">log.c</a> + <a class="source" href="ls-files.html">ls-files.c</a> + <a class="source" href="ls-remote.html">ls-remote.c</a> + <a class="source" href="merge.html">merge.c</a> + <a class="source" href="push.html">push.c</a> + <a class="source" href="remote.html">remote.c</a> + <a class="source" href="rev-list.html">rev-list.c</a> + <a class="source" href="rev-parse.html">rev-parse.c</a> + <a class="source" href="show-index.html">show-index.c</a> + <a class="source" href="stash.html">stash.c</a> + <a class="source" href="status.html">status.c</a> + <a class="source" href="tag.html">tag.c</a> + </div> + </div> + </div> + <table cellspacing=0 cellpadding=0> + <thead> + <tr> + <th class=docs><h1>general.c</h1></th> + <th class=code></th> + </tr> + </thead> + <tbody> + <tr id='section-1'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-1">¶</a> + </div> + + </td> + <td class=code> + <div class='highlight'><pre><span></span><span class="cm">/*</span> +<span class="cm"> * libgit2 "general" example - shows basic libgit2 concepts</span> +<span class="cm"> *</span> +<span class="cm"> * Written by the libgit2 contributors</span> +<span class="cm"> *</span> +<span class="cm"> * To the extent possible under law, the author(s) have dedicated all copyright</span> +<span class="cm"> * and related and neighboring rights to this software to the public domain</span> +<span class="cm"> * worldwide. This software is distributed without any warranty.</span> +<span class="cm"> *</span> +<span class="cm"> * You should have received a copy of the CC0 Public Domain Dedication along</span> +<span class="cm"> * with this software. If not, see</span> +<span class="cm"> * <http://creativecommons.org/publicdomain/zero/1.0/>.</span> +<span class="cm"> */</span></pre></div> + </td> + </tr> + <tr id='section-2'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-2">¶</a> + </div> + <p><a href="http://libgit2.github.com"><strong>libgit2</strong></a> is a portable, pure C implementation of the Git core +methods provided as a re-entrant linkable library with a solid API, +allowing you to write native speed custom Git applications in any +language which supports C bindings.</p> + +<p>This file is an example of using that API in a real, compilable C file. +As the API is updated, this file will be updated to demonstrate the new +functionality.</p> + +<p>If you're trying to write something in C using <a href="http://libgit2.github.com">libgit2</a>, you should +also check out the generated <a href="http://libgit2.github.com/libgit2">API documentation</a>. We try to link to +the relevant sections of the API docs in each section in this file.</p> + +<p><strong>libgit2</strong> (for the most part) only implements the core plumbing +functions, not really the higher level porcelain stuff. For a primer on +Git Internals that you will need to know to work with Git at this level, +check out <a href="https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain">Chapter 10</a> of the Pro Git book.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="cp">#include</span> <span class="cpf">"common.h"</span><span class="cp"></span></pre></div> + </td> + </tr> + <tr id='section-Includes'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Includes">¶</a> + </div> + <h3>Includes</h3> + +<p>Including the <code>git2.h</code> header will include all the other libgit2 headers +that you need. It should be the only thing you need to include in order +to compile properly and get all the libgit2 API.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="cp">#include</span> <span class="cpf">"git2.h"</span><span class="cp"></span> + +<span class="k">static</span> <span class="kt">void</span> <span class="nf">oid_parsing</span><span class="p">(</span><span class="n">git_oid</span> <span class="o">*</span><span class="n">out</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">object_database</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">,</span> <span class="n">git_oid</span> <span class="o">*</span><span class="n">oid</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">commit_writing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">commit_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">tag_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">tree_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">blob_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">revwalking</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">index_walking</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">reference_listing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span> +<span class="k">static</span> <span class="kt">void</span> <span class="nf">config_files</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">repo_path</span><span class="p">,</span> <span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-4'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-4">¶</a> + </div> + <p>Almost all libgit2 functions return 0 on success or negative on error. +This is not production quality error checking, but should be sufficient +as an example.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">check_error</span><span class="p">(</span><span class="kt">int</span> <span class="n">error_code</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">action</span><span class="p">)</span> +<span class="p">{</span> + <span class="k">const</span> <span class="n">git_error</span> <span class="o">*</span><span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_error_last-33" class="fnlink" href="../../#v1.6.1/group/error/git_error_last">git_error_last</a></span><span class="p">();</span> + <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">error_code</span><span class="p">)</span> + <span class="k">return</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"Error %d %s - %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">error_code</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> + <span class="p">(</span><span class="n">error</span> <span class="o">&&</span> <span class="n">error</span><span class="o">-></span><span class="n">message</span><span class="p">)</span> <span class="o">?</span> <span class="n">error</span><span class="o">-></span><span class="nl">message</span> <span class="p">:</span> <span class="s">"???"</span><span class="p">);</span> + + <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> +<span class="p">}</span> + +<span class="kt">int</span> <span class="nf">lg2_general</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">,</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span> +<span class="p">{</span> + <span class="kt">int</span> <span class="n">error</span><span class="p">;</span> + <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span> + <span class="kt">char</span> <span class="o">*</span><span class="n">repo_path</span><span class="p">;</span></pre></div> + </td> + </tr> + <tr id='section-5'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-5">¶</a> + </div> + <p>Initialize the library, this will set up any global state which libgit2 needs +including threading and crypto</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_libgit2_init-34" class="fnlink" href="../../#v1.6.1/group/libgit2/git_libgit2_init">git_libgit2_init</a></span><span class="p">();</span></pre></div> + </td> + </tr> + <tr id='section-Opening_the_Repository'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Opening_the_Repository">¶</a> + </div> + <h3>Opening the Repository</h3> + +<p>There are a couple of methods for opening a repository, this being the +simplest. There are also <a href="http://libgit2.github.com/libgit2/#HEAD/group/repository">methods</a> for specifying the index file +and work tree locations, here we assume they are in the normal places.</p> + +<p>(Try running this program against tests/resources/testrepo.git.)</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">repo_path</span> <span class="o">=</span> <span class="p">(</span><span class="n">argc</span> <span class="o">></span> <span class="mi">1</span><span class="p">)</span> <span class="o">?</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">:</span> <span class="s">"/opt/libgit2-test/.git"</span><span class="p">;</span> + + <span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_repository_open-59" class="fnlink" href="../../#v1.6.1/group/repository/git_repository_open">git_repository_open</a></span><span class="p">(</span><span class="o">&</span><span class="n">repo</span><span class="p">,</span> <span class="n">repo_path</span><span class="p">);</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"opening repository"</span><span class="p">);</span> + + <span class="n">oid_parsing</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">);</span> + <span class="n">object_database</span><span class="p">(</span><span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span> + <span class="n">commit_writing</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">commit_parsing</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">tag_parsing</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">tree_parsing</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">blob_parsing</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">revwalking</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">index_walking</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">reference_listing</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + <span class="n">config_files</span><span class="p">(</span><span class="n">repo_path</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-7'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-7">¶</a> + </div> + <p>Finally, when you're done with the repository, you can free it as well.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_repository_free-60" class="fnlink" href="../../#v1.6.1/group/repository/git_repository_free">git_repository_free</a></span><span class="p">(</span><span class="n">repo</span><span class="p">);</span> + + <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-SHA-1_Value_Conversions'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-SHA-1_Value_Conversions">¶</a> + </div> + <h3>SHA-1 Value Conversions</h3> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">oid_parsing</span><span class="p">(</span><span class="n">git_oid</span> <span class="o">*</span><span class="n">oid</span><span class="p">)</span> +<span class="p">{</span> + <span class="kt">char</span> <span class="n">out</span><span class="p">[</span><span class="n">GIT_OID_SHA1_HEXSIZE</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span> + <span class="kt">char</span> <span class="n">hex</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"4a202b346bb0fb0db7eff3cffeb3c70babbd2045"</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"*Hex to Raw*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-9'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-9">¶</a> + </div> + <p>For our first example, we will convert a 40 character hex value to the +20 byte raw SHA1 value.</p> + +<p>The <code>git_oid</code> is the structure that keeps the SHA value. We will use +this throughout the example for storing the value of the current SHA +key we're working with.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="n">oid</span><span class="p">,</span> <span class="n">hex</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="n">oid</span><span class="p">,</span> <span class="n">hex</span><span class="p">);</span> +<span class="cp">#endif</span> + + <span class="cm">/*</span> +<span class="cm"> * Once we've converted the string into the oid value, we can get the raw</span> +<span class="cm"> * value of the SHA by accessing `oid.id`</span> +<span class="cm"> *</span> +<span class="cm"> * Next we will convert the 20 byte raw SHA1 value to a human readable 40</span> +<span class="cm"> * char hex value.</span> +<span class="cm"> */</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Raw to Hex*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + <span class="n">out</span><span class="p">[</span><span class="n">GIT_OID_SHA1_HEXSIZE</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span></pre></div> + </td> + </tr> + <tr id='section-10'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-10">¶</a> + </div> + <p>If you have a oid, you can easily get the hex value of the SHA as well.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_oid_fmt-48" class="fnlink" href="../../#v1.6.1/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n">oid</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"SHA hex string: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">out</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Working_with_the_Object_Database'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Working_with_the_Object_Database">¶</a> + </div> + <h3>Working with the Object Database</h3> + +<p><strong>libgit2</strong> provides <a href="http://libgit2.github.com/libgit2/#HEAD/group/odb">direct access</a> to the object database. The +object database is where the actual objects are stored in Git. For +working with raw objects, we'll need to get this structure from the +repository.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">object_database</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">,</span> <span class="n">git_oid</span> <span class="o">*</span><span class="n">oid</span><span class="p">)</span> +<span class="p">{</span> + <span class="kt">char</span> <span class="n">oid_hex</span><span class="p">[</span><span class="n">GIT_OID_SHA1_HEXSIZE</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">};</span> + <span class="k">const</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">data</span><span class="p">;</span> + <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str_type</span><span class="p">;</span> + <span class="kt">int</span> <span class="n">error</span><span class="p">;</span> + <span class="n">git_odb_object</span> <span class="o">*</span><span class="n">obj</span><span class="p">;</span> + <span class="n">git_odb</span> <span class="o">*</span><span class="n">odb</span><span class="p">;</span> + <span class="n">git_object_t</span> <span class="n">otype</span><span class="p">;</span> + + <span class="n"><a name="git_repository_odb-63" class="fnlink" href="../../#v1.6.1/group/repository/git_repository_odb">git_repository_odb</a></span><span class="p">(</span><span class="o">&</span><span class="n">odb</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-Raw_Object_Reading'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Raw_Object_Reading">¶</a> + </div> + <h4>Raw Object Reading</h4> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Raw Object Read*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-13'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-13">¶</a> + </div> + <p>We can read raw objects directly from the object database if we have +the oid (SHA) of the object. This allows us to access objects without +knowing their type and inspect the raw bytes unparsed.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_odb_read-42" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_read">git_odb_read</a></span><span class="p">(</span><span class="o">&</span><span class="n">obj</span><span class="p">,</span> <span class="n">odb</span><span class="p">,</span> <span class="n">oid</span><span class="p">);</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"finding object in repository"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-14'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-14">¶</a> + </div> + <p>A raw object only has three properties - the type (commit, blob, tree +or tag), the size of the raw data and the raw, unparsed data itself. +For a commit or tag, that raw data is human readable plain ASCII +text. For a blob it is just file contents, so it could be text or +binary data. For a tree it is a special binary format, so it's unlikely +to be hugely helpful as a raw object.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">data</span> <span class="o">=</span> <span class="p">(</span><span class="k">const</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="n"><a name="git_odb_object_data-45" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_object_data">git_odb_object_data</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span> + <span class="n">otype</span> <span class="o">=</span> <span class="n"><a name="git_odb_object_type-47" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_object_type">git_odb_object_type</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-15'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-15">¶</a> + </div> + <p>We provide methods to convert from the object type which is an enum, to +a string representation of that value (and vice-versa).</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">str_type</span> <span class="o">=</span> <span class="n"><a name="git_object_type2string-39" class="fnlink" href="../../#v1.6.1/group/object/git_object_type2string">git_object_type2string</a></span><span class="p">(</span><span class="n">otype</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"object length and type: %d, %s</span><span class="se">\n</span><span class="s">object data: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> + <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n"><a name="git_odb_object_size-46" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_object_size">git_odb_object_size</a></span><span class="p">(</span><span class="n">obj</span><span class="p">),</span> + <span class="n">str_type</span><span class="p">,</span> <span class="n">data</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-16'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-16">¶</a> + </div> + <p>For proper memory management, close the object when you are done with +it or it will leak memory.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_odb_object_free-44" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_object_free">git_odb_object_free</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-Raw_Object_Writing'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Raw_Object_Writing">¶</a> + </div> + <h4>Raw Object Writing</h4> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Raw Object Write*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-18'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-18">¶</a> + </div> + <p>You can also write raw object data to Git. This is pretty cool because +it gives you direct access to the key/value properties of Git. Here +we'll write a new blob object that just contains a simple string. +Notice that we have to specify the object type as the <code>git_otype</code> enum.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_odb_write-43" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_write">git_odb_write</a></span><span class="p">(</span><span class="n">oid</span><span class="p">,</span> <span class="n">odb</span><span class="p">,</span> <span class="s">"test data"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="s">"test data"</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">GIT_OBJECT_BLOB</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-19'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-19">¶</a> + </div> + <p>Now that we've written the object, we can check out what SHA1 was +generated when the object was written to our database.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_oid_fmt-49" class="fnlink" href="../../#v1.6.1/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">oid_hex</span><span class="p">,</span> <span class="n">oid</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Written Object: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">oid_hex</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-20'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-20">¶</a> + </div> + <p>Free the object database after usage.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_odb_free-41" class="fnlink" href="../../#v1.6.1/group/odb/git_odb_free">git_odb_free</a></span><span class="p">(</span><span class="n">odb</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Writing_Commits'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Writing_Commits">¶</a> + </div> + <h4>Writing Commits</h4> + +<p>libgit2 provides a couple of methods to create commit objects easily as +well. There are four different create signatures, we'll just show one +of them here. You can read about the other ones in the <a href="http://libgit2.github.com/libgit2/#HEAD/group/commit">commit API +docs</a>.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">commit_writing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="n">git_oid</span> <span class="n">tree_id</span><span class="p">,</span> <span class="n">parent_id</span><span class="p">,</span> <span class="n">commit_id</span><span class="p">;</span> + <span class="n">git_tree</span> <span class="o">*</span><span class="n">tree</span><span class="p">;</span> + <span class="n">git_commit</span> <span class="o">*</span><span class="n">parent</span><span class="p">;</span> + <span class="n">git_signature</span> <span class="o">*</span><span class="n">author</span><span class="p">,</span> <span class="o">*</span><span class="n">committer</span><span class="p">;</span> + <span class="kt">char</span> <span class="n">oid_hex</span><span class="p">[</span><span class="n">GIT_OID_SHA1_HEXSIZE</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">};</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Commit Writing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-22'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-22">¶</a> + </div> + <p>Creating signatures for an authoring identity and time is simple. You +will need to do this to specify who created a commit and when. Default +values for the name and email should be found in the <code>user.name</code> and +<code>user.email</code> configuration options. See the <code>config</code> section of this +example file to see how to access config values.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_signature_new-70" class="fnlink" href="../../#v1.6.1/group/signature/git_signature_new">git_signature_new</a></span><span class="p">(</span><span class="o">&</span><span class="n">author</span><span class="p">,</span> + <span class="s">"Scott Chacon"</span><span class="p">,</span> <span class="s">"schacon@gmail.com"</span><span class="p">,</span> <span class="mi">123456789</span><span class="p">,</span> <span class="mi">60</span><span class="p">);</span> + <span class="n"><a name="git_signature_new-71" class="fnlink" href="../../#v1.6.1/group/signature/git_signature_new">git_signature_new</a></span><span class="p">(</span><span class="o">&</span><span class="n">committer</span><span class="p">,</span> + <span class="s">"Scott A Chacon"</span><span class="p">,</span> <span class="s">"scott@github.com"</span><span class="p">,</span> <span class="mi">987654321</span><span class="p">,</span> <span class="mi">90</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-23'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-23">¶</a> + </div> + <p>Commit objects need a tree to point to and optionally one or more +parents. Here we're creating oid objects to create the commit with, +but you can also use</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">tree_id</span><span class="p">,</span> <span class="s">"f60079018b664e4e79329a7ef9559c8d9e0378d1"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">parent_id</span><span class="p">,</span> <span class="s">"5b5b025afb0b4c913b4c338a42934a3863bf3644"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">tree_id</span><span class="p">,</span> <span class="s">"f60079018b664e4e79329a7ef9559c8d9e0378d1"</span><span class="p">);</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">parent_id</span><span class="p">,</span> <span class="s">"5b5b025afb0b4c913b4c338a42934a3863bf3644"</span><span class="p">);</span> +<span class="cp">#endif</span> + <span class="n"><a name="git_tree_lookup-81" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_lookup">git_tree_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">tree</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">tree_id</span><span class="p">);</span> + <span class="n"><a name="git_commit_lookup-6" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_lookup">git_commit_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">parent</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">parent_id</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-24'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-24">¶</a> + </div> + <p>Here we actually create the commit object with a single call with all +the values we need to create the commit. The SHA key is written to the +<code>commit_id</code> variable here.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_commit_create_v-25" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_create_v">git_commit_create_v</a></span><span class="p">(</span> + <span class="o">&</span><span class="n">commit_id</span><span class="p">,</span> <span class="cm">/* out id */</span> + <span class="n">repo</span><span class="p">,</span> + <span class="nb">NULL</span><span class="p">,</span> <span class="cm">/* do not update the HEAD */</span> + <span class="n">author</span><span class="p">,</span> + <span class="n">committer</span><span class="p">,</span> + <span class="nb">NULL</span><span class="p">,</span> <span class="cm">/* use default message encoding */</span> + <span class="s">"example commit"</span><span class="p">,</span> + <span class="n">tree</span><span class="p">,</span> + <span class="mi">1</span><span class="p">,</span> <span class="n">parent</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-25'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-25">¶</a> + </div> + <p>Now we can take a look at the commit SHA we've generated.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_oid_fmt-50" class="fnlink" href="../../#v1.6.1/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">oid_hex</span><span class="p">,</span> <span class="o">&</span><span class="n">commit_id</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"New Commit: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">oid_hex</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-26'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-26">¶</a> + </div> + <p>Free all objects used in the meanwhile.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_tree_free-83" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_free">git_tree_free</a></span><span class="p">(</span><span class="n">tree</span><span class="p">);</span> + <span class="n"><a name="git_commit_free-9" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">parent</span><span class="p">);</span> + <span class="n"><a name="git_signature_free-72" class="fnlink" href="../../#v1.6.1/group/signature/git_signature_free">git_signature_free</a></span><span class="p">(</span><span class="n">author</span><span class="p">);</span> + <span class="n"><a name="git_signature_free-73" class="fnlink" href="../../#v1.6.1/group/signature/git_signature_free">git_signature_free</a></span><span class="p">(</span><span class="n">committer</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Object_Parsing'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Object_Parsing">¶</a> + </div> + <h3>Object Parsing</h3> + +<p>libgit2 has methods to parse every object type in Git so you don't have +to work directly with the raw data. This is much faster and simpler +than trying to deal with the raw data yourself.</p> + </td> + <td class=code> + <div class='highlight'><pre></pre></div> + </td> + </tr> + <tr id='section-Commit_Parsing'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Commit_Parsing">¶</a> + </div> + <h4>Commit Parsing</h4> + +<p><a href="http://libgit2.github.com/libgit2/#HEAD/group/commit">Parsing commit objects</a> is simple and gives you access to all the +data in the commit - the author (name, email, datetime), committer +(same), tree, message, encoding and parent(s).</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">commit_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="k">const</span> <span class="n">git_signature</span> <span class="o">*</span><span class="n">author</span><span class="p">,</span> <span class="o">*</span><span class="n">cmtter</span><span class="p">;</span> + <span class="n">git_commit</span> <span class="o">*</span><span class="n">commit</span><span class="p">,</span> <span class="o">*</span><span class="n">parent</span><span class="p">;</span> + <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span> + <span class="kt">char</span> <span class="n">oid_hex</span><span class="p">[</span><span class="n">GIT_OID_SHA1_HEXSIZE</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span> + <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">message</span><span class="p">;</span> + <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">parents</span><span class="p">,</span> <span class="n">p</span><span class="p">;</span> + <span class="kt">int</span> <span class="n">error</span><span class="p">;</span> + <span class="kt">time_t</span> <span class="n">time</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Commit Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + +<span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"8496071c1b46c854b31185ea97743be6a8774479"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"8496071c1b46c854b31185ea97743be6a8774479"</span><span class="p">);</span> +<span class="cp">#endif</span> + + <span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_commit_lookup-7" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_lookup">git_commit_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">commit</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"looking up commit"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-29'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-29">¶</a> + </div> + <p>Each of the properties of the commit object are accessible via methods, +including commonly needed variations, such as <code><a name="git_commit_time-18" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_time">git_commit_time</a></code> which +returns the author time and <code><a name="git_commit_message-15" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_message">git_commit_message</a></code> which gives you the +commit message (as a NUL-terminated string).</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">message</span> <span class="o">=</span> <span class="n"><a name="git_commit_message-16" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_message">git_commit_message</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span> + <span class="n">author</span> <span class="o">=</span> <span class="n"><a name="git_commit_author-21" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_author">git_commit_author</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span> + <span class="n">cmtter</span> <span class="o">=</span> <span class="n"><a name="git_commit_committer-20" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_committer">git_commit_committer</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span> + <span class="n">time</span> <span class="o">=</span> <span class="n"><a name="git_commit_time-19" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_time">git_commit_time</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-30'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-30">¶</a> + </div> + <p>The author and committer methods return [git<em>signature] structures, +which give you name, email and <code>when</code>, which is a `git</em>time` structure, +giving you a timestamp and timezone offset.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"Author: %s (%s)</span><span class="se">\n</span><span class="s">Committer: %s (%s)</span><span class="se">\n</span><span class="s">Date: %s</span><span class="se">\n</span><span class="s">Message: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> + <span class="n">author</span><span class="o">-></span><span class="n">name</span><span class="p">,</span> <span class="n">author</span><span class="o">-></span><span class="n">email</span><span class="p">,</span> + <span class="n">cmtter</span><span class="o">-></span><span class="n">name</span><span class="p">,</span> <span class="n">cmtter</span><span class="o">-></span><span class="n">email</span><span class="p">,</span> + <span class="n">ctime</span><span class="p">(</span><span class="o">&</span><span class="n">time</span><span class="p">),</span> <span class="n">message</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-31'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-31">¶</a> + </div> + <p>Commits can have zero or more parents. The first (root) commit will +have no parents, most commits will have one (i.e. the commit it was +based on) and merge commits will have two or more. Commits can +technically have any number, though it's rare to have more than two.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">parents</span> <span class="o">=</span> <span class="n"><a name="git_commit_parentcount-23" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_parentcount">git_commit_parentcount</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span> + <span class="k">for</span> <span class="p">(</span><span class="n">p</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><span class="n">p</span> <span class="o"><</span> <span class="n">parents</span><span class="p">;</span><span class="n">p</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> + <span class="n">memset</span><span class="p">(</span><span class="n">oid_hex</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">oid_hex</span><span class="p">));</span> + + <span class="n"><a name="git_commit_parent-24" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_parent">git_commit_parent</a></span><span class="p">(</span><span class="o">&</span><span class="n">parent</span><span class="p">,</span> <span class="n">commit</span><span class="p">,</span> <span class="n">p</span><span class="p">);</span> + <span class="n"><a name="git_oid_fmt-51" class="fnlink" href="../../#v1.6.1/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">oid_hex</span><span class="p">,</span> <span class="n"><a name="git_commit_id-14" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_id">git_commit_id</a></span><span class="p">(</span><span class="n">parent</span><span class="p">));</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Parent: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">oid_hex</span><span class="p">);</span> + <span class="n"><a name="git_commit_free-10" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">parent</span><span class="p">);</span> + <span class="p">}</span> + + <span class="n"><a name="git_commit_free-11" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Tag_Parsing'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Tag_Parsing">¶</a> + </div> + <h4>Tag Parsing</h4> + +<p>You can parse and create tags with the <a href="http://libgit2.github.com/libgit2/#HEAD/group/tag">tag management API</a>, which +functions very similarly to the commit lookup, parsing and creation +methods, since the objects themselves are very similar.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">tag_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="n">git_commit</span> <span class="o">*</span><span class="n">commit</span><span class="p">;</span> + <span class="n">git_object_t</span> <span class="n">type</span><span class="p">;</span> + <span class="n">git_tag</span> <span class="o">*</span><span class="n">tag</span><span class="p">;</span> + <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span> + <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">message</span><span class="p">;</span> + <span class="kt">int</span> <span class="n">error</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Tag Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-33'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-33">¶</a> + </div> + <p>We create an oid for the tag object if we know the SHA and look it up +the same way that we would a commit (or any other object).</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"b25fa35b38051e4ae45d4222e795f9df2e43f1d1"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"b25fa35b38051e4ae45d4222e795f9df2e43f1d1"</span><span class="p">);</span> +<span class="cp">#endif</span> + + <span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_tag_lookup-75" class="fnlink" href="../../#v1.6.1/group/tag/git_tag_lookup">git_tag_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">tag</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"looking up tag"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-34'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-34">¶</a> + </div> + <p>Now that we have the tag object, we can extract the information it +generally contains: the target (usually a commit object), the type of +the target object (usually 'commit'), the name ('v1.0'), the tagger (a +git_signature - name, email, timestamp), and the tag message.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_tag_target-77" class="fnlink" href="../../#v1.6.1/group/tag/git_tag_target">git_tag_target</a></span><span class="p">((</span><span class="n">git_object</span> <span class="o">**</span><span class="p">)</span><span class="o">&</span><span class="n">commit</span><span class="p">,</span> <span class="n">tag</span><span class="p">);</span> + <span class="n">name</span> <span class="o">=</span> <span class="n"><a name="git_tag_name-79" class="fnlink" href="../../#v1.6.1/group/tag/git_tag_name">git_tag_name</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> <span class="cm">/* "test" */</span> + <span class="n">type</span> <span class="o">=</span> <span class="n"><a name="git_tag_target_type-78" class="fnlink" href="../../#v1.6.1/group/tag/git_tag_target_type">git_tag_target_type</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> <span class="cm">/* GIT_OBJECT_COMMIT (object_t enum) */</span> + <span class="n">message</span> <span class="o">=</span> <span class="n"><a name="git_tag_message-80" class="fnlink" href="../../#v1.6.1/group/tag/git_tag_message">git_tag_message</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> <span class="cm">/* "tag message\n" */</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Tag Name: %s</span><span class="se">\n</span><span class="s">Tag Type: %s</span><span class="se">\n</span><span class="s">Tag Message: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> + <span class="n">name</span><span class="p">,</span> <span class="n"><a name="git_object_type2string-40" class="fnlink" href="../../#v1.6.1/group/object/git_object_type2string">git_object_type2string</a></span><span class="p">(</span><span class="n">type</span><span class="p">),</span> <span class="n">message</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-35'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-35">¶</a> + </div> + <p>Free both the commit and tag after usage.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_commit_free-12" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span> + <span class="n"><a name="git_tag_free-76" class="fnlink" href="../../#v1.6.1/group/tag/git_tag_free">git_tag_free</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Tree_Parsing'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Tree_Parsing">¶</a> + </div> + <h4>Tree Parsing</h4> + +<p><a href="http://libgit2.github.com/libgit2/#HEAD/group/tree">Tree parsing</a> is a bit different than the other objects, in that +we have a subtype which is the tree entry. This is not an actual +object type in Git, but a useful structure for parsing and traversing +tree entries.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">tree_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="k">const</span> <span class="n">git_tree_entry</span> <span class="o">*</span><span class="n">entry</span><span class="p">;</span> + <span class="kt">size_t</span> <span class="n">cnt</span><span class="p">;</span> + <span class="n">git_object</span> <span class="o">*</span><span class="n">obj</span><span class="p">;</span> + <span class="n">git_tree</span> <span class="o">*</span><span class="n">tree</span><span class="p">;</span> + <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Tree Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-37'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-37">¶</a> + </div> + <p>Create the oid and lookup the tree object just like the other objects.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"f60079018b664e4e79329a7ef9559c8d9e0378d1"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"f60079018b664e4e79329a7ef9559c8d9e0378d1"</span><span class="p">);</span> +<span class="cp">#endif</span> + <span class="n"><a name="git_tree_lookup-82" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_lookup">git_tree_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">tree</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-38'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-38">¶</a> + </div> + <p>Getting the count of entries in the tree so you can iterate over them +if you want to.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">cnt</span> <span class="o">=</span> <span class="n"><a name="git_tree_entrycount-85" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_entrycount">git_tree_entrycount</a></span><span class="p">(</span><span class="n">tree</span><span class="p">);</span> <span class="cm">/* 2 */</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"tree entries: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">cnt</span><span class="p">);</span> + + <span class="n">entry</span> <span class="o">=</span> <span class="n"><a name="git_tree_entry_byindex-87" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_entry_byindex">git_tree_entry_byindex</a></span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Entry name: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n"><a name="git_tree_entry_name-88" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_entry_name">git_tree_entry_name</a></span><span class="p">(</span><span class="n">entry</span><span class="p">));</span> <span class="cm">/* "README" */</span></pre></div> + </td> + </tr> + <tr id='section-39'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-39">¶</a> + </div> + <p>You can also access tree entries by name if you know the name of the +entry you're looking for.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">entry</span> <span class="o">=</span> <span class="n"><a name="git_tree_entry_byname-86" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_entry_byname">git_tree_entry_byname</a></span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="s">"README"</span><span class="p">);</span> + <span class="n"><a name="git_tree_entry_name-89" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_entry_name">git_tree_entry_name</a></span><span class="p">(</span><span class="n">entry</span><span class="p">);</span> <span class="cm">/* "README" */</span></pre></div> + </td> + </tr> + <tr id='section-40'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-40">¶</a> + </div> + <p>Once you have the entry object, you can access the content or subtree +(or commit, in the case of submodules) that it points to. You can also +get the mode if you want.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_tree_entry_to_object-90" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_entry_to_object">git_tree_entry_to_object</a></span><span class="p">(</span><span class="o">&</span><span class="n">obj</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="n">entry</span><span class="p">);</span> <span class="cm">/* blob */</span></pre></div> + </td> + </tr> + <tr id='section-41'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-41">¶</a> + </div> + <p>Remember to close the looked-up object and tree once you are done using it</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_object_free-38" class="fnlink" href="../../#v1.6.1/group/object/git_object_free">git_object_free</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span> + <span class="n"><a name="git_tree_free-84" class="fnlink" href="../../#v1.6.1/group/tree/git_tree_free">git_tree_free</a></span><span class="p">(</span><span class="n">tree</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Blob_Parsing'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Blob_Parsing">¶</a> + </div> + <h4>Blob Parsing</h4> + +<p>The last object type is the simplest and requires the least parsing +help. Blobs are just file contents and can contain anything, there is +no structure to it. The main advantage to using the <a href="http://libgit2.github.com/libgit2/#HEAD/group/blob">simple blob +api</a> is that when you're creating blobs you don't have to calculate +the size of the content. There is also a helper for reading a file +from disk and writing it to the db and getting the oid back so you +don't have to do all those steps yourself.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">blob_parsing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="n">git_blob</span> <span class="o">*</span><span class="n">blob</span><span class="p">;</span> + <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Blob Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + +<span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"1385f264afb75a56a5bec74243be9b367ba4ca08"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"1385f264afb75a56a5bec74243be9b367ba4ca08"</span><span class="p">);</span> +<span class="cp">#endif</span> + <span class="n"><a name="git_blob_lookup-1" class="fnlink" href="../../#v1.6.1/group/blob/git_blob_lookup">git_blob_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">blob</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-43'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-43">¶</a> + </div> + <p>You can access a buffer with the raw contents of the blob directly. +Note that this buffer may not be contain ASCII data for certain blobs +(e.g. binary files): do not consider the buffer a NULL-terminated +string, and use the <code><a name="git_blob_rawsize-4" class="fnlink" href="../../#v1.6.1/group/blob/git_blob_rawsize">git_blob_rawsize</a></code> attribute to find out its exact +size in bytes</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"Blob Size: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">long</span><span class="p">)</span><span class="n"><a name="git_blob_rawsize-5" class="fnlink" href="../../#v1.6.1/group/blob/git_blob_rawsize">git_blob_rawsize</a></span><span class="p">(</span><span class="n">blob</span><span class="p">));</span> <span class="cm">/* 8 */</span> + <span class="n"><a name="git_blob_rawcontent-3" class="fnlink" href="../../#v1.6.1/group/blob/git_blob_rawcontent">git_blob_rawcontent</a></span><span class="p">(</span><span class="n">blob</span><span class="p">);</span> <span class="cm">/* "content" */</span></pre></div> + </td> + </tr> + <tr id='section-44'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-44">¶</a> + </div> + <p>Free the blob after usage.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_blob_free-2" class="fnlink" href="../../#v1.6.1/group/blob/git_blob_free">git_blob_free</a></span><span class="p">(</span><span class="n">blob</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Revwalking'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Revwalking">¶</a> + </div> + <h3>Revwalking</h3> + +<p>The libgit2 <a href="http://libgit2.github.com/libgit2/#HEAD/group/revwalk">revision walking api</a> provides methods to traverse the +directed graph created by the parent pointers of the commit objects. +Since all commits point back to the commit that came directly before +them, you can walk this parentage as a graph and find all the commits +that were ancestors of (reachable from) a given starting point. This +can allow you to create <code>git log</code> type functionality.</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">revwalking</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="k">const</span> <span class="n">git_signature</span> <span class="o">*</span><span class="n">cauth</span><span class="p">;</span> + <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">cmsg</span><span class="p">;</span> + <span class="kt">int</span> <span class="n">error</span><span class="p">;</span> + <span class="n">git_revwalk</span> <span class="o">*</span><span class="n">walk</span><span class="p">;</span> + <span class="n">git_commit</span> <span class="o">*</span><span class="n">wcommit</span><span class="p">;</span> + <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Revwalking*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + +<span class="cp">#ifdef GIT_EXPERIMENTAL_SHA256</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"5b5b025afb0b4c913b4c338a42934a3863bf3644"</span><span class="p">,</span> <span class="n">GIT_OID_SHA1</span><span class="p">);</span> +<span class="cp">#else</span> + <span class="n">git_oid_fromstr</span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"5b5b025afb0b4c913b4c338a42934a3863bf3644"</span><span class="p">);</span> +<span class="cp">#endif</span></pre></div> + </td> + </tr> + <tr id='section-46'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-46">¶</a> + </div> + <p>To use the revwalker, create a new walker, tell it how you want to sort +the output and then push one or more starting points onto the walker. +If you want to emulate the output of <code>git log</code> you would push the SHA +of the commit that HEAD points to into the walker and then start +traversing them. You can also 'hide' commits that you want to stop at +or not see any of their ancestors. So if you want to emulate <code>git log +branch1..branch2</code>, you would push the oid of <code>branch2</code> and hide the oid +of <code>branch1</code>.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_revwalk_new-65" class="fnlink" href="../../#v1.6.1/group/revwalk/git_revwalk_new">git_revwalk_new</a></span><span class="p">(</span><span class="o">&</span><span class="n">walk</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span> + <span class="n"><a name="git_revwalk_sorting-68" class="fnlink" href="../../#v1.6.1/group/revwalk/git_revwalk_sorting">git_revwalk_sorting</a></span><span class="p">(</span><span class="n">walk</span><span class="p">,</span> <span class="n">GIT_SORT_TOPOLOGICAL</span> <span class="o">|</span> <span class="n">GIT_SORT_REVERSE</span><span class="p">);</span> + <span class="n"><a name="git_revwalk_push-66" class="fnlink" href="../../#v1.6.1/group/revwalk/git_revwalk_push">git_revwalk_push</a></span><span class="p">(</span><span class="n">walk</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-47'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-47">¶</a> + </div> + <p>Now that we have the starting point pushed onto the walker, we start +asking for ancestors. It will return them in the sorting order we asked +for as commit oids. We can then lookup and parse the committed pointed +at by the returned OID; note that this operation is specially fast +since the raw contents of the commit object will be cached in memory</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="k">while</span> <span class="p">((</span><span class="n"><a name="git_revwalk_next-67" class="fnlink" href="../../#v1.6.1/group/revwalk/git_revwalk_next">git_revwalk_next</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="n">walk</span><span class="p">))</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> + <span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_commit_lookup-8" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_lookup">git_commit_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">wcommit</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"looking up commit during revwalk"</span><span class="p">);</span> + + <span class="n">cmsg</span> <span class="o">=</span> <span class="n"><a name="git_commit_message-17" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_message">git_commit_message</a></span><span class="p">(</span><span class="n">wcommit</span><span class="p">);</span> + <span class="n">cauth</span> <span class="o">=</span> <span class="n"><a name="git_commit_author-22" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_author">git_commit_author</a></span><span class="p">(</span><span class="n">wcommit</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"%s (%s)</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">cmsg</span><span class="p">,</span> <span class="n">cauth</span><span class="o">-></span><span class="n">email</span><span class="p">);</span> + + <span class="n"><a name="git_commit_free-13" class="fnlink" href="../../#v1.6.1/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">wcommit</span><span class="p">);</span> + <span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-48'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-48">¶</a> + </div> + <p>Like the other objects, be sure to free the revwalker when you're done +to prevent memory leaks. Also, make sure that the repository being +walked it not deallocated while the walk is in progress, or it will +result in undefined behavior</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_revwalk_free-69" class="fnlink" href="../../#v1.6.1/group/revwalk/git_revwalk_free">git_revwalk_free</a></span><span class="p">(</span><span class="n">walk</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Index_File_Manipulation_*'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Index_File_Manipulation_*">¶</a> + </div> + <h3>Index File Manipulation *</h3> + +<p>The <a href="http://libgit2.github.com/libgit2/#HEAD/group/index">index file API</a> allows you to read, traverse, update and write +the Git index file (sometimes thought of as the staging area).</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">index_walking</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="n">git_index</span> <span class="o">*</span><span class="n">index</span><span class="p">;</span> + <span class="kt">size_t</span> <span class="n">i</span><span class="p">,</span> <span class="n">ecount</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Index Walking*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-50'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-50">¶</a> + </div> + <p>You can either open the index from the standard location in an open +repository, as we're doing here, or you can open and manipulate any +index file with <code>git_index_open_bare()</code>. The index for the repository +will be located and loaded from disk.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_repository_index-64" class="fnlink" href="../../#v1.6.1/group/repository/git_repository_index">git_repository_index</a></span><span class="p">(</span><span class="o">&</span><span class="n">index</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-51'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-51">¶</a> + </div> + <p>For each entry in the index, you can get a bunch of information +including the SHA (oid), path and mode which map to the tree objects +that are written out. It also has filesystem properties to help +determine what to inspect for changes (ctime, mtime, dev, ino, uid, +gid, file<em>size and flags) All these properties are exported publicly in +the `git</em>index_entry` struct</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">ecount</span> <span class="o">=</span> <span class="n"><a name="git_index_entrycount-36" class="fnlink" href="../../#v1.6.1/group/index/git_index_entrycount">git_index_entrycount</a></span><span class="p">(</span><span class="n">index</span><span class="p">);</span> + <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">ecount</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> + <span class="k">const</span> <span class="n">git_index_entry</span> <span class="o">*</span><span class="n">e</span> <span class="o">=</span> <span class="n"><a name="git_index_get_byindex-37" class="fnlink" href="../../#v1.6.1/group/index/git_index_get_byindex">git_index_get_byindex</a></span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"path: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">e</span><span class="o">-></span><span class="n">path</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"mtime: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">e</span><span class="o">-></span><span class="n">mtime</span><span class="p">.</span><span class="n">seconds</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"fs: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">e</span><span class="o">-></span><span class="n">file_size</span><span class="p">);</span> + <span class="p">}</span> + + <span class="n"><a name="git_index_free-35" class="fnlink" href="../../#v1.6.1/group/index/git_index_free">git_index_free</a></span><span class="p">(</span><span class="n">index</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-References'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-References">¶</a> + </div> + <h3>References</h3> + +<p>The <a href="http://libgit2.github.com/libgit2/#HEAD/group/reference">reference API</a> allows you to list, resolve, create and update +references such as branches, tags and remote references (everything in +the .git/refs directory).</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">reference_listing</span><span class="p">(</span><span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="n">git_strarray</span> <span class="n">ref_list</span><span class="p">;</span> + <span class="kt">unsigned</span> <span class="n">i</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Reference Listing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-53'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-53">¶</a> + </div> + <p>Here we will implement something like <code>git for-each-ref</code> simply listing +out all available references and the object SHA they resolve to.</p> + +<p>Now that we have the list of reference names, we can lookup each ref +one at a time and resolve them to the SHA, then print both values out.</p> + </td> + <td class=code> + <div class='highlight'><pre> <span class="n"><a name="git_reference_list-57" class="fnlink" href="../../#v1.6.1/group/reference/git_reference_list">git_reference_list</a></span><span class="p">(</span><span class="o">&</span><span class="n">ref_list</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span> + + <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">ref_list</span><span class="p">.</span><span class="n">count</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> + <span class="n">git_reference</span> <span class="o">*</span><span class="n">ref</span><span class="p">;</span> + <span class="kt">char</span> <span class="n">oid_hex</span><span class="p">[</span><span class="n">GIT_OID_SHA1_HEXSIZE</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">GIT_OID_SHA1_HEXZERO</span><span class="p">;</span> + <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">refname</span><span class="p">;</span> + + <span class="n">refname</span> <span class="o">=</span> <span class="n">ref_list</span><span class="p">.</span><span class="n">strings</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> + <span class="n"><a name="git_reference_lookup-53" class="fnlink" href="../../#v1.6.1/group/reference/git_reference_lookup">git_reference_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">ref</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="n">refname</span><span class="p">);</span> + + <span class="k">switch</span> <span class="p">(</span><span class="n"><a name="git_reference_type-56" class="fnlink" href="../../#v1.6.1/group/reference/git_reference_type">git_reference_type</a></span><span class="p">(</span><span class="n">ref</span><span class="p">))</span> <span class="p">{</span> + <span class="k">case</span> <span class="nl">GIT_REFERENCE_DIRECT</span><span class="p">:</span> + <span class="n"><a name="git_oid_fmt-52" class="fnlink" href="../../#v1.6.1/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">oid_hex</span><span class="p">,</span> <span class="n"><a name="git_reference_target-54" class="fnlink" href="../../#v1.6.1/group/reference/git_reference_target">git_reference_target</a></span><span class="p">(</span><span class="n">ref</span><span class="p">));</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"%s [%s]</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">refname</span><span class="p">,</span> <span class="n">oid_hex</span><span class="p">);</span> + <span class="k">break</span><span class="p">;</span> + + <span class="k">case</span> <span class="nl">GIT_REFERENCE_SYMBOLIC</span><span class="p">:</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"%s => %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">refname</span><span class="p">,</span> <span class="n"><a name="git_reference_symbolic_target-55" class="fnlink" href="../../#v1.6.1/group/reference/git_reference_symbolic_target">git_reference_symbolic_target</a></span><span class="p">(</span><span class="n">ref</span><span class="p">));</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">default</span><span class="o">:</span> + <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Unexpected reference type</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + <span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> + <span class="p">}</span> + + <span class="n"><a name="git_reference_free-58" class="fnlink" href="../../#v1.6.1/group/reference/git_reference_free">git_reference_free</a></span><span class="p">(</span><span class="n">ref</span><span class="p">);</span> + <span class="p">}</span> + + <span class="n"><a name="git_strarray_dispose-74" class="fnlink" href="../../#v1.6.1/group/strarray/git_strarray_dispose">git_strarray_dispose</a></span><span class="p">(</span><span class="o">&</span><span class="n">ref_list</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + <tr id='section-Config_Files'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-Config_Files">¶</a> + </div> + <h3>Config Files</h3> + +<p>The <a href="http://libgit2.github.com/libgit2/#HEAD/group/config">config API</a> allows you to list and update config values +in any of the accessible config file locations (system, global, local).</p> + </td> + <td class=code> + <div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">config_files</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">repo_path</span><span class="p">,</span> <span class="n">git_repository</span><span class="o">*</span> <span class="n">repo</span><span class="p">)</span> +<span class="p">{</span> + <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">email</span><span class="p">;</span> + <span class="kt">char</span> <span class="n">config_path</span><span class="p">[</span><span class="mi">256</span><span class="p">];</span> + <span class="kt">int32_t</span> <span class="n">autocorrect</span><span class="p">;</span> + <span class="n">git_config</span> <span class="o">*</span><span class="n">cfg</span><span class="p">;</span> + <span class="n">git_config</span> <span class="o">*</span><span class="n">snap_cfg</span><span class="p">;</span> + <span class="kt">int</span> <span class="n">error_code</span><span class="p">;</span> + + <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Config Listing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div> + </td> + </tr> + <tr id='section-55'> + <td class=docs> + <div class="pilwrap"> + <a class="pilcrow" href="#section-55">¶</a> + </div> + <p>Open a config object so we can read global values from it.</p> + + </td> + <td class=code> + <div class='highlight'><pre> <span class="n">sprintf</span><span class="p">(</span><span class="n">config_path</span><span class="p">,</span> <span class="s">"%s/config"</span><span class="p">,</span> <span class="n">repo_path</span><span class="p">);</span> + <span class="n">check_error</span><span class="p">(</span><span class="n"><a name="git_config_open_ondisk-26" class="fnlink" href="../../#v1.6.1/group/config/git_config_open_ondisk">git_config_open_ondisk</a></span><span class="p">(</span><span class="o">&</span><span class="n">cfg</span><span class="p">,</span> <span class="n">config_path</span><span class="p">),</span> <span class="s">"opening config"</span><span class="p">);</span> + + <span class="k">if</span> <span class="p">(</span><span class="n"><a name="git_config_get_int32-29" class="fnlink" href="../../#v1.6.1/group/config/git_config_get_int32">git_config_get_int32</a></span><span class="p">(</span><span class="o">&</span><span class="n">autocorrect</span><span class="p">,</span> <span class="n">cfg</span><span class="p">,</span> <span class="s">"help.autocorrect"</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Autocorrect: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">autocorrect</span><span class="p">);</span> + + <span class="n">check_error</span><span class="p">(</span><span class="n"><a name="git_repository_config_snapshot-61" class="fnlink" href="../../#v1.6.1/group/repository/git_repository_config_snapshot">git_repository_config_snapshot</a></span><span class="p">(</span><span class="o">&</span><span class="n">snap_cfg</span><span class="p">,</span> <span class="n">repo</span><span class="p">),</span> <span class="s">"config snapshot"</span><span class="p">);</span> + <span class="n"><a name="git_config_get_string-31" class="fnlink" href="../../#v1.6.1/group/config/git_config_get_string">git_config_get_string</a></span><span class="p">(</span><span class="o">&</span><span class="n">email</span><span class="p">,</span> <span class="n">snap_cfg</span><span class="p">,</span> <span class="s">"user.email"</span><span class="p">);</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Email: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">email</span><span class="p">);</span> + + <span class="n">error_code</span> <span class="o">=</span> <span class="n"><a name="git_config_get_int32-30" class="fnlink" href="../../#v1.6.1/group/config/git_config_get_int32">git_config_get_int32</a></span><span class="p">(</span><span class="o">&</span><span class="n">autocorrect</span><span class="p">,</span> <span class="n">cfg</span><span class="p">,</span> <span class="s">"help.autocorrect"</span><span class="p">);</span> + <span class="k">switch</span> <span class="p">(</span><span class="n">error_code</span><span class="p">)</span> + <span class="p">{</span> + <span class="k">case</span> <span class="mi">0</span><span class="o">:</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Autocorrect: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">autocorrect</span><span class="p">);</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">case</span> <span class="nl">GIT_ENOTFOUND</span><span class="p">:</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Autocorrect: Undefined</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">default</span><span class="o">:</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error_code</span><span class="p">,</span> <span class="s">"get_int32 failed"</span><span class="p">);</span> + <span class="p">}</span> + <span class="n"><a name="git_config_free-27" class="fnlink" href="../../#v1.6.1/group/config/git_config_free">git_config_free</a></span><span class="p">(</span><span class="n">cfg</span><span class="p">);</span> + + <span class="n">check_error</span><span class="p">(</span><span class="n"><a name="git_repository_config_snapshot-62" class="fnlink" href="../../#v1.6.1/group/repository/git_repository_config_snapshot">git_repository_config_snapshot</a></span><span class="p">(</span><span class="o">&</span><span class="n">snap_cfg</span><span class="p">,</span> <span class="n">repo</span><span class="p">),</span> <span class="s">"config snapshot"</span><span class="p">);</span> + <span class="n">error_code</span> <span class="o">=</span> <span class="n"><a name="git_config_get_string-32" class="fnlink" href="../../#v1.6.1/group/config/git_config_get_string">git_config_get_string</a></span><span class="p">(</span><span class="o">&</span><span class="n">email</span><span class="p">,</span> <span class="n">snap_cfg</span><span class="p">,</span> <span class="s">"user.email"</span><span class="p">);</span> + <span class="k">switch</span> <span class="p">(</span><span class="n">error_code</span><span class="p">)</span> + <span class="p">{</span> + <span class="k">case</span> <span class="mi">0</span><span class="o">:</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Email: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">email</span><span class="p">);</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">case</span> <span class="nl">GIT_ENOTFOUND</span><span class="p">:</span> + <span class="n">printf</span><span class="p">(</span><span class="s">"Email: Undefined</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">default</span><span class="o">:</span> + <span class="n">check_error</span><span class="p">(</span><span class="n">error_code</span><span class="p">,</span> <span class="s">"get_string failed"</span><span class="p">);</span> + <span class="p">}</span> + + <span class="n"><a name="git_config_free-28" class="fnlink" href="../../#v1.6.1/group/config/git_config_free">git_config_free</a></span><span class="p">(</span><span class="n">snap_cfg</span><span class="p">);</span> +<span class="p">}</span></pre></div> + </td> + </tr> + </table> +</div> +</body> |