examples: general: extract function demonstrating reference listings

This commit is contained in:
Patrick Steinhardt 2016-08-16 09:29:14 +02:00
parent 986913f45b
commit f9a7973dd9

View File

@ -41,7 +41,9 @@
// to compile properly and get all the libgit2 API. // to compile properly and get all the libgit2 API.
#include <git2.h> #include <git2.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
static void reference_listing(git_repository *repo);
static void config_files(const char *repo_path); static void config_files(const char *repo_path);
// Almost all libgit2 functions return 0 on success or negative on error. // Almost all libgit2 functions return 0 on success or negative on error.
@ -459,34 +461,53 @@ int main (int argc, char** argv)
git_index_free(index); git_index_free(index);
// ### References reference_listing(repo);
config_files(repo_path);
// The [reference API][ref] allows you to list, resolve, create and update // Finally, when you're done with the repository, you can free it as well.
// references such as branches, tags and remote references (everything in git_repository_free(repo);
// the .git/refs directory).
// return 0;
// [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference }
/**
* ### References
*
* The [reference API][ref] allows you to list, resolve, create and update
* references such as branches, tags and remote references (everything in
* the .git/refs directory).
*
* [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference
*/
static void reference_listing(git_repository *repo)
{
git_strarray ref_list;
const char *refname;
git_reference *ref;
unsigned i;
char oid_hex[GIT_OID_HEXSZ+1];
printf("\n*Reference Listing*\n"); printf("\n*Reference Listing*\n");
// Here we will implement something like `git for-each-ref` simply listing /**
// out all available references and the object SHA they resolve to. * Here we will implement something like `git for-each-ref` simply listing
git_strarray ref_list; * out all available references and the object SHA they resolve to.
*
* 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.
*/
git_reference_list(&ref_list, repo); git_reference_list(&ref_list, repo);
const char *refname;
git_reference *ref;
// 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.
for (i = 0; i < ref_list.count; ++i) { for (i = 0; i < ref_list.count; ++i) {
memset(oid_hex, 0, sizeof(oid_hex));
refname = ref_list.strings[i]; refname = ref_list.strings[i];
git_reference_lookup(&ref, repo, refname); git_reference_lookup(&ref, repo, refname);
switch (git_reference_type(ref)) { switch (git_reference_type(ref)) {
case GIT_REF_OID: case GIT_REF_OID:
git_oid_fmt(out, git_reference_target(ref)); git_oid_fmt(oid_hex, git_reference_target(ref));
printf("%s [%s]\n", refname, out); printf("%s [%s]\n", refname, oid_hex);
break; break;
case GIT_REF_SYMBOLIC: case GIT_REF_SYMBOLIC:
@ -499,13 +520,6 @@ int main (int argc, char** argv)
} }
git_strarray_free(&ref_list); git_strarray_free(&ref_list);
config_files(repo_path);
// Finally, when you're done with the repository, you can free it as well.
git_repository_free(repo);
return 0;
} }
/** /**