diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2022-11-17 15:33:05 +0100 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2022-12-21 14:59:04 +0100 |
commit | 22e6727e44d709f8591c425699abea19d1dd2394 (patch) | |
tree | c9d0260918f26b43321fae886abaf63a7bd58cb1 /src | |
parent | 380e88d8e6863fec8a0209053b529adfb31a9dc0 (diff) | |
download | justbuild-22e6727e44d709f8591c425699abea19d1dd2394.tar.gz |
Just-MR: Add file path to WS root git map
Diffstat (limited to 'src')
-rw-r--r-- | src/other_tools/root_maps/TARGETS | 16 | ||||
-rw-r--r-- | src/other_tools/root_maps/fpath_git_map.cpp | 179 | ||||
-rw-r--r-- | src/other_tools/root_maps/fpath_git_map.hpp | 40 |
3 files changed, 235 insertions, 0 deletions
diff --git a/src/other_tools/root_maps/TARGETS b/src/other_tools/root_maps/TARGETS index 733e3d1b..e34377aa 100644 --- a/src/other_tools/root_maps/TARGETS +++ b/src/other_tools/root_maps/TARGETS @@ -32,4 +32,20 @@ , "private-deps": [["src/buildtool/file_system", "git_repo"], ["src/utils/cpp", "tmp_dir"]] } +, "fpath_git_map": + { "type": ["@", "rules", "CC", "library"] + , "name": ["fpath_git_map"] + , "hdrs": ["fpath_git_map.hpp"] + , "srcs": ["fpath_git_map.cpp"] + , "deps": + [ ["src/other_tools/ops_maps", "import_to_git_map"] + , ["@", "json", "", "json"] + ] + , "stage": ["src", "other_tools", "root_maps"] + , "private-deps": + [ ["src/other_tools/just_mr", "utils"] + , ["src/buildtool/execution_api/local", "config"] + , ["src/utils/cpp", "tmp_dir"] + ] + } } diff --git a/src/other_tools/root_maps/fpath_git_map.cpp b/src/other_tools/root_maps/fpath_git_map.cpp new file mode 100644 index 00000000..25218368 --- /dev/null +++ b/src/other_tools/root_maps/fpath_git_map.cpp @@ -0,0 +1,179 @@ +// Copyright 2022 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/root_maps/fpath_git_map.hpp" + +#include "src/buildtool/execution_api/local/config.hpp" +#include "src/other_tools/just_mr/utils.hpp" +#include "src/utils/cpp/tmp_dir.hpp" + +auto CreateFilePathGitMap( + std::optional<std::string> const& current_subcmd, + gsl::not_null<CriticalGitOpMap*> const& critical_git_op_map, + gsl::not_null<ImportToGitMap*> const& import_to_git_map, + std::size_t jobs) -> FilePathGitMap { + auto dir_to_git = [current_subcmd, critical_git_op_map, import_to_git_map]( + auto ts, + auto setter, + auto logger, + auto /*unused*/, + auto const& key) { + // setup wrapped logger + auto wrapped_logger = std::make_shared<AsyncMapConsumerLogger>( + [&logger](auto const& msg, bool fatal) { + (*logger)( + fmt::format("While getting repo root from path:\n{}", msg), + fatal); + }); + // check if path is a part of a git repo + auto repo_root = GitRepo::GetRepoRootFromPath( + key, wrapped_logger); // static function + if (not repo_root) { + return; + } + if (not repo_root->empty()) { // if repo root found + auto git_cas = GitCAS::Open(*repo_root); + if (not git_cas) { + (*logger)(fmt::format("Could not open object database for " + "repository {}", + repo_root->string()), + /*fatal=*/true); + return; + } + auto git_repo = GitRepo::Open(git_cas); // link fake repo to odb + if (not git_repo) { + (*logger)(fmt::format("Could not open repository {}", + repo_root->string()), + /*fatal=*/true); + return; + } + // get head commit + GitOpKey op_key = {{ + *repo_root, // target_path + "", // git_hash + "", // branch + }, + GitOpType::GET_HEAD_ID}; + critical_git_op_map->ConsumeAfterKeysReady( + ts, + {std::move(op_key)}, + [fpath = key, + git_cas = std::move(git_cas), + repo_root = std::move(*repo_root), + setter, + logger](auto const& values) { + GitOpValue op_result = *values[0]; + // check flag + if (not op_result.result) { + (*logger)("Get Git head id failed", + /*fatal=*/true); + return; + } + auto git_repo = + GitRepo::Open(git_cas); // link fake repo to odb + if (not git_repo) { + (*logger)(fmt::format("Could not open repository {}", + repo_root.string()), + /*fatal=*/true); + return; + } + // setup wrapped logger + auto wrapped_logger = + std::make_shared<AsyncMapConsumerLogger>( + [&logger](auto const& msg, bool fatal) { + (*logger)( + fmt::format("While getting subtree from " + "path:\n{}", + msg), + fatal); + }); + // get tree id and return workspace root + auto tree_hash = git_repo->GetSubtreeFromPath( + fpath, *op_result.result, wrapped_logger); + if (not tree_hash) { + return; + } + // set the workspace root + (*setter)(nlohmann::json::array( + {"git tree", *tree_hash, repo_root})); + }, + [logger, target_path = *repo_root](auto const& msg, + bool fatal) { + (*logger)( + fmt::format("While running critical Git op " + "GET_BRANCH_REFNAME for target {}:\n{}", + target_path.string(), + msg), + fatal); + }); + } + else { + // warn if import to git is inefficient + if (current_subcmd) { + (*logger)(fmt::format("Warning: Inefficient Git import of file " + "path \'{}\'.\nPlease consider using " + "\'just-mr setup\' and \'just {}\' " + "separately to cache the output.", + key.string(), + *current_subcmd), + /*fatal=*/false); + } + // it's not a git repo, so import it to git cache + auto tmp_dir = JustMR::Utils::CreateTypedTmpDir("file"); + if (not tmp_dir) { + (*logger)("Failed to create import-to-git tmp directory!", + /*fatal=*/true); + return; + } + // copy folder content to tmp dir + if (not FileSystemManager::CopyDirectoryImpl( + key, tmp_dir->GetPath(), /*recursively=*/true)) { + (*logger)( + fmt::format("Failed to copy content from directory {}", + key.string()), + /*fatal=*/true); + return; + } + // do import to git + CommitInfo c_info{tmp_dir->GetPath(), "file", key}; + import_to_git_map->ConsumeAfterKeysReady( + ts, + {std::move(c_info)}, + // tmp_dir passed, to ensure folder is not removed until import + // to git is done + [tmp_dir, setter, logger](auto const& values) { + // check for errors + if (not values[0]->second) { + (*logger)("Importing to git failed", + /*fatal=*/true); + return; + } + // we only need the tree + std::string tree = values[0]->first; + // set the workspace root + (*setter)(nlohmann::json::array( + {"git tree", tree, JustMR::Utils::GetGitCacheRoot()})); + }, + [logger, target_path = key](auto const& msg, bool fatal) { + (*logger)( + fmt::format("While importing target {} to git:\n{}", + target_path.string(), + msg), + fatal); + }); + } + }; + return AsyncMapConsumer<std::filesystem::path, nlohmann::json>(dir_to_git, + jobs); +}
\ No newline at end of file diff --git a/src/other_tools/root_maps/fpath_git_map.hpp b/src/other_tools/root_maps/fpath_git_map.hpp new file mode 100644 index 00000000..fdcc962c --- /dev/null +++ b/src/other_tools/root_maps/fpath_git_map.hpp @@ -0,0 +1,40 @@ +// Copyright 2022 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_ROOT_MAPS_FPATH_GIT_MAP_HPP +#define INCLUDED_SRC_OTHER_TOOLS_ROOT_MAPS_FPATH_GIT_MAP_HPP + +#include "nlohmann/json.hpp" +#include "src/other_tools/ops_maps/import_to_git_map.hpp" + +/// \brief Maps the path to a repo on the file system to its Git tree WS root. +using FilePathGitMap = AsyncMapConsumer<std::filesystem::path, nlohmann::json>; + +namespace std { +template <> +struct hash<std::filesystem::path> { + [[nodiscard]] auto operator()( + std::filesystem::path const& ct) const noexcept -> std::size_t { + return std::filesystem::hash_value(ct); + } +}; +} // namespace std + +[[nodiscard]] auto CreateFilePathGitMap( + std::optional<std::string> const& current_subcmd, + gsl::not_null<CriticalGitOpMap*> const& critical_git_op_map, + gsl::not_null<ImportToGitMap*> const& import_to_git_map, + std::size_t jobs) -> FilePathGitMap; + +#endif // INCLUDED_SRC_OTHER_TOOLS_ROOT_MAPS_FPATH_GIT_MAP_HPP
\ No newline at end of file |