LCOV - code coverage report
Current view: top level - src/esys/repo/exe - cmdinit.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 275 385 71.4 %
Date: 2026-08-01 10:43:40 Functions: 29 35 82.9 %

          Line data    Source code
       1             : /*!
       2             :  * \file esys/repo/exe/cmdinit.cpp
       3             :  * \brief
       4             :  *
       5             :  * \cond
       6             :  * __legal_b__
       7             :  *
       8             :  * Copyright (c) 2020-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/exe/cmdinit.h"
      20             : #include "esys/repo/filesystem.h"
      21             : #include "esys/repo/git/url.h"
      22             : #include "esys/repo/manifest/file.h"
      23             : #include "esys/repo/manifest/xmlfile.h"
      24             : #include "esys/repo/grepo/manifest.h"
      25             : #include "esys/repo/manifest/directorymngr.h"
      26             : 
      27             : #include <esys/trace/call.h>
      28             : #include <esys/trace/macros.h>
      29             : 
      30             : #include <boost/filesystem.hpp>
      31             : 
      32             : #include <cassert>
      33             : #include <iostream>
      34             : #include <sstream>
      35             : 
      36             : namespace esys::repo::exe
      37             : {
      38             : 
      39          36 : CmdInit::CmdInit()
      40          72 :     : Cmd("Init")
      41             : {
      42          36 : }
      43             : 
      44          37 : CmdInit::~CmdInit() = default;
      45             : 
      46          27 : void CmdInit::set_url(const std::string &url)
      47             : {
      48          27 :     m_url = url;
      49          27 : }
      50             : 
      51          89 : const std::string &CmdInit::get_url() const
      52             : {
      53          89 :     return m_url;
      54             : }
      55             : 
      56           2 : void CmdInit::set_branch(const std::string &branch)
      57             : {
      58           2 :     m_branch = branch;
      59           2 : }
      60             : 
      61          23 : const std::string &CmdInit::get_branch() const
      62             : {
      63          23 :     return m_branch;
      64             : }
      65             : 
      66           0 : void CmdInit::set_manifest_name(const std::string &manifest_name)
      67             : {
      68           0 :     m_manifest_name = manifest_name;
      69           0 : }
      70             : 
      71          22 : const std::string &CmdInit::get_manifest_name() const
      72             : {
      73          22 :     return m_manifest_name;
      74             : }
      75             : 
      76           1 : void CmdInit::set_project_name(const std::string &project_name)
      77             : {
      78           1 :     m_project_name = project_name;
      79           1 : }
      80             : 
      81           3 : const std::string &CmdInit::get_project_name() const
      82             : {
      83           3 :     return m_project_name;
      84             : }
      85             : 
      86          10 : void CmdInit::set_google_manifest(bool google_manifest)
      87             : {
      88          10 :     m_google_manifest = google_manifest;
      89          10 :     m_manifest_type = manifest::Type::GOOGLE_MANIFEST;
      90          10 : }
      91             : 
      92          24 : bool CmdInit::get_google_manifest() const
      93             : {
      94          24 :     return m_google_manifest;
      95             : }
      96             : 
      97           9 : void CmdInit::set_git_super_project(bool git_super_project)
      98             : {
      99           9 :     m_git_super_project = git_super_project;
     100           9 :     m_manifest_type = manifest::Type::GOOGLE_MANIFEST;
     101           9 : }
     102             : 
     103          43 : bool CmdInit::get_git_super_project() const
     104             : {
     105          43 :     return m_git_super_project;
     106             : }
     107             : 
     108           0 : std::string CmdInit::get_extra_start_msg()
     109             : {
     110           0 :     return "\n    url : " + get_url();
     111             : }
     112             : 
     113          21 : Result CmdInit::impl_run()
     114             : {
     115          21 :     Result result;
     116          21 :     ETRC_CALL_RET_NP(result);
     117             : 
     118          21 :     if (get_git_super_project()) warn("The option --git-super is not implemented yet");
     119             : 
     120          21 :     result = only_one_folder_or_workspace();
     121          21 :     if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     122             : 
     123          21 :     result = load_esysrepo_folder();
     124          21 :     if (result.ok())
     125             :     {
     126           1 :         if (get_url().empty() && !get_project_name().empty())
     127             :         {
     128           0 :             info("Finding URL for project " + get_project_name() + " ...");
     129           0 :             result = find_url_from_project_name();
     130           0 :             if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     131           0 :             if (!get_url().empty()) info("Found URL : '" + get_url() + "'.");
     132             :         }
     133           2 :         return ETRC_RET(load_esysrepo_folder_succeeded());
     134             :     }
     135          40 :     return ETRC_RET(load_esysrepo_folder_failed());
     136          21 : }
     137             : 
     138           1 : Result CmdInit::load_esysrepo_folder_succeeded()
     139             : {
     140           1 :     Result result;
     141           1 :     ETRC_CALL_RET_NP(result);
     142             : 
     143           1 :     assert(get_config_folder() != nullptr);
     144           2 :     if (get_config_folder() == nullptr)
     145             :     {
     146           0 :         error("Internal error [CmdInit::load_esysrepo_folder_succeeded] get_config_folder() == nullptr");
     147           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR));
     148             :     }
     149             : 
     150           1 :     auto config = get_config_folder()->get_config();
     151           1 :     assert(config != nullptr);
     152           1 :     if (config == nullptr)
     153             :     {
     154           0 :         error("Internal error [CmdInit::load_esysrepo_folder_succeeded] config == nullptr");
     155           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR));
     156             :     }
     157             : 
     158           1 :     git::URL url(get_url());
     159           1 :     if (url != config->get_manifest_url())
     160             :     {
     161           0 :         std::stringstream oss;
     162             : 
     163           0 :         oss << "Current manifest url is:" << std::endl;
     164           0 :         oss << "    '" << config->get_manifest_url() << "'" << std::endl;
     165           0 :         oss << "But manifest url given is different:" << std::endl;
     166           0 :         oss << "    '" << get_url() << "'" << std::endl;
     167           0 :         oss << "Use --force if this is really wanted.";
     168           0 :         error(oss.str());
     169           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::CMD_INCORRECT_PARAMETERS_IN_CONTEXT, oss.str()));
     170           0 :     }
     171             : 
     172           1 :     if (get_branch().empty())
     173             :     {
     174           1 :         warn("Nothing to do as a branch is not provided");
     175           2 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::OK));
     176             :     }
     177             : 
     178           0 :     boost::filesystem::path manifest_git_path = get_config_folder()->get_manifest_repo_path();
     179             : 
     180           0 :     auto git_helper = new_git_helper();
     181             : 
     182           0 :     debug(1, "Test debug, before git_helper");
     183           0 :     git_helper->debug(1, "Test debug with git_helper");
     184             : 
     185           0 :     result = git_helper->open(manifest_git_path.lexically_normal().make_preferred().string(), log::DEBUG);
     186           0 :     if (result.error())
     187             :     {
     188           0 :         std::string err_str = "Opening git repo holding the manifest failed.";
     189           0 :         error(err_str);
     190           0 :         return ETRC_RET(ESYSREPO_RESULT(result, err_str));
     191           0 :     }
     192             : 
     193           0 :     result = get_git()->checkout(get_branch() /*, get_force()*/);
     194           0 :     if (result.error())
     195             :     {
     196           0 :         std::string err_str = "Checkout branch failed.";
     197           0 :         error(err_str);
     198           0 :         return ETRC_RET(ESYSREPO_RESULT(result, err_str));
     199           0 :     }
     200             : 
     201           0 :     result = get_git()->close();
     202             : 
     203           0 :     return ETRC_RET(ESYSREPO_RESULT(result));
     204           2 : }
     205             : 
     206          20 : Result CmdInit::load_esysrepo_folder_failed()
     207             : {
     208          20 :     Result result;
     209          20 :     ETRC_CALL_RET_NP(result);
     210             : 
     211             :     // Since it failed, either there is no .esysrepo folder or the xml config
     212             :     // file is corrupted
     213          20 :     result = create_esysrepo_folder();
     214          20 :     if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     215             : 
     216             :     // Now there are 2 cases, we couldn't load be the ESysRepo config folder because
     217             :     // it got corrupted, or it's not there at all.
     218          20 :     result = fetch_manifest();
     219          20 :     if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     220             : 
     221          20 :     return ETRC_RET(Result::OK);
     222          20 : }
     223             : 
     224          20 : Result CmdInit::fetch_manifest()
     225             : {
     226          20 :     Result result;
     227          20 :     ETRC_CALL_RET_NP(result);
     228             : 
     229          20 :     if (get_google_manifest())
     230           2 :         return ETRC_RET(fetch_google_manifest());
     231          19 :     else if (get_git_super_project())
     232           0 :         return ETRC_RET(fetch_git_super_project());
     233             : 
     234             :     // Here we can't be sure, either it's a google manifest, a esysrepo manifest or a git super project
     235          38 :     return ETRC_RET(fetch_unknown_manifest());
     236          20 : }
     237             : 
     238          22 : std::string CmdInit::get_google_manifest_filename() const
     239             : {
     240          22 :     if (!get_manifest_name().empty()) return get_manifest_name();
     241          22 :     return "default.xml";
     242             : }
     243             : 
     244          20 : Result CmdInit::clone_manifest_repo(GitHelper &git_helper, std::string &git_repo_path)
     245             : {
     246          20 :     Result result;
     247          20 :     ETRC_CALL_RET_NP(result);
     248             : 
     249          40 :     if (get_git() == nullptr) return ETRC_RET(ESYSREPO_RESULT(ResultCode::INTERNAL_ERROR));
     250             : 
     251          20 :     if (get_url().empty() && !get_project_name().empty())
     252             :     {
     253           2 :         info("Finding URL for project " + get_project_name() + " ...");
     254           1 :         result = find_url_from_project_name();
     255           1 :         if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     256           2 :         if (!get_url().empty()) info("Found URL : '" + get_url() + "'.");
     257             :     }
     258             : 
     259          20 :     if (get_url().empty())
     260             :     {
     261           0 :         error("URL is empty");
     262           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::CMDINIT_NO_URL));
     263             :     }
     264             : 
     265          20 :     boost::filesystem::path path = get_config_folder()->get_temp_path();
     266          20 :     path /= "unknown_manifest";
     267          40 :     git_repo_path = path.lexically_normal().make_preferred().string();
     268             : 
     269          20 :     debug(1, "Test debug, before git_helper");
     270          20 :     git_helper.debug(1, "Test debug with git_helper");
     271             : 
     272          20 :     result = git_helper.clone(get_url(), git_repo_path, false, log::DEBUG);
     273          20 :     if (result.error())
     274             :     {
     275           0 :         error("Cloning failed.");
     276           0 :         return ETRC_RET(ESYSREPO_RESULT(result));
     277             :     }
     278             : 
     279          20 :     if (!get_branch().empty())
     280             :     {
     281           2 :         result = get_git()->checkout(get_branch() /*, get_force()*/);
     282           2 :         if (result.error())
     283             :         {
     284           0 :             std::string err_str = "Cloning failed : checkout branch failed.";
     285           0 :             error(err_str);
     286           0 :             return ETRC_RET(ESYSREPO_RESULT(result, err_str));
     287           0 :         }
     288             :     }
     289             : 
     290          20 :     result = git_helper.close(log::DEBUG);
     291          20 :     if (result.error())
     292             :     {
     293           0 :         std::string err_str = "Cloning failed : can't clod the git repo.";
     294           0 :         error(err_str);
     295           0 :         return ETRC_RET(ESYSREPO_RESULT(result, err_str));
     296           0 :     }
     297             : 
     298          40 :     return ETRC_RET(ESYSREPO_RESULT(ResultCode::OK));
     299          20 : }
     300             : 
     301          11 : Result CmdInit::install_google_manifest(GitHelper &git_helper, const std::string &git_repo_path)
     302             : {
     303          11 :     Result result;
     304          11 :     ETRC_CALL_RET(result, git_repo_path);
     305             : 
     306          11 :     auto config = get_config_folder()->get_or_new_config();
     307          11 :     config->set_manifest_type(manifest::Type::GOOGLE_MANIFEST);
     308          11 :     config->set_manifest_format(manifest::Format::XML);
     309          11 :     config->set_manifest_url(get_url());
     310             : 
     311          11 :     boost::filesystem::path source = git_repo_path;
     312          22 :     boost::filesystem::path rel_source = boost::filesystem::relative(source);
     313          11 :     boost::filesystem::path target = get_config_folder()->get_path();
     314          22 :     boost::filesystem::path rel_target = boost::filesystem::relative(target);
     315          11 :     target /= grepo::Manifest::get_folder_name();
     316          22 :     rel_target = boost::filesystem::relative(target);
     317             : 
     318          11 :     bool result_bool = boost::filesystem::create_directory(target);
     319          11 :     if (!result_bool)
     320             :     {
     321           0 :         std::string err_str = "Couldn't create the folder : " + rel_target.string();
     322           0 :         error(err_str);
     323           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::FOLDER_CREATION_ERROR, err_str));
     324           0 :     }
     325          11 :     debug(0, "Created folder : " + rel_target.string());
     326             : 
     327          11 :     result = git_helper.move(source.string(), target.string(), true, log::Level::DEBUG);
     328          11 :     if (result == ResultCode::FAILED_TO_COPY) return ETRC_RET(ESYSREPO_RESULT(result));
     329          11 :     if (result == ResultCode::FAILED_TO_REMOVE_ALL)
     330           0 :         warn("While moving folder " + rel_source.string() + " some files were left behind.");
     331             : 
     332          11 :     std::string manifest_path = "grepo/" + get_google_manifest_filename();
     333          22 :     get_config_folder()->get_config()->set_manifest_path(manifest_path);
     334          33 :     return ETRC_RET(get_config_folder()->write_config_file());
     335          22 : }
     336             : 
     337           1 : Result CmdInit::fetch_google_manifest()
     338             : {
     339           1 :     Result result;
     340           1 :     ETRC_CALL_RET_NP(result);
     341             : 
     342           1 :     info("Using Google repo tool manifest (--google).");
     343             : 
     344           1 :     auto git_helper = new_git_helper();
     345           1 :     std::string git_repo_path;
     346             : 
     347           1 :     result = clone_manifest_repo(*git_helper, git_repo_path);
     348           1 :     if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     349             : 
     350           1 :     boost::filesystem::path file_path = git_repo_path;
     351           2 :     file_path /= get_google_manifest_filename();
     352             : 
     353           1 :     if (!boost::filesystem::exists(file_path))
     354             :     {
     355           0 :         std::string err_str = "Google repo manifest file not found: " + get_google_manifest_filename();
     356           0 :         error(err_str);
     357           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::FILE_NOT_EXISTING, err_str));
     358           0 :     }
     359             : 
     360           1 :     result = install_google_manifest(*git_helper, git_repo_path);
     361           2 :     return ETRC_RET(ESYSREPO_RESULT(result));
     362           2 : }
     363             : 
     364           0 : Result CmdInit::read_esysrepo_manifest(std::shared_ptr<Manifest> manifest, const std::string &filename)
     365             : {
     366           0 :     Result result;
     367           0 :     ETRC_CALL_RET_NP(result);
     368             : 
     369           0 :     manifest::File manifest_file;
     370             : 
     371           0 :     manifest_file.set_data(manifest);
     372           0 :     result = manifest_file.read(filename);
     373           0 :     return ETRC_RET(ESYSREPO_RESULT(result));
     374           0 : }
     375             : 
     376           9 : Result CmdInit::read_esysrepo_manifest_xml(std::shared_ptr<Manifest> manifest, const std::string &filename)
     377             : {
     378           9 :     Result result;
     379           9 :     ETRC_CALL_RET_NP(result);
     380             : 
     381           9 :     manifest::XMLFile manifest_file;
     382             : 
     383          18 :     manifest_file.set_data(manifest);
     384           9 :     result = manifest_file.read(filename);
     385          27 :     return ETRC_RET(ESYSREPO_RESULT(result));
     386           9 : }
     387             : 
     388           0 : Result CmdInit::read_esysrepo_manifest_json(std::shared_ptr<Manifest> manifest, const std::string &filename)
     389             : {
     390             :     //! \TODO
     391           0 :     return ESYSREPO_RESULT(ResultCode::NOT_IMPLEMENTED);
     392             : }
     393             : 
     394           9 : Result CmdInit::fetch_esysrepo_manifest(GitHelper &git_helper, const std::string &git_repo_path,
     395             :                                         const std::string &manifest_filename)
     396             : {
     397           9 :     Result result;
     398           9 :     ETRC_CALL_RET(result, git_repo_path, manifest_filename);
     399             : 
     400           9 :     std::string manifest_path;
     401           9 :     boost::filesystem::path manifest_filename_path = manifest_filename;
     402           9 :     std::string manifest_filename_ext = manifest_filename_path.extension().string();
     403             : 
     404           9 :     info("ESysRepo manifest detected.");
     405           9 :     debug(0, "Manifest filename : " + manifest_filename);
     406             : 
     407           9 :     auto config_file = get_config_folder()->get_or_new_config();
     408             : 
     409           9 :     config_file->set_manifest_type(manifest::Type::ESYSREPO_MANIFEST);
     410           9 :     config_file->set_manifest_url(get_url());
     411             : 
     412           9 :     boost::filesystem::path source = git_repo_path;
     413          18 :     boost::filesystem::path rel_source = boost::filesystem::relative(source);
     414           9 :     boost::filesystem::path target;
     415           9 :     boost::filesystem::path file_path = git_repo_path;
     416           9 :     file_path /= manifest_filename;
     417             : 
     418           9 :     std::shared_ptr<Manifest> manifest = std::make_shared<Manifest>();
     419             : 
     420           9 :     if (manifest_filename_ext == ".manifest")
     421             :     {
     422           0 :         result = read_esysrepo_manifest(manifest, file_path.string());
     423           0 :         if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     424             :     }
     425           9 :     else if (manifest_filename_ext == ".xml")
     426             :     {
     427          18 :         result = read_esysrepo_manifest_xml(manifest, file_path.string());
     428           9 :         if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     429           9 :         manifest->set_format(manifest::Format::XML);
     430           9 :         config_file->set_manifest_format(manifest::Format::XML);
     431             :     }
     432           0 :     else if (manifest_filename_ext == ".json")
     433             :     {
     434           0 :         result = read_esysrepo_manifest_json(manifest, file_path.string());
     435           0 :         if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     436           0 :         manifest->set_format(manifest::Format::JSON);
     437           0 :         config_file->set_manifest_format(manifest::Format::JSON);
     438             :     }
     439             :     else
     440             :     {
     441           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::MANIFEST_FILE_EXT_UNKNOWN, manifest_filename_ext));
     442             :     }
     443             : 
     444           9 :     config_file->set_manifest_format(manifest->get_format());
     445             : 
     446           9 :     if (manifest->get_kind() == manifest::Kind::NOT_SET) manifest->set_kind(manifest::Kind::ISOLATED);
     447             : 
     448           9 :     if (manifest->get_kind() == manifest::Kind::EMBEDDED)
     449             :     {
     450           1 :         info("Embedded kind.");
     451           1 :         config_file->set_manifest_kind(manifest::Kind::EMBEDDED);
     452           2 :         target = get_config_folder()->get_workspace_path();
     453           1 :         result = git_helper.move(source.string(), target.string(), true, log::Level::DEBUG);
     454           1 :         if (result == ResultCode::FAILED_TO_COPY) return ETRC_RET(ESYSREPO_RESULT(result));
     455           1 :         if (result == ResultCode::FAILED_TO_REMOVE_ALL)
     456           0 :             warn("While moving folder " + rel_source.string() + " some files were left behind.");
     457           1 :         target = "..";
     458           1 :         target /= manifest_filename;
     459           1 :         manifest_path = target.string();
     460             :     }
     461           8 :     else if (manifest->get_kind() == manifest::Kind::ISOLATED)
     462             :     {
     463           8 :         info("Isolated kind.");
     464           8 :         config_file->set_manifest_kind(manifest::Kind::ISOLATED);
     465          16 :         target = get_config_folder()->get_path();
     466             : 
     467           8 :         target /= manifest::Base::get_folder_name();
     468           8 :         boost::filesystem::path rel_target;
     469          16 :         rel_target = boost::filesystem::relative(target);
     470             : 
     471           8 :         bool result_bool = boost::filesystem::create_directory(target);
     472           8 :         if (!result_bool)
     473             :         {
     474           0 :             error("Couldn't create the folder : " + rel_target.string());
     475           0 :             return ETRC_RET(ESYSREPO_RESULT(ResultCode::FOLDER_CREATION_ERROR, target.string()));
     476             :         }
     477             :         else
     478           8 :             debug(0, "Created folder : " + rel_target.string());
     479             : 
     480           8 :         result = git_helper.move(source.string(), target.string(), true, log::Level::DEBUG);
     481           8 :         if (result == ResultCode::FAILED_TO_COPY) return ETRC_RET(ESYSREPO_RESULT(result));
     482           8 :         if (result == ResultCode::FAILED_TO_REMOVE_ALL)
     483           0 :             warn("While moving folder " + rel_source.string() + " some files were left behind.");
     484           8 :         target = manifest::Base::get_folder_name();
     485           8 :         target /= manifest_filename;
     486          24 :         manifest_path = target.generic_path().string();
     487           8 :     }
     488             :     else
     489           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::MANIFEST_KIND_UNKNOWN));
     490             : 
     491           9 :     config_file->set_manifest_path(manifest_path);
     492           9 :     result = get_config_folder()->write_config_file();
     493          18 :     return ETRC_RET(ESYSREPO_RESULT(result));
     494          18 : }
     495             : 
     496           0 : Result CmdInit::fetch_git_super_project()
     497             : {
     498           0 :     return ESYSREPO_RESULT(ResultCode::NOT_IMPLEMENTED);
     499             : }
     500             : 
     501          19 : Result CmdInit::fetch_unknown_manifest()
     502             : {
     503          19 :     Result result;
     504          19 :     ETRC_CALL_RET_NP(result);
     505             : 
     506          19 :     info("Detecting the manifest type ...");
     507             : 
     508          19 :     auto git_helper = new_git_helper();
     509          19 :     std::string git_repo_path;
     510             : 
     511          19 :     result = clone_manifest_repo(*git_helper, git_repo_path);
     512          19 :     if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     513             : 
     514          19 :     boost::filesystem::path path = git_repo_path;
     515          19 :     boost::filesystem::path file_path = path;
     516          19 :     file_path /= ".esysrepo.manifest";
     517             : 
     518          19 :     if (boost::filesystem::exists(file_path))
     519             :     {
     520           0 :         result = fetch_esysrepo_manifest(*git_helper.get(), path.string(), ".esysrepo.manifest");
     521           0 :         return ETRC_RET(ESYSREPO_RESULT(result));
     522             :     }
     523             : 
     524          19 :     file_path = path;
     525          19 :     file_path /= ".esysrepo.manifest.xml";
     526             : 
     527          19 :     if (boost::filesystem::exists(file_path))
     528             :     {
     529           9 :         result = fetch_esysrepo_manifest(*git_helper.get(), path.string(), ".esysrepo.manifest.xml");
     530          18 :         return ETRC_RET(ESYSREPO_RESULT(result));
     531             :     }
     532             : 
     533          10 :     file_path = path;
     534          10 :     file_path /= ".esysrepo.manifest.json";
     535             : 
     536          10 :     if (boost::filesystem::exists(file_path))
     537             :     {
     538           0 :         result = fetch_esysrepo_manifest(*git_helper.get(), path.string(), ".esysrepo.manifest.json");
     539           0 :         return ETRC_RET(ESYSREPO_RESULT(result));
     540             :     }
     541             : 
     542          10 :     file_path = path;
     543          20 :     file_path /= get_google_manifest_filename();
     544             : 
     545          10 :     if (boost::filesystem::exists(file_path))
     546             :     {
     547          10 :         info("Google repo tool manifest detected.");
     548          10 :         result = install_google_manifest(*git_helper, git_repo_path);
     549          20 :         return ETRC_RET(ESYSREPO_RESULT(result));
     550             :     }
     551             : 
     552           0 :     file_path = path;
     553           0 :     file_path /= ".gitmodules";
     554             : 
     555           0 :     if (boost::filesystem::exists(file_path))
     556             :     {
     557           0 :         info("Git submodule detected.");
     558           0 :         auto config = get_config_folder()->get_or_new_config();
     559             : 
     560           0 :         config->set_manifest_type(manifest::Type::GIT_SUPER_PROJECT);
     561           0 :         config->set_manifest_url(get_url());
     562           0 :         boost::filesystem::path source = path.string();
     563           0 :         boost::filesystem::path target = get_config_folder()->get_workspace_path();
     564             : 
     565           0 :         result = git_helper->move(source.string(), target.string(), true, log::Level::DEBUG);
     566           0 :         if (result == ResultCode::FAILED_TO_COPY) return ETRC_RET(ESYSREPO_RESULT(result));
     567           0 :         if (result == ResultCode::FAILED_TO_REMOVE_ALL)
     568           0 :             warn("While moving folder " + source.string() + " some files were left behind.");
     569           0 :         return ETRC_RET(ESYSREPO_RESULT(ResultCode::OK));
     570           0 :     }
     571             : 
     572           0 :     return ETRC_RET(ESYSREPO_RESULT(ResultCode::CMDINIT_FAILED_FETCH_UNKNOWN_MANIFEST));
     573          57 : }
     574             : 
     575          21 : Result CmdInit::load_esysrepo_folder()
     576             : {
     577          21 :     Result result;
     578          21 :     ETRC_CALL_RET_NP(result);
     579             : 
     580          21 :     auto config_folder = std::make_shared<ConfigFolder>();
     581          21 :     config_folder->set_logger_if(get_logger_if());
     582             : 
     583          42 :     set_config_folder(config_folder);
     584             : 
     585          21 :     ETRC_MSG("parent_path = " + get_workspace_path());
     586             : 
     587          21 :     result = config_folder->open(get_workspace_path(), false);
     588          63 :     return ETRC_RET(ESYSREPO_RESULT(result));
     589          21 : }
     590             : 
     591          20 : Result CmdInit::create_esysrepo_folder()
     592             : {
     593          20 :     Result result;
     594          20 :     ETRC_CALL_RET_NP(result);
     595             : 
     596          20 :     auto config_folder = get_config_folder();
     597          20 :     if (config_folder == nullptr)
     598             :     {
     599           0 :         config_folder = std::make_shared<ConfigFolder>();
     600           0 :         config_folder->set_logger_if(get_logger_if());
     601             : 
     602           0 :         set_config_folder(config_folder);
     603             :     }
     604             : 
     605          20 :     ETRC_MSG("parent_path = " + get_workspace_path());
     606             : 
     607          20 :     result = config_folder->create(get_workspace_path(), true);
     608          60 :     return ESYSREPO_RESULT(result);
     609          20 : }
     610             : 
     611           1 : Result CmdInit::find_url_from_project_name()
     612             : {
     613           1 :     Result result;
     614           1 :     ETRC_CALL_RET_NP(result);
     615             : 
     616           1 :     manifest::DirectoryMngr mngr;
     617             : 
     618           1 :     boost::filesystem::path path = get_config_folder()->get_temp_path();
     619           1 :     path /= "manifest_directories";
     620           1 :     mngr.set_folder_path(path.string());
     621             : 
     622           1 :     if (get_directories() == nullptr)
     623             :     {
     624           1 :         auto directories = std::make_shared<manifest::Directories>();
     625           1 :         directories->add_default_libesys();
     626             : 
     627           2 :         mngr.set_directories(directories);
     628           3 :         set_directories(directories);
     629           1 :     }
     630             : 
     631           1 :     result = mngr.load();
     632           1 :     if (result.error()) return ETRC_RET(ESYSREPO_RESULT(result));
     633             : 
     634           1 :     auto project = get_directories()->find_project(get_project_name());
     635           1 :     if (project == nullptr) return ETRC_RET(ESYSREPO_RESULT(ResultCode::GENERIC_ERROR));
     636             : 
     637           1 :     set_url(project->get_url_ssh());
     638           2 :     return ETRC_RET(ESYSREPO_RESULT(ResultCode::OK));
     639           1 : }
     640             : 
     641           1 : void CmdInit::set_directories(std::shared_ptr<manifest::Directories> directories)
     642             : {
     643           1 :     m_directories = directories;
     644           1 : }
     645             : 
     646           2 : std::shared_ptr<manifest::Directories> CmdInit::get_directories() const
     647             : {
     648           2 :     return m_directories;
     649             : }
     650             : 
     651             : } // namespace esys::repo::exe

Generated by: LCOV version 1.14