summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaksim Denisov <denisov.maksim@huawei.com>2024-09-10 18:16:39 +0200
committerMaksim Denisov <denisov.maksim@huawei.com>2024-09-13 14:41:00 +0200
commited8f24fb246142ffaa88707ae86b53c34df82986 (patch)
tree302cd1fef8b8847ceeffd5d5d2c5b33539eabc88 /src
parent76ab9217628084c661176118d5a8020a3366f04c (diff)
downloadjustbuild-ed8f24fb246142ffaa88707ae86b53c34df82986.tar.gz
Move to GitHashesConverter functionality related to conversion of git hashes
...from Compatibility.
Diffstat (limited to 'src')
-rw-r--r--src/buildtool/common/TARGETS11
-rw-r--r--src/buildtool/common/git_hashes_converter.hpp84
-rw-r--r--src/buildtool/compatibility/TARGETS5
-rw-r--r--src/buildtool/compatibility/compatibility.hpp57
-rw-r--r--src/buildtool/execution_engine/executor/TARGETS1
-rw-r--r--src/buildtool/execution_engine/executor/executor.hpp4
-rw-r--r--src/buildtool/file_system/TARGETS1
-rw-r--r--src/buildtool/file_system/file_root.hpp6
8 files changed, 106 insertions, 63 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS
index 6e768cee..e20f2996 100644
--- a/src/buildtool/common/TARGETS
+++ b/src/buildtool/common/TARGETS
@@ -181,4 +181,15 @@
, ["src/buildtool/logging", "logging"]
]
}
+, "git_hashes_converter":
+ { "type": ["@", "rules", "CC", "library"]
+ , "name": ["git_hashes_converter"]
+ , "hdrs": ["git_hashes_converter.hpp"]
+ , "deps":
+ [ ["src/buildtool/logging", "log_level"]
+ , ["src/buildtool/logging", "logging"]
+ , ["src/buildtool/crypto", "hash_function"]
+ ]
+ , "stage": ["src", "buildtool", "common"]
+ }
}
diff --git a/src/buildtool/common/git_hashes_converter.hpp b/src/buildtool/common/git_hashes_converter.hpp
new file mode 100644
index 00000000..c856b749
--- /dev/null
+++ b/src/buildtool/common/git_hashes_converter.hpp
@@ -0,0 +1,84 @@
+// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef INCLUDED_SRC_BUILDTOOL_COMMON_GIT_HASHES_CONVERTER_HPP
+#define INCLUDED_SRC_BUILDTOOL_COMMON_GIT_HASHES_CONVERTER_HPP
+
+#include <mutex> //std::unique_lock
+#include <optional>
+#include <shared_mutex>
+#include <string>
+#include <unordered_map>
+#include <utility>
+
+#include "src/buildtool/crypto/hash_function.hpp"
+#include "src/buildtool/logging/log_level.hpp"
+#include "src/buildtool/logging/logger.hpp"
+
+class GitHashesConverter final {
+ using git_hash = std::string;
+ using compat_hash = std::string;
+ using git_repo = std::string;
+ using GitToCompatibleMap = std::unordered_map<git_hash, compat_hash>;
+ using CompatibleToGitMap =
+ std::unordered_map<compat_hash, std::pair<git_hash, git_repo>>;
+
+ public:
+ [[nodiscard]] static auto Instance() noexcept -> GitHashesConverter& {
+ static GitHashesConverter instance;
+ return instance;
+ }
+
+ [[nodiscard]] auto RegisterGitEntry(std::string const& git_hash,
+ std::string const& data,
+ std::string const& repo)
+ -> compat_hash {
+ {
+ std::shared_lock lock_{mutex_};
+ auto it = git_to_compatible_.find(git_hash);
+ if (it != git_to_compatible_.end()) {
+ return it->second;
+ }
+ }
+ // This is only used in compatible mode.
+ HashFunction const hash_function{HashFunction::Type::PlainSHA256};
+ auto compatible_hash = hash_function.PlainHashData(data).HexString();
+ std::unique_lock lock_{mutex_};
+ git_to_compatible_[git_hash] = compatible_hash;
+ compatible_to_git_[compatible_hash] = {git_hash, repo};
+ return compatible_hash;
+ }
+
+ [[nodiscard]] auto GetGitEntry(std::string const& compatible_hash)
+ -> std::optional<std::pair<git_hash const&, git_repo const&>> {
+ std::shared_lock lock_{mutex_};
+ auto it = compatible_to_git_.find(compatible_hash);
+ if (it != compatible_to_git_.end()) {
+ return it->second;
+ }
+ Logger::Log(LogLevel::Warning,
+ "Unable to get the git-sha1 code associated to {}",
+ compatible_hash);
+ return std::nullopt;
+ }
+
+ private:
+ explicit GitHashesConverter() noexcept = default;
+
+ GitToCompatibleMap git_to_compatible_{};
+ CompatibleToGitMap compatible_to_git_{};
+ std::shared_mutex mutex_;
+};
+
+#endif // INCLUDED_SRC_BUILDTOOL_COMMON_GIT_HASHES_CONVERTER_HPP \ No newline at end of file
diff --git a/src/buildtool/compatibility/TARGETS b/src/buildtool/compatibility/TARGETS
index e585efa6..1bb56609 100644
--- a/src/buildtool/compatibility/TARGETS
+++ b/src/buildtool/compatibility/TARGETS
@@ -2,11 +2,6 @@
{ "type": ["@", "rules", "CC", "library"]
, "name": ["compatibility"]
, "hdrs": ["compatibility.hpp"]
- , "deps":
- [ ["src/buildtool/crypto", "hash_function"]
- , ["src/buildtool/logging", "log_level"]
- , ["src/buildtool/logging", "logging"]
- ]
, "stage": ["src", "buildtool", "compatibility"]
}
}
diff --git a/src/buildtool/compatibility/compatibility.hpp b/src/buildtool/compatibility/compatibility.hpp
index e276c562..5e6b6703 100644
--- a/src/buildtool/compatibility/compatibility.hpp
+++ b/src/buildtool/compatibility/compatibility.hpp
@@ -14,21 +14,8 @@
#ifndef INCLUDED_SRC_BUILDTOOL_COMPATIBILITY_COMPATIBILITY_HPP
#define INCLUDED_SRC_BUILDTOOL_COMPATIBILITY_COMPATIBILITY_HPP
-#include <shared_mutex>
-#include <unordered_map>
-#include <utility>
-
-#include "src/buildtool/crypto/hash_function.hpp"
-#include "src/buildtool/logging/log_level.hpp"
-#include "src/buildtool/logging/logger.hpp"
-class Compatibility {
- using git_hash = std::string;
- using compat_hash = std::string;
- using git_repo = std::string;
- using GitToCompatibleMap = std::unordered_map<git_hash, compat_hash>;
- using CompatibleToGitMap =
- std::unordered_map<compat_hash, std::pair<git_hash, git_repo>>;
+class Compatibility final {
public:
[[nodiscard]] static auto Instance() noexcept -> Compatibility& {
static Compatibility instance{};
@@ -41,47 +28,7 @@ class Compatibility {
Instance().compatible_ = value;
}
- [[nodiscard]] static auto RegisterGitEntry(std::string const& git_hash,
- std::string const& data,
- std::string const& repo)
- -> compat_hash {
-
- {
- auto& git_to_compatible = Instance().git_to_compatible_;
- std::shared_lock lock_{Instance().mutex_};
- auto it = git_to_compatible.find(git_hash);
- if (it != git_to_compatible.end()) {
- return it->second;
- }
- }
- // This is only used in compatible mode.
- HashFunction const hash_function{HashFunction::Type::PlainSHA256};
- auto compatible_hash = hash_function.PlainHashData(data).HexString();
- std::unique_lock lock_{Instance().mutex_};
- Instance().git_to_compatible_[git_hash] = compatible_hash;
- Instance().compatible_to_git_[compatible_hash] = {git_hash, repo};
- return compatible_hash;
- }
-
- [[nodiscard]] static auto GetGitEntry(std::string const& compatible_hash)
- -> std::optional<std::pair<git_hash const&, git_repo const&>> {
- auto const& compatible_to_git = Instance().compatible_to_git_;
- std::shared_lock lock_{Instance().mutex_};
- auto it = compatible_to_git.find(compatible_hash);
- if (it != compatible_to_git.end()) {
- return it->second;
- }
- Logger::Log(
- LogLevel::Warning,
- fmt::format("Unable to get the git-sha1 code associated to {}",
- compatible_hash));
- return std::nullopt;
- }
-
private:
- GitToCompatibleMap git_to_compatible_{};
- CompatibleToGitMap compatible_to_git_{};
- bool compatible_{false};
- std::shared_mutex mutex_;
+ bool compatible_ = false;
};
#endif // INCLUDED_SRC_BUILDTOOL_COMPATIBILITY_COMPATIBILITY_HPP
diff --git a/src/buildtool/execution_engine/executor/TARGETS b/src/buildtool/execution_engine/executor/TARGETS
index fb407a8d..f24030b2 100644
--- a/src/buildtool/execution_engine/executor/TARGETS
+++ b/src/buildtool/execution_engine/executor/TARGETS
@@ -26,6 +26,7 @@
, ["@", "gsl", "", "gsl"]
, ["src/buildtool/common", "common"]
, ["src/buildtool/crypto", "hash_function"]
+ , ["src/buildtool/common", "git_hashes_converter"]
]
, "stage": ["src", "buildtool", "execution_engine", "executor"]
}
diff --git a/src/buildtool/execution_engine/executor/executor.hpp b/src/buildtool/execution_engine/executor/executor.hpp
index 556e47c0..1d94d2ca 100644
--- a/src/buildtool/execution_engine/executor/executor.hpp
+++ b/src/buildtool/execution_engine/executor/executor.hpp
@@ -30,6 +30,7 @@
#include "gsl/gsl"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/common/artifact_digest_factory.hpp"
+#include "src/buildtool/common/git_hashes_converter.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/common/tree.hpp"
#include "src/buildtool/compatibility/compatibility.hpp"
@@ -437,7 +438,8 @@ class ExecutorImpl {
gsl::not_null<const RepositoryConfig*> const& repo_config,
Artifact::ObjectInfo const& info) noexcept -> bool {
if (Compatibility::IsCompatible()) {
- auto opt = Compatibility::GetGitEntry(info.digest.hash());
+ auto opt =
+ GitHashesConverter::Instance().GetGitEntry(info.digest.hash());
if (opt) {
auto const& [git_sha1_hash, comp_repo] = *opt;
return VerifyOrUploadGitArtifact(
diff --git a/src/buildtool/file_system/TARGETS b/src/buildtool/file_system/TARGETS
index b0c0acbd..9d5be7a3 100644
--- a/src/buildtool/file_system/TARGETS
+++ b/src/buildtool/file_system/TARGETS
@@ -179,6 +179,7 @@
, ["@", "json", "", "json"]
, ["src/utils/cpp", "concepts"]
, ["src/utils/cpp", "json"]
+ , ["src/buildtool/common", "git_hashes_converter"]
]
, "stage": ["src", "buildtool", "file_system"]
}
diff --git a/src/buildtool/file_system/file_root.hpp b/src/buildtool/file_system/file_root.hpp
index d7f0c000..381f5c23 100644
--- a/src/buildtool/file_system/file_root.hpp
+++ b/src/buildtool/file_system/file_root.hpp
@@ -28,6 +28,7 @@
#include "src/buildtool/common/artifact_description.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/common/artifact_digest_factory.hpp"
+#include "src/buildtool/common/git_hashes_converter.hpp"
#include "src/buildtool/compatibility/compatibility.hpp"
#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
@@ -584,8 +585,9 @@ class FileRoot {
file_path)) {
if (entry->IsBlob()) {
if (Compatibility::IsCompatible()) {
- auto compatible_hash = Compatibility::RegisterGitEntry(
- entry->Hash(), *entry->Blob(), repository);
+ auto compatible_hash =
+ GitHashesConverter::Instance().RegisterGitEntry(
+ entry->Hash(), *entry->Blob(), repository);
auto digest = ArtifactDigestFactory::Create(
HashFunction::Type::PlainSHA256,
compatible_hash,