diff --git a/CMakeLists.txt b/CMakeLists.txt index 824aad9..3a5a8e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/utils/common.hpp ${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp ${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp + ${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.cpp + ${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.cpp ${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.cpp @@ -57,6 +59,7 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.cpp ${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.hpp + ${GIT2CPP_SOURCE_DIR}/wrapper/wrapper_base.hpp ${GIT2CPP_SOURCE_DIR}/main.cpp ${GIT2CPP_SOURCE_DIR}/version.hpp ) diff --git a/src/utils/common.hpp b/src/utils/common.hpp index afc388b..44667d8 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -19,38 +19,6 @@ class noncopyable_nonmovable ~noncopyable_nonmovable() = default; }; -template -class wrapper_base -{ -public: - using resource_type = T; - - wrapper_base(const wrapper_base&) = delete; - wrapper_base& operator=(const wrapper_base&) = delete; - - wrapper_base(wrapper_base&& rhs) - : p_resource(rhs.p_resource) - { - rhs.p_resource = nullptr; - } - wrapper_base& operator=(wrapper_base&& rhs) - { - std::swap(p_resource, rhs.p_resource); - return *this; - } - - operator resource_type*() const noexcept - { - return p_resource; - } - -protected: - // Allocation and deletion of p_resource must be handled by inheriting class. - wrapper_base() = default; - ~wrapper_base() = default; - resource_type* p_resource = nullptr; -}; - class libgit2_object : private noncopyable_nonmovable { public: diff --git a/src/wrapper/commit_wrapper.cpp b/src/wrapper/commit_wrapper.cpp new file mode 100644 index 0000000..b562ec5 --- /dev/null +++ b/src/wrapper/commit_wrapper.cpp @@ -0,0 +1,19 @@ +#include "commit_wrapper.hpp" +#include "../utils/git_exception.hpp" + +commit_wrapper::~commit_wrapper() +{ + git_commit_free(p_resource); + p_resource = nullptr; +} + + +commit_wrapper commit_wrapper::last_commit(const repository_wrapper& repo, const std::string& ref_name) +{ + git_oid oid_parent_commit; + throwIfError(git_reference_name_to_id(&oid_parent_commit, repo, ref_name.c_str())); + + commit_wrapper cw; + throwIfError(git_commit_lookup(&(cw.p_resource), repo, &oid_parent_commit)); + return cw; +} diff --git a/src/wrapper/commit_wrapper.hpp b/src/wrapper/commit_wrapper.hpp new file mode 100644 index 0000000..4fccc47 --- /dev/null +++ b/src/wrapper/commit_wrapper.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include + +#include "../wrapper/repository_wrapper.hpp" +#include "../wrapper/wrapper_base.hpp" + +class commit_wrapper : public wrapper_base +{ +public: + + ~commit_wrapper(); + + commit_wrapper(commit_wrapper&&) noexcept = default; + commit_wrapper& operator=(commit_wrapper&&) noexcept = default; + + static commit_wrapper + last_commit(const repository_wrapper& repo, const std::string& ref_name = "HEAD"); + +private: + + commit_wrapper() = default; +}; diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index c7c02ec..4baff3f 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -1,4 +1,5 @@ #include "index_wrapper.hpp" +#include "../utils/common.hpp" #include "../utils/git_exception.hpp" #include "../wrapper/repository_wrapper.hpp" diff --git a/src/wrapper/index_wrapper.hpp b/src/wrapper/index_wrapper.hpp index f478e48..f8c1282 100644 --- a/src/wrapper/index_wrapper.hpp +++ b/src/wrapper/index_wrapper.hpp @@ -5,7 +5,7 @@ #include -#include "../utils/common.hpp" +#include "../wrapper/wrapper_base.hpp" class repository_wrapper; @@ -15,8 +15,8 @@ class index_wrapper : public wrapper_base ~index_wrapper(); - index_wrapper(index_wrapper&&) = default; - index_wrapper& operator=(index_wrapper&&) = default; + index_wrapper(index_wrapper&&) noexcept = default; + index_wrapper& operator=(index_wrapper&&) noexcept = default; static index_wrapper init(repository_wrapper& rw); diff --git a/src/wrapper/refs_wrapper.hpp b/src/wrapper/refs_wrapper.hpp index 056e19d..32f139b 100644 --- a/src/wrapper/refs_wrapper.hpp +++ b/src/wrapper/refs_wrapper.hpp @@ -1,10 +1,11 @@ #pragma once -// #include + #include #include #include "../wrapper/repository_wrapper.hpp" +#include "../wrapper/wrapper_base.hpp" class reference_wrapper : public wrapper_base { @@ -12,8 +13,8 @@ class reference_wrapper : public wrapper_base ~reference_wrapper(); - reference_wrapper(reference_wrapper&&) = default; - reference_wrapper& operator=(reference_wrapper&&) = default; + reference_wrapper(reference_wrapper&&) noexcept = default; + reference_wrapper& operator=(reference_wrapper&&) noexcept = default; static std::string get_ref_name(const repository_wrapper& repo); diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index dd0b8f7..16d9dfa 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -4,8 +4,8 @@ #include -#include "../utils/common.hpp" #include "../wrapper/index_wrapper.hpp" +#include "../wrapper/wrapper_base.hpp" class repository_wrapper : public wrapper_base { @@ -13,8 +13,8 @@ class repository_wrapper : public wrapper_base ~repository_wrapper(); - repository_wrapper(repository_wrapper&&) = default; - repository_wrapper& operator=(repository_wrapper&&) = default; + repository_wrapper(repository_wrapper&&) noexcept = default; + repository_wrapper& operator=(repository_wrapper&&) noexcept = default; static repository_wrapper init(const std::string& directory, bool bare); static repository_wrapper open(const std::string& directory); diff --git a/src/wrapper/status_wrapper.hpp b/src/wrapper/status_wrapper.hpp index 4b7ee23..2a8335d 100644 --- a/src/wrapper/status_wrapper.hpp +++ b/src/wrapper/status_wrapper.hpp @@ -6,6 +6,7 @@ #include #include "../wrapper/repository_wrapper.hpp" +#include "../wrapper/wrapper_base.hpp" class status_list_wrapper : public wrapper_base { @@ -14,8 +15,8 @@ class status_list_wrapper : public wrapper_base ~status_list_wrapper(); - status_list_wrapper(status_list_wrapper&&) = default; - status_list_wrapper& operator=(status_list_wrapper&&) = default; + status_list_wrapper(status_list_wrapper&&) noexcept = default; + status_list_wrapper& operator=(status_list_wrapper&&) noexcept = default; static status_list_wrapper status_list(const repository_wrapper& wrapper); diff --git a/src/wrapper/wrapper_base.hpp b/src/wrapper/wrapper_base.hpp new file mode 100644 index 0000000..4750f64 --- /dev/null +++ b/src/wrapper/wrapper_base.hpp @@ -0,0 +1,35 @@ +#pragma once + + +template +class wrapper_base +{ +public: + using resource_type = T; + + wrapper_base(const wrapper_base&) = delete; + wrapper_base& operator=(const wrapper_base&) = delete; + + wrapper_base(wrapper_base&& rhs) noexcept + : p_resource(rhs.p_resource) + { + rhs.p_resource = nullptr; + } + wrapper_base& operator=(wrapper_base&& rhs) noexcept + { + std::swap(p_resource, rhs.p_resource); + return *this; + } + + operator resource_type*() const noexcept + { + return p_resource; + } + +protected: + // Allocation and deletion of p_resource must be handled by inheriting class. + wrapper_base() = default; + ~wrapper_base() = default; + + resource_type* p_resource = nullptr; +};