From 65944566d8d3ae81d3dc3acb8c82944f5698ca5d Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Wed, 15 Feb 2023 17:12:15 +0100 Subject: structure cleanup: move remote operations of GitRepo to other_tools... ...in order to not include unwanted dependencies in just proper. The new class extends the GitRepo class used for just's Git tree operations and gets used in all of just-mr's async maps. --- src/other_tools/git_operations/git_repo_remote.hpp | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/other_tools/git_operations/git_repo_remote.hpp (limited to 'src/other_tools/git_operations/git_repo_remote.hpp') diff --git a/src/other_tools/git_operations/git_repo_remote.hpp b/src/other_tools/git_operations/git_repo_remote.hpp new file mode 100644 index 00000000..83099994 --- /dev/null +++ b/src/other_tools/git_operations/git_repo_remote.hpp @@ -0,0 +1,105 @@ +// Copyright 2023 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_GIT_OPERATIONS_GIT_REPO_REMOTE_HPP +#define INCLUDED_SRC_OTHER_TOOLS_GIT_OPERATIONS_GIT_REPO_REMOTE_HPP + +#include "src/buildtool/file_system/git_repo.hpp" + +/// \brief Extension to a Git repository, allowing remote Git operations. +class GitRepoRemote : public GitRepo { + public: + GitRepoRemote() = delete; // no default ctor + ~GitRepoRemote() noexcept = default; + + // allow only move, no copy + GitRepoRemote(GitRepoRemote const&) = delete; + GitRepoRemote(GitRepoRemote&&) noexcept; + auto operator=(GitRepoRemote const&) = delete; + auto operator=(GitRepoRemote&& other) noexcept -> GitRepoRemote&; + + /// \brief Factory to wrap existing open CAS in a "fake" repository. + [[nodiscard]] static auto Open(GitCASPtr git_cas) noexcept + -> std::optional; + + /// \brief Factory to open existing real repository at given location. + [[nodiscard]] static auto Open( + std::filesystem::path const& repo_path) noexcept + -> std::optional; + + /// \brief Factory to initialize and open new real repository at location. + /// Returns nullopt if repository init fails even after repeated tries. + [[nodiscard]] static auto InitAndOpen( + std::filesystem::path const& repo_path, + bool is_bare) noexcept -> std::optional; + + /// \brief Retrieve commit hash from remote branch given its name. + /// Only possible with real repository and thus non-thread-safe. + /// Returns the retrieved commit hash, or nullopt if failure. + /// It guarantees the logger is called exactly once with fatal if failure. + [[nodiscard]] auto GetCommitFromRemote( + std::string const& repo_url, + std::string const& branch, + anon_logger_ptr const& logger) noexcept -> std::optional; + + /// \brief Fetch from given remote. It can either fetch a given named + /// branch, or it can fetch with base refspecs. + /// Only possible with real repository and thus non-thread-safe. + /// Returns a success flag. + /// It guarantees the logger is called exactly once with fatal if failure. + [[nodiscard]] auto FetchFromRemote(std::string const& repo_url, + std::optional const& branch, + anon_logger_ptr const& logger) noexcept + -> bool; + + /// \brief Get commit from remote via a temporary repository. + /// Calling it from a fake repository allows thread-safe use. + /// Creates a temporary real repository at the given location and uses it to + /// retrieve from the remote the commit of a branch given its name. + /// Caller needs to make sure the temporary directory exists and that the + /// given path is thread- and process-safe! + /// Returns the commit hash, as a string, or nullopt if failure. + /// It guarantees the logger is called exactly once with fatal if failure. + [[nodiscard]] auto UpdateCommitViaTmpRepo( + std::filesystem::path const& tmp_repo_path, + std::string const& repo_url, + std::string const& branch, + anon_logger_ptr const& logger) const noexcept + -> std::optional; + + /// \brief Fetch from a remote via a temporary repository. + /// Calling it from a fake repository allows thread-safe use. + /// Creates a temporary real repository at the given location and uses a + /// custom backend to redirect the fetched objects into the desired odb. + /// Caller needs to make sure the temporary directory exists and that the + /// given path is thread- and process-safe! + /// Uses either a given branch, or fetches using base refspecs. + /// Returns a success flag. + /// It guarantees the logger is called exactly once with fatal if failure. + [[nodiscard]] auto FetchViaTmpRepo( + std::filesystem::path const& tmp_repo_path, + std::string const& repo_url, + std::optional const& branch, + anon_logger_ptr const& logger) noexcept -> bool; + + private: + /// \brief Open "fake" repository wrapper for existing CAS. + explicit GitRepoRemote(GitCASPtr git_cas) noexcept; + /// \brief Open real repository at given location. + explicit GitRepoRemote(std::filesystem::path const& repo_path) noexcept; + /// \brief Construct from inherited class. + explicit GitRepoRemote(GitRepo&&) noexcept; +}; + +#endif // INCLUDED_SRC_OTHER_TOOLS_GIT_OPERATIONS_GIT_REPO_REMOTE_HPP -- cgit v1.2.3