diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2023-09-25 15:01:52 +0200 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2023-11-02 12:06:50 +0100 |
commit | 1f997bfa62e5feac45f84fbc2ae7ee62ffd40f3d (patch) | |
tree | 41dead54bc263add0d304f98f68f20e7b7869ec0 /src/buildtool/file_system/git_repo.cpp | |
parent | 9b32ad44261d5c2ca25c21d879d9dad6b451583f (diff) | |
download | justbuild-1f997bfa62e5feac45f84fbc2ae7ee62ffd40f3d.tar.gz |
GitRepo: Add blob existence checker
Also fixes a small typo in tree existence checker log messages.
Diffstat (limited to 'src/buildtool/file_system/git_repo.cpp')
-rw-r--r-- | src/buildtool/file_system/git_repo.cpp | 67 |
1 files changed, 63 insertions, 4 deletions
diff --git a/src/buildtool/file_system/git_repo.cpp b/src/buildtool/file_system/git_repo.cpp index a989c608..1b9eaf1f 100644 --- a/src/buildtool/file_system/git_repo.cpp +++ b/src/buildtool/file_system/git_repo.cpp @@ -1127,7 +1127,7 @@ auto GitRepo::CheckTreeExists(std::string const& tree_id, // preferably with a "fake" repository! if (not IsRepoFake()) { Logger::Log(LogLevel::Debug, - "Subtree lookup called on a real repository"); + "Tree lookup called on a real repository"); } // get git oid git_oid tree_oid; @@ -1161,14 +1161,73 @@ auto GitRepo::CheckTreeExists(std::string const& tree_id, } return true; // tree found } catch (std::exception const& ex) { - Logger::Log(LogLevel::Error, - "check subtree exists failed with:\n{}", - ex.what()); + Logger::Log( + LogLevel::Error, "check tree 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>> { +#ifdef BOOTSTRAP_BUILD_TOOL + return std::pair(false, 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::pair(false, 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 std::pair(true, std::nullopt); // blob not found + } + (*logger)(fmt::format("blob lookup in git repository {} failed " + "with:\n{}", + GetGitCAS()->git_path_.string(), + GitLastError()), + true /*fatal*/); + return std::pair(false, std::nullopt); + } + // get data of found blob + if (auto data = GetGitCAS()->ReadObject(blob_id, /*is_hex_id=*/true)) { + return std::pair(true, std::move(*data)); + } + (*logger)(fmt::format( + "failed to read target for blob {} in git repository {}", + blob_id, + GetGitCAS()->git_path_.string()), + true /*fatal*/); + return std::pair(false, std::nullopt); + } catch (std::exception const& ex) { + Logger::Log( + LogLevel::Error, "check blob exists failed with:\n{}", ex.what()); + return std::pair(false, std::nullopt); + } +#endif // BOOTSTRAP_BUILD_TOOL +} + auto GitRepo::GetObjectByPathFromTree(std::string const& tree_id, std::string const& rel_path) noexcept -> std::optional<TreeEntryInfo> { |