Line data Source code
1 : /*! 2 : * \file esys/repo/git/repostatus.hpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2020 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/git/repostatus.h" 20 : 21 : #include <algorithm> 22 : 23 : namespace esys::repo::git 24 : { 25 : 26 31 : RepoStatus::RepoStatus() = default; 27 : 28 31 : RepoStatus::~RepoStatus() = default; 29 : 30 6 : void RepoStatus::add(std::shared_ptr<Status> status) 31 : { 32 6 : m_all.push_back(status); 33 : 34 6 : switch (status->get_type()) 35 : { 36 0 : case StatusType::CURRENT: m_current.push_back(status); break; 37 0 : case StatusType::INDEX: m_head_to_index.push_back(status); break; 38 6 : case StatusType::WORKING_DIR: m_index_to_work_dir.push_back(status); break; 39 0 : case StatusType::IGNORED: m_ignored.push_back(status); break; 40 0 : case StatusType::CONFLICTED: m_conflicted.push_back(status); break; 41 6 : default:; 42 : } 43 : 44 12 : std::string old_file = status->get_diff_delta().get_old_file().get_path(); 45 6 : if (old_file.empty()) return; 46 : 47 6 : auto it = m_map_file_status.find(old_file); 48 6 : std::shared_ptr<FileStatus> file_status; 49 : 50 6 : if (it == m_map_file_status.end()) 51 : { 52 6 : file_status = std::make_shared<FileStatus>(old_file); 53 6 : m_map_file_status[old_file] = file_status; 54 6 : m_file_status.push_back(file_status); 55 : } 56 : else 57 0 : file_status = it->second; 58 : 59 18 : file_status->add(status); 60 : } 61 : 62 1 : std::vector<std::shared_ptr<Status>> &RepoStatus::get_all() 63 : { 64 1 : return m_all; 65 : } 66 : 67 0 : const std::vector<std::shared_ptr<Status>> &RepoStatus::get_all() const 68 : { 69 0 : return m_all; 70 : } 71 : 72 0 : std::vector<std::shared_ptr<Status>> &RepoStatus::get_current() 73 : { 74 0 : return m_current; 75 : } 76 : 77 0 : const std::vector<std::shared_ptr<Status>> &RepoStatus::get_current() const 78 : { 79 0 : return m_current; 80 : } 81 : 82 1 : std::vector<std::shared_ptr<Status>> &RepoStatus::get_head_to_index() 83 : { 84 1 : return m_head_to_index; 85 : } 86 : 87 0 : const std::vector<std::shared_ptr<Status>> &RepoStatus::get_head_to_index() const 88 : { 89 0 : return m_head_to_index; 90 : } 91 : 92 1 : std::vector<std::shared_ptr<Status>> &RepoStatus::get_index_to_work_dir() 93 : { 94 1 : return m_index_to_work_dir; 95 : } 96 : 97 0 : const std::vector<std::shared_ptr<Status>> &RepoStatus::get_index_to_work_dir() const 98 : { 99 0 : return m_index_to_work_dir; 100 : } 101 : 102 0 : std::vector<std::shared_ptr<Status>> &RepoStatus::get_ignored() 103 : { 104 0 : return m_ignored; 105 : } 106 : 107 0 : const std::vector<std::shared_ptr<Status>> &RepoStatus::get_ignored() const 108 : { 109 0 : return m_ignored; 110 : } 111 : 112 0 : std::vector<std::shared_ptr<Status>> &RepoStatus::get_conflicted() 113 : { 114 0 : return m_conflicted; 115 : } 116 : 117 0 : const std::vector<std::shared_ptr<Status>> &RepoStatus::get_conflicted() const 118 : { 119 0 : return m_conflicted; 120 : } 121 : 122 33 : std::vector<std::shared_ptr<FileStatus>> &RepoStatus::get_file_status() 123 : { 124 33 : return m_file_status; 125 : } 126 : 127 0 : const std::vector<std::shared_ptr<FileStatus>> &RepoStatus::get_file_status() const 128 : { 129 0 : return m_file_status; 130 : } 131 : 132 30 : void RepoStatus::sort_file_status() 133 : { 134 32 : auto sort_by_name = [](const std::shared_ptr<FileStatus> f0, const std::shared_ptr<FileStatus> f1) -> bool { 135 2 : return f0->get_old_name() < f1->get_old_name(); 136 : }; 137 : 138 30 : std::sort(m_file_status.begin(), m_file_status.end(), sort_by_name); 139 30 : } 140 : 141 0 : std::map<std::string, std::shared_ptr<FileStatus>> &RepoStatus::get_map_file_status() 142 : { 143 0 : return m_map_file_status; 144 : } 145 : 146 0 : const std::map<std::string, std::shared_ptr<FileStatus>> &RepoStatus::get_map_file_status() const 147 : { 148 0 : return m_map_file_status; 149 : } 150 : 151 0 : bool RepoStatus::operator==(const RepoStatus &other) const 152 : { 153 0 : bool equal = is_equal(get_all(), other.get_all()); 154 0 : if (!equal) return false; 155 : 156 0 : equal = is_equal(get_current(), other.get_current()); 157 0 : if (!equal) return false; 158 : 159 0 : equal = is_equal(get_head_to_index(), other.get_head_to_index()); 160 0 : if (!equal) return false; 161 : 162 0 : equal = is_equal(get_index_to_work_dir(), other.get_index_to_work_dir()); 163 0 : if (!equal) return false; 164 : 165 0 : equal = is_equal(get_ignored(), other.get_ignored()); 166 0 : if (!equal) return false; 167 : 168 0 : equal = is_equal(get_conflicted(), other.get_conflicted()); 169 0 : if (!equal) return false; 170 : 171 0 : equal = is_equal(get_file_status(), other.get_file_status()); 172 0 : if (!equal) return false; 173 : 174 : return true; 175 : } 176 : 177 0 : bool RepoStatus::operator!=(const RepoStatus &other) const 178 : { 179 0 : return !operator==(other); 180 : } 181 : 182 0 : bool RepoStatus::is_equal(const std::vector<std::shared_ptr<Status>> &left, 183 : const std::vector<std::shared_ptr<Status>> &right) const 184 : { 185 0 : if (left.size() != right.size()) return false; 186 : 187 0 : for (auto idx = 0; idx < left.size(); ++idx) 188 : { 189 0 : if ((left[idx] == nullptr) && (right[idx] == nullptr)) return true; 190 0 : if ((left[idx] == nullptr) || (right[idx] == nullptr)) return false; 191 0 : if (*left[idx] != *right[idx]) return false; 192 : } 193 : return true; 194 : } 195 : 196 0 : bool RepoStatus::is_equal(const std::vector<std::shared_ptr<FileStatus>> &left, 197 : const std::vector<std::shared_ptr<FileStatus>> &right) const 198 : { 199 0 : if (left.size() != right.size()) return false; 200 : 201 0 : for (auto idx = 0; idx < left.size(); ++idx) 202 : { 203 0 : if ((left[idx] == nullptr) && (right[idx] == nullptr)) return true; 204 0 : if ((left[idx] == nullptr) || (right[idx] == nullptr)) return false; 205 0 : if (*left[idx] != *right[idx]) return false; 206 : } 207 : return true; 208 : } 209 : 210 : } // namespace esys::repo::git 211 : 212 : namespace std 213 : { 214 : 215 0 : ESYSREPO_API ostream &operator<<(ostream &os, const esys::repo::git::RepoStatus &repo_status) 216 : { 217 : //! \TODO 218 0 : os << "RepoStatus TBD"; 219 0 : return os; 220 : } 221 : 222 : } // namespace std