#include "src/buildtool/file_system/git_tree.hpp" #include #include "src/buildtool/logging/logger.hpp" extern "C" { #include } namespace { constexpr auto kOIDRawSize{GIT_OID_RAWSZ}; auto const kLoadTreeError = std::make_shared>(std::nullopt); [[nodiscard]] auto PermToType(std::string const& perm_str) noexcept -> std::optional { constexpr auto kPermBase = 8; constexpr auto kTreePerm = 040000; constexpr auto kFilePerm = 0100644; constexpr auto kExecPerm = 0100755; constexpr auto kLinkPerm = 0120000; int perm = std::stoi(perm_str, nullptr, kPermBase); switch (perm) { case kTreePerm: return ObjectType::Tree; case kFilePerm: return ObjectType::File; case kExecPerm: return ObjectType::Executable; case kLinkPerm: Logger::Log(LogLevel::Error, "symlinks are not yet supported"); return std::nullopt; default: Logger::Log(LogLevel::Error, "unsupported permission {}", perm_str); return std::nullopt; } } auto ParseRawTreeObject(GitCASPtr const& cas, std::string const& raw_tree) noexcept -> std::optional { std::string perm{}; std::string path{}; std::string hash(kOIDRawSize, '\0'); std::istringstream iss{raw_tree}; GitTree::entries_t entries{}; // raw tree format is: " \0[next entries...]" while (std::getline(iss, perm, ' ') and // std::getline(iss, path, '\0') and // iss.read(hash.data(), // static_cast(hash.size()))) { auto type = PermToType(perm); if (not type) { return std::nullopt; } try { entries.emplace(path, std::make_shared(cas, hash, *type)); } catch (std::exception const& ex) { Logger::Log(LogLevel::Error, "parsing git raw tree object failed with:\n{}", ex.what()); return std::nullopt; } } return entries; } // resolve '.' and '..' in path. [[nodiscard]] auto ResolveRelativePath( std::filesystem::path const& path) noexcept -> std::filesystem::path { auto normalized = path.lexically_normal(); return (normalized / "").parent_path(); // strip trailing slash } // NOLINTNEXTLINE(misc-no-recursion) [[nodiscard]] auto LookupEntryPyPath( GitTree const& tree, std::filesystem::path::const_iterator it, std::filesystem::path::const_iterator const& end) noexcept -> GitTreeEntryPtr { auto segment = *it; auto entry = tree.LookupEntryByName(segment); if (not entry) { return nullptr; } if (++it != end) { if (not entry->IsTree()) { return nullptr; } return LookupEntryPyPath(*entry->Tree(), it, end); } return entry; } } // namespace auto GitTree::Read(std::filesystem::path const& repo_path, std::string const& tree_id) noexcept -> std::optional { auto cas = GitCAS::Open(repo_path); if (not cas) { return std::nullopt; } return Read(cas, tree_id); } auto GitTree::Read(gsl::not_null const& cas, std::string const& tree_id) noexcept -> std::optional { auto obj = cas->ReadObject(tree_id, /*is_hex_id=*/true); if (not obj) { return std::nullopt; } auto entries = ParseRawTreeObject(cas, *obj); if (not entries) { return std::nullopt; } return GitTree{cas, std::move(*entries)}; } auto GitTree::LookupEntryByName(std::string const& name) const noexcept -> GitTreeEntryPtr { auto entry_it = entries_.find(name); if (entry_it == entries_.end()) { Logger::Log( LogLevel::Error, "git tree does not contain entry {}", name); return nullptr; } return entry_it->second; } auto GitTree::LookupEntryByPath( std::filesystem::path const& path) const noexcept -> GitTreeEntryPtr { auto resolved = ResolveRelativePath(path); return LookupEntryPyPath(*this, resolved.begin(), resolved.end()); } auto GitTreeEntry::Blob() const noexcept -> std::optional { if (not IsBlob()) { return std::nullopt; } return cas_->ReadObject(raw_id_); } auto GitTreeEntry::Tree() const& noexcept -> std::optional const& { auto ptr = tree_cached_.load(); if (not ptr) { if (not tree_loading_.exchange(true)) { ptr = kLoadTreeError; std::optional obj{}; if (IsTree() and (obj = cas_->ReadObject(raw_id_))) { if (auto entries = ParseRawTreeObject(cas_, *obj)) { ptr = std::make_shared>( GitTree{cas_, std::move(*entries)}); } } tree_cached_.store(ptr); tree_cached_.notify_all(); } else { tree_cached_.wait(nullptr); ptr = tree_cached_.load(); } } return *ptr; } auto GitTreeEntry::Size() const noexcept -> std::optional { if (auto header = cas_->ReadHeader(raw_id_)) { return header->first; } return std::nullopt; }