LCOV - code coverage report
Current view: top level - src/esys/repo/manifest - directorymngr_manifest.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 55 61 90.2 %
Date: 2026-09-26 15:38:18 Functions: 8 8 100.0 %

          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             : 
      77           2 :     const auto &url_ssh = directories_item->get_url_ssh();
      78           2 :     const auto &url_https = directories_item->get_url_https();
      79           2 :     if (url_ssh.empty() && url_https.empty()) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR);
      80             : 
      81           2 :     Result result;
      82           2 :     auto name = directories_item->get_name();
      83           2 :     boost::filesystem::path git_folder_path = get_folder_path();
      84           2 :     git_folder_path /= name;
      85           4 :     boost::filesystem::path git_git_folder_path = git_folder_path / ".git";
      86           2 :     auto git = std::make_shared<libgit2::Git>();
      87           4 :     GitHelper git_helper;
      88           4 :     git_helper.set_git(git);
      89           2 :     git_helper.set_auto_close(true);
      90             : 
      91           2 :     if (!boost::filesystem::exists(git_git_folder_path))
      92             :     {
      93           2 :         const std::string &first_url = !url_ssh.empty() ? url_ssh : url_https;
      94           2 :         result = git_helper.clone(first_url, git_folder_path.string(), true, log::Level::DEBUG);
      95           2 :         if (result.error() && !url_https.empty() && url_https != first_url)
      96           0 :             result = git_helper.clone(url_https, git_folder_path.string(), true, log::Level::DEBUG);
      97           2 :         if (result.error()) return ESYSREPO_RESULT(result);
      98             :     }
      99             :     else
     100             :     {
     101           0 :         result = git_helper.open(git_folder_path.string(), log::Level::DEBUG);
     102           0 :         if (result.error())
     103             :         {
     104             :             //error("Couldn't open manifest directory '" + name + "'.");
     105             :         }
     106             :         else
     107             :         {
     108           0 :             result = git_helper.fetch(log::Level::DEBUG);
     109           0 :             if (result.error())
     110             :             {
     111             : 
     112             :             }
     113             : 
     114             :         }
     115             :     }
     116             : 
     117           2 :     if (result.error()) return ESYSREPO_RESULT(result);
     118             : 
     119           4 :     boost::filesystem::path file_path = git_folder_path / "directory.xml";
     120           2 :     if (!boost::filesystem::exists(file_path))
     121           0 :         return ESYSREPO_RESULT(ResultCode::FILE_NOT_EXISTING, file_path.string());
     122             : 
     123           2 :     DirectoryFileXML file_xml;
     124             : 
     125           2 :     result = file_xml.read(file_path.string());
     126           2 :     if (result.error()) return ESYSREPO_RESULT(result);
     127             : 
     128           2 :     directories_item->set_directory(file_xml.get_directory());
     129             : 
     130           4 :     if (get_directories() == nullptr) return ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR);
     131           4 :     get_directories()->add_projects(file_xml.get_directory());
     132           2 :     return ESYSREPO_RESULT(ResultCode::OK);
     133           6 : }
     134             : 
     135             : } // namespace esys::repo::manifest

Generated by: LCOV version 1.14