git2wrap

C++20 wrapper for libgit2
git clone git://git.dimitrijedobrota.com/git2wrap.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

branch.cpp (1453B)


1 #include "git2wrap/branch.hpp" 2 3 #include "git2wrap/error.hpp" 4 5 namespace git2wrap 6 { 7 8 branch::branch(git_reference* ref, git_branch_t type) 9 : m_ref(ref) 10 , m_type(type) 11 { 12 } 13 14 branch::branch(reference ref, git_branch_t type) 15 : m_ref(std::move(ref)) 16 , m_type(type) 17 { 18 } 19 20 branch branch::dup() const 21 { 22 return {m_ref.dup(), m_type}; 23 } 24 25 const std::string& branch::get_name() 26 { 27 if (!m_name.empty()) { 28 return m_name; 29 } 30 31 const char* name = nullptr; 32 switch (git_branch_name(&name, m_ref.get())) { 33 case error_code_t::OK: 34 break; 35 case error_code_t::EINVALID: 36 throw error<error_code_t::EINVALID>(); 37 default: 38 throw error<error_code_t::ERROR>(); 39 } 40 41 return m_name = name; 42 } 43 44 branch_iterator::branch_iterator(git_branch_iterator* iter) 45 : m_iter(iter, git_branch_iterator_free) 46 { 47 if (iter != nullptr) { 48 ++*this; 49 } 50 } 51 52 branch_iterator& branch_iterator::operator++() 53 { 54 git_reference* ref = nullptr; 55 git_branch_t type = {}; 56 57 switch (git_branch_next(&ref, &type, get())) { 58 case error_code_t::OK: 59 case error_code_t::ITEROVER: 60 break; 61 default: 62 throw error<error_code_t::ERROR>(); 63 } 64 65 m_branch = branch(ref, type); 66 return *this; 67 } 68 69 bool operator==(const branch_iterator& lhs, const branch_iterator& rhs) 70 { 71 return lhs.m_branch == rhs.m_branch; 72 } 73 74 bool operator!=(const branch_iterator& lhs, const branch_iterator& rhs) 75 { 76 return !(lhs == rhs); 77 } 78 79 } // namespace git2wrap