diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/file_system/git_cas.cpp | 112 | ||||
-rw-r--r-- | src/buildtool/file_system/git_cas.hpp | 7 |
2 files changed, 48 insertions, 71 deletions
diff --git a/src/buildtool/file_system/git_cas.cpp b/src/buildtool/file_system/git_cas.cpp index db119c31..2d1c4c33 100644 --- a/src/buildtool/file_system/git_cas.cpp +++ b/src/buildtool/file_system/git_cas.cpp @@ -56,19 +56,56 @@ GitCAS::GitCAS() noexcept { auto GitCAS::Open(std::filesystem::path const& repo_path) noexcept -> GitCASPtr { -#ifndef BOOTSTRAP_BUILD_TOOL +#ifdef BOOTSTRAP_BUILD_TOOL + return nullptr; +#else + auto result = std::make_shared<GitCAS>(); + + // lock as git_repository API has no thread-safety guarantees + static std::mutex repo_mutex{}; + std::unique_lock lock{repo_mutex}; + git_repository* repo_ptr = nullptr; + if (git_repository_open_ext(&repo_ptr, + repo_path.c_str(), + GIT_REPOSITORY_OPEN_NO_SEARCH, + nullptr) != 0 or + repo_ptr == nullptr) { + Logger::Log(LogLevel::Error, + "Opening git repository {} failed with:\n{}", + repo_path.string(), + GitLastError()); + return nullptr; + } + auto const repo = + std::unique_ptr<git_repository, decltype(&git_repository_free)>( + repo_ptr, git_repository_free); + + git_odb* odb_ptr = nullptr; + if (git_repository_odb(&odb_ptr, repo.get()) != 0 or odb_ptr == nullptr) { + Logger::Log(LogLevel::Error, + "Obtaining git object database {} failed with:\n{}", + repo_path.string(), + GitLastError()); + return nullptr; + } + result->odb_.reset(odb_ptr); + + auto const git_path = + git_repository_is_bare(repo.get()) != 0 + ? ToNormalPath(git_repository_path(repo.get())) + : ToNormalPath(git_repository_workdir(repo.get())); + try { - auto cas = std::make_shared<GitCAS>(); - if (cas->OpenODB(repo_path)) { - return std::static_pointer_cast<GitCAS const>(cas); - } - } catch (std::exception const& ex) { + result->git_path_ = std::filesystem::absolute(git_path); + } catch (std::exception const& e) { Logger::Log(LogLevel::Error, - "opening git object database failed with:\n{}", - ex.what()); + "Failed to obtain absolute path for {}: {}", + git_path.string(), + e.what()); + return nullptr; } + return std::const_pointer_cast<GitCAS const>(result); #endif - return nullptr; } auto GitCAS::ReadObject(std::string const& id, bool is_hex_id) const noexcept @@ -137,60 +174,3 @@ auto GitCAS::ReadHeader(std::string const& id, bool is_hex_id) const noexcept #endif return std::nullopt; } - -auto GitCAS::OpenODB(std::filesystem::path const& repo_path) noexcept -> bool { - static std::mutex repo_mutex{}; -#ifdef BOOTSTRAP_BUILD_TOOL - return false; -#else - { // lock as git_repository API has no thread-safety guarantees - std::unique_lock lock{repo_mutex}; - git_repository* repo_ptr = nullptr; - if (git_repository_open_ext(&repo_ptr, - repo_path.c_str(), - GIT_REPOSITORY_OPEN_NO_SEARCH, - nullptr) != 0) { - Logger::Log(LogLevel::Debug, - "opening git repository {} failed with:\n{}", - repo_path.string(), - GitLastError()); - return false; - } - auto const repo = - std::unique_ptr<git_repository, decltype(&git_repository_free)>( - repo_ptr, git_repository_free); - - git_odb* odb_ptr{nullptr}; - git_repository_odb(&odb_ptr, repo.get()); - odb_.reset(odb_ptr); // retain odb pointer - // set root - std::filesystem::path git_path{}; - if (git_repository_is_bare(repo.get()) != 0) { - git_path = ToNormalPath((git_repository_path(repo.get()))); - } - else { - git_path = ToNormalPath(git_repository_workdir(repo.get())); - } - if (not git_path.is_absolute()) { - try { - git_path = std::filesystem::absolute(git_path); - } catch (std::exception const& e) { - Logger::Log(LogLevel::Error, - "Failed to obtain absolute path for {}: {}", - git_path.string(), - e.what()); - return false; - } - } - git_path_ = git_path; - } - if (not odb_) { - Logger::Log(LogLevel::Error, - "obtaining git object database {} failed with:\n{}", - repo_path.string(), - GitLastError()); - return false; - } - return true; -#endif -} diff --git a/src/buildtool/file_system/git_cas.hpp b/src/buildtool/file_system/git_cas.hpp index fff572e1..8751b1e6 100644 --- a/src/buildtool/file_system/git_cas.hpp +++ b/src/buildtool/file_system/git_cas.hpp @@ -32,8 +32,8 @@ using GitCASPtr = std::shared_ptr<GitCAS const>; /// \brief Git CAS that maintains its Git context. class GitCAS { public: - static auto Open(std::filesystem::path const& repo_path) noexcept - -> GitCASPtr; + [[nodiscard]] static auto Open( + std::filesystem::path const& repo_path) noexcept -> GitCASPtr; GitCAS() noexcept; ~GitCAS() noexcept = default; @@ -70,9 +70,6 @@ class GitCAS { // to share it. mutable std::shared_mutex mutex_; - [[nodiscard]] auto OpenODB(std::filesystem::path const& repo_path) noexcept - -> bool; - friend class GitRepo; // allow access to ODB }; |