#include <git2.h>
#include <stdio.h>
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_libgit2_init();
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");
printf("*Hex to Raw*\n");
char hex[] = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045";
git_oid oid;
git_oid_fromstr(&oid, hex);
printf("\n*Raw to Hex*\n");
char out[GIT_OID_HEXSZ+1];
out[GIT_OID_HEXSZ] = '\0';
git_oid_fmt(out, &oid);
printf("SHA hex string: %s\n", out);
git_odb *odb;
git_repository_odb(&odb, repo);
printf("\n*Raw Object Read*\n");
git_odb_object *obj;
git_otype otype;
const unsigned char *data;
const char *str_type;
error = git_odb_read(&obj, odb, &oid);
check_error(error, "finding object in repository");
data = (const unsigned char *)git_odb_object_data(obj);
otype = git_odb_object_type(obj);
str_type = git_object_type2string(otype);
printf("object length and type: %d, %s\n",
(int)git_odb_object_size(obj),
str_type);
git_odb_object_free(obj);
printf("\n*Raw Object Write*\n");
git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB);
git_oid_fmt(out, &oid);
printf("Written Object: %s\n", out);
printf("\n*Commit Parsing*\n");
git_commit *commit;
git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479");
error = git_commit_lookup(&commit, repo, &oid);
check_error(error, "looking up commit");
const git_signature *author, *cmtter;
const char *message;
time_t ctime;
unsigned int parents, p;
message = git_commit_message(commit);
author = git_commit_author(commit);
cmtter = git_commit_committer(commit);
ctime = git_commit_time(commit);
printf("Author: %s (%s)\n", author->name, author->email);
parents = git_commit_parentcount(commit);
for (p = 0;p < parents;p++) {
git_commit *parent;
git_commit_parent(&parent, commit, p);
git_oid_fmt(out, git_commit_id(parent));
printf("Parent: %s\n", out);
git_commit_free(parent);
}
git_commit_free(commit);
printf("\n*Commit Writing*\n");
git_oid tree_id, parent_id, commit_id;
git_tree *tree;
git_commit *parent;
git_signature_new((git_signature **)&author,
"Scott Chacon", "schacon@gmail.com", 123456789, 60);
git_signature_new((git_signature **)&cmtter,
"Scott A Chacon", "scott@github.com", 987654321, 90);
git_oid_fromstr(&tree_id, "f60079018b664e4e79329a7ef9559c8d9e0378d1");
git_tree_lookup(&tree, repo, &tree_id);
git_oid_fromstr(&parent_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
git_commit_lookup(&parent, repo, &parent_id);
git_commit_create_v(
&commit_id,
repo,
NULL,
author,
cmtter,
NULL,
"example commit",
tree,
1, parent);
git_oid_fmt(out, &commit_id);
printf("New Commit: %s\n", out);
printf("\n*Tag Parsing*\n");
git_tag *tag;
const char *tmessage, *tname;
git_otype ttype;
git_oid_fromstr(&oid, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
error = git_tag_lookup(&tag, repo, &oid);
check_error(error, "looking up tag");
git_tag_target((git_object **)&commit, tag);
tname = git_tag_name(tag); ttype = git_tag_target_type(tag); tmessage = git_tag_message(tag); printf("Tag Message: %s\n", tmessage);
git_commit_free(commit);
printf("\n*Tree Parsing*\n");
const git_tree_entry *entry;
git_object *objt;
git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5");
git_tree_lookup(&tree, repo, &oid);
size_t cnt = git_tree_entrycount(tree); printf("tree entries: %d\n", (int)cnt);
entry = git_tree_entry_byindex(tree, 0);
printf("Entry name: %s\n", git_tree_entry_name(entry));
entry = git_tree_entry_byname(tree, "README");
git_tree_entry_name(entry);
git_tree_entry_to_object(&objt, repo, entry);
git_object_free(objt);
printf("\n*Blob Parsing*\n");
git_blob *blob;
git_oid_fromstr(&oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
git_blob_lookup(&blob, repo, &oid);
printf("Blob Size: %ld\n", (long)git_blob_rawsize(blob)); git_blob_rawcontent(blob);
printf("\n*Revwalking*\n");
git_revwalk *walk;
git_commit *wcommit;
git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
git_revwalk_new(&walk, repo);
git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE);
git_revwalk_push(walk, &oid);
const git_signature *cauth;
const char *cmsg;
while ((git_revwalk_next(&oid, walk)) == 0) {
error = git_commit_lookup(&wcommit, repo, &oid);
check_error(error, "looking up commit during revwalk");
cmsg = git_commit_message(wcommit);
cauth = git_commit_author(wcommit);
printf("%s (%s)\n", cmsg, cauth->email);
git_commit_free(wcommit);
}
git_revwalk_free(walk);
printf("\n*Index Walking*\n");
git_index *index;
unsigned int i, ecount;
git_repository_index(&index, repo);
ecount = git_index_entrycount(index);
for (i = 0; i < ecount; ++i) {
const git_index_entry *e = git_index_get_byindex(index, i);
printf("path: %s\n", e->path);
printf("mtime: %d\n", (int)e->mtime.seconds);
printf("fs: %d\n", (int)e->file_size);
}
git_index_free(index);
printf("\n*Reference Listing*\n");
git_strarray ref_list;
git_reference_list(&ref_list, repo);
const char *refname;
git_reference *ref;
for (i = 0; i < ref_list.count; ++i) {
refname = ref_list.strings[i];
git_reference_lookup(&ref, repo, refname);
switch (git_reference_type(ref)) {
case GIT_REF_OID:
git_oid_fmt(out, git_reference_target(ref));
printf("%s [%s]\n", refname, out);
break;
case GIT_REF_SYMBOLIC:
printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
break;
default:
fprintf(stderr, "Unexpected reference type\n");
exit(1);
}
}
git_strarray_free(&ref_list);
printf("\n*Config Listing*\n");
const char *email;
int32_t j;
git_config *cfg;
char config_path[256];
sprintf(config_path, "%s/config", repo_path);
check_error(git_config_open_ondisk(&cfg, config_path), "opening config");
git_config_get_int32(&j, cfg, "help.autocorrect");
printf("Autocorrect: %d\n", j);
git_config_get_string(&email, cfg, "user.email");
printf("Email: %s\n", email);
git_repository_free(repo);
return 0;
}