git2wrapC++20 wrapper for libgit2 | 
          
| git clone git://git.dimitrijedobrota.com/git2wrap.git | 
| Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | 
| commit | bf3f4b789fa7e8cde351ea1625e617777d10c083 | 
| parent | 53dd34646d95815b781900a3ee6ba6bffeb723b7 | 
| author | Dimitrije Dobrota < mail@dimitrijedobrota.com > | 
| date | Sun, 5 Jan 2025 23:41:15 +0100 | 
Rule of 0 with smart pointers
| M | CMakeLists.txt | | | +++ -- | 
| M | include/git2wrap/git2wrap.hpp | | | +++++++++++++++++ --------------------- | 
| M | source/branch.cpp | | | + ----------------------- | 
| A | source/reference.cpp | | | ++++++++++++++++ | 
| M | source/repository.cpp | | | ++++++++++++ ---------- | 
5 files changed, 49 insertions(+), 56 deletions(-)
diff --git a/ CMakeLists.txt b/ CMakeLists.txt
          @@ -4,7 +4,7 @@ 
          include(cmake/prelude.cmake)
        
        
          project(
              git2wrap
              VERSION 0.1.2
              VERSION 0.1.3
              DESCRIPTION "C++ 20 wrapper for libgit2"
              HOMEPAGE_URL "https://git.dimitrijedobrota.com/git2wrap.git"
              LANGUAGES CXX
        
        
          @@ -17,9 +17,10 @@ 
          include(cmake/variables.cmake)
        
        
          add_library(
              git2wrap_git2wrap
              source/branch.cpp
              source/libgit2.cpp
              source/reference.cpp
              source/repository.cpp
              source/branch.cpp
          )
          add_library(git2wrap::git2wrap ALIAS git2wrap_git2wrap)
          diff --git a/ include/git2wrap/git2wrap.hpp b/ include/git2wrap/git2wrap.hpp
@@ -1,6 +1,8 @@
#pragma once
          #include <exception>
          #include <functional>
          #include <memory>
          #include <string>
          #include <git2.h>
        
        
          @@ -54,29 +56,30 @@ 
          private:
        
        
            int m_cinit = 0;
          };
          class GIT2WRAP_EXPORT branch
          class GIT2WRAP_EXPORT reference
          {
          public:
            branch(git_reference* ref, git_branch_t type);
            branch() = default;
            branch(const branch&) = delete;
            branch& operator=(const branch&) = delete;
            explicit reference(git_reference* ref = nullptr);
            branch(branch&& rhs) noexcept;
            branch& operator=(branch&& rhs) noexcept;
            git_reference* get() const;
            ~branch();
          private:
            std::unique_ptr<git_reference, std::function<void(git_reference*)>> m_ref;
          };
            git_reference* get_reference() { return m_ref; }
            const git_reference* get_reference() const { return m_ref; }
          class GIT2WRAP_EXPORT branch
          {
          public:
            explicit branch(git_reference* ref = nullptr,
                            git_branch_t type = git_branch_t(0));
            git_branch_t get_type() const { return m_type; }
            git_reference* get_reference() const { return m_ref.get(); }
            const std::string& get_name();
          private:
            git_reference* m_ref = nullptr;
            reference m_ref;
            git_branch_t m_type = {};
            std::string m_name;
        
        
          @@ -91,12 +94,6 @@ 
          public:
        
        
            explicit repository(git_repository* repo);
            repository(const char* path, unsigned is_bare);
            repository(const char* path, init_options* opts);
            ~repository();
            repository(const repository&) = delete;
            repository(repository&&) = delete;
            repository& operator=(const repository&) = delete;
            repository& operator=(repository&&) = delete;
            static repository clone(const char* url,
                                    const char* local_path,
        
        
          @@ -130,9 +127,8 @@ 
          public:
        
        
              friend bool operator!=(const branch_iterator& lhs,
                                     const branch_iterator& rhs);
              git_branch_iterator* m_iter = nullptr;
            private:
              git_branch_iterator* m_iter = nullptr;
              branch m_branch;
            };
          
          @@ -140,7 +136,7 @@ 
          public:
        
        
            branch_iterator branch_end() const;
          private:
            git_repository* m_repo = nullptr;
            std::unique_ptr<git_repository, std::function<void(git_repository*)>> m_repo;
          };
          }  // namespace git2wrap
        
        diff --git a/ source/branch.cpp b/ source/branch.cpp
          @@ -9,28 +9,6 @@ 
          branch::branch(git_reference* ref, git_branch_t type)
        
        
          {
          }
          branch::branch(branch&& rhs) noexcept
          {
            std::swap(m_ref, rhs.m_ref);
            std::swap(m_type, rhs.m_type);
            std::swap(m_name, rhs.m_name);
          }
          branch& branch::operator=(branch&& rhs) noexcept
          {
            std::swap(m_ref, rhs.m_ref);
            std::swap(m_type, rhs.m_type);
            std::swap(m_name, rhs.m_name);
            return *this;
          }
          branch::~branch()
          {
            git_reference_free(m_ref);
            m_ref = nullptr;
          }
          const std::string& branch::get_name()
          {
            if (!m_name.empty()) {
        
        
          @@ -38,7 +16,7 @@ 
          const std::string& branch::get_name()
        
        
            }
            const char* name = nullptr;
            if (auto err = git_branch_name(&name, m_ref)) {
            if (auto err = git_branch_name(&name, m_ref.get())) {
              throw error(err, git_error_last(), __FILE__, __LINE__);
            }
          diff --git a/ source/reference.cpp b/ source/reference.cpp
@@ -0,0 +1,16 @@
#include "git2wrap/git2wrap.hpp"
          namespace git2wrap
          {
          reference::reference(git_reference* ref)
              : m_ref(ref, git_reference_free)
          {
          }
          git_reference* reference::get() const
          {
            return m_ref.get();
          }
          }  // namespace git2wrap
        
        diff --git a/ source/repository.cpp b/ source/repository.cpp
          @@ -4,28 +4,30 @@ 
          namespace git2wrap
        
        
          {
          repository::repository(git_repository* repo)
              : m_repo(repo)
              : m_repo(repo, git_repository_free)
          {
          }
          repository::repository(const char* path, unsigned is_bare)
          {
            if (auto err = git_repository_init(&m_repo, path, is_bare)) {
            git_repository* repo = nullptr;
            if (auto err = git_repository_init(&repo, path, is_bare)) {
              throw error(err, git_error_last(), __FILE__, __LINE__);
            }
            m_repo = {repo, git_repository_free};
          }
          repository::repository(const char* path, init_options* opts)
          {
            if (auto err = git_repository_init_ext(&m_repo, path, opts)) {
            git_repository* repo = nullptr;
            if (auto err = git_repository_init_ext(&repo, path, opts)) {
              throw error(err, git_error_last(), __FILE__, __LINE__);
            }
          }
          repository::~repository()
          {
            git_repository_free(m_repo);
            m_repo = nullptr;
            m_repo = {repo, git_repository_free};
          }
          repository repository::clone(const char* url,
        
        
          @@ -88,7 +90,7 @@ 
          repository::branch_iterator& repository::branch_iterator::operator++()
        
        
              }
            }
            m_branch = {ref, type};
            m_branch = branch(ref, type);
            return *this;
          }
          
          @@ -97,7 +99,7 @@ 
          repository::branch_iterator repository::branch_begin(
        
        
          {
            git_branch_iterator* iter = nullptr;
            if (auto err = git_branch_iterator_new(&iter, m_repo, list_flags)) {
            if (auto err = git_branch_iterator_new(&iter, m_repo.get(), list_flags)) {
              throw error(err, git_error_last(), __FILE__, __LINE__);
            }