Blame examples/general.c

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