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

repository.cpp (3543B)


0 #include "git2wrap/repository.hpp"
2 #include "git2wrap/commit.hpp"
3 #include "git2wrap/error.hpp"
4 #include "git2wrap/tag.hpp"
6 namespace git2wrap
7 {
9 repository::repository(git_repository* repo)
10 : m_repo(repo, git_repository_free)
11 {
12 }
14 repository::repository(repositoryPtr repo)
15 : m_repo(std::move(repo))
16 {
17 }
19 repository::repository(const char* path, unsigned is_bare)
20 {
21 git_repository* repo = nullptr;
23 if (git_repository_init(&repo, path, is_bare) != 0) {
24 throw error<error_code_t::error>();
25 }
27 m_repo = {repo, git_repository_free};
28 }
30 repository::repository(const char* path, init_options* opts)
31 {
32 git_repository* repo = nullptr;
34 if (git_repository_init_ext(&repo, path, opts) != 0) {
35 throw error<error_code_t::error>();
36 }
38 m_repo = {repo, git_repository_free};
39 }
41 repository repository::clone(
42 const char* url, const char* local_path, const clone_options* options
43 )
44 {
45 git_repository* repo = nullptr;
47 if (git_clone(&repo, url, local_path, options) != 0) {
48 throw error<error_code_t::error>();
49 }
51 return repository(repo);
52 }
54 repository repository::open(const char* path)
55 {
56 git_repository* repo = nullptr;
58 if (git_repository_open(&repo, path) != 0) {
59 throw error<error_code_t::error>();
60 }
62 return repository(repo);
63 }
65 repository repository::open(
66 const char* path, flags_open::type flags, const char* ceiling_dirs
67 )
68 {
69 git_repository* repo = nullptr;
71 const auto err = error_code_t(
72 git_repository_open_ext(&repo, path, flags.value, ceiling_dirs)
73 );
75 if (err == error_code_t::ok) {
76 return repository(repo);
77 }
79 if (err == error_code_t::enotfound) {
80 throw error<error_code_t::enotfound>();
81 }
83 throw error<error_code_t::error>();
84 }
86 object repository::revparse(const char* spec) const
87 {
88 git_object* obj = nullptr;
90 const auto err = error_code_t(git_revparse_single(&obj, m_repo.get(), spec));
91 if (err == error_code_t::ok) {
92 return {obj, m_repo};
93 }
95 if (err == error_code_t::enotfound) {
96 throw error<error_code_t::enotfound>();
97 }
99 if (err == error_code_t::eambiguous) {
100 throw error<error_code_t::eambiguous>();
103 if (err == error_code_t::einvalidspec) {
104 throw error<error_code_t::einvalidspec>();
107 throw error<error_code_t::error>();
110 commit repository::commit_lookup(const oid& objid) const
112 git_commit* commit = nullptr;
114 if (git_commit_lookup(&commit, m_repo.get(), objid.ptr()) != 0) {
115 throw error<error_code_t::error>();
118 return {commit, m_repo};
121 blob repository::blob_lookup(const oid& objid) const
123 git_blob* blob = nullptr;
125 if (git_blob_lookup(&blob, m_repo.get(), objid.ptr()) != 0) {
126 throw error<error_code_t::error>();
129 return {blob, m_repo};
132 tag repository::tag_lookup(const oid& objid) const
134 git_tag* tagg = nullptr;
136 if (git_tag_lookup(&tagg, m_repo.get(), objid.ptr()) != 0) {
137 throw error<error_code_t::error>();
140 return {tagg, m_repo};
143 branch_iterator repository::branch_end() const // NOLINT
145 return branch_iterator();
148 branch_iterator repository::branch_begin(branch::flags_list::type flags) const
150 git_branch_iterator* iter = nullptr;
152 if (git_branch_iterator_new(&iter, m_repo.get(), git_branch_t(flags.value))
153 != 0)
155 throw error<error_code_t::error>();
158 return branch_iterator(iter);
161 void repository::tag_foreach(git_tag_foreach_cb callback, void* payload) const
163 if (git_tag_foreach(m_repo.get(), callback, payload) != 0) {
164 throw error<error_code_t::error>();
168 } // namespace git2wrap