From c974b1eb4df53dc23bd80dd13ad514d8828dc986 Mon Sep 17 00:00:00 2001 From: Oliver Reiche Date: Mon, 1 Aug 2022 14:30:37 +0200 Subject: GitCAS: Implement reading git tree via libgit2 --- src/buildtool/file_system/git_tree.hpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/buildtool/file_system/git_tree.hpp') diff --git a/src/buildtool/file_system/git_tree.hpp b/src/buildtool/file_system/git_tree.hpp index ff7dc828..eb1c2147 100644 --- a/src/buildtool/file_system/git_tree.hpp +++ b/src/buildtool/file_system/git_tree.hpp @@ -59,6 +59,26 @@ class GitTree { : cas_{std::move(cas)}, entries_{std::move(entries)}, raw_id_{std::move(raw_id)} {} + + [[nodiscard]] static auto FromEntries(gsl::not_null cas, + GitCAS::tree_entries_t&& entries, + std::string raw_id) noexcept + -> std::optional { + entries_t e{}; + e.reserve(entries.size()); + for (auto& [id, es] : entries) { + for (auto& entry : es) { + try { + e.emplace( + std::move(entry.name), + std::make_shared(cas, id, entry.type)); + } catch (...) { + return std::nullopt; + } + } + } + return GitTree(std::move(cas), std::move(e), std::move(raw_id)); + } }; class GitTreeEntry { @@ -77,8 +97,8 @@ class GitTreeEntry { [[nodiscard]] auto Hash() const noexcept { return ToHexString(raw_id_); } [[nodiscard]] auto Type() const noexcept { return type_; } - // Use with care. Implementation might read entire object to obtain size. - // Consider using Blob()->size() instead. + // Use with care. Implementation might read entire object to obtain + // size. Consider using Blob()->size() instead. [[nodiscard]] auto Size() const noexcept -> std::optional; private: -- cgit v1.2.3