1 module libgit2_d.example.index_pack;
2 
3 
4 private static import core.stdc.stdio;
5 private static import core.stdc.stdlib;
6 private static import libgit2_d.example.common;
7 private static import libgit2_d.indexer;
8 private static import libgit2_d.oid;
9 private static import libgit2_d.types;
10 
11 package:
12 
13 /*
14  * This could be run in the main loop whilst the application waits for
15  * the indexing to finish in a worker thread
16  */
17 nothrow @nogc
18 private int index_cb(const (libgit2_d.indexer.git_indexer_progress)* stats, void* data)
19 
20 	in
21 	{
22 	}
23 
24 	do
25 	{
26 		//cast(void)(data);
27 		core.stdc.stdio.printf("\rProcessing %u of %u", stats.indexed_objects, stats.total_objects);
28 
29 		return 0;
30 	}
31 
32 extern (C)
33 nothrow @nogc
34 public int lg2_index_pack(libgit2_d.types.git_repository* repo, int argc, char** argv)
35 
36 	in
37 	{
38 	}
39 
40 	do
41 	{
42 		//cast(void)(repo);
43 
44 		if (argc < 2) {
45 			core.stdc.stdio.fprintf(core.stdc.stdio.stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
46 
47 			return core.stdc.stdlib.EXIT_FAILURE;
48 		}
49 
50 		libgit2_d.indexer.git_indexer* idx = null;
51 
52 		if (libgit2_d.indexer.git_indexer_new(&idx, ".", 0, null, null) < 0) {
53 			core.stdc.stdio.puts("bad idx");
54 
55 			return -1;
56 		}
57 
58 		int fd = libgit2_d.example.common.open(argv[1], 0);
59 
60 		if (fd < 0) {
61 			core.stdc.stdio.perror("open");
62 
63 			return -1;
64 		}
65 
66 		libgit2_d.indexer.git_indexer_progress stats = {0, 0};
67 		int error;
68 		libgit2_d.example.common.ssize_t read_bytes;
69 		char[512] buf;
70 
71 		scope (exit) {
72 			libgit2_d.example.common.close(fd);
73 			libgit2_d.indexer.git_indexer_free(idx);
74 		}
75 
76 		do {
77 			read_bytes = libgit2_d.example.common.read(fd, &(buf[0]), buf.length);
78 
79 			if (read_bytes < 0) {
80 				break;
81 			}
82 
83 			error = libgit2_d.indexer.git_indexer_append(idx, &(buf[0]), read_bytes, &stats);
84 
85 			if (error < 0) {
86 				return error;
87 			}
88 
89 			.index_cb(&stats, null);
90 		} while (read_bytes > 0);
91 
92 		if (read_bytes < 0) {
93 			error = -1;
94 			core.stdc.stdio.perror("failed reading");
95 
96 			return error;
97 		}
98 
99 		error = libgit2_d.indexer.git_indexer_commit(idx, &stats);
100 
101 		if (error < 0) {
102 			return error;
103 		}
104 
105 		core.stdc.stdio.printf("\rIndexing %u of %u\n", stats.indexed_objects, stats.total_objects);
106 
107 		char[libgit2_d.oid.GIT_OID_HEXSZ + 1] hash  = '\0';
108 		libgit2_d.oid.git_oid_fmt(&(hash[0]), libgit2_d.indexer.git_indexer_hash(idx));
109 		core.stdc.stdio.puts(&(hash[0]));
110 
111 		return error;
112 	}