-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit.cpp
34 lines (25 loc) · 830 Bytes
/
commit.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
#include "commit.h"
#include <tomlplusplus/toml.h>
namespace tog {
// TODO fix format in .clang-format
Commit::Commit(Handle<Tree> tree, std::optional<Handle<Commit>> parent,
std::string message)
: _tree{std::move(tree)}, _parent{parent}, _message{std::move(message)} {}
const std::vector<unsigned char>& Commit::serialize() {
// serialize lazily
if (_serialized) {
return *_serialized;
}
// encode as toml
auto commit_toml = toml::table{{
{"message", _message},
{"tree", _tree.hash()},
{"parent", _parent ? _parent->hash() : ""},
}};
std::stringstream stream{};
stream << commit_toml;
std::string commit_str = stream.str();
_serialized.emplace(commit_str.begin(), commit_str.end());
return *_serialized;
}
} // namespace tog