/
main.cpp
242 lines (202 loc) · 4.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "gitdb.h"
#include "text_reader.h"
#include "file.h"
#include <time.h>
#include <iostream>
#include "cmdline.h"
#include "console.h"
namespace gh_opts {
enum
{
cmd,
repo,
time,
profile,
bare,
wd_dir,
ref,
};
};
namespace gh_subparser {
enum
{
none,
init,
test_checkout,
status,
};
}
static cmdline_entry const cmdline_entries[] =
{
{ gh_opts::cmd, 0, "command", "", 1, 0, "the command to execute" },
{ gh_opts::repo, 'R', "--repo", ".", 1, 0, "the path to the repository or inside a working dir" },
{ gh_opts::time, 0, "--time", "", 0, 0, "print the total time spent executing gh" },
{ gh_opts::profile, 0, "--profile", "1", 1, 0, "repeat the execution of the command (for profiling)" },
{ gh_opts::bare, 0, "--bare", "", 0, gh_subparser::init, "create a bare repo" },
{ gh_opts::wd_dir, 0, "wd_dir", "", 0, gh_subparser::test_checkout, "" },
{ gh_opts::ref, 0, "ref", "", 0, gh_subparser::test_checkout, "" },
};
void print_stream(istream & s)
{
stream_reader sr(s);
std::string line;
while (sr.read_line(line))
{
std::cout << line << '\n';
}
}
static void checkout_tree(gitdb & db, string_view dir, gitdb::tree_t const & t)
{
for (auto && te: t)
{
std::string name = dir.to_string() + "/" + te.name;
if ((te.mode & 0xe000) == 0xe000)
{
// XXX gitlink
}
else if (te.mode & 0x4000)
{
make_directory(name);
checkout_tree(db, name, db.get_tree(te.oid));
}
else
{
file ff(name, /*readonly=*/false);
std::vector<uint8_t> v = db.get_blob(te.oid);
file::ofile fo = ff.seekp(0);
write_all(fo, v.data(), v.size());
}
}
}
static int gh_init(cmdline & args)
{
std::string repo_arg = args.pop_string(gh_opts::repo);
bool bare = args.pop_switch(gh_opts::bare);
if (!bare)
{
std::string path = repo_arg + "/.git";
if (!make_directory(path))
{
std::cerr << "error: the directory already exists: " << path << "\n";
return 1;
}
gitdb::create(path);
}
else
{
gitdb::create(repo_arg);
}
return 0;
}
static bool open_wd(gitdb & db, git_wd & wd, string_view path)
{
std::string apath = absolute_path(clean_path(path));
path = apath;
while (!path.empty())
{
std::string nonbare_path = join_paths(path, ".git");
if (file::is_directory(nonbare_path))
{
db.open(nonbare_path);
wd.open(db, path);
return true;
}
path = path_head(path);
}
return false;
}
static void print_status(std::map<std::string, git_wd::file_status> const & fs, bool untracked)
{
for (auto && kv : fs)
{
char const * stati = untracked? "?DM": "ADM";
uint8_t const colors[] = { untracked? 0xe: 0xa, 0xa, 0xc };
console_color_guard concolor(colors[static_cast<int>(kv.second)]);
std::cout << stati[static_cast<int>(kv.second)] << " " << kv.first << "\n";
}
}
static int gh_status(cmdline & args)
{
std::string repo_arg = args.pop_string(gh_opts::repo);
gitdb db;
git_wd wd;
if (!open_wd(db, wd, repo_arg))
{
std::cerr << "error: not a git repository: " << repo_arg << "\n";
return 2;
}
std::string real_ref;
object_id head_oid = db.get_ref("HEAD", real_ref);
std::cout << "B " << real_ref << "\n";
std::map<std::string, git_wd::file_status> fs;
wd.commit_status(fs, head_oid);
if (!fs.empty())
{
print_status(fs, /*untracked=*/false);
std::cout << "\n";
fs.clear();
}
git_ignore ign;
ign.add_pattern("", "/.git");
wd.status(fs, ign);
print_status(fs, /*untracked=*/true);
return 0;
}
class timer
{
public:
explicit timer(bool active)
{
m_start_time = clock();
m_active = active;
}
~timer()
{
if (m_active)
std::cout << "Spent time: " << (double)(clock() - m_start_time) / CLOCKS_PER_SEC << "\n";
}
private:
bool m_active;
clock_t m_start_time;
};
int main(int argc, char * argv[])
{
try
{
cmdline args(cmdline_entries, std::end(cmdline_entries), argc - 1, argv + 1);
bool profile = args.pop_switch(gh_opts::time);
std::string cmd = args.pop_string(gh_opts::cmd);
int profile_repeat = atoi(args.pop_string(gh_opts::profile).c_str());
int r = 1;
for (int i = 0; i < profile_repeat; ++i)
{
timer tmr(profile);
cmdline subargs = args;
if (cmd == "init")
{
r = gh_init(subargs);
}
else if (cmd == "test-checkout")
{
subargs.set_subparser(gh_subparser::test_checkout);
gitdb db0;
db0.open(subargs.pop_string(gh_opts::repo, "."));
object_id head_oid = db0.get_ref(subargs.pop_string(gh_opts::ref));
gitdb::commit_t cc = db0.get_commit(head_oid);
checkout_tree(db0, subargs.pop_string(gh_opts::wd_dir), db0.get_tree(cc.tree_oid));
r = 0;
}
else if (cmd == "st" || cmd == "status")
{
r = gh_status(subargs);
}
}
return r;
}
catch (std::exception const & e)
{
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}