1 module index_pack;
2 
3 import git.c;
4 
5 import std.stdio;
6 import std.exception;
7 
8 // This could be run in the main loop while the application waits for
9 // the indexing to finish in a worker thread
10 int index_cb(const git_indexer_stats* stats, void* data)
11 {
12     writefln("\rProcessing %s of %s", stats.processed, stats.total);
13     return 0;
14 }
15 
16 int run_index_pack(git_repository* repo, int argc, string[] argv)
17 {
18     git_indexer_stream* idx;
19     git_indexer_stats stats;
20     int error;
21     int fd;
22     char[GIT_OID_HEXSZ + 1] hash = '\0';
23     byte[512] buf;
24 
25     if (argc < 2)
26     {
27         writeln("I need a packfile\n");
28         return -1;
29     }
30 
31     if (git_indexer_stream_new(&idx, ".git") < 0)
32     {
33         writeln("bad idx");
34         return -1;
35     }
36 
37     scope(exit)
38         git_indexer_stream_free(idx);
39 
40     File file = File(argv[1], "r");
41 
42     while (1)
43     {
44         byte[] read_bytes = file.rawRead(buf);
45 
46         enforce(git_indexer_stream_add(idx, read_bytes.ptr, read_bytes.length, &stats) >= 0);
47         printf("\rIndexing %d of %d", stats.processed, stats.total);
48 
49         if (read_bytes.length < buf.length)
50             break;
51     }
52 
53     enforce(git_indexer_stream_finalize(idx, &stats) >= 0);
54 
55     writefln("\nIndexing %d of %d", stats.processed, stats.total);
56 
57     git_oid_fmt(hash.ptr, git_indexer_stream_hash(idx));
58     writefln("Hash: %s\n", hash);
59     return 0;
60 }
61 
62 int index_pack_old(git_repository* repo, int argc, char** argv)
63 {
64     git_indexer* indexer;
65     git_indexer_stats stats;
66     int  error;
67     char[GIT_OID_HEXSZ + 1] hash;
68 
69     if (argc < 2)
70     {
71         writeln("I need a packfile\n");
72         return -1;
73     }
74 
75     // Create a new indexer
76     error = git_indexer_new(&indexer, argv[1]);
77 
78     if (error < 0)
79         return error;
80 
81     // Index the packfile. This function can take a very long time and
82     // should be run in a worker thread.
83     error = git_indexer_run(indexer, &stats);
84 
85     if (error < 0)
86         return error;
87 
88     // Write the information out to an index file
89     error = git_indexer_write(indexer);
90 
91     // Get the packfile's hash (which should become it's filename)
92     git_oid_fmt(hash.ptr, git_indexer_hash(indexer));
93     writeln(hash);
94 
95     git_indexer_free(indexer);
96 
97     return 0;
98 }