]> git.proxmox.com Git - libgit2.git/blob - examples/show-index.c
Library transition to 1.0.0
[libgit2.git] / examples / show-index.c
1 /*
2 * libgit2 "showindex" example - shows how to extract data from the index
3 *
4 * Written by the libgit2 contributors
5 *
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 */
14
15 #include "common.h"
16
17 int lg2_show_index(git_repository *repo, int argc, char **argv)
18 {
19 git_index *index;
20 size_t i, ecount;
21 char *dir = ".";
22 size_t dirlen;
23 char out[GIT_OID_HEXSZ+1];
24 out[GIT_OID_HEXSZ] = '\0';
25
26 if (argc > 2)
27 fatal("usage: showindex [<repo-dir>]", NULL);
28 if (argc > 1)
29 dir = argv[1];
30
31 dirlen = strlen(dir);
32 if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
33 check_lg2(git_index_open(&index, dir), "could not open index", dir);
34 } else {
35 check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
36 check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
37 git_repository_free(repo);
38 }
39
40 git_index_read(index, 0);
41
42 ecount = git_index_entrycount(index);
43 if (!ecount)
44 printf("Empty index\n");
45
46 for (i = 0; i < ecount; ++i) {
47 const git_index_entry *e = git_index_get_byindex(index, i);
48
49 git_oid_fmt(out, &e->id);
50
51 printf("File Path: %s\n", e->path);
52 printf(" Stage: %d\n", git_index_entry_stage(e));
53 printf(" Blob SHA: %s\n", out);
54 printf("File Mode: %07o\n", e->mode);
55 printf("File Size: %d bytes\n", (int)e->file_size);
56 printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
57 printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
58 printf(" ctime: %d\n", (int)e->ctime.seconds);
59 printf(" mtime: %d\n", (int)e->mtime.seconds);
60 printf("\n");
61 }
62
63 git_index_free(index);
64
65 return 0;
66 }