summaryrefslogtreecommitdiff
path: root/examples/show-index.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-01-24 11:31:49 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 12:06:54 +0100
commitead10785dcd9e7599a52a0349a69c113c76e650d (patch)
tree14c30c430befd05eb160108fe49a14d72faeb3d7 /examples/show-index.c
parent7562422ab9310b81142986f69964194f491948cc (diff)
downloadlibgit2-ead10785dcd9e7599a52a0349a69c113c76e650d.tar.gz
examples: create common lg2 executable
Inside of our networking example code, we have a git2 executable that acts as an entry point to all the different network examples. As such, it is kind of the same like the normal git(1) executable in that it simply arbitrates to the respective subcommands. Let's extend this approach and merge all examples into a single standalone lg2 executable. Instead of building an executable for all the existing examples we have, we now bundle them all inside of the lg2 one and let them be callable via subcommands. In the process, we can get rid of duplicated library initialization, deinitialization and repository discovery code. Instead of having each subcommand handle these on its own, we simply do it inside of the single main function now.
Diffstat (limited to 'examples/show-index.c')
-rw-r--r--examples/show-index.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/show-index.c b/examples/show-index.c
new file mode 100644
index 000000000..6acadbeb6
--- /dev/null
+++ b/examples/show-index.c
@@ -0,0 +1,66 @@
+/*
+ * 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 lg2_show_index(git_repository *repo, int argc, char** argv)
+{
+ git_index *index;
+ unsigned int i, ecount;
+ char *dir = ".";
+ size_t dirlen;
+ char out[GIT_OID_HEXSZ+1];
+ out[GIT_OID_HEXSZ] = '\0';
+
+ if (argc > 2)
+ fatal("usage: showindex [<repo-dir>]", NULL);
+ if (argc > 1)
+ dir = argv[1];
+
+ dirlen = strlen(dir);
+ if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
+ check_lg2(git_index_open(&index, dir), "could not open index", dir);
+ } else {
+ 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, 0);
+
+ ecount = git_index_entrycount(index);
+ if (!ecount)
+ printf("Empty index\n");
+
+ for (i = 0; i < ecount; ++i) {
+ const git_index_entry *e = git_index_get_byindex(index, i);
+
+ git_oid_fmt(out, &e->id);
+
+ printf("File Path: %s\n", e->path);
+ printf(" Stage: %d\n", git_index_entry_stage(e));
+ printf(" Blob SHA: %s\n", out);
+ printf("File Mode: %07o\n", e->mode);
+ printf("File Size: %d bytes\n", (int)e->file_size);
+ printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
+ printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
+ printf(" ctime: %d\n", (int)e->ctime.seconds);
+ printf(" mtime: %d\n", (int)e->mtime.seconds);
+ printf("\n");
+ }
+
+ git_index_free(index);
+
+ return 0;
+}