Line data Source code
1 : /*! 2 : * \file esys/repo/manifest/directorymngr_manifest.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2023 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/manifest/directorymngr.h" 20 : #include "esys/repo/manifest/directoryfilexml.h" 21 : #include "esys/repo/githelper.h" 22 : #include "esys/repo/libgit2/git.h" 23 : 24 : #include <boost/filesystem.hpp> 25 : 26 : namespace esys::repo::manifest 27 : { 28 : 29 2 : DirectoryMngr::DirectoryMngr() = default; 30 : 31 4 : DirectoryMngr::~DirectoryMngr() = default; 32 : 33 2 : void DirectoryMngr::set_folder_path(const std::string &folder_path) 34 : { 35 2 : m_folder_path = folder_path; 36 2 : } 37 : 38 7 : const std::string &DirectoryMngr::get_folder_path() const 39 : { 40 7 : return m_folder_path; 41 : } 42 : 43 2 : void DirectoryMngr::set_directories(std::shared_ptr<Directories> directories) 44 : { 45 2 : m_directories = directories; 46 2 : } 47 : 48 8 : std::shared_ptr<Directories> DirectoryMngr::get_directories() const 49 : { 50 8 : return m_directories; 51 : } 52 : 53 2 : Result DirectoryMngr::load() 54 : { 55 2 : if (get_folder_path().empty()) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR); 56 4 : if (get_directories() == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR); 57 : 58 2 : if (!boost::filesystem::exists(get_folder_path())) 59 : { 60 1 : bool result_bool = boost::filesystem::create_directories(get_folder_path()); 61 1 : if (!result_bool) return ESYSREPO_RESULT(ResultCode::FOLDER_CREATION_ERROR, get_folder_path()); 62 : } 63 : 64 2 : Result rresult; 65 6 : for (auto item : get_directories()->get_items()) 66 : { 67 4 : auto result = load(item); 68 2 : if (result.error()) rresult.add(ESYSREPO_RESULT(result)); 69 4 : } 70 4 : return ESYSREPO_RESULT(rresult); 71 2 : } 72 : 73 2 : Result DirectoryMngr::load(std::shared_ptr<Directories::Item> directories_item) 74 : { 75 2 : if (directories_item == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR); 76 2 : if (directories_item->get_url_ssh().empty()) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR); 77 : 78 2 : Result result; 79 2 : auto name = directories_item->get_name(); 80 2 : boost::filesystem::path git_folder_path = get_folder_path(); 81 2 : git_folder_path /= name; 82 4 : boost::filesystem::path git_git_folder_path = git_folder_path / ".git"; 83 2 : auto git = std::make_shared<libgit2::Git>(); 84 4 : GitHelper git_helper; 85 4 : git_helper.set_git(git); 86 2 : git_helper.set_auto_close(true); 87 : 88 2 : if (!boost::filesystem::exists(git_git_folder_path)) 89 : { 90 2 : result = git_helper.clone(directories_item->get_url_ssh(), git_folder_path.string(), true, log::Level::DEBUG); 91 2 : if (result.error()) return ESYSREPO_RESULT(result); 92 : } 93 : else 94 : { 95 0 : result = git_helper.open(git_folder_path.string(), log::Level::DEBUG); 96 0 : if (result.error()) 97 : { 98 : //error("Couldn't open manifest directory '" + name + "'."); 99 : } 100 : else 101 : { 102 0 : result = git_helper.fetch(log::Level::DEBUG); 103 0 : if (result.error()) 104 : { 105 : 106 : } 107 : 108 : } 109 : } 110 : 111 2 : if (result.error()) return ESYSREPO_RESULT(result); 112 : 113 4 : boost::filesystem::path file_path = git_folder_path / "directory.xml"; 114 2 : if (!boost::filesystem::exists(file_path)) 115 0 : return ESYSREPO_RESULT(ResultCode::FILE_NOT_EXISTING, file_path.string()); 116 : 117 2 : DirectoryFileXML file_xml; 118 : 119 2 : result = file_xml.read(file_path.string()); 120 2 : if (result.error()) return ESYSREPO_RESULT(result); 121 : 122 2 : directories_item->set_directory(file_xml.get_directory()); 123 : 124 4 : if (get_directories() == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR); 125 4 : get_directories()->add_projects(file_xml.get_directory()); 126 2 : return ESYSREPO_RESULT(ResultCode::OK); 127 6 : } 128 : 129 : } // namespace esys::repo::manifest