LCOV - code coverage report
Current view: top level - src/esys/repo/exe - cmdinit.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 289 403 71.7 %
Date: 2026-08-12 14:26:50 Functions: 33 39 84.6 %

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

Generated by: LCOV version 1.14