diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/file_system/git_repo.cpp | 51 | ||||
-rw-r--r-- | src/buildtool/file_system/git_repo.hpp | 19 |
2 files changed, 65 insertions, 5 deletions
diff --git a/src/buildtool/file_system/git_repo.cpp b/src/buildtool/file_system/git_repo.cpp index d30f6614..f679647e 100644 --- a/src/buildtool/file_system/git_repo.cpp +++ b/src/buildtool/file_system/git_repo.cpp @@ -1313,6 +1313,57 @@ auto GitRepo::CheckTreeExists(std::string const& tree_id, #endif // BOOTSTRAP_BUILD_TOOL } +auto GitRepo::CheckBlobExists(std::string const& blob_id, + anon_logger_ptr const& logger) noexcept + -> std::optional<bool> { +#ifdef BOOTSTRAP_BUILD_TOOL + return std::nullopt; +#else + try { + // preferably with a "fake" repository! + if (not IsRepoFake()) { + Logger::Log(LogLevel::Debug, + "Blob lookup called on a real repository"); + } + // get git oid + git_oid blob_oid; + if (git_oid_fromstr(&blob_oid, blob_id.c_str()) != 0) { + (*logger)(fmt::format("blob ID parsing in git repository {} failed " + "with:\n{}", + GetGitCAS()->git_path_.string(), + GitLastError()), + true /*fatal*/); + return std::nullopt; + } + // get blob object + git_blob* blob_ptr = nullptr; + int lookup_res{}; + { + // share the odb lock + std::shared_lock lock{GetGitCAS()->mutex_}; + lookup_res = git_blob_lookup(&blob_ptr, repo_->Ptr(), &blob_oid); + } + git_blob_free(blob_ptr); + if (lookup_res != 0) { + if (lookup_res == GIT_ENOTFOUND) { + return false; // blob not found + } + (*logger)(fmt::format("blob lookup in git repository {} failed " + "with:\n{}", + GetGitCAS()->git_path_.string(), + GitLastError()), + true /*fatal*/); + return std::nullopt; + } + return true; // blob found + } catch (std::exception const& ex) { + Logger::Log( + LogLevel::Error, "check blob exists failed with:\n{}", ex.what()); + return std::nullopt; + } +#endif // BOOTSTRAP_BUILD_TOOL +} + auto GitRepo::TryReadBlob(std::string const& blob_id, anon_logger_ptr const& logger) noexcept -> std::pair<bool, std::optional<std::string>> { diff --git a/src/buildtool/file_system/git_repo.hpp b/src/buildtool/file_system/git_repo.hpp index 9c31f21b..05bcfaf3 100644 --- a/src/buildtool/file_system/git_repo.hpp +++ b/src/buildtool/file_system/git_repo.hpp @@ -234,12 +234,21 @@ class GitRepo { -> std::optional<bool>; /// \brief Check if given blob ID is present in the directory structure of - /// the local repository and try to return it. + /// the local repository. /// Calling it from a fake repository allows thread-safe use. - /// Returns a pair of a success flag, stating that no errors occurred while - /// performing the libgit2 calls, and an optional string containing the - /// content of the blob, if the blob is found. It guarantees the logger is - /// called exactly once with fatal if failure. + /// Returns a status of blob presence, or nullopt if failure. + /// It guarantees the logger is called exactly once with fatal if failure. + [[nodiscard]] auto CheckBlobExists(std::string const& blob_id, + anon_logger_ptr const& logger) noexcept + -> std::optional<bool>; + + /// \brief Check if given blob ID is present in the directory structure + /// of the local repository and try to return it. Calling it from a fake + /// repository allows thread-safe use. Returns a pair of a success flag, + /// stating that no errors occurred while performing the libgit2 calls, + /// and an optional string containing the content of the blob, if the + /// blob is found. It guarantees the logger is called exactly once with + /// fatal if failure. [[nodiscard]] auto TryReadBlob(std::string const& blob_id, anon_logger_ptr const& logger) noexcept -> std::pair<bool, std::optional<std::string>>; |