<commit_before>/*
 * libgit2 "general" example - shows basic libgit2 concepts
 *
 * Written by the libgit2 contributors
 *
 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

// [**libgit2**][lg] 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.
//
// 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.
//
// If you're trying to write something in C using [libgit2][lg], you should
// also check out the generated [API documentation][ap]. We try to link to
// the relevant sections of the API docs in each section in this file.
//
// **libgit2** (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 [Chapter 10][pg] of the Pro Git book.
//
// [lg]: http://libgit2.github.com
// [ap]: http://libgit2.github.com/libgit2
// [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain

// ### Includes

// Including the `git2.h` 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.
#include <git2.h>
#include <stdio.h>
#include <string.h>

static void oid_parsing(git_oid *out);
static void object_database(git_repository *repo, git_oid *oid);
static void commit_writing(git_repository *repo);
static void commit_parsing(git_repository *repo);
static void tag_parsing(git_repository *repo);
static void tree_parsing(git_repository *repo);
static void blob_parsing(git_repository *repo);
static void revwalking(git_repository *repo);
static void index_walking(git_repository *repo);
static void reference_listing(git_repository *repo);
static void config_files(const char *repo_path);

// 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.
static void check_error(int error_code, const char *action)
{
	const git_error *error = giterr_last();
	if (!error_code)
		return;

	printf("Error %d %s - %s\n", error_code, action,
			(error && error->message) ? error->message : "???");

	exit(1);
}

int main (int argc, char** argv)
{
	git_oid oid;

	// Initialize the library, this will set up any global state which libgit2 needs
	// including threading and crypto
	git_libgit2_init();

	// ### Opening the Repository

	// There are a couple of methods for opening a repository, this being the
	// simplest.  There are also [methods][me] for specifying the index file
	// and work tree locations, here we assume they are in the normal places.
	//
	// (Try running this program against tests/resources/testrepo.git.)
	//
	// [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
	int error;
	const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
	git_repository *repo;

	error = git_repository_open(&repo, repo_path);
	check_error(error, "opening repository");

	oid_parsing(&oid);
	object_database(repo, &oid);
	commit_writing(repo);
	commit_parsing(repo);
	tag_parsing(repo);
	tree_parsing(repo);
	blob_parsing(repo);
	revwalking(repo);
	index_walking(repo);
	reference_listing(repo);
	config_files(repo_path);

	// Finally, when you're done with the repository, you can free it as well.
	git_repository_free(repo);

	return 0;
}

/**
 * ### SHA-1 Value Conversions
 */
static void oid_parsing(git_oid *oid)
{
	char out[GIT_OID_HEXSZ+1];
	char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";

	printf("*Hex to Raw*\n");

	/**
	 * For our first example, we will convert a 40 character hex value to the
	 * 20 byte raw SHA1 value.
	 *
	 * The `git_oid` 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.
	 */
	git_oid_fromstr(oid, hex);<commit_msg>examples: general: convert C99 comments to C90 comments<commit_after>/*
 * libgit2 "general" example - shows basic libgit2 concepts
 *
 * Written by the libgit2 contributors
 *
 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

/**
 * [**libgit2**][lg] 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.
 *
 * 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.
 *
 * If you're trying to write something in C using [libgit2][lg], you should
 * also check out the generated [API documentation][ap]. We try to link to
 * the relevant sections of the API docs in each section in this file.
 *
 * **libgit2** (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 [Chapter 10][pg] of the Pro Git book.
 *
 * [lg]: http://libgit2.github.com
 * [ap]: http://libgit2.github.com/libgit2
 * [pg]: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain
 */

/**
 * ### Includes
 *
 * Including the `git2.h` 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.
 */
#include <git2.h>
#include <stdio.h>
#include <string.h>

static void oid_parsing(git_oid *out);
static void object_database(git_repository *repo, git_oid *oid);
static void commit_writing(git_repository *repo);
static void commit_parsing(git_repository *repo);
static void tag_parsing(git_repository *repo);
static void tree_parsing(git_repository *repo);
static void blob_parsing(git_repository *repo);
static void revwalking(git_repository *repo);
static void index_walking(git_repository *repo);
static void reference_listing(git_repository *repo);
static void config_files(const char *repo_path);

/**
 * 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.
 */
static void check_error(int error_code, const char *action)
{
	const git_error *error = giterr_last();
	if (!error_code)
		return;

	printf("Error %d %s - %s\n", error_code, action,
			(error && error->message) ? error->message : "???");

	exit(1);
}

int main (int argc, char** argv)
{
	git_oid oid;

	/**
	 * Initialize the library, this will set up any global state which libgit2 needs
	 * including threading and crypto
	 */
	git_libgit2_init();

	/**
	 * ### Opening the Repository
	 *
	 * There are a couple of methods for opening a repository, this being the
	 * simplest.  There are also [methods][me] for specifying the index file
	 * and work tree locations, here we assume they are in the normal places.
	 *
	 * (Try running this program against tests/resources/testrepo.git.)
	 *
	 * [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository
	 */
	int error;
	const char *repo_path = (argc > 1) ? argv[1] : "/opt/libgit2-test/.git";
	git_repository *repo;

	error = git_repository_open(&repo, repo_path);
	check_error(error, "opening repository");

	oid_parsing(&oid);
	object_database(repo, &oid);
	commit_writing(repo);
	commit_parsing(repo);
	tag_parsing(repo);
	tree_parsing(repo);
	blob_parsing(repo);
	revwalking(repo);
	index_walking(repo);
	reference_listing(repo);
	config_files(repo_path);

	/**
	 * Finally, when you're done with the repository, you can free it as well.
	 */
	git_repository_free(repo);

	return 0;
}

/**
 * ### SHA-1 Value Conversions
 */
static void oid_parsing(git_oid *oid)
{
	char out[GIT_OID_HEXSZ+1];
	char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";

	printf("*Hex to Raw*\n");

	/**
	 * For our first example, we will convert a 40 character hex value to the
	 * 20 byte raw SHA1 value.
	 *
	 * The `git_oid` 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.
	 */
	git_oid_fromstr(oid, hex);
{'fraction_non_alphanumeric': 0.10422350758485212, 'fraction_numerical': 0.01244134017243261, 'mean_word_length': 4.1715575620767495, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3579180', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2768, 'n_words': 1094}