Line data Source code
1 : /*! 2 : * \file esys/repo/git/branches_git.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/git/branches.h" 20 : 21 : #include <algorithm> 22 : 23 : namespace esys::repo::git 24 : { 25 : 26 151 : Branches::Branches() = default; 27 : 28 2 : Branches::Branches(const std::vector<Branch> &branches) 29 : { 30 6 : for (auto &branch_raw : branches) 31 : { 32 4 : std::shared_ptr<Branch> branch = std::make_shared<Branch>(branch_raw); 33 : 34 12 : add(branch); 35 4 : } 36 2 : } 37 : 38 257 : Branches::~Branches() = default; 39 : 40 252 : void Branches::add(std::shared_ptr<Branch> branch) 41 : { 42 252 : m_branches.push_back(branch); 43 252 : if (branch->get_is_head()) m_head = branch; 44 252 : } 45 : 46 54 : void Branches::clear() 47 : { 48 54 : m_head = nullptr; 49 54 : m_branches.clear(); 50 54 : } 51 : 52 66 : std::shared_ptr<Branch> Branches::get_head() const 53 : { 54 66 : return m_head; 55 : } 56 : 57 151 : std::size_t Branches::size() const 58 : { 59 151 : return m_branches.size(); 60 : } 61 : 62 105 : void Branches::sort() 63 : { 64 146 : auto head_first = [](const std::shared_ptr<git::Branch> b0, const std::shared_ptr<git::Branch> b1) -> bool { 65 41 : return b0->get_is_head(); 66 : }; 67 : 68 105 : std::sort(get().begin(), get().end(), head_first); 69 105 : } 70 : 71 394 : std::vector<std::shared_ptr<Branch>> &Branches::get() 72 : { 73 394 : return m_branches; 74 : } 75 : 76 0 : const std::vector<std::shared_ptr<Branch>> &Branches::get() const 77 : { 78 0 : return m_branches; 79 : } 80 : 81 0 : std::shared_ptr<Branch> Branches::find(const std::string &name) 82 : { 83 0 : for (auto branch : get()) 84 : { 85 0 : if (branch->get_name() == name) return branch; 86 0 : } 87 0 : return nullptr; 88 : } 89 : 90 0 : bool Branches::operator==(const Branches &other) const 91 : { 92 0 : if (get().size() != other.get().size()) return false; 93 0 : for (auto idx = 0; idx < get().size(); ++idx) 94 : { 95 0 : if ((get()[idx] == nullptr) && (other.get()[idx] == nullptr)) continue; 96 0 : if ((get()[idx] == nullptr) || (other.get()[idx] == nullptr)) return false; 97 0 : if (*get()[idx] != *other.get()[idx]) return false; 98 : } 99 0 : if ((get_head() == nullptr) && (other.get_head() == nullptr)) return true; 100 0 : if ((get_head() == nullptr) || (other.get_head() == nullptr)) return false; 101 0 : return (*get_head() == *other.get_head()); 102 : } 103 : 104 0 : bool Branches::operator!=(const Branches &other) const 105 : { 106 0 : return !operator==(other); 107 : } 108 : 109 : } // namespace esys::repo::git 110 : 111 : namespace std 112 : { 113 : 114 0 : ESYSREPO_API ostream &operator<<(ostream &os, const esys::repo::git::Branches &branches) 115 : { 116 0 : os << "Branches(" << branches.size() << ")" << std::endl; 117 0 : os << "{" << std::endl; 118 0 : for (auto branch : branches.get()) 119 : { 120 0 : os << " name: " << branch->get_name(); 121 0 : if (branch->get_is_head()) os << " [head]"; 122 0 : os << std::endl; 123 0 : } 124 0 : os << "}"; 125 0 : return os; 126 : } 127 : 128 : } // namespace std