diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2024-04-04 12:57:31 +0200 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2024-04-10 14:06:21 +0200 |
commit | 1b0aa19f20d102bf96ada7a1e77843eeef20985c (patch) | |
tree | a557bddd21d7482429563aeb2f1720902447920e /src | |
parent | 646d52aac3b5e14ae0c2a552987d84f184345178 (diff) | |
download | justbuild-1b0aa19f20d102bf96ada7a1e77843eeef20985c.tar.gz |
git_repo: Add blob writer method
Also extends the tests accordingly.
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/file_system/git_repo.cpp | 32 | ||||
-rw-r--r-- | src/buildtool/file_system/git_repo.hpp | 9 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/buildtool/file_system/git_repo.cpp b/src/buildtool/file_system/git_repo.cpp index c0774971..c7ecddda 100644 --- a/src/buildtool/file_system/git_repo.cpp +++ b/src/buildtool/file_system/git_repo.cpp @@ -1433,6 +1433,38 @@ auto GitRepo::TryReadBlob(std::string const& blob_id, #endif // BOOTSTRAP_BUILD_TOOL } +auto GitRepo::WriteBlob(std::string const& content, + anon_logger_ptr const& logger) noexcept + -> std::optional<std::string> { +#ifdef BOOTSTRAP_BUILD_TOOL + return std::nullopt; +#else + try { + // preferably with a "fake" repository! + if (not IsRepoFake()) { + Logger::Log(LogLevel::Debug, + "Blob writer called on a real repository"); + } + // share the odb lock + std::shared_lock lock{GetGitCAS()->mutex_}; + + git_oid blob_oid; + if (git_blob_create_from_buffer( + &blob_oid, repo_->Ptr(), content.c_str(), content.size()) != + 0) { + (*logger)(fmt::format("writing blob into database failed with:\n{}", + GitLastError()), + /*fatal=*/true); + return std::nullopt; + } + return std::string{git_oid_tostr_s(&blob_oid)}; + } catch (std::exception const& ex) { + Logger::Log(LogLevel::Error, "write blob failed with:\n{}", ex.what()); + return std::nullopt; + } +#endif // BOOTSTRAP_BUILD_TOOL +} + auto GitRepo::GetObjectByPathFromTree(std::string const& tree_id, std::string const& rel_path) noexcept -> std::optional<TreeEntryInfo> { diff --git a/src/buildtool/file_system/git_repo.hpp b/src/buildtool/file_system/git_repo.hpp index 885ddde1..60353a00 100644 --- a/src/buildtool/file_system/git_repo.hpp +++ b/src/buildtool/file_system/git_repo.hpp @@ -263,6 +263,15 @@ class GitRepo { anon_logger_ptr const& logger) noexcept -> std::pair<bool, std::optional<std::string>>; + /// \brief Write given content as a blob into underlying object database. + /// Calling it from a fake repository allows thread-safe use. + /// \returns Git ID of the written blob, or nullopt on errors. + /// It guarantees the logger is called exactly once with fatal if failure. + /// Use with care, especially for large objects. + [[nodiscard]] auto WriteBlob(std::string const& content, + anon_logger_ptr const& logger) noexcept + -> std::optional<std::string>; + /// \brief Get the object info related to a given path inside a Git tree. /// Unlike GetSubtreeFromTree, we here ignore errors and only return a value /// when all is successful. |