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 (1463B)


0 #include "git2wrap/branch.hpp"
2 #include "git2wrap/error.hpp"
4 namespace git2wrap
5 {
7 branch::branch(git_reference* ref, git_branch_t type)
8 : m_ref(ref)
9 , m_type(type)
10 {
11 }
13 branch::branch(reference ref, git_branch_t type)
14 : m_ref(std::move(ref))
15 , m_type(type)
16 {
17 }
19 branch branch::dup() const
20 {
21 return {m_ref.dup(), m_type};
22 }
24 const std::string& branch::get_name()
25 {
26 if (!m_name.empty()) {
27 return m_name;
28 }
30 const char* name = nullptr;
31 const auto err = error_code_t(git_branch_name(&name, m_ref.get()));
33 if (err == error_code_t::ok) {
34 return m_name = name;
35 }
37 if (err == error_code_t::einvalid) {
38 throw error<error_code_t::einvalid>();
39 }
41 throw error<error_code_t::error>();
42 }
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 }
52 branch_iterator& branch_iterator::operator++()
53 {
54 git_reference* ref = nullptr;
55 git_branch_t type = {};
57 const auto err = error_code_t(git_branch_next(&ref, &type, get()));
58 if (err == error_code_t::ok || err == error_code_t::iterover) {
59 m_branch = branch(ref, type);
60 return *this;
61 }
63 throw error<error_code_t::error>();
64 }
66 bool operator==(const branch_iterator& lhs, const branch_iterator& rhs)
67 {
68 return lhs.m_branch == rhs.m_branch;
69 }
71 bool operator!=(const branch_iterator& lhs, const branch_iterator& rhs)
72 {
73 return !(lhs == rhs);
74 }
76 } // namespace git2wrap