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  * <https://creativecommons.org/publicdomain/zero/1.0/>.
13  */
14 /**
15  * License: $(LINK2 https://creativecommons.org/publicdomain/zero/1.0/, CC0 1.0 Universal)
16  */
17 module libgit2.example.show_index;
18 
19 
20 private static import core.stdc.stdio;
21 private static import core.stdc.string;
22 private static import libgit2.example.common;
23 private static import libgit2.index;
24 private static import libgit2.oid;
25 private static import libgit2.repository;
26 private static import libgit2.types;
27 
28 extern (C)
29 nothrow @nogc
30 public int lg2_show_index(libgit2.types.git_repository* repo, int argc, char** argv)
31 
32 	in
33 	{
34 	}
35 
36 	do
37 	{
38 		char[libgit2.oid.GIT_OID_SHA1_HEXSIZE + 1] out_ = '\0';
39 
40 		if (argc > 2) {
41 			libgit2.example.common.fatal("usage: showindex [<repo-dir>]", null);
42 		}
43 
44 		const char* dir = (argc > 1) ? (argv[1]) : (".");
45 		size_t dirlen = core.stdc..string.strlen(dir);
46 
47 		libgit2.types.git_index* index;
48 
49 		if ((dirlen > 5) && (core.stdc..string.strcmp(dir + dirlen - 5, "index") == 0)) {
50 			libgit2.example.common.check_lg2(libgit2.index.git_index_open(&index, dir), "could not open index", dir);
51 		} else {
52 			libgit2.example.common.check_lg2(libgit2.repository.git_repository_open_ext(&repo, dir, 0, null), "could not open repository", dir);
53 			libgit2.example.common.check_lg2(libgit2.repository.git_repository_index(&index, repo), "could not open repository index", null);
54 			libgit2.repository.git_repository_free(repo);
55 		}
56 
57 		libgit2.index.git_index_read(index, 0);
58 
59 		size_t ecount = libgit2.index.git_index_entrycount(index);
60 
61 		if (!ecount) {
62 			core.stdc.stdio.printf("Empty index\n");
63 		}
64 	
65 		for (size_t i = 0; i < ecount; ++i) {
66 			const (libgit2.index.git_index_entry)* e = libgit2.index.git_index_get_byindex(index, i);
67 
68 			libgit2.oid.git_oid_fmt(&(out_[0]), &e.id);
69 
70 			core.stdc.stdio.printf("File Path: %s\n", e.path);
71 			core.stdc.stdio.printf("    Stage: %d\n", libgit2.index.git_index_entry_stage(e));
72 			core.stdc.stdio.printf(" Blob SHA: %s\n", &(out_[0]));
73 			core.stdc.stdio.printf("File Mode: %07o\n", e.mode);
74 			core.stdc.stdio.printf("File Size: %d bytes\n", cast(int)(e.file_size));
75 			core.stdc.stdio.printf("Dev/Inode: %d/%d\n", cast(int)(e.dev), cast(int)(e.ino));
76 			core.stdc.stdio.printf("  UID/GID: %d/%d\n", cast(int)(e.uid), cast(int)(e.gid));
77 			core.stdc.stdio.printf("    ctime: %d\n", cast(int)(e.ctime.seconds));
78 			core.stdc.stdio.printf("    mtime: %d\n", cast(int)(e.mtime.seconds));
79 			core.stdc.stdio.printf("\n");
80 		}
81 
82 		libgit2.index.git_index_free(index);
83 
84 		return 0;
85 	}