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_cas.hpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/buildtool/file_system/git_cas.hpp') diff --git a/src/buildtool/file_system/git_cas.hpp b/src/buildtool/file_system/git_cas.hpp index c964a739..051e40f1 100644 --- a/src/buildtool/file_system/git_cas.hpp +++ b/src/buildtool/file_system/git_cas.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "src/buildtool/file_system/object_type.hpp" @@ -17,6 +19,25 @@ using GitCASPtr = std::shared_ptr; /// \brief Git CAS that maintains its own libgit2 global state. class GitCAS { public: + // Stores the data for defining a single Git tree entry, which consists of + // a name (flat basename) and an object type (file/executable/tree). + struct tree_entry_t { + tree_entry_t(std::string n, ObjectType t) + : name{std::move(n)}, type{t} {} + std::string name; + ObjectType type; + [[nodiscard]] auto operator==(tree_entry_t const& other) const noexcept + -> bool { + return name == other.name and type == other.type; + } + }; + + // Tree entries by raw id. The same id might refer to multiple entries. + // Note that sharding by id is used as this format enables a more efficient + // internal implementation for creating trees. + using tree_entries_t = + std::unordered_map>; + static auto Open(std::filesystem::path const& repo_path) noexcept -> GitCASPtr; @@ -47,6 +68,15 @@ class GitCAS { bool is_hex_id = false) const noexcept -> std::optional>; + /// \brief Read entries from tree in CAS. + /// Reading a tree must be backed by an object database. Therefore, a valid + /// instance of this class is required. + /// \param id The object id. + /// \param is_hex_id Specify whether `id` is hex string or raw. + [[nodiscard]] auto ReadTree(std::string const& id, + bool is_hex_id = false) const noexcept + -> std::optional; + private: git_odb* odb_{nullptr}; bool initialized_{false}; -- cgit v1.2.3