-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.cpp
427 lines (320 loc) · 12 KB
/
repository.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "repository.h"
#include <tomlplusplus/toml.h>
#include <iostream>
#include "blob.h"
#include "commit.h"
#include "crypto.h"
#include "handle.h"
#include "tree.h"
namespace fs = std::filesystem;
namespace tog {
void Repository::init(const fs::path& path) {
auto togdir_path = path / ".tog";
// Check if .tog directory exists
if (fs::exists(togdir_path)) {
throw TogException{"already a tog repository"};
return;
}
// Create repository directories
fs::create_directories(togdir_path / "objects");
fs::create_directories(togdir_path / "refs" / "branches");
// Create default config file
auto config = toml::table{{
{"version", "0.0.0-alpha"},
{"worktree", ".."},
}};
std::ofstream config_file{togdir_path / "config.toml"};
config_file << config << std::endl;
// Create (empty) main and head refs
std::ofstream{togdir_path / "refs" / "branches" / "main"};
std::ofstream head_file{togdir_path / "refs" / "head"};
}
Repository::Repository(const fs::path& togdir_path)
: _togdir_path{togdir_path} {
auto config_path = togdir_path / "config.toml";
toml::table config;
try {
config = toml::parse_file(config_path.string());
} catch (const toml::parse_error& err) {
throw TogException{"Unable to load repository."};
}
auto rel_worktree_path{config["worktree"].value<std::string>()};
if (!rel_worktree_path) {
throw TogException{"Unable to load repository"};
} else {
this->_worktree_path = fs::canonical(togdir_path / *rel_worktree_path);
}
_head = load_ref(togdir_path / "refs" / "head");
_main = load_ref(togdir_path / "refs" / "branches" / "main");
}
std::string Repository::commit(const std::string& message) {
// cannot commit if head is not at the latest commit of the main branch
// (which is the only branch for now). In git, this is known as "detached
// head".
if (_head && (_head->hash() != _main->hash())) {
throw TogException{"not at latest commit of current branch"};
}
auto tree_handle = add_directory(_worktree_path);
auto commit =
register_object(std::make_unique<Commit>(tree_handle, _head, message));
// Update refs (by copy)
_head = commit;
_main = commit;
// TODO unify persist-loops once I have a better understanding of templates
auto objects_path = _togdir_path / "objects";
// persist commit
if (commit.dirty()) {
auto bytes = commit.object()->serialize();
std::ofstream file{objects_path / commit.hash(), std::ios_base::binary};
file.write(reinterpret_cast<char*>(bytes.data()), bytes.size());
}
// persist blob objects
for (const auto& [hash, handle] : _blobs) {
if (handle.dirty()) {
auto bytes = handle.object()->serialize();
std::ofstream file{objects_path / hash, std::ios_base::binary};
file.write(reinterpret_cast<char*>(bytes.data()), bytes.size());
}
}
// persist commit objects
for (const auto& [hash, handle] : _trees) {
if (handle.dirty()) {
auto bytes = handle.object()->serialize();
std::ofstream file{objects_path / hash, std::ios_base::binary};
file.write(reinterpret_cast<char*>(bytes.data()), bytes.size());
}
}
// persist refs
persist_ref(_togdir_path / "refs" / "head", _head);
persist_ref(_togdir_path / "refs" / "branches" / "main", _main);
return commit.hash();
}
void Repository::checkout(const std::string& hash) {
auto commit = Handle<Commit>{hash};
resolve(commit);
// clear working directory (except .tog)
for (const auto& entry : fs::directory_iterator(_worktree_path)) {
if (entry.path().filename() != ".tog") {
fs::remove_all(entry.path());
}
}
auto tree = commit.object()->tree();
resolve(tree);
restoreTree(tree, _worktree_path);
_head = commit;
persist_ref(_togdir_path / "refs" / "head", _head);
}
std::vector<std::string> Repository::history(int n) {
std::vector<std::string> commits;
std::optional<Handle<Commit>> current = _head;
for (int i = 0; i < n; ++i) {
if (!current) {
return commits;
}
commits.push_back(current->hash());
resolve(*current);
current = (current->object()->parent());
}
return commits;
}
void Repository::resolve(Handle<Blob>& blob) {
if (blob.resolved()) {
return;
}
auto hash = blob.hash();
auto path = _togdir_path / "objects" / hash;
if (!fs::exists(path)) {
throw TogException{"object not found"};
}
// cache for future resolutions
if (!_blobs.contains(hash)) {
// Explicitly copy hash/object to avoid dangling references
_blobs.emplace(std::string{hash}, Handle<Blob>{blob});
}
auto& cached = _blobs.at(hash);
if (!cached.resolved()) {
// TODO error management; what if object is not a blob?
// TODO using the blob constructor works, because blobs are not yet
// compressed. Revisit this when we have compression.
cached.resolve(std::make_shared<Blob>(path));
}
// copy assignment operator
blob = cached;
}
void Repository::resolve(Handle<Tree>& tree) {
if (tree.resolved()) {
return;
}
auto hash = tree.hash();
auto path = _togdir_path / "objects" / hash;
if (!fs::exists(path)) {
throw TogException{"object not found"};
}
// cache for future resolutions
if (!_trees.contains(hash)) {
// Explicitly copy hash/object to avoid dangling references
_trees.emplace(std::string{hash}, Handle<Tree>{tree});
}
auto& cached = _trees.at(hash);
if (!cached.resolved()) {
// TODO error management; what if object is not a tree?
// load commit from disk
auto deserialized = toml::parse_file(path.string());
// parse blobs
std::unordered_map<std::string, Handle<Blob>> blobs;
for (const auto& [key, value] : *deserialized["blobs"].as_table()) {
std::string blob_hash{value.value_or("")};
if (blob_hash.empty()) {
throw TogException{"corrupt tree object " + hash};
}
blobs.emplace(key, Handle<Blob>{blob_hash});
}
// parse trees
std::unordered_map<std::string, Handle<Tree>> trees;
for (const auto& [key, value] : *deserialized["trees"].as_table()) {
std::string tree_hash{value.value_or("")};
if (tree_hash.empty()) {
throw TogException{"corrupt tree object " + hash};
}
trees.emplace(key, Handle<Tree>{tree_hash});
}
cached.resolve(
std::make_shared<Tree>(std::move(blobs), std::move(trees)));
}
// copy assignment operator
tree = cached;
}
void Repository::resolve(Handle<Commit>& commit) {
if (commit.resolved()) {
return;
}
auto hash = commit.hash();
auto path = _togdir_path / "objects" / hash;
if (!fs::exists(path)) {
throw TogException{"commit does not exist"};
}
// cache for future resolutions
if (!_commits.contains(hash)) {
// Explicitly copy hash/object to avoid dangling references
_commits.emplace(std::string{hash}, Handle<Commit>{commit});
}
auto& cached = _commits.at(hash);
if (!cached.resolved()) {
// TODO error management; what if object is not a commit?
// load commit from disk
auto deserialized = toml::parse_file(path.string());
// parse tree
auto tree_hash = deserialized["tree"].value<std::string>();
auto tree = Handle<Tree>{*tree_hash};
// parse parent
auto parent_hash = deserialized["parent"].value<std::string>();
std::optional<Handle<Commit>> parent;
if (parent_hash && !parent_hash->empty()) {
parent = Handle<Commit>{*parent_hash};
}
// parse message
auto message = deserialized["message"].value<std::string>();
cached.resolve(
std::make_shared<Commit>(tree, parent, message.value_or("")));
}
// copy assignment operator
commit = cached;
}
void Repository::restoreTree(Handle<Tree>& tree, const fs::path& path) {
resolve(tree);
// Create the directory if it doesn't exist
fs::create_directories(path);
// Populate the directory with files
for (auto& [name, blob] : tree.object()->blobs()) {
restoreBlob(blob, path / name);
}
// Populate the directory with subdirectories
for (auto& [name, sub_tree] : tree.object()->trees()) {
restoreTree(sub_tree, path / name);
}
}
void Repository::restoreBlob(Handle<Blob>& blob, const fs::path& path) {
resolve(blob);
const auto& data = blob.object()->data();
std::ofstream stream{path, std::ios::out | std::ios::binary};
stream.write(reinterpret_cast<const char*>(data.data()), data.size());
}
Handle<Blob>& Repository::add_file(const fs::path& file_path) {
return register_object(std::make_unique<Blob>(file_path));
}
Handle<Tree>& Repository::add_directory(const fs::path& directory_path) {
std::unordered_map<std::string, Handle<Blob>> files;
std::unordered_map<std::string, Handle<Tree>> directories;
for (const auto& entry : fs::directory_iterator(directory_path)) {
if (entry.is_directory()) {
// skip togdir
if (entry.path().filename() == ".tog") {
continue;
}
directories.emplace(entry.path().filename(),
add_directory(entry.path()));
} else if (entry.is_regular_file()) {
files.emplace(entry.path().filename().string(),
add_file(entry.path()));
}
}
return register_object(
std::make_unique<Tree>(std::move(files), std::move(directories)));
}
Handle<Blob>& Repository::register_object(std::unique_ptr<Blob> blob) {
auto bytes = blob->serialize();
auto hash = sha256(bytes);
if (!_blobs.contains(hash)) {
// If there is a matching file in .tog/objects/ the blob is already
// persisted. Otherwise, mark it as dirty to persist it later.
auto dirty = !fs::exists(_togdir_path / "objects" / hash);
_blobs.emplace(hash, Handle<Blob>{hash, std::move(blob), dirty});
}
return _blobs.at(hash);
}
Handle<Tree>& Repository::register_object(std::unique_ptr<Tree> tree) {
auto bytes = tree->serialize();
auto hash = sha256(bytes);
if (!_trees.contains(hash)) {
auto dirty = !fs::exists(_togdir_path / "objects" / hash);
_trees.emplace(hash, Handle<Tree>{hash, std::move(tree), dirty});
}
return _trees.at(hash);
}
Handle<Commit>& Repository::register_object(std::unique_ptr<Commit> commit) {
auto bytes = commit->serialize();
auto hash = sha256(bytes);
if (!_commits.contains(hash)) {
auto dirty = !fs::exists(_togdir_path / "objects" / hash);
_commits.emplace(hash, Handle<Commit>{hash, std::move(commit), dirty});
}
return _commits.at(hash);
}
std::optional<Handle<Commit>> Repository::load_ref(const fs::path& path) {
std::ifstream stream{path};
if (!stream) {
throw TogException{"Unable to load ref " + path.string()};
}
std::string hash;
std::getline(stream, hash);
if (hash.empty()) {
return std::nullopt;
}
if (!fs::exists(_togdir_path / "objects" / hash)) {
throw TogException{"ref " + path.string() +
" points to non-existent commit " + hash};
}
auto commit = Handle<Commit>{hash};
return std::optional<Handle<Commit>>{std::move(commit)};
}
void Repository::persist_ref(const fs::path& path,
const std::optional<Handle<Commit>>& commit) {
std::ofstream stream{path, std::ofstream::trunc};
if (!stream) {
throw TogException{"Unable to save ref " + path.string()};
}
if (commit) {
stream << commit->hash() << std::endl;
}
}
} // namespace tog