From 541ba9586b8a3aabe90f2dd01d994428dfab4cb9 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Mon, 13 Nov 2023 12:17:53 +0100 Subject: "git tree" repositories: honor "inherit env" In the specification of the action generating a fixed git tree, also honor "inherit env", i.e., inherit the environment variables specified in this field from the environment just-mr is invoked in. As the expected output is fixed ahead of time anyway, this lack of isolation does not affect correctness. --- src/other_tools/repo_map/repos_to_setup_map.cpp | 19 +++++++++++++++++++ src/other_tools/root_maps/tree_id_git_map.cpp | 16 +++++++++++----- src/other_tools/root_maps/tree_id_git_map.hpp | 2 ++ 3 files changed, 32 insertions(+), 5 deletions(-) (limited to 'src') 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 a72903d1..a65a6c50 100644 --- a/src/other_tools/repo_map/repos_to_setup_map.cpp +++ b/src/other_tools/repo_map/repos_to_setup_map.cpp @@ -611,6 +611,24 @@ void GitTreeCheckout(ExpressionPtr const& repo_desc, } } } + std::vector 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"); auto pragma_special = repo_desc_pragma @@ -632,6 +650,7 @@ void GitTreeCheckout(ExpressionPtr const& repo_desc, TreeIdInfo tree_id_info = { .hash = repo_desc_hash->get()->String(), .env_vars = std::move(env), + .inherit_env = std::move(inherit_env), .command = std::move(cmd), .ignore_special = pragma_special_value == PragmaSpecial::Ignore, .absent = not fetch_absent and pragma_absent_value}; diff --git a/src/other_tools/root_maps/tree_id_git_map.cpp b/src/other_tools/root_maps/tree_id_git_map.cpp index a8db186a..ce2fe5e5 100644 --- a/src/other_tools/root_maps/tree_id_git_map.cpp +++ b/src/other_tools/root_maps/tree_id_git_map.cpp @@ -14,6 +14,8 @@ #include "src/other_tools/root_maps/tree_id_git_map.hpp" +#include + #include "fmt/core.h" #include "src/buildtool/execution_api/common/execution_common.hpp" #include "src/buildtool/file_system/file_root.hpp" @@ -167,11 +169,15 @@ auto CreateTreeIdGitMap( std::copy(key.command.begin(), key.command.end(), std::back_inserter(cmdline)); - auto const command_output = - system.Execute(cmdline, - key.env_vars, - tmp_dir->GetPath(), - out_dir->GetPath()); + std::map env{key.env_vars}; + for (auto const& k : key.inherit_env) { + const char* v = std::getenv(k.c_str()); + if (v != nullptr) { + env[k] = std::string(v); + } + } + auto const command_output = system.Execute( + cmdline, env, tmp_dir->GetPath(), out_dir->GetPath()); if (not command_output) { (*logger)(fmt::format("Failed to execute command:\n{}", nlohmann::json(cmdline).dump()), diff --git a/src/other_tools/root_maps/tree_id_git_map.hpp b/src/other_tools/root_maps/tree_id_git_map.hpp index 3c029117..8041919d 100644 --- a/src/other_tools/root_maps/tree_id_git_map.hpp +++ b/src/other_tools/root_maps/tree_id_git_map.hpp @@ -17,6 +17,7 @@ #include #include +#include #include "nlohmann/json.hpp" #include "src/other_tools/ops_maps/critical_git_op_map.hpp" @@ -25,6 +26,7 @@ struct TreeIdInfo { std::string hash{}; /* key */ std::map env_vars{}; + std::vector inherit_env{}; std::vector command{}; // name of repository for which work is done; used in progress reporting std::string origin{}; -- cgit v1.2.3