]> git.proxmox.com Git - libgit2.git/blame - examples/showindex.c
New upstream version 0.28.4+dfsg.1
[libgit2.git] / examples / showindex.c
CommitLineData
dbdb22b3 1/*
6cb831bd 2 * libgit2 "showindex" example - shows how to extract data from the index
dbdb22b3 3 *
6cb831bd
BS
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/>.
dbdb22b3
BS
13 */
14
15#include "common.h"
388f37b3
SC
16
17int main (int argc, char** argv)
18{
b90500f0
RB
19 git_index *index;
20 unsigned int i, ecount;
21 char *dir = ".";
dbc4aa07 22 size_t dirlen;
3b2cb2c9
CS
23 char out[GIT_OID_HEXSZ+1];
24 out[GIT_OID_HEXSZ] = '\0';
b90500f0 25
799e22ea 26 git_libgit2_init();
37f9e409 27
dbdb22b3
BS
28 if (argc > 2)
29 fatal("usage: showindex [<repo-dir>]", NULL);
b90500f0
RB
30 if (argc > 1)
31 dir = argv[1];
b90500f0 32
dbc4aa07
RB
33 dirlen = strlen(dir);
34 if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
dbdb22b3 35 check_lg2(git_index_open(&index, dir), "could not open index", dir);
dbc4aa07 36 } else {
dbdb22b3
BS
37 git_repository *repo;
38 check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
39 check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
40 git_repository_free(repo);
b90500f0
RB
41 }
42
4bf630b6 43 git_index_read(index, 0);
b90500f0
RB
44
45 ecount = git_index_entrycount(index);
46 if (!ecount)
47 printf("Empty index\n");
48
49 for (i = 0; i < ecount; ++i) {
50 const git_index_entry *e = git_index_get_byindex(index, i);
51
d541170c 52 git_oid_fmt(out, &e->id);
b90500f0
RB
53
54 printf("File Path: %s\n", e->path);
55 printf(" Stage: %d\n", git_index_entry_stage(e));
56 printf(" Blob SHA: %s\n", out);
dbc4aa07
RB
57 printf("File Mode: %07o\n", e->mode);
58 printf("File Size: %d bytes\n", (int)e->file_size);
59 printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
60 printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
b90500f0
RB
61 printf(" ctime: %d\n", (int)e->ctime.seconds);
62 printf(" mtime: %d\n", (int)e->mtime.seconds);
63 printf("\n");
64 }
65
66 git_index_free(index);
799e22ea 67 git_libgit2_shutdown();
37f9e409 68
b90500f0 69 return 0;
388f37b3 70}