-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStoreCommand.cpp
More file actions
364 lines (314 loc) · 9.87 KB
/
Copy pathStoreCommand.cpp
File metadata and controls
364 lines (314 loc) · 9.87 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
*
* @file StoreCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/commands/StoreCommand.hpp>
#include <vix/cli/util/Ui.hpp>
#include <vix/cli/Style.hpp>
#include <vix/utils/Env.hpp>
#include <nlohmann/json.hpp>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
namespace fs = std::filesystem;
using json = nlohmann::json;
namespace vix::commands
{
using namespace vix::cli::style;
namespace
{
std::string home_dir()
{
#ifdef _WIN32
const char *home = vix::utils::vix_getenv("USERPROFILE");
#else
const char *home = vix::utils::vix_getenv("HOME");
#endif
return home ? std::string(home) : std::string();
}
fs::path vix_root()
{
const std::string h = home_dir();
if (h.empty())
return fs::path(".vix");
return fs::path(h) / ".vix";
}
fs::path store_root_git()
{
return vix_root() / "store" / "git";
}
fs::path lock_path()
{
return fs::current_path() / "vix.lock";
}
json read_json_or_throw(const fs::path &p)
{
std::ifstream in(p);
if (!in)
throw std::runtime_error("cannot open: " + p.string());
json j;
in >> j;
return j;
}
const json *get_deps_ptr(const json &lock)
{
if (lock.is_array())
return &lock;
if (lock.is_object() && lock.contains("dependencies") && lock["dependencies"].is_array())
return &lock["dependencies"];
return nullptr;
}
std::string pkg_dir_from_id(const std::string &id)
{
// "namespace/name" -> "namespace.name"
const auto pos = id.find('/');
if (pos == std::string::npos)
return {};
const std::string ns = id.substr(0, pos);
const std::string name = id.substr(pos + 1);
if (ns.empty() || name.empty())
return {};
return ns + "." + name;
}
std::uintmax_t dir_size_bytes(const fs::path &p)
{
std::uintmax_t total = 0;
std::error_code ec;
if (!fs::exists(p, ec))
return 0;
for (auto it = fs::recursive_directory_iterator(
p, fs::directory_options::skip_permission_denied, ec);
it != fs::recursive_directory_iterator(); it.increment(ec))
{
if (ec)
continue;
std::error_code ec2;
if (it->is_regular_file(ec2))
{
const auto sz = it->file_size(ec2);
if (!ec2)
total += sz;
}
}
return total;
}
std::string human_bytes(std::uintmax_t bytes)
{
const char *units[] = {"B", "KB", "MB", "GB", "TB"};
double v = static_cast<double>(bytes);
int u = 0;
while (v >= 1024.0 && u < 4)
{
v /= 1024.0;
++u;
}
// simple formatting: no <iomanip> needed
char buf[64];
if (u == 0)
std::snprintf(buf, sizeof(buf), "%llu %s",
static_cast<unsigned long long>(bytes), units[u]);
else
std::snprintf(buf, sizeof(buf), "%.2f %s", v, units[u]);
return std::string(buf);
}
struct KeepKey
{
std::string pkg;
std::string commit;
bool operator==(const KeepKey &o) const noexcept
{
return pkg == o.pkg && commit == o.commit;
}
};
struct KeepKeyHash
{
std::size_t operator()(const KeepKey &k) const noexcept
{
std::hash<std::string> h;
return (h(k.pkg) * 1315423911u) ^ h(k.commit);
}
};
int store_path()
{
vix::cli::util::section(std::cout, "Store");
vix::cli::util::kv(std::cout, "root", store_root_git().string());
return 0;
}
int store_gc_project(bool dryRun = false)
{
const fs::path root = store_root_git();
const fs::path lockP = lock_path();
vix::cli::util::section(std::cout, "Store GC");
vix::cli::util::kv(std::cout, "scope", "project");
vix::cli::util::kv(std::cout, "lock", lockP.string());
vix::cli::util::kv(std::cout, "root", root.string());
if (dryRun)
vix::cli::util::kv(std::cout, "mode", "dry-run");
if (!fs::exists(lockP))
{
vix::cli::util::err_line(std::cerr, "missing lock file: " + lockP.string());
vix::cli::util::warn_line(std::cerr, "Tip: run in a project folder containing vix.lock");
return 1;
}
vix::cli::util::warn_line(std::cerr, "WARNING: This will remove any store entries NOT used by THIS project.");
vix::cli::util::warn_line(std::cerr, "If you have other Vix projects, their cached dependencies might be deleted.");
std::cerr << "\n";
if (!fs::exists(root))
{
vix::cli::util::ok_line(std::cout, "GC done (store empty).");
return 0;
}
json lock;
try
{
lock = read_json_or_throw(lockP);
}
catch (const std::exception &ex)
{
vix::cli::util::err_line(std::cerr, std::string("failed to read lock: ") + ex.what());
return 1;
}
const json *depsPtr = get_deps_ptr(lock);
if (!depsPtr)
{
vix::cli::util::err_line(std::cerr, "invalid lock: expected array or { dependencies: [] }");
return 1;
}
std::unordered_set<KeepKey, KeepKeyHash> keep;
for (const auto &d : *depsPtr)
{
const std::string id = d.value("id", "");
const std::string commit = d.value("commit", "");
const std::string pkg = pkg_dir_from_id(id);
if (pkg.empty() || commit.empty())
continue;
keep.insert(KeepKey{pkg, commit});
}
std::size_t removedCommits = 0;
std::size_t removedPackages = 0;
std::uintmax_t freed = 0;
std::error_code ec;
for (const auto &pkgIt : fs::directory_iterator(root, fs::directory_options::skip_permission_denied, ec))
{
if (ec)
break;
if (!pkgIt.is_directory())
continue;
const fs::path pkgDir = pkgIt.path();
const std::string pkgName = pkgDir.filename().string();
std::vector<fs::path> commitsToRemove;
std::error_code ec2;
for (const auto &cIt : fs::directory_iterator(pkgDir, fs::directory_options::skip_permission_denied, ec2))
{
if (ec2)
break;
if (!cIt.is_directory())
continue;
const fs::path commitDir = cIt.path();
const std::string sha = commitDir.filename().string();
if (keep.find(KeepKey{pkgName, sha}) == keep.end())
commitsToRemove.push_back(commitDir);
}
for (const auto &cdir : commitsToRemove)
{
const auto sz = dir_size_bytes(cdir);
freed += sz;
if (!dryRun)
{
std::error_code rmec;
fs::remove_all(cdir, rmec);
if (!rmec)
{
vix::cli::util::info_line(std::cout, "removed: " + pkgName + " [" + cdir.filename().string().substr(0, 8) + "...]");
++removedCommits;
}
}
else
{
vix::cli::util::info_line(std::cout, "would remove: " + pkgName + " [" + cdir.filename().string().substr(0, 8) + "...]");
++removedCommits;
}
}
// If package dir empty -> remove it
if (!dryRun)
{
std::error_code ec3;
const bool empty = fs::is_empty(pkgDir, ec3);
if (!ec3 && empty)
{
std::error_code rmec2;
fs::remove(pkgDir, rmec2);
if (!rmec2)
++removedPackages;
}
}
}
vix::cli::util::one_line_spacer(std::cout);
vix::cli::util::ok_line(std::cout, dryRun ? "GC dry-run finished." : "GC finished.");
vix::cli::util::kv(std::cout, dryRun ? "would remove commits" : "removed commits", std::to_string(removedCommits));
vix::cli::util::kv(std::cout, dryRun ? "would remove packages" : "removed packages", std::to_string(removedPackages));
vix::cli::util::kv(std::cout, dryRun ? "would free" : "freed", human_bytes(freed));
return 0;
}
}
int StoreCommand::run(const std::vector<std::string> &args)
{
if (args.empty())
return help();
const std::string sub = args[0];
if (sub == "path")
return store_path();
if (sub == "gc")
{
bool dryRun = false;
bool projectScope = false;
for (std::size_t i = 1; i < args.size(); ++i)
{
if (args[i] == "--dry-run")
dryRun = true;
else if (args[i] == "--project")
projectScope = true;
}
if (!projectScope)
{
vix::cli::util::err_line(std::cerr, "GC requires --project scope in this version.");
vix::cli::util::warn_line(std::cerr, "Use: vix store gc --project");
return 1;
}
return store_gc_project(dryRun);
}
vix::cli::util::err_line(std::cerr, "unknown store subcommand: " + sub);
vix::cli::util::warn_line(std::cerr, "Try: vix store path");
vix::cli::util::warn_line(std::cerr, "Try: vix store gc --project");
return help();
}
int StoreCommand::help()
{
std::cout
<< "Usage:\n"
<< " vix store <subcommand> [options]\n\n"
<< "Subcommands:\n"
<< " path Print local store root path\n"
<< " gc Garbage collect the store\n\n"
<< "GC Options:\n"
<< " --project Scope GC to the current project (uses vix.lock)\n"
<< " --dry-run List files that would be removed without deleting them\n\n"
<< "Notes:\n"
<< " - GC --project keeps commits referenced by ./vix.lock\n"
<< " - WARNING: GC --project is destructive for other projects sharing the same store.\n";
return 0;
}
}