git2wrapC++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 (3556B)
1 #include "git2wrap/repository.hpp" 2 3 #include "git2wrap/commit.hpp" 4 #include "git2wrap/error.hpp" 5 #include "git2wrap/tag.hpp" 6 7 namespace git2wrap 8 { 9 10 repository::repository(git_repository* repo) 11 : m_repo(repo, git_repository_free) 12 { 13 } 14 15 repository::repository(repositoryPtr repo) 16 : m_repo(std::move(repo)) 17 { 18 } 19 20 repository::repository(const char* path, unsigned is_bare) 21 { 22 git_repository* repo = nullptr; 23 24 if (git_repository_init(&repo, path, is_bare) != 0) { 25 throw error<error_code_t::ERROR>(); 26 } 27 28 m_repo = {repo, git_repository_free}; 29 } 30 31 repository::repository(const char* path, init_options* opts) 32 { 33 git_repository* repo = nullptr; 34 35 if (git_repository_init_ext(&repo, path, opts) != 0) { 36 throw error<error_code_t::ERROR>(); 37 } 38 39 m_repo = {repo, git_repository_free}; 40 } 41 42 repository repository::clone(const char* url, 43 const char* local_path, 44 const clone_options* options) 45 { 46 git_repository* repo = nullptr; 47 48 if (git_clone(&repo, url, local_path, options) != 0) { 49 throw error<error_code_t::ERROR>(); 50 } 51 52 return repository(repo); 53 } 54 55 repository repository::open(const char* path) 56 { 57 git_repository* repo = nullptr; 58 59 if (git_repository_open(&repo, path) != 0) { 60 throw error<error_code_t::ERROR>(); 61 } 62 63 return repository(repo); 64 } 65 66 repository repository::open(const char* path, 67 unsigned flags, 68 const char* ceiling_dirs) 69 { 70 git_repository* repo = nullptr; 71 72 switch (git_repository_open_ext(&repo, path, flags, ceiling_dirs)) { 73 case error_code_t::OK: 74 break; 75 case error_code_t::ENOTFOUND: 76 throw error<error_code_t::ENOTFOUND>(); 77 default: 78 throw error<error_code_t::ERROR>(); 79 } 80 81 return repository(repo); 82 } 83 84 object repository::revparse(const char* spec) const 85 { 86 git_object* obj = nullptr; 87 88 switch (git_revparse_single(&obj, m_repo.get(), spec)) { 89 case error_code_t::OK: 90 break; 91 case error_code_t::ENOTFOUND: 92 throw error<error_code_t::ENOTFOUND>(); 93 case error_code_t::EAMBIGUOUS: 94 throw error<error_code_t::EAMBIGUOUS>(); 95 case error_code_t::EINVALIDSPEC: 96 throw error<error_code_t::EINVALIDSPEC>(); 97 default: 98 throw error<error_code_t::ERROR>(); 99 } 100 101 return {obj, m_repo}; 102 } 103 104 commit repository::commit_lookup(const oid& objid) const 105 { 106 git_commit* commit = nullptr; 107 108 if (git_commit_lookup(&commit, m_repo.get(), objid.ptr()) != 0) { 109 throw error<error_code_t::ERROR>(); 110 } 111 112 return {commit, m_repo}; 113 } 114 115 blob repository::blob_lookup(const oid& objid) const 116 { 117 git_blob* blob = nullptr; 118 119 if (git_blob_lookup(&blob, m_repo.get(), objid.ptr()) != 0) { 120 throw error<error_code_t::ERROR>(); 121 } 122 123 return {blob, m_repo}; 124 } 125 126 tag repository::tag_lookup(const oid& objid) const 127 { 128 git_tag* tagg = nullptr; 129 130 if (git_tag_lookup(&tagg, m_repo.get(), objid.ptr()) != 0) { 131 throw error<error_code_t::ERROR>(); 132 } 133 134 return {tagg, m_repo}; 135 } 136 137 branch_iterator repository::branch_end() const // NOLINT 138 { 139 return branch_iterator(); 140 } 141 142 branch_iterator repository::branch_begin(git_branch_t list_flags) const 143 { 144 git_branch_iterator* iter = nullptr; 145 146 if (git_branch_iterator_new(&iter, m_repo.get(), list_flags) != 0) { 147 throw error<error_code_t::ERROR>(); 148 } 149 150 return branch_iterator(iter); 151 } 152 153 void repository::tag_foreach(git_tag_foreach_cb callback, void* payload) const 154 { 155 if (git_tag_foreach(m_repo.get(), callback, payload) != 0) { 156 throw error<error_code_t::ERROR>(); 157 } 158 } 159 160 } // namespace git2wrap