]>
Commit | Line | Data |
---|---|---|
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 | 16 | |
0c9c969a | 17 | int lg2_show_index(git_repository *repo, int argc, char **argv) |
388f37b3 | 18 | { |
b90500f0 | 19 | git_index *index; |
0c9c969a | 20 | size_t i, ecount; |
b90500f0 | 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 | |
dbdb22b3 BS |
26 | if (argc > 2) |
27 | fatal("usage: showindex [<repo-dir>]", NULL); | |
b90500f0 RB |
28 | if (argc > 1) |
29 | dir = argv[1]; | |
b90500f0 | 30 | |
dbc4aa07 RB |
31 | dirlen = strlen(dir); |
32 | if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) { | |
dbdb22b3 | 33 | check_lg2(git_index_open(&index, dir), "could not open index", dir); |
dbc4aa07 | 34 | } else { |
dbdb22b3 BS |
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); | |
b90500f0 RB |
38 | } |
39 | ||
4bf630b6 | 40 | git_index_read(index, 0); |
b90500f0 RB |
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 | ||
d541170c | 49 | git_oid_fmt(out, &e->id); |
b90500f0 RB |
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); | |
dbc4aa07 RB |
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); | |
b90500f0 RB |
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); | |
37f9e409 | 64 | |
b90500f0 | 65 | return 0; |
388f37b3 | 66 | } |