Blame examples/general.c

Packit ae9e2a
/*
Packit ae9e2a
 * libgit2 "general" example - shows basic libgit2 concepts
Packit ae9e2a
 *
Packit ae9e2a
 * Written by the libgit2 contributors
Packit ae9e2a
 *
Packit ae9e2a
 * To the extent possible under law, the author(s) have dedicated all copyright
Packit ae9e2a
 * and related and neighboring rights to this software to the public domain
Packit ae9e2a
 * worldwide. This software is distributed without any warranty.
Packit ae9e2a
 *
Packit ae9e2a
 * You should have received a copy of the CC0 Public Domain Dedication along
Packit ae9e2a
 * with this software. If not, see
Packit ae9e2a
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * [**libgit2**][lg] is a portable, pure C implementation of the Git core
Packit ae9e2a
 * methods provided as a re-entrant linkable library with a solid API,
Packit ae9e2a
 * allowing you to write native speed custom Git applications in any
Packit ae9e2a
 * language which supports C bindings.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is an example of using that API in a real, compilable C file.
Packit ae9e2a
 * As the API is updated, this file will be updated to demonstrate the new
Packit ae9e2a
 * functionality.
Packit ae9e2a
 *
Packit ae9e2a
 * If you're trying to write something in C using [libgit2][lg], you should
Packit ae9e2a
 * also check out the generated [API documentation][ap]. We try to link to
Packit ae9e2a
 * the relevant sections of the API docs in each section in this file.
Packit ae9e2a
 *
Packit ae9e2a
 * **libgit2** (for the most part) only implements the core plumbing
Packit ae9e2a
 * functions, not really the higher level porcelain stuff. For a primer on
Packit ae9e2a
 * Git Internals that you will need to know to work with Git at this level,
Packit ae9e2a
 * check out [Chapter 10][pg] of the Pro Git book.
Packit ae9e2a
 *
Packit ae9e2a
 * [lg]: http://libgit2.github.com
Packit ae9e2a
 * [ap]: http://libgit2.github.com/libgit2
Packit ae9e2a
 * [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### Includes
Packit ae9e2a
 *
Packit ae9e2a
 * Including the `git2.h` header will include all the other libgit2 headers
Packit ae9e2a
 * that you need.  It should be the only thing you need to include in order
Packit ae9e2a
 * to compile properly and get all the libgit2 API.
Packit ae9e2a
 */
Packit ae9e2a
#include <git2.h>
Packit ae9e2a
#include <stdio.h>
Packit ae9e2a
#include <string.h>
Packit ae9e2a
Packit ae9e2a
static void oid_parsing(git_oid *out);
Packit ae9e2a
static void object_database(git_repository *repo, git_oid *oid);
Packit ae9e2a
static void commit_writing(git_repository *repo);
Packit ae9e2a
static void commit_parsing(git_repository *repo);
Packit ae9e2a
static void tag_parsing(git_repository *repo);
Packit ae9e2a
static void tree_parsing(git_repository *repo);
Packit ae9e2a
static void blob_parsing(git_repository *repo);
Packit ae9e2a
static void revwalking(git_repository *repo);
Packit ae9e2a
static void index_walking(git_repository *repo);
Packit ae9e2a
static void reference_listing(git_repository *repo);
Packit ae9e2a
static void config_files(const char *repo_path, git_repository *repo);
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Almost all libgit2 functions return 0 on success or negative on error.
Packit ae9e2a
 * This is not production quality error checking, but should be sufficient
Packit ae9e2a
 * as an example.
Packit ae9e2a
 */
Packit ae9e2a
static void check_error(int error_code, const char *action)
Packit ae9e2a
{
Packit ae9e2a
	const git_error *error = giterr_last();
Packit ae9e2a
	if (!error_code)
Packit ae9e2a
		return;
Packit ae9e2a
Packit ae9e2a
	printf("Error %d %s - %s\n", error_code, action,
Packit ae9e2a
			(error && error->message) ? error->message : "???");
Packit ae9e2a
Packit ae9e2a
	exit(1);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int main (int argc, char** argv)
Packit ae9e2a
{
Packit ae9e2a
	int error;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
	char *repo_path;
Packit ae9e2a
	git_repository *repo;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Initialize the library, this will set up any global state which libgit2 needs
Packit ae9e2a
	 * including threading and crypto
Packit ae9e2a
	 */
Packit ae9e2a
	git_libgit2_init();
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * ### Opening the Repository
Packit ae9e2a
	 *
Packit ae9e2a
	 * There are a couple of methods for opening a repository, this being the
Packit ae9e2a
	 * simplest.  There are also [methods][me] for specifying the index file
Packit ae9e2a
	 * and work tree locations, here we assume they are in the normal places.
Packit ae9e2a
	 *
Packit ae9e2a
	 * (Try running this program against tests/resources/testrepo.git.)
Packit ae9e2a
	 *
Packit ae9e2a
	 * [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
Packit ae9e2a
	 */
Packit ae9e2a
	repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
Packit ae9e2a
Packit ae9e2a
	error = git_repository_open(&repo, repo_path);
Packit ae9e2a
	check_error(error, "opening repository");
Packit ae9e2a
Packit ae9e2a
	oid_parsing(&oid;;
Packit ae9e2a
	object_database(repo, &oid;;
Packit ae9e2a
	commit_writing(repo);
Packit ae9e2a
	commit_parsing(repo);
Packit ae9e2a
	tag_parsing(repo);
Packit ae9e2a
	tree_parsing(repo);
Packit ae9e2a
	blob_parsing(repo);
Packit ae9e2a
	revwalking(repo);
Packit ae9e2a
	index_walking(repo);
Packit ae9e2a
	reference_listing(repo);
Packit ae9e2a
	config_files(repo_path, repo);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Finally, when you're done with the repository, you can free it as well.
Packit ae9e2a
	 */
Packit ae9e2a
	git_repository_free(repo);
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### SHA-1 Value Conversions
Packit ae9e2a
 */
Packit ae9e2a
static void oid_parsing(git_oid *oid)
Packit ae9e2a
{
Packit ae9e2a
	char out[GIT_OID_HEXSZ+1];
Packit ae9e2a
	char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
Packit ae9e2a
Packit ae9e2a
	printf("*Hex to Raw*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * For our first example, we will convert a 40 character hex value to the
Packit ae9e2a
	 * 20 byte raw SHA1 value.
Packit ae9e2a
	 *
Packit ae9e2a
	 * The `git_oid` is the structure that keeps the SHA value. We will use
Packit ae9e2a
	 * this throughout the example for storing the value of the current SHA
Packit ae9e2a
	 * key we're working with.
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fromstr(oid, hex);
Packit ae9e2a
Packit ae9e2a
	// Once we've converted the string into the oid value, we can get the raw
Packit ae9e2a
	// value of the SHA by accessing `oid.id`
Packit ae9e2a
Packit ae9e2a
	// Next we will convert the 20 byte raw SHA1 value to a human readable 40
Packit ae9e2a
	// char hex value.
Packit ae9e2a
	printf("\n*Raw to Hex*\n");
Packit ae9e2a
	out[GIT_OID_HEXSZ] = '\0';
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * If you have a oid, you can easily get the hex value of the SHA as well.
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fmt(out, oid);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * If you have a oid, you can easily get the hex value of the SHA as well.
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fmt(out, oid);
Packit ae9e2a
	printf("SHA hex string: %s\n", out);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### Working with the Object Database
Packit ae9e2a
 *
Packit ae9e2a
 * **libgit2** provides [direct access][odb] to the object database.  The
Packit ae9e2a
 * object database is where the actual objects are stored in Git. For
Packit ae9e2a
 * working with raw objects, we'll need to get this structure from the
Packit ae9e2a
 * repository.
Packit ae9e2a
 *
Packit ae9e2a
 * [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb
Packit ae9e2a
 */
Packit ae9e2a
static void object_database(git_repository *repo, git_oid *oid)
Packit ae9e2a
{
Packit ae9e2a
	char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
Packit ae9e2a
	const unsigned char *data;
Packit ae9e2a
	const char *str_type;
Packit ae9e2a
	int error;
Packit ae9e2a
	git_odb_object *obj;
Packit ae9e2a
	git_odb *odb;
Packit ae9e2a
	git_otype otype;
Packit ae9e2a
Packit ae9e2a
	git_repository_odb(&odb, repo);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * #### Raw Object Reading
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	printf("\n*Raw Object Read*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * We can read raw objects directly from the object database if we have
Packit ae9e2a
	 * the oid (SHA) of the object.  This allows us to access objects without
Packit ae9e2a
	 * knowing their type and inspect the raw bytes unparsed.
Packit ae9e2a
	 */
Packit ae9e2a
	error = git_odb_read(&obj, odb, oid);
Packit ae9e2a
	check_error(error, "finding object in repository");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * A raw object only has three properties - the type (commit, blob, tree
Packit ae9e2a
	 * or tag), the size of the raw data and the raw, unparsed data itself.
Packit ae9e2a
	 * For a commit or tag, that raw data is human readable plain ASCII
Packit ae9e2a
	 * text. For a blob it is just file contents, so it could be text or
Packit ae9e2a
	 * binary data. For a tree it is a special binary format, so it's unlikely
Packit ae9e2a
	 * to be hugely helpful as a raw object.
Packit ae9e2a
	 */
Packit ae9e2a
	data = (const unsigned char *)git_odb_object_data(obj);
Packit ae9e2a
	otype = git_odb_object_type(obj);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * We provide methods to convert from the object type which is an enum, to
Packit ae9e2a
	 * a string representation of that value (and vice-versa).
Packit ae9e2a
	 */
Packit ae9e2a
	str_type = git_object_type2string(otype);
Packit ae9e2a
	printf("object length and type: %d, %s\nobject data: %s\n",
Packit ae9e2a
			(int)git_odb_object_size(obj),
Packit ae9e2a
			str_type, data);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * For proper memory management, close the object when you are done with
Packit ae9e2a
	 * it or it will leak memory.
Packit ae9e2a
	 */
Packit ae9e2a
	git_odb_object_free(obj);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * #### Raw Object Writing
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	printf("\n*Raw Object Write*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * You can also write raw object data to Git. This is pretty cool because
Packit ae9e2a
	 * it gives you direct access to the key/value properties of Git.  Here
Packit ae9e2a
	 * we'll write a new blob object that just contains a simple string.
Packit ae9e2a
	 * Notice that we have to specify the object type as the `git_otype` enum.
Packit ae9e2a
	 */
Packit ae9e2a
	git_odb_write(oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Now that we've written the object, we can check out what SHA1 was
Packit ae9e2a
	 * generated when the object was written to our database.
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fmt(oid_hex, oid);
Packit ae9e2a
	printf("Written Object: %s\n", oid_hex);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Free the object database after usage.
Packit ae9e2a
	 */
Packit ae9e2a
	git_odb_free(odb);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * #### Writing Commits
Packit ae9e2a
 *
Packit ae9e2a
 * libgit2 provides a couple of methods to create commit objects easily as
Packit ae9e2a
 * well. There are four different create signatures, we'll just show one
Packit ae9e2a
 * of them here.  You can read about the other ones in the [commit API
Packit ae9e2a
 * docs][cd].
Packit ae9e2a
 *
Packit ae9e2a
 * [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit
Packit ae9e2a
 */
Packit ae9e2a
static void commit_writing(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	git_oid tree_id, parent_id, commit_id;
Packit ae9e2a
	git_tree *tree;
Packit ae9e2a
	git_commit *parent;
Packit ae9e2a
	git_signature *author, *committer;
Packit ae9e2a
	char oid_hex[GIT_OID_HEXSZ+1] = { 0 };
Packit ae9e2a
Packit ae9e2a
	printf("\n*Commit Writing*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Creating signatures for an authoring identity and time is simple.  You
Packit ae9e2a
	 * will need to do this to specify who created a commit and when.  Default
Packit ae9e2a
	 * values for the name and email should be found in the `user.name` and
Packit ae9e2a
	 * `user.email` configuration options.  See the `config` section of this
Packit ae9e2a
	 * example file to see how to access config values.
Packit ae9e2a
	 */
Packit ae9e2a
	git_signature_new(&author,
Packit ae9e2a
			"Scott Chacon", "schacon@gmail.com", 123456789, 60);
Packit ae9e2a
	git_signature_new(&committer,
Packit ae9e2a
			"Scott A Chacon", "scott@github.com", 987654321, 90);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Commit objects need a tree to point to and optionally one or more
Packit ae9e2a
	 * parents.  Here we're creating oid objects to create the commit with,
Packit ae9e2a
	 * but you can also use
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
Packit ae9e2a
	git_tree_lookup(&tree, repo, &tree_id);
Packit ae9e2a
	git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
Packit ae9e2a
	git_commit_lookup(&parent, repo, &parent_id);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Here we actually create the commit object with a single call with all
Packit ae9e2a
	 * the values we need to create the commit.  The SHA key is written to the
Packit ae9e2a
	 * `commit_id` variable here.
Packit ae9e2a
	 */
Packit ae9e2a
	git_commit_create_v(
Packit ae9e2a
			&commit_id, /* out id */
Packit ae9e2a
			repo,
Packit ae9e2a
			NULL, /* do not update the HEAD */
Packit ae9e2a
			author,
Packit ae9e2a
			committer,
Packit ae9e2a
			NULL, /* use default message encoding */
Packit ae9e2a
			"example commit",
Packit ae9e2a
			tree,
Packit ae9e2a
			1, parent);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Now we can take a look at the commit SHA we've generated.
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fmt(oid_hex, &commit_id);
Packit ae9e2a
	printf("New Commit: %s\n", oid_hex);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Free all objects used in the meanwhile.
Packit ae9e2a
	 */
Packit ae9e2a
	git_tree_free(tree);
Packit ae9e2a
	git_commit_free(parent);
Packit ae9e2a
	git_signature_free(author);
Packit ae9e2a
	git_signature_free(committer);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### Object Parsing
Packit ae9e2a
 *
Packit ae9e2a
 * libgit2 has methods to parse every object type in Git so you don't have
Packit ae9e2a
 * to work directly with the raw data. This is much faster and simpler
Packit ae9e2a
 * than trying to deal with the raw data yourself.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * #### Commit Parsing
Packit ae9e2a
 *
Packit ae9e2a
 * [Parsing commit objects][pco] is simple and gives you access to all the
Packit ae9e2a
 * data in the commit - the author (name, email, datetime), committer
Packit ae9e2a
 * (same), tree, message, encoding and parent(s).
Packit ae9e2a
 *
Packit ae9e2a
 * [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit
Packit ae9e2a
 */
Packit ae9e2a
static void commit_parsing(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	const git_signature *author, *cmtter;
Packit ae9e2a
	git_commit *commit, *parent;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
	char oid_hex[GIT_OID_HEXSZ+1];
Packit ae9e2a
	const char *message;
Packit ae9e2a
	unsigned int parents, p;
Packit ae9e2a
	int error;
Packit ae9e2a
	time_t time;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Commit Parsing*\n");
Packit ae9e2a
Packit ae9e2a
	git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
Packit ae9e2a
Packit ae9e2a
	error = git_commit_lookup(&commit, repo, &oid;;
Packit ae9e2a
	check_error(error, "looking up commit");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Each of the properties of the commit object are accessible via methods,
Packit ae9e2a
	 * including commonly needed variations, such as `git_commit_time` which
Packit ae9e2a
	 * returns the author time and `git_commit_message` which gives you the
Packit ae9e2a
	 * commit message (as a NUL-terminated string).
Packit ae9e2a
	 */
Packit ae9e2a
	message  = git_commit_message(commit);
Packit ae9e2a
	author   = git_commit_author(commit);
Packit ae9e2a
	cmtter   = git_commit_committer(commit);
Packit ae9e2a
	time    = git_commit_time(commit);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * The author and committer methods return [git_signature] structures,
Packit ae9e2a
	 * which give you name, email and `when`, which is a `git_time` structure,
Packit ae9e2a
	 * giving you a timestamp and timezone offset.
Packit ae9e2a
	 */
Packit ae9e2a
	printf("Author: %s (%s)\nCommitter: %s (%s)\nDate: %s\nMessage: %s\n",
Packit ae9e2a
		author->name, author->email,
Packit ae9e2a
		cmtter->name, cmtter->email,
Packit ae9e2a
		ctime(&time), message);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Commits can have zero or more parents. The first (root) commit will
Packit ae9e2a
	 * have no parents, most commits will have one (i.e. the commit it was
Packit ae9e2a
	 * based on) and merge commits will have two or more.  Commits can
Packit ae9e2a
	 * technically have any number, though it's rare to have more than two.
Packit ae9e2a
	 */
Packit ae9e2a
	parents  = git_commit_parentcount(commit);
Packit ae9e2a
	for (p = 0;p < parents;p++) {
Packit ae9e2a
		memset(oid_hex, 0, sizeof(oid_hex));
Packit ae9e2a
Packit ae9e2a
		git_commit_parent(&parent, commit, p);
Packit ae9e2a
		git_oid_fmt(oid_hex, git_commit_id(parent));
Packit ae9e2a
		printf("Parent: %s\n", oid_hex);
Packit ae9e2a
		git_commit_free(parent);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_commit_free(commit);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * #### Tag Parsing
Packit ae9e2a
 *
Packit ae9e2a
 * You can parse and create tags with the [tag management API][tm], which
Packit ae9e2a
 * functions very similarly to the commit lookup, parsing and creation
Packit ae9e2a
 * methods, since the objects themselves are very similar.
Packit ae9e2a
 *
Packit ae9e2a
 * [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag
Packit ae9e2a
 */
Packit ae9e2a
static void tag_parsing(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	git_commit *commit;
Packit ae9e2a
	git_otype type;
Packit ae9e2a
	git_tag *tag;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
	const char *name, *message;
Packit ae9e2a
	int error;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Tag Parsing*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * We create an oid for the tag object if we know the SHA and look it up
Packit ae9e2a
	 * the same way that we would a commit (or any other object).
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
Packit ae9e2a
Packit ae9e2a
	error = git_tag_lookup(&tag, repo, &oid;;
Packit ae9e2a
	check_error(error, "looking up tag");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Now that we have the tag object, we can extract the information it
Packit ae9e2a
	 * generally contains: the target (usually a commit object), the type of
Packit ae9e2a
	 * the target object (usually 'commit'), the name ('v1.0'), the tagger (a
Packit ae9e2a
	 * git_signature - name, email, timestamp), and the tag message.
Packit ae9e2a
	 */
Packit ae9e2a
	git_tag_target((git_object **)&commit, tag);
Packit ae9e2a
	name = git_tag_name(tag);		/* "test" */
Packit ae9e2a
	type = git_tag_target_type(tag);	/* GIT_OBJ_COMMIT (otype enum) */
Packit ae9e2a
	message = git_tag_message(tag);		/* "tag message\n" */
Packit ae9e2a
	printf("Tag Name: %s\nTag Type: %s\nTag Message: %s\n",
Packit ae9e2a
		name, git_object_type2string(type), message);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Free both the commit and tag after usage.
Packit ae9e2a
	 */
Packit ae9e2a
	git_commit_free(commit);
Packit ae9e2a
	git_tag_free(tag);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * #### Tree Parsing
Packit ae9e2a
 *
Packit ae9e2a
 * [Tree parsing][tp] is a bit different than the other objects, in that
Packit ae9e2a
 * we have a subtype which is the tree entry.  This is not an actual
Packit ae9e2a
 * object type in Git, but a useful structure for parsing and traversing
Packit ae9e2a
 * tree entries.
Packit ae9e2a
 *
Packit ae9e2a
 * [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree
Packit ae9e2a
 */
Packit ae9e2a
static void tree_parsing(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	const git_tree_entry *entry;
Packit ae9e2a
	size_t cnt;
Packit ae9e2a
	git_object *obj;
Packit ae9e2a
	git_tree *tree;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Tree Parsing*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Create the oid and lookup the tree object just like the other objects.
Packit ae9e2a
	 */
Packit ae9e2a
	git_oid_fromstr(&oid, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
Packit ae9e2a
	git_tree_lookup(&tree, repo, &oid;;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Getting the count of entries in the tree so you can iterate over them
Packit ae9e2a
	 * if you want to.
Packit ae9e2a
	 */
Packit ae9e2a
	cnt = git_tree_entrycount(tree); /* 2 */
Packit ae9e2a
	printf("tree entries: %d\n", (int) cnt);
Packit ae9e2a
Packit ae9e2a
	entry = git_tree_entry_byindex(tree, 0);
Packit ae9e2a
	printf("Entry name: %s\n", git_tree_entry_name(entry)); /* "README" */
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * You can also access tree entries by name if you know the name of the
Packit ae9e2a
	 * entry you're looking for.
Packit ae9e2a
	 */
Packit ae9e2a
	entry = git_tree_entry_byname(tree, "README");
Packit ae9e2a
	git_tree_entry_name(entry); /* "README" */
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Once you have the entry object, you can access the content or subtree
Packit ae9e2a
	 * (or commit, in the case of submodules) that it points to.  You can also
Packit ae9e2a
	 * get the mode if you want.
Packit ae9e2a
	 */
Packit ae9e2a
	git_tree_entry_to_object(&obj, repo, entry); /* blob */
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Remember to close the looked-up object and tree once you are done using it
Packit ae9e2a
	 */
Packit ae9e2a
	git_object_free(obj);
Packit ae9e2a
	git_tree_free(tree);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * #### Blob Parsing
Packit ae9e2a
 *
Packit ae9e2a
 * The last object type is the simplest and requires the least parsing
Packit ae9e2a
 * help. Blobs are just file contents and can contain anything, there is
Packit ae9e2a
 * no structure to it. The main advantage to using the [simple blob
Packit ae9e2a
 * api][ba] is that when you're creating blobs you don't have to calculate
Packit ae9e2a
 * the size of the content.  There is also a helper for reading a file
Packit ae9e2a
 * from disk and writing it to the db and getting the oid back so you
Packit ae9e2a
 * don't have to do all those steps yourself.
Packit ae9e2a
 *
Packit ae9e2a
 * [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob
Packit ae9e2a
 */
Packit ae9e2a
static void blob_parsing(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	git_blob *blob;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Blob Parsing*\n");
Packit ae9e2a
Packit ae9e2a
	git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
Packit ae9e2a
	git_blob_lookup(&blob, repo, &oid;;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * You can access a buffer with the raw contents of the blob directly.
Packit ae9e2a
	 * Note that this buffer may not be contain ASCII data for certain blobs
Packit ae9e2a
	 * (e.g. binary files): do not consider the buffer a NULL-terminated
Packit ae9e2a
	 * string, and use the `git_blob_rawsize` attribute to find out its exact
Packit ae9e2a
	 * size in bytes
Packit ae9e2a
	 * */
Packit ae9e2a
	printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); /* 8 */
Packit ae9e2a
	git_blob_rawcontent(blob); /* "content" */
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Free the blob after usage.
Packit ae9e2a
	 */
Packit ae9e2a
	git_blob_free(blob);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### Revwalking
Packit ae9e2a
 *
Packit ae9e2a
 * The libgit2 [revision walking api][rw] provides methods to traverse the
Packit ae9e2a
 * directed graph created by the parent pointers of the commit objects.
Packit ae9e2a
 * Since all commits point back to the commit that came directly before
Packit ae9e2a
 * them, you can walk this parentage as a graph and find all the commits
Packit ae9e2a
 * that were ancestors of (reachable from) a given starting point.  This
Packit ae9e2a
 * can allow you to create `git log` type functionality.
Packit ae9e2a
 *
Packit ae9e2a
 * [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk
Packit ae9e2a
 */
Packit ae9e2a
static void revwalking(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	const git_signature *cauth;
Packit ae9e2a
	const char *cmsg;
Packit ae9e2a
	int error;
Packit ae9e2a
	git_revwalk *walk;
Packit ae9e2a
	git_commit *wcommit;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Revwalking*\n");
Packit ae9e2a
Packit ae9e2a
	git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * To use the revwalker, create a new walker, tell it how you want to sort
Packit ae9e2a
	 * the output and then push one or more starting points onto the walker.
Packit ae9e2a
	 * If you want to emulate the output of `git log` you would push the SHA
Packit ae9e2a
	 * of the commit that HEAD points to into the walker and then start
Packit ae9e2a
	 * traversing them.  You can also 'hide' commits that you want to stop at
Packit ae9e2a
	 * or not see any of their ancestors.  So if you want to emulate `git log
Packit ae9e2a
	 * branch1..branch2`, you would push the oid of `branch2` and hide the oid
Packit ae9e2a
	 * of `branch1`.
Packit ae9e2a
	 */
Packit ae9e2a
	git_revwalk_new(&walk, repo);
Packit ae9e2a
	git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
Packit ae9e2a
	git_revwalk_push(walk, &oid;;
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Now that we have the starting point pushed onto the walker, we start
Packit ae9e2a
	 * asking for ancestors. It will return them in the sorting order we asked
Packit ae9e2a
	 * for as commit oids.  We can then lookup and parse the committed pointed
Packit ae9e2a
	 * at by the returned OID; note that this operation is specially fast
Packit ae9e2a
	 * since the raw contents of the commit object will be cached in memory
Packit ae9e2a
	 */
Packit ae9e2a
	while ((git_revwalk_next(&oid, walk)) == 0) {
Packit ae9e2a
		error = git_commit_lookup(&wcommit, repo, &oid;;
Packit ae9e2a
		check_error(error, "looking up commit during revwalk");
Packit ae9e2a
Packit ae9e2a
		cmsg  = git_commit_message(wcommit);
Packit ae9e2a
		cauth = git_commit_author(wcommit);
Packit ae9e2a
		printf("%s (%s)\n", cmsg, cauth->email);
Packit ae9e2a
Packit ae9e2a
		git_commit_free(wcommit);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Like the other objects, be sure to free the revwalker when you're done
Packit ae9e2a
	 * to prevent memory leaks.  Also, make sure that the repository being
Packit ae9e2a
	 * walked it not deallocated while the walk is in progress, or it will
Packit ae9e2a
	 * result in undefined behavior
Packit ae9e2a
	 */
Packit ae9e2a
	git_revwalk_free(walk);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### Index File Manipulation *
Packit ae9e2a
 * The [index file API][gi] allows you to read, traverse, update and write
Packit ae9e2a
 * the Git index file (sometimes thought of as the staging area).
Packit ae9e2a
 *
Packit ae9e2a
 * [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index
Packit ae9e2a
 */
Packit ae9e2a
static void index_walking(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	git_index *index;
Packit ae9e2a
	unsigned int i, ecount;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Index Walking*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * You can either open the index from the standard location in an open
Packit ae9e2a
	 * repository, as we're doing here, or you can open and manipulate any
Packit ae9e2a
	 * index file with `git_index_open_bare()`. The index for the repository
Packit ae9e2a
	 * will be located and loaded from disk.
Packit ae9e2a
	 */
Packit ae9e2a
	git_repository_index(&index, repo);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * For each entry in the index, you can get a bunch of information
Packit ae9e2a
	 * including the SHA (oid), path and mode which map to the tree objects
Packit ae9e2a
	 * that are written out.  It also has filesystem properties to help
Packit ae9e2a
	 * determine what to inspect for changes (ctime, mtime, dev, ino, uid,
Packit ae9e2a
	 * gid, file_size and flags) All these properties are exported publicly in
Packit ae9e2a
	 * the `git_index_entry` struct
Packit ae9e2a
	 */
Packit ae9e2a
	ecount = git_index_entrycount(index);
Packit ae9e2a
	for (i = 0; i < ecount; ++i) {
Packit ae9e2a
		const git_index_entry *e = git_index_get_byindex(index, i);
Packit ae9e2a
Packit ae9e2a
		printf("path: %s\n", e->path);
Packit ae9e2a
		printf("mtime: %d\n", (int)e->mtime.seconds);
Packit ae9e2a
		printf("fs: %d\n", (int)e->file_size);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_index_free(index);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### References
Packit ae9e2a
 *
Packit ae9e2a
 * The [reference API][ref] allows you to list, resolve, create and update
Packit ae9e2a
 * references such as branches, tags and remote references (everything in
Packit ae9e2a
 * the .git/refs directory).
Packit ae9e2a
 *
Packit ae9e2a
 * [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference
Packit ae9e2a
 */
Packit ae9e2a
static void reference_listing(git_repository *repo)
Packit ae9e2a
{
Packit ae9e2a
	git_strarray ref_list;
Packit ae9e2a
	unsigned i;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Reference Listing*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Here we will implement something like `git for-each-ref` simply listing
Packit ae9e2a
	 * out all available references and the object SHA they resolve to.
Packit ae9e2a
	 *
Packit ae9e2a
	 * Now that we have the list of reference names, we can lookup each ref
Packit ae9e2a
	 * one at a time and resolve them to the SHA, then print both values out.
Packit ae9e2a
	 */
Packit ae9e2a
Packit ae9e2a
	git_reference_list(&ref_list, repo);
Packit ae9e2a
Packit ae9e2a
	for (i = 0; i < ref_list.count; ++i) {
Packit ae9e2a
		git_reference *ref;
Packit ae9e2a
		char oid_hex[GIT_OID_HEXSZ+1] = GIT_OID_HEX_ZERO;
Packit ae9e2a
		const char *refname;
Packit ae9e2a
Packit ae9e2a
		refname = ref_list.strings[i];
Packit ae9e2a
		git_reference_lookup(&ref, repo, refname);
Packit ae9e2a
Packit ae9e2a
		switch (git_reference_type(ref)) {
Packit ae9e2a
			case GIT_REF_OID:
Packit ae9e2a
				git_oid_fmt(oid_hex, git_reference_target(ref));
Packit ae9e2a
				printf("%s [%s]\n", refname, oid_hex);
Packit ae9e2a
				break;
Packit ae9e2a
Packit ae9e2a
			case GIT_REF_SYMBOLIC:
Packit ae9e2a
				printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
Packit ae9e2a
				break;
Packit ae9e2a
			default:
Packit ae9e2a
				fprintf(stderr, "Unexpected reference type\n");
Packit ae9e2a
				exit(1);
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		git_reference_free(ref);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_strarray_free(&ref_list);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * ### Config Files
Packit ae9e2a
 *
Packit ae9e2a
 * The [config API][config] allows you to list and updatee config values
Packit ae9e2a
 * in any of the accessible config file locations (system, global, local).
Packit ae9e2a
 *
Packit ae9e2a
 * [config]: http://libgit2.github.com/libgit2/#HEAD/group/config
Packit ae9e2a
 */
Packit ae9e2a
static void config_files(const char *repo_path, git_repository* repo)
Packit ae9e2a
{
Packit ae9e2a
	const char *email;
Packit ae9e2a
	char config_path[256];
Packit ae9e2a
	int32_t autocorrect;
Packit ae9e2a
	git_config *cfg;
Packit ae9e2a
	git_config *snap_cfg;
Packit ae9e2a
Packit ae9e2a
	printf("\n*Config Listing*\n");
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Open a config object so we can read global values from it.
Packit ae9e2a
	 */
Packit ae9e2a
	sprintf(config_path, "%s/config", repo_path);
Packit ae9e2a
	check_error(git_config_open_ondisk(&cfg, config_path), "opening config");
Packit ae9e2a
Packit ae9e2a
	if (git_config_get_int32(&autocorrect, cfg, "help.autocorrect") == 0)
Packit ae9e2a
		printf("Autocorrect: %d\n", autocorrect);
Packit ae9e2a
Packit ae9e2a
	check_error(git_repository_config_snapshot(&snap_cfg, repo), "config snapshot");
Packit ae9e2a
	git_config_get_string(&email, snap_cfg, "user.email");
Packit ae9e2a
	printf("Email: %s\n", email);
Packit ae9e2a
Packit ae9e2a
	/**
Packit ae9e2a
	 * Remember to free the configurations after usage.
Packit ae9e2a
	 */
Packit ae9e2a
	git_config_free(cfg);
Packit ae9e2a
	git_config_free(snap_cfg);
Packit ae9e2a
}