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