diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-08-29 09:52:47 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-08-30 17:17:09 +0200 |
commit | 98884d6d3f5c31efb3390ab75f1952dcdff0221c (patch) | |
tree | 2fce24d85c77a99c999efeca89d5f8d68db9b88a /src/buildtool/file_system/git_tree.cpp | |
parent | 3a771602f8ee6c7940208d447463061aa9b6af8c (diff) | |
download | justbuild-98884d6d3f5c31efb3390ab75f1952dcdff0221c.tar.gz |
Unify symlink checks in git tree
Diffstat (limited to 'src/buildtool/file_system/git_tree.cpp')
-rw-r--r-- | src/buildtool/file_system/git_tree.cpp | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/src/buildtool/file_system/git_tree.cpp b/src/buildtool/file_system/git_tree.cpp index e7a2f41d..e0b25593 100644 --- a/src/buildtool/file_system/git_tree.cpp +++ b/src/buildtool/file_system/git_tree.cpp @@ -14,7 +14,9 @@ #include "src/buildtool/file_system/git_tree.hpp" +#include <algorithm> #include <sstream> +#include <vector> #include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/logging/log_level.hpp" @@ -55,6 +57,25 @@ namespace { return entry; } +class SymlinksChecker final { + public: + explicit SymlinksChecker(gsl::not_null<GitCASPtr> const& cas) noexcept + : cas_{*cas} {} + + [[nodiscard]] auto operator()( + std::vector<bazel_re::Digest> const& ids) const noexcept -> bool { + return std::all_of( + ids.begin(), ids.end(), [&cas = cas_](bazel_re::Digest const& id) { + auto content = cas.ReadObject(ArtifactDigest(id).hash(), + /*is_hex_id=*/true); + return content.has_value() and PathIsNonUpwards(*content); + }); + }; + + private: + GitCAS const& cas_; +}; + } // namespace auto GitTree::Read(std::filesystem::path const& repo_path, @@ -70,22 +91,11 @@ auto GitTree::Read(std::filesystem::path const& repo_path, auto GitTree::Read(gsl::not_null<GitCASPtr> const& cas, std::string const& tree_id, bool ignore_special) noexcept -> std::optional<GitTree> { - // create symlinks checker - auto check_symlinks = [&cas](std::vector<bazel_re::Digest> const& ids) { - for (auto const& id : ids) { - auto content = - cas->ReadObject(ArtifactDigest(id).hash(), /*is_hex_id=*/true); - if (not content or not PathIsNonUpwards(*content)) { - return false; - } - } - return true; - }; if (auto raw_id = FromHexString(tree_id)) { auto repo = GitRepo::Open(cas); if (repo != std::nullopt) { if (auto entries = repo->ReadTree(*raw_id, - check_symlinks, + SymlinksChecker{cas}, /*is_hex_id=*/false, ignore_special)) { // NOTE: the raw_id value is NOT recomputed when @@ -146,20 +156,9 @@ auto GitTreeEntry::Tree(bool ignore_special) const& noexcept if (repo == std::nullopt) { return std::nullopt; } - // create symlinks checker - auto check_symlinks = - [cas = cas_](std::vector<bazel_re::Digest> const& ids) { - for (auto const& id : ids) { - auto content = cas->ReadObject( - ArtifactDigest(id).hash(), /*is_hex_id=*/true); - if (not content or not PathIsNonUpwards(*content)) { - return false; - } - } - return true; - }; + if (auto entries = repo->ReadTree(raw_id_, - check_symlinks, + SymlinksChecker{cas_}, /*is_hex_id=*/false, ignore_special)) { return GitTree::FromEntries( |