1 module main;
2 
3 import git.c;
4 
5 import std.zlib;
6 import core.thread;
7 import std.array;
8 import std.algorithm;
9 import std.concurrency;
10 import std.conv;
11 import std.file;
12 import std.stdio;
13 import std.string;
14 import std.range;
15 import std.exception;
16 import std.path;
17 
18 import common;
19 import fetch;
20 import clone;
21 import ls_remote;
22 import index_pack;
23 
24 struct Command
25 {
26 	string name;
27 	git_cb fn;
28 }
29 
30 Command[] commands =
31 [
32 	{"ls-remote", &run_ls_remote},
33 	{"fetch", &run_fetch},
34 	{"clone", &do_clone},
35 	{"index-pack", &run_index_pack},
36 	{ null, null}
37 ];
38 
39 int run_command(git_cb fn, int argc, string[] args)
40 {
41 	int error;
42 	git_repository *repo;
43 
44     // Before running the actual command, create an instance of the local
45     // repository and pass it to the function.
46     error = git_repository_open(&repo, ".git");
47 	if (error < 0)
48 		repo = null;
49 
50 	// Run the command. If something goes wrong, print the error message to stderr
51 	error = fn(repo, argc, args);
52 	if (error < 0)
53     {
54 		if (giterr_last() == null)
55 			writeln("Error without message");
56 		else
57 			printf("Bad news:\n %s\n", giterr_last().message);
58 	}
59 
60 	if (repo)
61 		git_repository_free(repo);
62 
63 	return !!error;
64 }
65 
66 int main(string[] args)
67 {
68 	int i;
69 
70 	if (args.length < 2)
71     {
72 		writefln("usage: %s <cmd> [repo]\n", args[0]);
73 		return 1;
74 	}
75 
76 	for (i = 0; commands[i].name != null; ++i)
77     {
78 		if (args[1] == commands[i].name)
79         {
80             args.popFront();
81 			return run_command(commands[i].fn, args.length, args);
82         }
83 	}
84 
85 	writeln("Command not found: %s\n", args[1]);
86 	return 0;
87 }
88 
89 /+
90 enum Search : SpanMode
91 {
92      deep = SpanMode.depth
93     ,wide = SpanMode.breadth
94     ,flat = SpanMode.shallow
95 }
96 
97 string[] fileList(string root, string ext, Search search = Search.flat)
98 {
99     string[] result;
100 
101     if (!exists(root))
102     {
103         writefln("Warning: %s dir not found.", root);
104         return null;
105     }
106 
107     foreach (string entry; dirEntries(root, cast(SpanMode)search))
108     {
109         if (entry.isFile && entry.extension == ext)
110             result ~= entry;
111     }
112 
113     return result;
114 }
115 
116 void main(string[] args)
117 {
118     if (args.length < 2)
119     {
120         writeln("Error: Pass the path to a git objects directory.");
121         return;
122     }
123 
124     foreach (file; fileList(args[1], "", Search.deep))
125     {
126         byte[] bytes = cast(byte[])std.file.read(file);
127         char[] text = cast(char[])uncompress(bytes);
128 
129         if (text.startsWith("commit"))
130         {
131             sizediff_t nulByte = text.countUntil(0);
132             if (nulByte != -1)
133             {
134                 writefln("-- %s --\n%s", text[0 .. nulByte], text[nulByte+1 .. $]);
135             }
136         }
137     }
138 } +/