diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/other_tools/just_mr/TARGETS | 1 | ||||
-rw-r--r-- | src/other_tools/just_mr/fetch.cpp | 103 | ||||
-rw-r--r-- | src/other_tools/repo_map/TARGETS | 1 | ||||
-rw-r--r-- | src/other_tools/repo_map/repos_to_setup_map.cpp | 84 | ||||
-rw-r--r-- | src/other_tools/utils/TARGETS | 13 | ||||
-rw-r--r-- | src/other_tools/utils/parse_git_tree.cpp | 101 | ||||
-rw-r--r-- | src/other_tools/utils/parse_git_tree.hpp | 30 |
7 files changed, 160 insertions, 173 deletions
diff --git a/src/other_tools/just_mr/TARGETS b/src/other_tools/just_mr/TARGETS index 5d03675a..ca433364 100644 --- a/src/other_tools/just_mr/TARGETS +++ b/src/other_tools/just_mr/TARGETS @@ -134,6 +134,7 @@ , ["src/other_tools/ops_maps", "git_tree_fetch_map"] , ["src/other_tools/ops_maps", "import_to_git_map"] , ["src/other_tools/utils", "parse_archive"] + , ["src/other_tools/utils", "parse_git_tree"] , "setup_utils" , ["src/buildtool/execution_api/common", "common"] , ["src/buildtool/execution_api/common", "api_bundle"] diff --git a/src/other_tools/just_mr/fetch.cpp b/src/other_tools/just_mr/fetch.cpp index c1d65010..658e9988 100644 --- a/src/other_tools/just_mr/fetch.cpp +++ b/src/other_tools/just_mr/fetch.cpp @@ -46,6 +46,7 @@ #include "src/other_tools/ops_maps/git_tree_fetch_map.hpp" #include "src/other_tools/ops_maps/import_to_git_map.hpp" #include "src/other_tools/utils/parse_archive.hpp" +#include "src/other_tools/utils/parse_git_tree.hpp" auto MultiRepoFetch(std::shared_ptr<Configuration> const& config, MultiRepoCommonArguments const& common_args, @@ -272,107 +273,17 @@ auto MultiRepoFetch(std::shared_ptr<Configuration> const& config, // explicitly told to fetch absent archives if (not pragma_absent_value or common_args.fetch_absent) { // enforce mandatory fields - auto repo_desc_hash = (*resolved_repo_desc)->At("id"); - if (not repo_desc_hash) { + auto tree_info = + ParseGitTree(*resolved_repo_desc, repo_name); + if (not tree_info) { Logger::Log( LogLevel::Error, - "Config: Mandatory field \"id\" is missing"); + fmt::format("Config: {}", + std::move(tree_info).error())); return kExitFetchError; } - if (not repo_desc_hash->get()->IsString()) { - Logger::Log( - LogLevel::Error, - fmt::format("Config: Unsupported value {} for " - "mandatory field \"id\"", - repo_desc_hash->get()->ToString())); - return kExitFetchError; - } - auto repo_desc_cmd = (*resolved_repo_desc)->At("cmd"); - if (not repo_desc_cmd) { - Logger::Log( - LogLevel::Error, - "Config: Mandatory field \"cmd\" is missing"); - return kExitFetchError; - } - if (not repo_desc_cmd->get()->IsList()) { - Logger::Log( - LogLevel::Error, - fmt::format("Config: Unsupported value {} for " - "mandatory field \"cmd\"", - repo_desc_cmd->get()->ToString())); - return kExitFetchError; - } - std::vector<std::string> cmd{}; - for (auto const& token : repo_desc_cmd->get()->List()) { - if (token.IsNotNull() and token->IsString()) { - cmd.emplace_back(token->String()); - } - else { - Logger::Log( - LogLevel::Error, - fmt::format("Config: Unsupported entry {} " - "in mandatory field \"cmd\"", - token->ToString())); - return kExitFetchError; - } - } - std::map<std::string, std::string> env{}; - auto repo_desc_env = - (*resolved_repo_desc) - ->Get("env", Expression::none_t{}); - if (repo_desc_env.IsNotNull() and - repo_desc_env->IsMap()) { - for (auto const& envar : - repo_desc_env->Map().Items()) { - if (envar.second.IsNotNull() and - envar.second->IsString()) { - env.insert( - {envar.first, envar.second->String()}); - } - else { - Logger::Log( - LogLevel::Error, - fmt::format( - "Config: Unsupported value {} for " - "key {} in optional field \"envs\"", - envar.second->ToString(), - nlohmann::json(envar.first) - .dump())); - return kExitFetchError; - } - } - } - std::vector<std::string> inherit_env{}; - auto repo_desc_inherit_env = - (*resolved_repo_desc) - ->Get("inherit env", Expression::none_t{}); - if (repo_desc_inherit_env.IsNotNull() and - repo_desc_inherit_env->IsList()) { - for (auto const& envvar : - repo_desc_inherit_env->List()) { - if (envvar->IsString()) { - inherit_env.emplace_back(envvar->String()); - } - else { - Logger::Log( - LogLevel::Error, - fmt::format("Config: Not a variable " - "name in the specification " - "of \"inherit env\": {}", - envvar->ToString())); - return kExitFetchError; - } - } - } - // populate struct - GitTreeInfo tree_info = { - .hash = repo_desc_hash->get()->String(), - .env_vars = std::move(env), - .inherit_env = std::move(inherit_env), - .command = std::move(cmd), - .origin = repo_name}; // add to list - git_trees_to_fetch.emplace_back(std::move(tree_info)); + git_trees_to_fetch.emplace_back(*std::move(tree_info)); } } break; default: diff --git a/src/other_tools/repo_map/TARGETS b/src/other_tools/repo_map/TARGETS index 428b8981..dd938a5b 100644 --- a/src/other_tools/repo_map/TARGETS +++ b/src/other_tools/repo_map/TARGETS @@ -25,6 +25,7 @@ , ["src/other_tools/ops_maps", "content_cas_map"] , ["src/other_tools/ops_maps", "git_tree_fetch_map"] , ["src/other_tools/utils", "parse_archive"] + , ["src/other_tools/utils", "parse_git_tree"] , ["src/buildtool/crypto", "hash_function"] ] } diff --git a/src/other_tools/repo_map/repos_to_setup_map.cpp b/src/other_tools/repo_map/repos_to_setup_map.cpp index 29a0715c..02ee6311 100644 --- a/src/other_tools/repo_map/repos_to_setup_map.cpp +++ b/src/other_tools/repo_map/repos_to_setup_map.cpp @@ -27,6 +27,7 @@ #include "src/other_tools/ops_maps/content_cas_map.hpp" #include "src/other_tools/ops_maps/git_tree_fetch_map.hpp" #include "src/other_tools/utils/parse_archive.hpp" +#include "src/other_tools/utils/parse_git_tree.hpp" namespace { @@ -568,81 +569,13 @@ void GitTreeCheckout(ExpressionPtr const& repo_desc, gsl::not_null<TaskSystem*> const& ts, ReposToSetupMap::SetterPtr const& setter, ReposToSetupMap::LoggerPtr const& logger) { - // enforce mandatory fields - auto repo_desc_hash = repo_desc->At("id"); - if (not repo_desc_hash) { - (*logger)("GitTreeCheckout: Mandatory field \"id\" is missing", - /*fatal=*/true); - return; - } - if (not repo_desc_hash->get()->IsString()) { - (*logger)(fmt::format("GitTreeCheckout: Unsupported value {} for " - "mandatory field \"id\"", - repo_desc_hash->get()->ToString()), - /*fatal=*/true); - return; - } - auto repo_desc_cmd = repo_desc->At("cmd"); - if (not repo_desc_cmd) { - (*logger)("GitTreeCheckout: Mandatory field \"cmd\" is missing", - /*fatal=*/true); - return; - } - if (not repo_desc_cmd->get()->IsList()) { - (*logger)(fmt::format("GitTreeCheckout: Unsupported value {} for " - "mandatory field \"cmd\"", - repo_desc_cmd->get()->ToString()), - /*fatal=*/true); + auto tree_info = ParseGitTree(repo_desc); + if (not tree_info) { + (*logger)( + fmt::format("GitTreeCheckout: {}", std::move(tree_info).error()), + /*fatal=*/true); return; } - std::vector<std::string> cmd{}; - for (auto const& token : repo_desc_cmd->get()->List()) { - if (token.IsNotNull() and token->IsString()) { - cmd.emplace_back(token->String()); - } - else { - (*logger)(fmt::format("GitTreeCheckout: Unsupported entry {} in " - "mandatory field \"cmd\"", - token->ToString()), - /*fatal=*/true); - return; - } - } - std::map<std::string, std::string> env{}; - auto repo_desc_env = repo_desc->Get("env", Expression::none_t{}); - if (repo_desc_env.IsNotNull() and repo_desc_env->IsMap()) { - for (auto const& envar : repo_desc_env->Map().Items()) { - if (envar.second.IsNotNull() and envar.second->IsString()) { - env.insert({envar.first, envar.second->String()}); - } - else { - (*logger)(fmt::format("GitTreeCheckout: Unsupported value {} " - "for key {} in optional field \"envs\"", - envar.second->ToString(), - nlohmann::json(envar.first).dump()), - /*fatal=*/true); - return; - } - } - } - std::vector<std::string> inherit_env{}; - auto repo_desc_inherit_env = - repo_desc->Get("inherit env", Expression::none_t{}); - if (repo_desc_inherit_env.IsNotNull() and repo_desc_inherit_env->IsList()) { - for (auto const& envvar : repo_desc_inherit_env->List()) { - if (envvar->IsString()) { - inherit_env.emplace_back(envvar->String()); - } - else { - (*logger)( - fmt::format("GitTreeCheckout: Not a variable name in the " - "specification of \"inherit env\": {}", - envvar->ToString()), - /*fatal=*/true); - return; - } - } - } // check "special" pragma auto repo_desc_pragma = repo_desc->At("pragma"); bool const& pragma_is_map = @@ -663,10 +596,7 @@ void GitTreeCheckout(ExpressionPtr const& repo_desc, pragma_absent->get()->Bool(); // populate struct TreeIdInfo tree_id_info = { - .tree_info = GitTreeInfo{.hash = repo_desc_hash->get()->String(), - .env_vars = std::move(env), - .inherit_env = std::move(inherit_env), - .command = std::move(cmd)}, + .tree_info = *std::move(tree_info), .ignore_special = pragma_special_value == PragmaSpecial::Ignore, .absent = not fetch_absent and pragma_absent_value}; // get the WS root as git tree diff --git a/src/other_tools/utils/TARGETS b/src/other_tools/utils/TARGETS index 9d4e5734..0987e6e5 100644 --- a/src/other_tools/utils/TARGETS +++ b/src/other_tools/utils/TARGETS @@ -65,4 +65,17 @@ , "private-deps": [["@", "fmt", "", "fmt"]] , "stage": ["src", "other_tools", "utils"] } +, "parse_git_tree": + { "type": ["@", "rules", "CC", "library"] + , "name": ["parse_git_tree"] + , "hdrs": ["parse_git_tree.hpp"] + , "srcs": ["parse_git_tree.cpp"] + , "deps": + [ ["src/buildtool/build_engine/expression", "expression"] + , ["src/other_tools/ops_maps", "git_tree_fetch_map"] + , ["src/utils/cpp", "expected"] + ] + , "private-deps": [["@", "fmt", "", "fmt"]] + , "stage": ["src", "other_tools", "utils"] + } } diff --git a/src/other_tools/utils/parse_git_tree.cpp b/src/other_tools/utils/parse_git_tree.cpp new file mode 100644 index 00000000..dde759b9 --- /dev/null +++ b/src/other_tools/utils/parse_git_tree.cpp @@ -0,0 +1,101 @@ +// 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. + +#include "src/other_tools/utils/parse_git_tree.hpp" + +#include <map> +#include <utility> // std::move +#include <vector> + +#include "fmt/core.h" + +[[nodiscard]] auto ParseGitTree(ExpressionPtr const& repo_desc, + std::optional<std::string> origin) + -> expected<GitTreeInfo, std::string> { + auto repo_desc_hash = repo_desc->At("id"); + if (not repo_desc_hash) { + return unexpected<std::string>{"Mandatory field \"id\" is missing"}; + } + if (not repo_desc_hash->get()->IsString()) { + return unexpected{ + fmt::format("Unsupported value {} for " + "mandatory field \"id\"", + repo_desc_hash->get()->ToString())}; + } + + auto repo_desc_cmd = repo_desc->At("cmd"); + if (not repo_desc_cmd) { + return unexpected<std::string>{"Mandatory field \"cmd\" is missing"}; + } + if (not repo_desc_cmd->get()->IsList()) { + return unexpected{ + fmt::format("Unsupported value {} for " + "mandatory field \"cmd\"", + repo_desc_cmd->get()->ToString())}; + } + std::vector<std::string> cmd{}; + for (auto const& token : repo_desc_cmd->get()->List()) { + if (token.IsNotNull() and token->IsString()) { + cmd.emplace_back(token->String()); + } + else { + return unexpected{ + fmt::format("Unsupported entry {} " + "in mandatory field \"cmd\"", + token->ToString())}; + } + } + std::map<std::string, std::string> env{}; + auto repo_desc_env = repo_desc->Get("env", Expression::none_t{}); + if (repo_desc_env.IsNotNull() and repo_desc_env->IsMap()) { + for (auto const& envar : repo_desc_env->Map().Items()) { + if (envar.second.IsNotNull() and envar.second->IsString()) { + env.insert({envar.first, envar.second->String()}); + } + else { + return unexpected{ + fmt::format("Unsupported value {} for " + "key {} in optional field \"envs\"", + envar.second->ToString(), + nlohmann::json(envar.first).dump())}; + } + } + } + std::vector<std::string> inherit_env{}; + auto repo_desc_inherit_env = + repo_desc->Get("inherit env", Expression::none_t{}); + if (repo_desc_inherit_env.IsNotNull() and repo_desc_inherit_env->IsList()) { + for (auto const& envvar : repo_desc_inherit_env->List()) { + if (envvar->IsString()) { + inherit_env.emplace_back(envvar->String()); + } + else { + return unexpected{ + fmt::format("Not a variable " + "name in the specification " + "of \"inherit env\": {}", + envvar->ToString())}; + } + } + } + // populate struct + auto info = GitTreeInfo{.hash = repo_desc_hash->get()->String(), + .env_vars = std::move(env), + .inherit_env = std::move(inherit_env), + .command = std::move(cmd)}; + if (origin) { + info.origin = *std::move(origin); + } + return info; +} diff --git a/src/other_tools/utils/parse_git_tree.hpp b/src/other_tools/utils/parse_git_tree.hpp new file mode 100644 index 00000000..5aab1a5b --- /dev/null +++ b/src/other_tools/utils/parse_git_tree.hpp @@ -0,0 +1,30 @@ +// 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_OTHER_TOOLS_UTILS_PARSE_GIT_TREE_HPP +#define INCLUDED_SRC_OTHER_TOOLS_UTILS_PARSE_GIT_TREE_HPP + +#include <optional> +#include <string> + +#include "src/buildtool/build_engine/expression/expression.hpp" +#include "src/other_tools/ops_maps/git_tree_fetch_map.hpp" +#include "src/utils/cpp/expected.hpp" + +[[nodiscard]] auto ParseGitTree( + ExpressionPtr const& repo_desc, + std::optional<std::string> origin = std::nullopt) + -> expected<GitTreeInfo, std::string>; + +#endif // INCLUDED_SRC_OTHER_TOOLS_UTILS_PARSE_GIT_TREE_HPP |