Line data Source code
1 : /*! 2 : * \file esys/repo/manifest/location_manifest.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020-2021 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/git/url.h" 20 : #include "esys/repo/manifest/location.h" 21 : #include "esys/repo/manifest/repository.h" 22 : 23 : #include <boost/filesystem.hpp> 24 : 25 : namespace esys::repo::manifest 26 : { 27 : 28 76 : Location::Location() 29 76 : : std::enable_shared_from_this<Location>() 30 : { 31 76 : } 32 : 33 4 : Location::Location(const std::string &name, const std::string &address) 34 : : std::enable_shared_from_this<Location>() 35 4 : , m_name(name) 36 4 : , m_address(address) 37 : { 38 4 : } 39 : 40 80 : Location::~Location() = default; 41 : 42 76 : void Location::set_name(const std::string &name) 43 : { 44 76 : m_name = name; 45 76 : } 46 : 47 1043 : const std::string &Location::get_name() const 48 : { 49 1043 : return m_name; 50 : } 51 : 52 76 : void Location::set_address(const std::string &address) 53 : { 54 76 : m_address = address; 55 76 : } 56 : 57 193 : const std::string &Location::get_address() const 58 : { 59 193 : return m_address; 60 : } 61 : 62 20 : bool Location::is_address_ssh() const 63 : { 64 20 : return get_address().find("ssh://") != get_address().npos; 65 : } 66 : 67 26 : void Location::set_alt_address(const std::string &alt_address) 68 : { 69 26 : m_alt_address = alt_address; 70 26 : } 71 : 72 3 : const std::string &Location::get_alt_address() const 73 : { 74 3 : return m_alt_address; 75 : } 76 : 77 0 : bool Location::is_alt_address_ssh() const 78 : { 79 0 : return get_alt_address().find("ssh://") != get_alt_address().npos; 80 : } 81 : 82 55 : std::shared_ptr<Repository> Location::add_repo(const std::string &name, const std::string &path) 83 : { 84 55 : auto repo = std::make_shared<Repository>(name, path); 85 : 86 110 : add_repo(repo); 87 55 : return repo; 88 0 : } 89 : 90 534 : void Location::add_repo(std::shared_ptr<Repository> repo) 91 : { 92 534 : repo->set_location(this); 93 : 94 534 : get_repos().push_back(repo); 95 534 : m_map_repos_by_name[repo->get_name()] = repo; 96 534 : m_map_repos_by_path[repo->get_path()] = repo; 97 534 : } 98 : 99 755 : std::vector<std::shared_ptr<Repository>> &Location::get_repos() 100 : { 101 755 : return m_repos; 102 : } 103 : 104 27 : const std::vector<std::shared_ptr<Repository>> &Location::get_repos() const 105 : { 106 27 : return m_repos; 107 : } 108 : 109 35 : std::shared_ptr<Repository> Location::find_repo_by_path(const std::string &path) 110 : { 111 35 : boost::filesystem::path gen_path = path; 112 : 113 70 : auto it = m_map_repos_by_path.find(gen_path.generic_path().string()); 114 : 115 35 : if (it == m_map_repos_by_path.end()) return nullptr; 116 53 : return it->second; 117 35 : } 118 : 119 0 : std::shared_ptr<Repository> Location::find_repo_by_name(const std::string &name) 120 : { 121 0 : auto it = m_map_repos_by_name.find(name); 122 : 123 0 : if (it == m_map_repos_by_name.end()) return nullptr; 124 0 : return it->second; 125 : } 126 : 127 0 : std::string Location::find_repo_path_by_url(const std::string &url) 128 : { 129 0 : auto repo = find_repo_by_url(url); 130 : 131 0 : if (repo == nullptr) return ""; 132 0 : return repo->get_path(); 133 0 : } 134 : 135 0 : std::shared_ptr<manifest::Repository> Location::find_repo_by_url(const std::string &url) 136 : { 137 0 : git::URL url_repo(url); 138 : 139 0 : for (auto repo : get_repos()) 140 : { 141 0 : std::string uri_txt = get_address(); 142 0 : if (repo->get_name()[0] != '/') uri_txt += "/"; 143 0 : uri_txt += repo->get_name(); 144 0 : if (url_repo == uri_txt) return repo; 145 0 : } 146 : 147 0 : return nullptr; 148 0 : } 149 : 150 0 : void Location::remove_dot_git(std::string &text) 151 : { 152 0 : auto idx = text.rfind(".git"); 153 0 : if (idx == text.npos) return; 154 : 155 0 : text = text.substr(0, idx); 156 : } 157 : 158 1 : bool Location::operator==(const Location &location) const 159 : { 160 1 : if (get_name() != location.get_name()) return false; 161 1 : if (get_address() != location.get_address()) return false; 162 : 163 1 : if (get_repos().size() != location.get_repos().size()) return false; 164 : 165 9 : for (std::size_t idx = 0; idx < get_repos().size(); ++idx) 166 : { 167 8 : if (*get_repos()[idx] != *location.get_repos()[idx]) return false; 168 : } 169 : return true; 170 : } 171 : 172 1 : bool Location::operator!=(const Location &location) const 173 : { 174 1 : return !operator==(location); 175 : } 176 : 177 0 : ESYSREPO_API std::ostream &operator<<(std::ostream &os, const Location &location) 178 : { 179 0 : return os; 180 : } 181 : 182 : } // namespace esys::repo::manifest