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 163 : Location::Location() 29 163 : : std::enable_shared_from_this<Location>() 30 : { 31 163 : } 32 : 33 15 : Location::Location(const std::string &name, const std::string &address) 34 : : std::enable_shared_from_this<Location>() 35 15 : , m_name(name) 36 15 : , m_address(address) 37 : { 38 15 : } 39 : 40 178 : Location::~Location() = default; 41 : 42 163 : void Location::set_name(const std::string &name) 43 : { 44 163 : m_name = name; 45 163 : } 46 : 47 1710 : const std::string &Location::get_name() const 48 : { 49 1710 : return m_name; 50 : } 51 : 52 163 : void Location::set_address(const std::string &address) 53 : { 54 163 : m_address = address; 55 163 : } 56 : 57 265 : const std::string &Location::get_address() const 58 : { 59 265 : return m_address; 60 : } 61 : 62 26 : bool Location::is_address_ssh() const 63 : { 64 26 : return get_address().find("ssh://") != get_address().npos; 65 : } 66 : 67 39 : void Location::set_alt_address(const std::string &alt_address) 68 : { 69 39 : m_alt_address = alt_address; 70 39 : } 71 : 72 20 : const std::string &Location::get_alt_address() const 73 : { 74 20 : 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 850 : void Location::add_repo(std::shared_ptr<Repository> repo) 91 : { 92 850 : repo->set_location(this); 93 : 94 850 : get_repos().push_back(repo); 95 850 : m_map_repos_by_name[repo->get_name()] = repo; 96 850 : m_map_repos_by_path[repo->get_path()] = repo; 97 850 : } 98 : 99 1468 : std::vector<std::shared_ptr<Repository>> &Location::get_repos() 100 : { 101 1468 : 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 702 : std::string generic_repo_path(const std::string &path) 110 : { 111 702 : std::string norm = path; 112 9083 : for (char &c : norm) 113 : { 114 8381 : if (c == '\\') c = '/'; 115 : } 116 1404 : norm = boost::filesystem::path(norm).lexically_normal().generic_string(); 117 702 : if (norm.size() > 1 && norm.back() == '/') norm.pop_back(); 118 702 : return norm; 119 0 : } 120 : 121 616 : std::shared_ptr<Repository> Location::find_repo_by_path(const std::string &path) 122 : { 123 616 : auto it = m_map_repos_by_path.find(generic_repo_path(path)); 124 : 125 616 : if (it == m_map_repos_by_path.end()) return nullptr; 126 493 : return it->second; 127 : } 128 : 129 1 : std::shared_ptr<Repository> Location::find_repo_by_name(const std::string &name) 130 : { 131 1 : auto it = m_map_repos_by_name.find(name); 132 : 133 1 : if (it == m_map_repos_by_name.end()) return nullptr; 134 1 : return it->second; 135 : } 136 : 137 0 : std::string Location::find_repo_path_by_url(const std::string &url) 138 : { 139 0 : auto repo = find_repo_by_url(url); 140 : 141 0 : if (repo == nullptr) return ""; 142 0 : return repo->get_path(); 143 0 : } 144 : 145 0 : std::shared_ptr<manifest::Repository> Location::find_repo_by_url(const std::string &url) 146 : { 147 0 : git::URL url_repo(url); 148 : 149 0 : for (auto repo : get_repos()) 150 : { 151 0 : std::string uri_txt = get_address(); 152 0 : if (repo->get_name()[0] != '/') uri_txt += "/"; 153 0 : uri_txt += repo->get_name(); 154 0 : if (url_repo == uri_txt) return repo; 155 0 : } 156 : 157 0 : return nullptr; 158 0 : } 159 : 160 0 : void Location::remove_dot_git(std::string &text) 161 : { 162 0 : auto idx = text.rfind(".git"); 163 0 : if (idx == text.npos) return; 164 : 165 0 : text = text.substr(0, idx); 166 : } 167 : 168 1 : bool Location::operator==(const Location &location) const 169 : { 170 1 : if (get_name() != location.get_name()) return false; 171 1 : if (get_address() != location.get_address()) return false; 172 : 173 1 : if (get_repos().size() != location.get_repos().size()) return false; 174 : 175 9 : for (std::size_t idx = 0; idx < get_repos().size(); ++idx) 176 : { 177 8 : if (*get_repos()[idx] != *location.get_repos()[idx]) return false; 178 : } 179 : return true; 180 : } 181 : 182 1 : bool Location::operator!=(const Location &location) const 183 : { 184 1 : return !operator==(location); 185 : } 186 : 187 0 : ESYSREPO_API std::ostream &operator<<(std::ostream &os, const Location &location) 188 : { 189 0 : return os; 190 : } 191 : 192 : } // namespace esys::repo::manifest