summaryrefslogtreecommitdiff
path: root/examples/showindex.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/showindex.c')
-rw-r--r--examples/showindex.c49
1 files changed, 26 insertions, 23 deletions
diff --git a/examples/showindex.c b/examples/showindex.c
index e92a9c8de..7f7c38379 100644
--- a/examples/showindex.c
+++ b/examples/showindex.c
@@ -1,10 +1,21 @@
-#include <git2.h>
-#include <stdio.h>
-#include <string.h>
+/*
+ * libgit2 "showindex" example - shows how to extract data from the index
+ *
+ * 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/>.
+ */
+
+#include "common.h"
int main (int argc, char** argv)
{
- git_repository *repo = NULL;
git_index *index;
unsigned int i, ecount;
char *dir = ".";
@@ -12,31 +23,24 @@ int main (int argc, char** argv)
char out[41];
out[40] = '\0';
+ git_threads_init();
+
+ if (argc > 2)
+ fatal("usage: showindex [<repo-dir>]", NULL);
if (argc > 1)
dir = argv[1];
- if (!dir || argc > 2) {
- fprintf(stderr, "usage: showindex [<repo-dir>]\n");
- return 1;
- }
dirlen = strlen(dir);
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
- if (git_index_open(&index, dir) < 0) {
- fprintf(stderr, "could not open index: %s\n", dir);
- return 1;
- }
+ check_lg2(git_index_open(&index, dir), "could not open index", dir);
} else {
- if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) {
- fprintf(stderr, "could not open repository: %s\n", dir);
- return 1;
- }
- if (git_repository_index(&index, repo) < 0) {
- fprintf(stderr, "could not open repository index\n");
- return 1;
- }
+ git_repository *repo;
+ check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
+ check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
+ git_repository_free(repo);
}
- git_index_read(index);
+ git_index_read(index, 0);
ecount = git_index_entrycount(index);
if (!ecount)
@@ -60,8 +64,7 @@ int main (int argc, char** argv)
}
git_index_free(index);
- git_repository_free(repo);
+ git_threads_shutdown();
return 0;
}
-