From 0dd4d54f600def7b848701435d0b980dac110810 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sun, 13 Nov 2022 22:43:33 -0300 Subject: [PATCH 1/2] initial commit --- include/cppgit/{git.hpp => git2api.hpp} | 20 +++++++++----- include/cppgit/repository.hpp | 3 ++- src/git.cpp | 21 --------------- src/git2api.cpp | 36 +++++++++++++++++++++++++ test/src/version_test.cpp | 23 +++++++++++++++- 5 files changed, 73 insertions(+), 30 deletions(-) rename include/cppgit/{git.hpp => git2api.hpp} (70%) delete mode 100644 src/git.cpp create mode 100644 src/git2api.cpp diff --git a/include/cppgit/git.hpp b/include/cppgit/git2api.hpp similarity index 70% rename from include/cppgit/git.hpp rename to include/cppgit/git2api.hpp index bf7a1af..778793d 100644 --- a/include/cppgit/git.hpp +++ b/include/cppgit/git2api.hpp @@ -1,19 +1,24 @@ -#ifndef CPPGIT_GIT_HPP -#define CPPGIT_GIT_HPP +#ifndef CPPGIT_GIT2API_HPP +#define CPPGIT_GIT2API_HPP #include - +#include namespace cppgit { // ===================================================================================== // Class: Git // Description: // ===================================================================================== -class Git { +class Git2API { public: // ==================== LIFECYCLE ======================================= - Git(); - ~Git(); + Git2API(); + Git2API(const Git2API &); + Git2API(Git2API &&) = delete; + Git2API &operator=(const Git2API &) = default; + Git2API &operator=(Git2API &&) = delete; + virtual ~Git2API(); // ==================== ACCESSORS ======================================= + [[nodiscard]] static int counter() noexcept; [[nodiscard]] static std::tuple version(); // ==================== MUTATORS ======================================= // ==================== OPERATORS ======================================= @@ -23,7 +28,8 @@ class Git { private: // ==================== METHODS ======================================= // ==================== DATA MEMBERS ======================================= -}; // ----- end of class Git ----- + inline static std::atomic m_init_counter{0}; +}; // ----- end of class Git2API ----- } // namespace cppgit #endif diff --git a/include/cppgit/repository.hpp b/include/cppgit/repository.hpp index bf87a64..62ad91e 100644 --- a/include/cppgit/repository.hpp +++ b/include/cppgit/repository.hpp @@ -1,6 +1,7 @@ #ifndef CPPGIT_HPP #define CPPGIT_HPP +#include "cppgit/git2api.hpp" #include namespace cppgit { @@ -9,7 +10,7 @@ namespace cppgit { // Description: // // ===================================================================================== -class Repository { +class Repository: private Git2API { public: // ==================== LIFECYCLE ======================================= Repository(); diff --git a/src/git.cpp b/src/git.cpp deleted file mode 100644 index 357f277..0000000 --- a/src/git.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "cppgit/git.hpp" -#include - -namespace cppgit { - -Git::Git() { - git_libgit2_init(); -} - -Git::~Git() { - git_libgit2_shutdown(); -} -std::tuple Git::version() { - int major = 0, minor = 0, revision = 0; - if (git_libgit2_version(&major, &minor, &revision) != 0) { - // - } - return {major, minor, revision}; -} - -} // namespace cppgit diff --git a/src/git2api.cpp b/src/git2api.cpp new file mode 100644 index 0000000..1d2b4c1 --- /dev/null +++ b/src/git2api.cpp @@ -0,0 +1,36 @@ +#include "cppgit/git2api.hpp" +#include "cppgit/exception.hpp" +#include + +namespace cppgit { + +Git2API::Git2API() { + if (++m_init_counter == 1) { + git_libgit2_init(); + } +} + +Git2API::Git2API(const Git2API&) { + ++m_init_counter; +} + + +Git2API::~Git2API() { + if (--m_init_counter == 0){ + git_libgit2_shutdown(); + } +} + +int Git2API::counter() noexcept{ + return Git2API::m_init_counter; +} +std::tuple Git2API::version() { + int major = 0, minor = 0, revision = 0; + if (git_libgit2_version(&major, &minor, &revision) != 0) { + // + throw Exception(); + } + return {major, minor, revision}; +} + +} // namespace cppgit diff --git a/test/src/version_test.cpp b/test/src/version_test.cpp index 1cb0ed3..689dd4d 100644 --- a/test/src/version_test.cpp +++ b/test/src/version_test.cpp @@ -1,10 +1,31 @@ +#include "cppgit/git2api.hpp" #include "cppgit/version.hpp" #include #include #include -TEST_CASE("Greeter version") { +class Object: private cppgit::Git2API { + +}; + +TEST_CASE("Version") { static_assert(std::string_view(CPPGIT_VERSION) == std::string_view("1.0.0")); CHECK(std::string(CPPGIT_VERSION) == std::string("1.0.0")); } + +TEST_CASE("Initialization Test") { + { + const cppgit::Git2API git_obj1; + { + const cppgit::Git2API git_obj2; + const Object git_obj3; + CHECK(cppgit::Git2API::counter() == 3); + } + CHECK(cppgit::Git2API::counter() == 1); + const cppgit::Git2API git_obj2; + CHECK(cppgit::Git2API::counter() == 2); + } + CHECK(cppgit::Git2API::counter() == 0); + +} From fad38b757a0afb892af2237fe280370d6a123f4f Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Mon, 14 Nov 2022 13:59:47 -0300 Subject: [PATCH 2/2] adding some functions to repository --- include/cppgit/repository.hpp | 23 +++++++++++++++++++--- src/repository.cpp | 37 ++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/include/cppgit/repository.hpp b/include/cppgit/repository.hpp index 62ad91e..4732bfa 100644 --- a/include/cppgit/repository.hpp +++ b/include/cppgit/repository.hpp @@ -2,9 +2,21 @@ #define CPPGIT_HPP #include "cppgit/git2api.hpp" -#include +#include +#include +#include +#include + namespace cppgit { + + enum class branch_type + { + LOCAL, + REMOTE, + ALL + }; + // ===================================================================================== // Class: Repository // Description: @@ -13,8 +25,12 @@ namespace cppgit { class Repository: private Git2API { public: // ==================== LIFECYCLE ======================================= - Repository(); + Repository(std::string_view path, bool is_bare); // ==================== ACCESSORS ======================================= + [[nodiscard]] bool is_bare() const; + [[nodiscard]] const char* path() const; + [[nodiscard]] const char* workdir() const; + [[nodiscard]] static std::optional discover_path(std::string_view start_path, bool across_fs = false, std::string_view ceiling_dirs = ""); // ==================== MUTATORS ======================================= // ==================== OPERATORS ======================================= @@ -28,7 +44,8 @@ class Repository: private Git2API { // ==================== METHODS ======================================= // ==================== DATA MEMBERS ======================================= - + struct Destroy { void operator() (git_repository *) const; }; + std::unique_ptr m_repo; }; // ----- end of class Repository ----- } // namespace cppgit diff --git a/src/repository.cpp b/src/repository.cpp index c3866c7..f9b975d 100644 --- a/src/repository.cpp +++ b/src/repository.cpp @@ -1,8 +1,39 @@ #include "cppgit/repository.hpp" +#include "cppgit/exception.hpp" #include namespace cppgit { - Repository::Repository(){ - - } + Repository::Repository(std::string_view path, bool is_bare){ + git_repository* repo = nullptr; + if (git_repository_init(&repo, path.data(), static_cast(is_bare)) != 0) { + throw Exception(); + } + m_repo.reset(repo); + } + + void Repository::Destroy::operator() (git_repository* repo) const + { + git_repository_free(repo); + } + + bool Repository::is_bare() const { + return git_repository_is_bare(m_repo.get()) != 0; + } + + const char* Repository::path() const + { + return git_repository_path(m_repo.get()); + } + const char* Repository::workdir() const + { + return git_repository_workdir(m_repo.get()); + } + + std::optional Repository::discover_path(std::string_view start_path, bool across_fs, std::string_view ceiling_dirs) { + git_buf buffer = {.ptr = nullptr, .size = 0}; + if (git_repository_discover(&buffer, start_path.data(), static_cast(across_fs), ceiling_dirs.data()) != 0) { + return std::nullopt; + } + return std::string(buffer.ptr, buffer.size); + } }