diff options
Diffstat (limited to 'src/buildtool/common')
-rw-r--r-- | src/buildtool/common/TARGETS | 9 | ||||
-rw-r--r-- | src/buildtool/common/artifact_digest_factory.cpp | 6 | ||||
-rw-r--r-- | src/buildtool/common/bazel_digest_factory.cpp | 3 | ||||
-rw-r--r-- | src/buildtool/common/protocol_traits.hpp | 12 |
4 files changed, 25 insertions, 5 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS index 1f280d78..4372db88 100644 --- a/src/buildtool/common/TARGETS +++ b/src/buildtool/common/TARGETS @@ -52,7 +52,7 @@ , ["src/utils/cpp", "gsl"] , ["src/utils/cpp", "expected"] ] - , "private-deps": [["src/buildtool/crypto", "hasher"]] + , "private-deps": [["src/buildtool/crypto", "hasher"], "protocol_traits"] , "stage": ["src", "buildtool", "common"] } , "artifact_digest_factory": @@ -68,7 +68,11 @@ , ["src/utils/cpp", "expected"] ] , "private-deps": - ["bazel_digest_factory", "bazel_types", ["@", "gsl", "", "gsl"]] + [ "bazel_digest_factory" + , "bazel_types" + , ["@", "gsl", "", "gsl"] + , "protocol_traits" + ] , "stage": ["src", "buildtool", "common"] } , "common": @@ -196,6 +200,7 @@ { "type": ["@", "rules", "CC", "library"] , "name": ["protocol_traits"] , "hdrs": ["protocol_traits.hpp"] + , "deps": [["src/buildtool/crypto", "hash_function"]] , "stage": ["src", "buildtool", "common"] } } diff --git a/src/buildtool/common/artifact_digest_factory.cpp b/src/buildtool/common/artifact_digest_factory.cpp index dd9da28d..f3ea30c1 100644 --- a/src/buildtool/common/artifact_digest_factory.cpp +++ b/src/buildtool/common/artifact_digest_factory.cpp @@ -17,15 +17,17 @@ #include "gsl/gsl" #include "src/buildtool/common/bazel_digest_factory.hpp" #include "src/buildtool/common/bazel_types.hpp" +#include "src/buildtool/common/protocol_traits.hpp" auto ArtifactDigestFactory::Create(HashFunction::Type hash_type, std::string hash, std::size_t size, bool is_tree) noexcept -> expected<ArtifactDigest, std::string> { - const bool kTreesAllowed = hash_type == HashFunction::Type::GitSHA1; auto hash_info = - HashInfo::Create(hash_type, std::move(hash), kTreesAllowed and is_tree); + HashInfo::Create(hash_type, + std::move(hash), + ProtocolTraits::IsTreeAllowed(hash_type) and is_tree); if (not hash_info) { return unexpected{std::move(hash_info).error()}; } diff --git a/src/buildtool/common/bazel_digest_factory.cpp b/src/buildtool/common/bazel_digest_factory.cpp index 4ff885aa..3a1f8519 100644 --- a/src/buildtool/common/bazel_digest_factory.cpp +++ b/src/buildtool/common/bazel_digest_factory.cpp @@ -16,12 +16,13 @@ #include <utility> +#include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/crypto/hasher.hpp" auto BazelDigestFactory::Create(HashInfo const& hash_info, std::int64_t size) noexcept -> bazel_re::Digest { - auto hash = hash_info.HashType() == HashFunction::Type::GitSHA1 + auto hash = ProtocolTraits::IsNative(hash_info.HashType()) ? Prefix(hash_info.Hash(), hash_info.IsTree()) : hash_info.Hash(); diff --git a/src/buildtool/common/protocol_traits.hpp b/src/buildtool/common/protocol_traits.hpp index 49850233..f1a51cbe 100644 --- a/src/buildtool/common/protocol_traits.hpp +++ b/src/buildtool/common/protocol_traits.hpp @@ -15,6 +15,8 @@ #ifndef INCLUDED_SRC_BUILDTOOL_COMMON_PROTOCOL_TRAITS_HPP #define INCLUDED_SRC_BUILDTOOL_COMMON_PROTOCOL_TRAITS_HPP +#include "src/buildtool/crypto/hash_function.hpp" + class ProtocolTraits final { public: [[nodiscard]] static auto Instance() noexcept -> ProtocolTraits& { @@ -26,6 +28,16 @@ class ProtocolTraits final { } void SetCompatible(bool value = true) noexcept { compatible_ = value; } + inline static constexpr auto IsNative(HashFunction::Type hash_type) noexcept + -> bool { + return hash_type == HashFunction::Type::GitSHA1; + } + + inline static constexpr auto IsTreeAllowed( + HashFunction::Type hash_type) noexcept -> bool { + return IsNative(hash_type); + } + private: bool compatible_ = false; }; |