diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-02-22 17:03:21 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-02-22 17:03:21 +0100 |
commit | 619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 (patch) | |
tree | 01868de723cb82c86842f33743fa7b14e24c1fa3 /src/buildtool/file_system/git_cas.hpp | |
download | justbuild-619def44c1cca9f3cdf63544d5f24f2c7a7d9b77.tar.gz |
Initial self-hosting commit
This is the initial version of our tool that is able to
build itself. In can be bootstrapped by
./bin/bootstrap.py
Co-authored-by: Oliver Reiche <oliver.reiche@huawei.com>
Co-authored-by: Victor Moreno <victor.moreno1@huawei.com>
Diffstat (limited to 'src/buildtool/file_system/git_cas.hpp')
-rw-r--r-- | src/buildtool/file_system/git_cas.hpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/buildtool/file_system/git_cas.hpp b/src/buildtool/file_system/git_cas.hpp new file mode 100644 index 00000000..d4341482 --- /dev/null +++ b/src/buildtool/file_system/git_cas.hpp @@ -0,0 +1,60 @@ +#ifndef INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_GIT_CAS_HPP +#define INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_GIT_CAS_HPP + +#include <filesystem> +#include <memory> +#include <mutex> +#include <optional> + +#include "src/buildtool/file_system/object_type.hpp" + +extern "C" { +using git_odb = struct git_odb; +} + +class GitCAS; +using GitCASPtr = std::shared_ptr<GitCAS const>; + +/// \brief Git CAS that maintains its own libgit2 global state. +class GitCAS { + public: + static auto Open(std::filesystem::path const& repo_path) noexcept + -> GitCASPtr; + + GitCAS() noexcept; + ~GitCAS() noexcept; + + // prohibit moves and copies + GitCAS(GitCAS const&) = delete; + GitCAS(GitCAS&& other) = delete; + auto operator=(GitCAS const&) = delete; + auto operator=(GitCAS&& other) = delete; + + /// \brief Read object from CAS. + /// \param id The object id. + /// \param is_hex_id Specify whether `id` is hex string or raw. + [[nodiscard]] auto ReadObject(std::string const& id, + bool is_hex_id = false) const noexcept + -> std::optional<std::string>; + + /// \brief Read object header from CAS. + /// \param id The object id. + /// \param is_hex_id Specify whether `id` is hex string or raw. + // Use with care. Quote from git2/odb.h:138: + // Note that most backends do not support reading only the header of an + // object, so the whole object will be read and then the header will be + // returned. + [[nodiscard]] auto ReadHeader(std::string const& id, + bool is_hex_id = false) const noexcept + -> std::optional<std::pair<std::size_t, ObjectType>>; + + private: + static inline std::mutex repo_mutex_{}; + git_odb* odb_{nullptr}; + bool initialized_{false}; + + [[nodiscard]] auto OpenODB(std::filesystem::path const& repo_path) noexcept + -> bool; +}; + +#endif // INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_GIT_CAS_HPP |