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