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 |

commit4be291064b1f88449f7e5f1d052fd6d0941e69c9
parente61c8b837306b18bb2c2cd61861dbbac50ddc5fa
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateThu, 16 Jan 2025 18:12:28 +0100

Proper exception throwing

Diffstat:
M.clang-tidy|++--
MCMakeLists.txt|+-
Minclude/git2wrap/error.hpp|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Msource/blob.cpp|+-------
Msource/branch.cpp|+++++++++++++------
Msource/commit.cpp|++++++++++++-----------
Msource/diff.cpp|++++++-----
Msource/libgit2.cpp|+-
Msource/object.cpp|+++-------
Msource/oid.cpp|+-----
Msource/reference.cpp|+-----
Msource/repository.cpp|++++++++++++++++++++++++++++++++++++----------------------
Msource/revwalk.cpp|+++++++++++++++------------
Msource/signature.cpp|+-----
Msource/tag.cpp|+-----
Msource/tree.cpp|+++++++++++++++-------------
Mtest/source/git2wrap_test.cpp|++-----

17 files changed, 299 insertions(+), 133 deletions(-)


diff --git a/.clang-tidy b/.clang-tidy

@@ -79,7 +79,7 @@ CheckOptions:

- key: 'readability-identifier-naming.EnumCase'
value: 'lower_case'
- key: 'readability-identifier-naming.EnumConstantCase'
value: 'lower_case'
value: 'UPPER_CASE'
- key: 'readability-identifier-naming.FunctionCase'
value: 'lower_case'
- key: 'readability-identifier-naming.GlobalConstantCase'

@@ -133,7 +133,7 @@ CheckOptions:

- key: 'readability-identifier-naming.PublicMethodCase'
value: 'lower_case'
- key: 'readability-identifier-naming.ScopedEnumConstantCase'
value: 'lower_case'
value: 'UPPER_CASE'
- key: 'readability-identifier-naming.StaticConstantCase'
value: 'lower_case'
- key: 'readability-identifier-naming.StaticVariableCase'

diff --git a/CMakeLists.txt b/CMakeLists.txt

@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)

project(
git2wrap
VERSION 0.1.17
VERSION 0.1.18
DESCRIPTION "C++ 20 wrapper for libgit2"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/git2wrap.git"
LANGUAGES CXX

diff --git a/include/git2wrap/error.hpp b/include/git2wrap/error.hpp

@@ -1,41 +1,208 @@

#pragma once
#include <exception>
#include <stdexcept>
#include <string>
#include <git2.h>
#include "git2wrap/git2wrap_export.hpp"
namespace git2wrap
{
class GIT2WRAP_EXPORT error : public std::exception
enum error_code_t // NOLINT
{
OK = GIT_OK,
ERROR = GIT_ERROR,
ENOTFOUND = GIT_ENOTFOUND,
EEXISTS = GIT_EEXISTS,
EAMBIGUOUS = GIT_EAMBIGUOUS,
EBUFS = GIT_EBUFS,
EUSER = GIT_EUSER,
EBAREREPO = GIT_EBAREREPO,
EUNBORNBRANCH = GIT_EUNBORNBRANCH,
EUNMERGED = GIT_EUNMERGED,
ENONFASTFORWARD = GIT_ENONFASTFORWARD,
EINVALIDSPEC = GIT_EINVALIDSPEC,
ECONFLICT = GIT_ECONFLICT,
ELOCKED = GIT_ELOCKED,
EMODIFIED = GIT_EMODIFIED,
EAUTH = GIT_EAUTH,
ECERTIFICATE = GIT_ECERTIFICATE,
EAPPLIED = GIT_EAPPLIED,
EPEEL = GIT_EPEEL,
EEOF = GIT_EEOF,
EINVALID = GIT_EINVALID,
EUNCOMMITTED = GIT_EUNCOMMITTED,
EDIRECTORY = GIT_EDIRECTORY,
EMERGECONFLICT = GIT_EMERGECONFLICT,
PASSTHROUGH = GIT_PASSTHROUGH,
ITEROVER = GIT_ITEROVER,
RETRY = GIT_RETRY,
EMISMATCH = GIT_EMISMATCH,
EINDEXDIRTY = GIT_EINDEXDIRTY,
EAPPLYFAIL = GIT_EAPPLYFAIL,
EOWNER = GIT_EOWNER,
TIMEOUT = GIT_TIMEOUT,
EUNCHANGED = GIT_EUNCHANGED,
ENOTSUPPORTED = GIT_ENOTSUPPORTED,
EREADONLY = GIT_EREADONLY,
};
enum error_t // NOLINT
{
NONE = GIT_ERROR_NONE,
NOMEMORY = GIT_ERROR_NOMEMORY,
OS = GIT_ERROR_OS,
INVALID = GIT_ERROR_INVALID,
REFERENCE = GIT_ERROR_REFERENCE,
ZLIB = GIT_ERROR_ZLIB,
REPOSITORY = GIT_ERROR_REPOSITORY,
CONFIG = GIT_ERROR_CONFIG,
REGEX = GIT_ERROR_REGEX,
ODB = GIT_ERROR_ODB,
INDEX = GIT_ERROR_INDEX,
OBJECT = GIT_ERROR_OBJECT,
NET = GIT_ERROR_NET,
TAG = GIT_ERROR_TAG,
TREE = GIT_ERROR_TREE,
INDEXER = GIT_ERROR_INDEXER,
SSL = GIT_ERROR_SSL,
SUBMODULE = GIT_ERROR_SUBMODULE,
THREAD = GIT_ERROR_THREAD,
STASH = GIT_ERROR_STASH,
CHECKOUT = GIT_ERROR_CHECKOUT,
FETCHHEAD = GIT_ERROR_FETCHHEAD,
MERGE = GIT_ERROR_MERGE,
SSH = GIT_ERROR_SSH,
FILTER = GIT_ERROR_FILTER,
REVERT = GIT_ERROR_REVERT,
CALLBACK = GIT_ERROR_CALLBACK,
CHERRYPICK = GIT_ERROR_CHERRYPICK,
DESCRIBE = GIT_ERROR_DESCRIBE,
REBASE = GIT_ERROR_REBASE,
FILESYSTEM = GIT_ERROR_FILESYSTEM,
PATCH = GIT_ERROR_PATCH,
WORKTREE = GIT_ERROR_WORKTREE,
SHA = GIT_ERROR_SHA,
HTTP = GIT_ERROR_HTTP,
INTERNAL = GIT_ERROR_INTERNAL,
GRAFTS = GIT_ERROR_GRAFTS,
};
static inline std::string error_get_message(error_code_t error)
{
switch (error) {
case error_code_t::OK:
return "No error occurred; the call was successful.";
case error_code_t::ERROR:
return "An error occurred; call git_error_last for more information.";
case error_code_t::ENOTFOUND:
return "Requested object could not be found.";
case error_code_t::EEXISTS:
return "Object exists preventing operation.";
case error_code_t::EAMBIGUOUS:
return "More than one object matches.";
case error_code_t::EBUFS:
return "Output buffer too short to hold data.";
case error_code_t::EUSER:
return "callback generated";
case error_code_t::EBAREREPO:
return "Operation not allowed on bare repository.";
case error_code_t::EUNBORNBRANCH:
return "HEAD refers to branch with no commits.";
case error_code_t::EUNMERGED:
return "Merge in progress prevented operation";
case error_code_t::ENONFASTFORWARD:
return "Reference was not fast-forwardable";
case error_code_t::EINVALIDSPEC:
return "Name/ref spec was not in a valid format";
case error_code_t::ECONFLICT:
return "Checkout conflicts prevented operation";
case error_code_t::ELOCKED:
return "Lock file prevented operation";
case error_code_t::EMODIFIED:
return "Reference value does not match expected";
case error_code_t::EAUTH:
return "Authentication error";
case error_code_t::ECERTIFICATE:
return "Server certificate is invalid";
case error_code_t::EAPPLIED:
return "Patch/merge has already been applied";
case error_code_t::EPEEL:
return "The requested peel operation is not possible";
case error_code_t::EEOF:
return "Unexpected EOF";
case error_code_t::EINVALID:
return "Invalid operation or input";
case error_code_t::EUNCOMMITTED:
return "Uncommitted changes in index prevented operation";
case error_code_t::EDIRECTORY:
return "The operation is not valid for a directory";
case error_code_t::EMERGECONFLICT:
return "A merge conflict exists and cannot continue";
case error_code_t::PASSTHROUGH:
return "A user-configured callback refused to act";
case error_code_t::ITEROVER:
return "Signals end of iteration with iterator";
case error_code_t::RETRY:
return "Internal only";
case error_code_t::EMISMATCH:
return "Hashsum mismatch in object";
case error_code_t::EINDEXDIRTY:
return "Unsaved changes in the index would be overwritten";
case error_code_t::EAPPLYFAIL:
return "Patch application failed";
case error_code_t::EOWNER:
return "The object is not owned by the current user";
case error_code_t::TIMEOUT:
return "The operation timed out";
case error_code_t::EUNCHANGED:
return "There were no changes";
case error_code_t::ENOTSUPPORTED:
return "An option is not supported";
case error_code_t::EREADONLY:
return "The subject is read-only";
}
return "git2wrap error, should not happen...";
}
class GIT2WRAP_EXPORT runtime_error : public std::runtime_error
{
public:
explicit runtime_error(const std::string& err)
: std::runtime_error(err)
{
}
};
template<error_code_t E>
class GIT2WRAP_EXPORT error : public runtime_error
{
public:
explicit error()
: runtime_error(error_get_message(E))
{
}
private:
};
template<>
class GIT2WRAP_EXPORT error<error_code_t::ERROR> : public runtime_error
{
public:
explicit error(int err,
const git_error* git_err,
const char* file,
unsigned line)
: m_error(err)
, m_klass(git_err->klass)
, m_message(git_err->message)
, m_file(file)
, m_line(line)
explicit error()
: runtime_error(git_error_last()->message)
, m_err(static_cast<error_t>(git_error_last()->klass))
{
}
int get_klass() const { return m_klass; }
int get_error() const { return m_error; }
unsigned get_line() const { return m_line; }
const char* get_file() const { return m_file; }
const char* get_message() const { return m_message.c_str(); }
error_t get_error() const { return m_err; }
private:
int m_error;
int m_klass;
std::string m_message;
const char* m_file;
unsigned m_line;
error_t m_err;
};
} // namespace git2wrap

diff --git a/source/blob.cpp b/source/blob.cpp

@@ -1,7 +1,5 @@

#include "git2wrap/blob.hpp"
#include "git2wrap/error.hpp"
namespace git2wrap
{

@@ -14,11 +12,7 @@ blob::blob(git_blob* blb, repositoryPtr repo)

blob blob::dup() const
{
git_blob* blb = nullptr;
if (auto err = git_blob_dup(&blb, m_blob.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_blob_dup(&blb, m_blob.get());
return {blb, m_repo};
}

diff --git a/source/branch.cpp b/source/branch.cpp

@@ -29,8 +29,13 @@ const std::string& branch::get_name()

}
const char* name = nullptr;
if (auto err = git_branch_name(&name, m_ref.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
switch (git_branch_name(&name, m_ref.get())) {
case error_code_t::OK:
break;
case error_code_t::EINVALID:
throw error<error_code_t::EINVALID>();
default:
throw error<error_code_t::ERROR>();
}
return m_name = name;

@@ -49,10 +54,12 @@ branch_iterator& branch_iterator::operator++()

git_reference* ref = nullptr;
git_branch_t type = {};
if (auto err = git_branch_next(&ref, &type, get())) {
if (err != GIT_ITEROVER) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
switch (git_branch_next(&ref, &type, get())) {
case error_code_t::OK:
case error_code_t::ITEROVER:
break;
default:
throw error<error_code_t::ERROR>();
}
m_branch = branch(ref, type);

diff --git a/source/commit.cpp b/source/commit.cpp

@@ -16,11 +16,7 @@ commit::commit(git_commit* cmt, repositoryPtr repo)

commit commit::dup() const
{
git_commit* cmt = nullptr;
if (auto err = git_commit_dup(&cmt, m_commit.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_commit_dup(&cmt, m_commit.get());
return {cmt, m_repo};
}

@@ -88,8 +84,8 @@ commit commit::get_parent(unsigned n) const

{
git_commit* cmt = nullptr;
if (auto err = git_commit_parent(&cmt, m_commit.get(), n)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_commit_parent(&cmt, m_commit.get(), n) != 0) {
throw error<error_code_t::ERROR>();
}
return {cmt, m_repo};

@@ -99,8 +95,13 @@ buf commit::get_header_field(const char* field) const

{
buf bufr;
if (auto err = git_commit_header_field(bufr.get(), m_commit.get(), field)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
switch (git_commit_header_field(bufr.get(), m_commit.get(), field)) {
case error_code_t::OK:
break;
case error_code_t::ENOTFOUND:
throw error<error_code_t::ENOTFOUND>();
default:
throw error<error_code_t::ERROR>();
}
return bufr;

@@ -110,8 +111,8 @@ tree commit::get_tree() const

{
git_tree* tre = nullptr;
if (auto err = git_commit_tree(&tre, m_commit.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_commit_tree(&tre, m_commit.get()) != 0) {
throw error<error_code_t::ERROR>();
}
return {tre, m_repo};

diff --git a/source/diff.cpp b/source/diff.cpp

@@ -27,10 +27,11 @@ diff diff::tree_to_tree(const tree& old,

{
git_diff* dif = nullptr;
if (auto err = git_diff_tree_to_tree(
&dif, old.get_owner().get(), old.ptr(), crnt.ptr(), opts))
if (git_diff_tree_to_tree(
&dif, crnt.get_owner().get(), old.ptr(), crnt.ptr(), opts)
!= 0)
{
throw error(err, git_error_last(), __FILE__, __LINE__);
throw error<error_code_t::ERROR>();
}
return {dif, old.get_owner()};

@@ -45,8 +46,8 @@ diff_stats diff::get_stats() const

{
git_diff_stats* stats = nullptr;
if (auto err = git_diff_get_stats(&stats, m_diff.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_diff_get_stats(&stats, m_diff.get()) != 0) {
throw error<error_code_t::ERROR>();
}
return diff_stats(stats);

diff --git a/source/libgit2.cpp b/source/libgit2.cpp

@@ -8,7 +8,7 @@ namespace git2wrap

libgit2::libgit2()
{
if (m_cinit = git_libgit2_init(); m_cinit < 0) {
throw error(m_cinit, git_error_last(), __FILE__, __LINE__);
throw error<error_code_t::ERROR>();
}
}

diff --git a/source/object.cpp b/source/object.cpp

@@ -14,11 +14,7 @@ object::object(git_object* obj, repositoryPtr repo)

object object::dup() const
{
git_object* obj = nullptr;
if (auto err = git_object_dup(&obj, m_obj.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_object_dup(&obj, m_obj.get());
return {obj, m_repo};
}

@@ -31,8 +27,8 @@ buf object::get_id_short() const

{
buf bufr;
if (auto err = git_object_short_id(bufr.get(), m_obj.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_object_short_id(bufr.get(), m_obj.get()) != 0) {
throw error<error_code_t::ERROR>();
}
return bufr;

diff --git a/source/oid.cpp b/source/oid.cpp

@@ -1,7 +1,5 @@

#include "git2wrap/oid.hpp"
#include "git2wrap/error.hpp"
namespace git2wrap
{

@@ -15,9 +13,7 @@ std::string oid::get_hex_string(size_t n)

std::string str(n, 0);
char* bfr = const_cast<char*>(str.c_str()); // NOLINT
if (git_oid_tostr(bfr, n + 1, m_oid.get()) == nullptr) {
throw error(0, git_error_last(), __FILE__, __LINE__);
}
git_oid_tostr(bfr, n + 1, m_oid.get());
return str;
}

diff --git a/source/reference.cpp b/source/reference.cpp

@@ -13,11 +13,7 @@ reference::reference(git_reference* ref)

reference reference::dup() const
{
git_reference* ref = nullptr;
if (auto err = git_reference_dup(&ref, m_ref.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_reference_dup(&ref, m_ref.get());
return reference(ref);
}

diff --git a/source/repository.cpp b/source/repository.cpp

@@ -21,8 +21,8 @@ repository::repository(const char* path, unsigned is_bare)

{
git_repository* repo = nullptr;
if (auto err = git_repository_init(&repo, path, is_bare)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_repository_init(&repo, path, is_bare) != 0) {
throw error<error_code_t::ERROR>();
}
m_repo = {repo, git_repository_free};

@@ -32,8 +32,8 @@ repository::repository(const char* path, init_options* opts)

{
git_repository* repo = nullptr;
if (auto err = git_repository_init_ext(&repo, path, opts)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_repository_init_ext(&repo, path, opts) != 0) {
throw error<error_code_t::ERROR>();
}
m_repo = {repo, git_repository_free};

@@ -45,8 +45,8 @@ repository repository::clone(const char* url,

{
git_repository* repo = nullptr;
if (auto err = git_clone(&repo, url, local_path, options)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_clone(&repo, url, local_path, options) != 0) {
throw error<error_code_t::ERROR>();
}
return repository(repo);

@@ -56,8 +56,8 @@ repository repository::open(const char* path)

{
git_repository* repo = nullptr;
if (auto err = git_repository_open(&repo, path)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_repository_open(&repo, path) != 0) {
throw error<error_code_t::ERROR>();
}
return repository(repo);

@@ -69,8 +69,13 @@ repository repository::open(const char* path,

{
git_repository* repo = nullptr;
if (auto err = git_repository_open_ext(&repo, path, flags, ceiling_dirs)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
switch (git_repository_open_ext(&repo, path, flags, ceiling_dirs)) {
case error_code_t::OK:
break;
case error_code_t::ENOTFOUND:
throw error<error_code_t::ENOTFOUND>();
default:
throw error<error_code_t::ERROR>();
}
return repository(repo);

@@ -80,8 +85,17 @@ object repository::revparse(const char* spec) const

{
git_object* obj = nullptr;
if (auto err = git_revparse_single(&obj, m_repo.get(), spec)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
switch (git_revparse_single(&obj, m_repo.get(), spec)) {
case error_code_t::OK:
break;
case error_code_t::ENOTFOUND:
throw error<error_code_t::ENOTFOUND>();
case error_code_t::EAMBIGUOUS:
throw error<error_code_t::EAMBIGUOUS>();
case error_code_t::EINVALIDSPEC:
throw error<error_code_t::EINVALIDSPEC>();
default:
throw error<error_code_t::ERROR>();
}
return {obj, m_repo};

@@ -91,8 +105,8 @@ commit repository::commit_lookup(const oid& objid) const

{
git_commit* commit = nullptr;
if (auto err = git_commit_lookup(&commit, m_repo.get(), objid.ptr())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_commit_lookup(&commit, m_repo.get(), objid.ptr()) != 0) {
throw error<error_code_t::ERROR>();
}
return {commit, m_repo};

@@ -102,8 +116,8 @@ blob repository::blob_lookup(const oid& objid) const

{
git_blob* blob = nullptr;
if (auto err = git_blob_lookup(&blob, m_repo.get(), objid.ptr())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_blob_lookup(&blob, m_repo.get(), objid.ptr()) != 0) {
throw error<error_code_t::ERROR>();
}
return {blob, m_repo};

@@ -113,8 +127,8 @@ tag repository::tag_lookup(const oid& objid) const

{
git_tag* tagg = nullptr;
if (auto err = git_tag_lookup(&tagg, m_repo.get(), objid.ptr())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_tag_lookup(&tagg, m_repo.get(), objid.ptr()) != 0) {
throw error<error_code_t::ERROR>();
}
return {tagg, m_repo};

@@ -129,8 +143,8 @@ branch_iterator repository::branch_begin(git_branch_t list_flags) const

{
git_branch_iterator* iter = nullptr;
if (auto err = git_branch_iterator_new(&iter, m_repo.get(), list_flags)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_branch_iterator_new(&iter, m_repo.get(), list_flags) != 0) {
throw error<error_code_t::ERROR>();
}
return branch_iterator(iter);

@@ -138,8 +152,8 @@ branch_iterator repository::branch_begin(git_branch_t list_flags) const

void repository::tag_foreach(git_tag_foreach_cb callback, void* payload) const
{
if (auto err = git_tag_foreach(m_repo.get(), callback, payload)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_tag_foreach(m_repo.get(), callback, payload) != 0) {
throw error<error_code_t::ERROR>();
}
}

diff --git a/source/revwalk.cpp b/source/revwalk.cpp

@@ -11,8 +11,8 @@ revwalk::revwalk(repositoryPtr repo)

{
git_revwalk* rwalk = nullptr;
if (auto err = git_revwalk_new(&rwalk, m_repo.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_revwalk_new(&rwalk, m_repo.get()) != 0) {
throw error<error_code_t::ERROR>();
}
m_revwalk = {rwalk, git_revwalk_free};

@@ -20,22 +20,22 @@ revwalk::revwalk(repositoryPtr repo)

void revwalk::push(const oid& objid)
{
if (auto err = git_revwalk_push(m_revwalk.get(), objid.ptr())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_revwalk_push(m_revwalk.get(), objid.ptr()) != 0) {
throw error<error_code_t::ERROR>();
}
}
void revwalk::push_glob(const char* glob)
{
if (auto err = git_revwalk_push_glob(m_revwalk.get(), glob)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_revwalk_push_glob(m_revwalk.get(), glob) != 0) {
throw error<error_code_t::ERROR>();
}
}
void revwalk::push_head()
{
if (auto err = git_revwalk_push_head(m_revwalk.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_revwalk_push_head(m_revwalk.get()) != 0) {
throw error<error_code_t::ERROR>();
}
}

@@ -43,8 +43,11 @@ commit revwalk::next()

{
static git_oid objid;
if (git_revwalk_next(&objid, m_revwalk.get()) == 0) {
return repository(m_repo).commit_lookup(oid(&objid));
switch (git_revwalk_next(&objid, m_revwalk.get())) {
case error_code_t::OK:
return repository(m_repo).commit_lookup(oid(&objid));
case error_code_t::ITEROVER:
break;
}
return {};

@@ -52,8 +55,8 @@ commit revwalk::next()

void revwalk::reset()
{
if (auto err = git_revwalk_reset(m_revwalk.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_revwalk_reset(m_revwalk.get()) != 0) {
throw error<error_code_t::ERROR>();
}
}

diff --git a/source/signature.cpp b/source/signature.cpp

@@ -19,11 +19,7 @@ signature::signature(git_signature* sig)

signature signature::dup() const
{
git_signature* sig = nullptr;
if (auto err = git_signature_dup(&sig, m_sig.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_signature_dup(&sig, m_sig.get());
return signature(sig);
}

diff --git a/source/tag.cpp b/source/tag.cpp

@@ -14,11 +14,7 @@ tag::tag(git_tag* tagg, repositoryPtr repo)

tag tag::dup() const
{
git_tag* tagg = nullptr;
if (auto err = git_tag_dup(&tagg, m_tag.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_tag_dup(&tagg, m_tag.get());
return {tagg, m_repo};
}

diff --git a/source/tree.cpp b/source/tree.cpp

@@ -16,11 +16,7 @@ tree::tree(git_tree* tre, repositoryPtr repo)

tree tree::dup() const
{
git_tree* tre = nullptr;
if (auto err = git_tree_dup(&tre, m_tree.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
}
git_tree_dup(&tre, m_tree.get());
return {tre, m_repo};
}

@@ -58,8 +54,14 @@ tree_entry tree::get_entry_path(const char* path) const

{
git_tree_entry* entry = nullptr;
if (auto err = git_tree_entry_bypath(&entry, m_tree.get(), path)) {
throw error(err, git_error_last(), __FILE__, __LINE__);
switch (git_tree_entry_bypath(&entry, m_tree.get(), path)) {
case error_code_t::OK:
break;
case error_code_t::ENOTFOUND:
throw error<error_code_t::ENOTFOUND>();
default:
// should not happen
throw error<error_code_t::ERROR>();
}
return {entry, m_repo};

@@ -81,8 +83,8 @@ tree_entry tree_entry::dup() const

{
git_tree_entry* entry = nullptr;
if (auto err = git_tree_entry_dup(&entry, m_entry.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_tree_entry_dup(&entry, m_entry.get()) != 0) {
throw error<error_code_t::ERROR>();
}
return {entry, m_repo};

@@ -92,8 +94,8 @@ object tree_entry::to_object() const

{
git_object* obj = nullptr;
if (auto err = git_tree_entry_to_object(&obj, m_repo.get(), m_entry.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_tree_entry_to_object(&obj, m_repo.get(), m_entry.get()) != 0) {
throw error<error_code_t::ERROR>();
}
return {obj, m_repo};

@@ -103,8 +105,8 @@ tree tree_entry::to_tree() const

{
git_object* obj = nullptr;
if (auto err = git_tree_entry_to_object(&obj, m_repo.get(), m_entry.get())) {
throw error(err, git_error_last(), __FILE__, __LINE__);
if (git_tree_entry_to_object(&obj, m_repo.get(), m_entry.get()) != 0) {
throw error<error_code_t::ERROR>();
}
return {reinterpret_cast<git_tree*>(obj), m_repo}; // NOLINT

diff --git a/test/source/git2wrap_test.cpp b/test/source/git2wrap_test.cpp

@@ -11,11 +11,8 @@ int main()

const libgit2 git;
} catch (const git2wrap::error& err) {
std::cerr << std::format("Error %d/%d: %s\n",
err.get_error(),
err.get_klass(),
err.get_message());
} catch (const git2wrap::runtime_error& err) {
std::cerr << std::format("Error: %s\n", err.what());
}
return 0;