diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/file_system/TARGETS | 13 | ||||
-rw-r--r-- | src/buildtool/file_system/git_repo.cpp | 18 | ||||
-rw-r--r-- | src/buildtool/file_system/git_repo.hpp | 10 | ||||
-rw-r--r-- | src/buildtool/file_system/git_types.hpp | 23 | ||||
-rw-r--r-- | src/buildtool/serve_api/serve_service/source_tree.cpp | 11 | ||||
-rw-r--r-- | src/other_tools/ops_maps/import_to_git_map.cpp | 6 | ||||
-rw-r--r-- | src/other_tools/root_maps/commit_git_map.cpp | 12 |
7 files changed, 65 insertions, 28 deletions
diff --git a/src/buildtool/file_system/TARGETS b/src/buildtool/file_system/TARGETS index b8b86d29..51a8e077 100644 --- a/src/buildtool/file_system/TARGETS +++ b/src/buildtool/file_system/TARGETS @@ -113,7 +113,12 @@ , "name": ["git_repo"] , "hdrs": ["git_repo.hpp"] , "srcs": ["git_repo.cpp"] - , "deps": ["git_cas", ["src/buildtool/common", "bazel_types"]] + , "deps": + [ "git_cas" + , "git_types" + , ["src/buildtool/common", "bazel_types"] + , ["src/utils/cpp", "expected"] + ] , "stage": ["src", "buildtool", "file_system"] , "private-deps": [ ["src/buildtool/logging", "log_level"] @@ -143,6 +148,12 @@ , ["src/utils/cpp", "hex_string"] ] } +, "git_types": + { "type": ["@", "rules", "CC", "library"] + , "name": ["git_types"] + , "hdrs": ["git_types.hpp"] + , "stage": ["src", "buildtool", "file_system"] + } , "file_root": { "type": ["@", "rules", "CC", "library"] , "name": ["file_root"] diff --git a/src/buildtool/file_system/git_repo.cpp b/src/buildtool/file_system/git_repo.cpp index 4fefc76b..39c0970b 100644 --- a/src/buildtool/file_system/git_repo.cpp +++ b/src/buildtool/file_system/git_repo.cpp @@ -1060,9 +1060,9 @@ auto GitRepo::KeepTree(std::string const& tree_id, auto GitRepo::GetSubtreeFromCommit(std::string const& commit, std::string const& subdir, anon_logger_ptr const& logger) noexcept - -> std::variant<bool, std::string> { + -> expected<std::string, GitLookupError> { #ifdef BOOTSTRAP_BUILD_TOOL - return true; + return unexpected{GitLookupError::Fatal}; #else try { // preferably with a "fake" repository! @@ -1083,7 +1083,7 @@ auto GitRepo::GetSubtreeFromCommit(std::string const& commit, GetGitCAS()->git_path_.string(), GitLastError()), true /*fatal*/); - return true; // fatal failure + return unexpected{GitLookupError::Fatal}; } git_commit* commit_ptr{nullptr}; @@ -1096,7 +1096,7 @@ auto GitRepo::GetSubtreeFromCommit(std::string const& commit, true /*fatal*/); // cleanup resources git_commit_free(commit_ptr); - return false; // non-fatal failure + return unexpected{GitLookupError::NotFound}; // non-fatal failure } auto commit_obj = std::unique_ptr<git_commit, decltype(&commit_closer)>( commit_ptr, commit_closer); @@ -1113,7 +1113,7 @@ auto GitRepo::GetSubtreeFromCommit(std::string const& commit, true /*fatal*/); // cleanup resources git_tree_free(tree_ptr); - return true; // fatal failure + return unexpected{GitLookupError::Fatal}; } auto tree = std::unique_ptr<git_tree, decltype(&tree_closer)>( tree_ptr, tree_closer); @@ -1132,7 +1132,7 @@ auto GitRepo::GetSubtreeFromCommit(std::string const& commit, true /*fatal*/); // cleanup resources git_tree_entry_free(subtree_entry_ptr); - return true; // fatal failure + return unexpected{GitLookupError::Fatal}; } auto subtree_entry = std::unique_ptr<git_tree_entry, decltype(&tree_entry_closer)>( @@ -1149,7 +1149,7 @@ auto GitRepo::GetSubtreeFromCommit(std::string const& commit, Logger::Log(LogLevel::Error, "get subtree from commit failed with:\n{}", ex.what()); - return true; // fatal failure + return unexpected{GitLookupError::Fatal}; } #endif // BOOTSTRAP_BUILD_TOOL } @@ -1274,10 +1274,10 @@ auto GitRepo::GetSubtreeFromPath(std::filesystem::path const& fpath, auto subdir = std::filesystem::relative(fpath, *root).string(); // get subtree from head commit and subdir auto res = GetSubtreeFromCommit(head_commit, subdir, wrapped_logger); - if (std::holds_alternative<bool>(res)) { + if (not res) { return std::nullopt; } - return std::get<std::string>(res); + return *std::move(res); } catch (std::exception const& ex) { Logger::Log(LogLevel::Error, "get subtree from path failed with:\n{}", diff --git a/src/buildtool/file_system/git_repo.hpp b/src/buildtool/file_system/git_repo.hpp index 54706e41..d43a85ec 100644 --- a/src/buildtool/file_system/git_repo.hpp +++ b/src/buildtool/file_system/git_repo.hpp @@ -21,11 +21,12 @@ #include <string> #include <unordered_map> #include <utility> // std::move -#include <variant> #include <vector> #include "src/buildtool/common/bazel_types.hpp" #include "src/buildtool/file_system/git_cas.hpp" +#include "src/buildtool/file_system/git_types.hpp" +#include "src/utils/cpp/expected.hpp" extern "C" { struct git_repository; @@ -194,15 +195,12 @@ class GitRepo { /// \brief Get the tree id of a subtree given the root commit /// Calling it from a fake repository allows thread-safe use. - /// Returns an error + data union, where at index 0 is a flag stating the - /// nature of the error on failure (true is fatal, false is non-fatal, i.e., - /// commit not found), and at index 1 is the subtree hash on success. - /// It guarantees the logger is called exactly once with fatal if failure. + /// Returns the subtree hash on success or an unexpected error. [[nodiscard]] auto GetSubtreeFromCommit( std::string const& commit, std::string const& subdir, anon_logger_ptr const& logger) noexcept - -> std::variant<bool, std::string>; + -> expected<std::string, GitLookupError>; /// \brief Get the tree id of a subtree given the root tree hash /// Calling it from a fake repository allows thread-safe use. diff --git a/src/buildtool/file_system/git_types.hpp b/src/buildtool/file_system/git_types.hpp new file mode 100644 index 00000000..52974397 --- /dev/null +++ b/src/buildtool/file_system/git_types.hpp @@ -0,0 +1,23 @@ +// 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_FILE_SYSTEM_GIT_TYPES_HPP +#define INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_GIT_TYPES_HPP + +enum class GitLookupError { + Fatal = 0, + NotFound = 1, +}; + +#endif // INCLUDED_SRC_BUILDTOOL_FILE_SYSTEM_GIT_TYPES_HPP diff --git a/src/buildtool/serve_api/serve_service/source_tree.cpp b/src/buildtool/serve_api/serve_service/source_tree.cpp index 2fb5acfb..f79a6492 100644 --- a/src/buildtool/serve_api/serve_service/source_tree.cpp +++ b/src/buildtool/serve_api/serve_service/source_tree.cpp @@ -117,7 +117,12 @@ auto SourceTreeService::GetSubtreeFromCommit( msg); } }); - return repo->GetSubtreeFromCommit(commit, subdir, wrapped_logger); + auto res = + repo->GetSubtreeFromCommit(commit, subdir, wrapped_logger); + if (not res) { + return res.error() == GitLookupError::Fatal; + } + return *std::move(res); } } return true; // fatal failure @@ -594,11 +599,11 @@ auto SourceTreeService::CommonImportToGit( // get the root tree of this commit; this is thread-safe auto res = just_git_repo->GetSubtreeFromCommit(*commit_hash, ".", wrapped_logger); - if (not std::holds_alternative<std::string>(res)) { + if (not res) { return result_t(std::in_place_index<0>, err); } // return the root tree id - return result_t(std::in_place_index<1>, std::get<std::string>(res)); + return result_t(std::in_place_index<1>, *std::move(res)); } auto SourceTreeService::ArchiveImportToGit( diff --git a/src/other_tools/ops_maps/import_to_git_map.cpp b/src/other_tools/ops_maps/import_to_git_map.cpp index 6aacbaf4..5c30f3d5 100644 --- a/src/other_tools/ops_maps/import_to_git_map.cpp +++ b/src/other_tools/ops_maps/import_to_git_map.cpp @@ -70,11 +70,11 @@ void KeepCommitAndSetTree( }); auto res = just_git_repo->GetSubtreeFromCommit( commit, ".", wrapped_logger); - if (not std::holds_alternative<std::string>(res)) { + if (not res) { return; } - (*setter)(std::pair<std::string, GitCASPtr>( - std::get<std::string>(res), just_git_cas)); + (*setter)(std::pair<std::string, GitCASPtr>(*std::move(res), + just_git_cas)); }, [logger, commit, target_path](auto const& msg, bool fatal) { (*logger)(fmt::format("While running critical Git op KEEP_TAG for " diff --git a/src/other_tools/root_maps/commit_git_map.cpp b/src/other_tools/root_maps/commit_git_map.cpp index 22f85f35..a8ef5eaf 100644 --- a/src/other_tools/root_maps/commit_git_map.cpp +++ b/src/other_tools/root_maps/commit_git_map.cpp @@ -340,7 +340,7 @@ void NetworkFetchAndSetPresentRoot( // get tree id and return workspace root auto res = git_repo->GetSubtreeFromCommit( repo_info.hash, repo_info.subdir, wrapped_logger); - if (not std::holds_alternative<std::string>(res)) { + if (not res) { return; } // set the workspace root as present @@ -350,7 +350,7 @@ void NetworkFetchAndSetPresentRoot( {repo_info.ignore_special ? FileRoot::kGitTreeIgnoreSpecialMarker : FileRoot::kGitTreeMarker, - std::get<std::string>(res), // subtree id + *std::move(res), // subtree id repo_root}), /*is_cache_hit=*/false)); }, @@ -380,7 +380,7 @@ void NetworkFetchAndSetPresentRoot( // get tree id and return workspace root auto res = git_repo->GetSubtreeFromCommit( repo_info.hash, repo_info.subdir, wrapped_logger); - if (not std::holds_alternative<std::string>(res)) { + if (not res) { return; } // set the workspace root as present @@ -389,7 +389,7 @@ void NetworkFetchAndSetPresentRoot( nlohmann::json::array({repo_info.ignore_special ? FileRoot::kGitTreeIgnoreSpecialMarker : FileRoot::kGitTreeMarker, - std::get<std::string>(res), // subtree id + *std::move(res), // subtree id repo_root}), /*is_cache_hit=*/false)); } @@ -882,10 +882,10 @@ void EnsureCommit(GitRepoInfo const& repo_info, // get tree id and return workspace root auto res = git_repo->GetSubtreeFromCommit( repo_info.hash, repo_info.subdir, wrapped_logger); - if (not std::holds_alternative<std::string>(res)) { + if (not res) { return; } - auto subtree = std::get<std::string>(res); + auto subtree = *std::move(res); // set the workspace root if (repo_info.absent and not fetch_absent) { // try by all available means to generate and set the absent root |