Line data Source code
1 : /*! 2 : * \file esys/repo/exe/cmdinfo.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/exe/cmdinfo.h" 20 : #include "esys/repo/exe/nested.h" 21 : #include "esys/repo/manifest/repository.h" 22 : #include "esys/repo/githelper.h" 23 : 24 : #include <boost/filesystem.hpp> 25 : 26 : #include <sstream> 27 : #include <iomanip> 28 : 29 : namespace esys::repo::exe 30 : { 31 : 32 17 : CmdInfo::CmdInfo() 33 34 : : Cmd("Info") 34 : { 35 17 : } 36 : 37 19 : CmdInfo::~CmdInfo() = default; 38 : 39 0 : void CmdInfo::set_diff(bool diff) 40 : { 41 0 : m_diff = diff; 42 0 : } 43 : 44 0 : bool CmdInfo::get_diff() const 45 : { 46 0 : return m_diff; 47 : } 48 : 49 0 : void CmdInfo::set_overview(bool overview) 50 : { 51 0 : m_overview = overview; 52 0 : } 53 : 54 0 : bool CmdInfo::get_overview() const 55 : { 56 0 : return m_overview; 57 : } 58 : 59 0 : void CmdInfo::set_current_branch(bool current_branch) 60 : { 61 0 : m_current_branch = current_branch; 62 0 : } 63 : 64 0 : bool CmdInfo::get_current_branch() const 65 : { 66 0 : return m_current_branch; 67 : } 68 : 69 0 : void CmdInfo::set_local_only(bool local_only) 70 : { 71 0 : m_local_only = local_only; 72 0 : } 73 : 74 0 : bool CmdInfo::get_local_only() const 75 : { 76 0 : return m_local_only; 77 : } 78 : 79 2 : Result CmdInfo::impl_run() 80 : { 81 2 : Result result = default_handling_folder_workspace(); 82 2 : if (result.error()) return ESYSREPO_RESULT(result); 83 : 84 2 : result = open_esysrepo_folder(); 85 2 : if (result.error()) return ESYSREPO_RESULT(result); 86 : 87 2 : result = load_manifest(); 88 2 : if (result.error()) return ESYSREPO_RESULT(result); 89 : 90 2 : Result rresult; 91 : 92 8 : for (auto location : get_manifest()->get_locations()) 93 : { 94 24 : for (auto repo : location->get_repos()) 95 : { 96 40 : result = open_repo(repo); 97 20 : if (result.error()) 98 : { 99 0 : rresult.add(ESYSREPO_RESULT(result)); 100 0 : continue; 101 : } 102 60 : print_repo(repo); 103 20 : } 104 4 : } 105 : 106 2 : NestedContext nested_scratch; 107 2 : NestedContext *nested_ctx = get_nested_context() != nullptr ? get_nested_context() : &nested_scratch; 108 2 : set_nested_context(nested_ctx); 109 2 : Result nested = for_each_nested(*this, NestedMissingMode::WarnSkip, *nested_ctx, 110 0 : [this](const std::string &child_abs, std::shared_ptr<manifest::Repository>) { 111 0 : CmdInfo child; 112 0 : prepare_nested_cmd(*this, child, child_abs); 113 0 : child.set_diff(get_diff()); 114 0 : child.set_overview(get_overview()); 115 0 : child.set_local_only(get_local_only()); 116 0 : return child.run(); 117 2 : }); 118 2 : if (nested.error()) rresult.add(ESYSREPO_RESULT(nested)); 119 : 120 4 : return ESYSREPO_RESULT(rresult); 121 2 : } 122 : 123 20 : Result CmdInfo::open_repo(std::shared_ptr<manifest::Repository> repo) 124 : { 125 20 : auto git_helper = new_git_helper(); 126 : 127 20 : boost::filesystem::path rel_repo_path; 128 20 : boost::filesystem::path repo_path = get_config_folder()->get_workspace_path(); 129 20 : repo_path /= repo->get_path(); 130 60 : repo_path = boost::filesystem::absolute(repo_path).lexically_normal().make_preferred(); 131 40 : rel_repo_path = boost::filesystem::relative(repo_path); 132 : 133 20 : m_rel_repo_path = rel_repo_path.string(); 134 20 : m_repo_path = repo_path.string(); 135 : 136 20 : Result result = git_helper->open(repo_path.string(), log::Level::DEBUG); 137 20 : if (result.error()) return ESYSREPO_RESULT(result); 138 : 139 20 : m_last_commit = std::make_shared<git::CommitHash>(); 140 20 : result = get_git()->get_last_commit(*m_last_commit); 141 20 : if (result.error()) m_last_commit.reset(); 142 : 143 20 : m_branches.clear(); 144 : 145 20 : result = git_helper->get_branches(m_branches, git::BranchType::LOCAL, log::Level::DEBUG); 146 20 : if (result.error()) 147 : { 148 0 : git_helper->close_on_error(log::Level::DEBUG); 149 0 : return ESYSREPO_RESULT(result); 150 : } 151 20 : result = git_helper->close(log::Level::DEBUG); 152 40 : return ESYSREPO_RESULT(result); 153 40 : } 154 : 155 20 : void CmdInfo::print_repo(std::shared_ptr<manifest::Repository> repo) 156 : { 157 20 : std::ostringstream oss; 158 20 : std::shared_ptr<git::Branch> cur_branch = nullptr; 159 : 160 20 : for (auto branch : m_branches.get()) 161 : { 162 20 : if (branch->get_is_head()) 163 : { 164 20 : cur_branch = branch; 165 20 : break; 166 : } 167 0 : } 168 : 169 20 : boost::filesystem::path p; 170 : 171 20 : oss << "Repo : " << repo->get_name() << std::endl; 172 20 : p = m_rel_repo_path; 173 40 : p = p.generic_path(); 174 : 175 20 : oss << " Path : " << p.string() << std::endl; 176 20 : if (m_last_commit) 177 20 : oss << " Cur rev. : " << m_last_commit->get_hash() << std::endl; 178 : else 179 0 : oss << " Cur rev. : ???" << std::endl; 180 20 : if (cur_branch) 181 20 : oss << " Cur branch : " << cur_branch->get_name() << std::endl; 182 : else 183 0 : oss << " Cur branch : ???" << std::endl; 184 80 : oss << " Manifest rev. : " << get_manifest()->get_repo_revision(repo) << std::endl; 185 20 : oss << " Local branches : " << m_branches.size(); 186 : 187 20 : if (m_branches.size() == 0) 188 : { 189 0 : info(oss.str()); 190 0 : return; 191 : } 192 : 193 20 : std::size_t idx = 0; 194 40 : for (auto branch : m_branches.get()) 195 : { 196 20 : oss << std::endl << " [" << idx << "] " << std::left << std::setw(20) << branch->get_name(); 197 20 : oss << " -> " << branch->get_remote_branch(); 198 20 : ++idx; 199 20 : } 200 : 201 40 : info(oss.str()); 202 40 : } 203 : 204 : } // namespace esys::repo::exe