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 | b94d3e9d439f83ee05a7b7a35980d1f0f4c8c969 |
parent | e052d852bf784d2e9c13a4ea88aee1f02497c693 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Thu, 9 Jan 2025 18:16:16 +0100 |
Tree and tree_entry classes
Diffstat:M | CMakeLists.txt | | | ++- |
M | include/git2wrap/commit.hpp | | | +++ |
M | include/git2wrap/repository.hpp | | | ++-- |
A | include/git2wrap/tree.hpp | | | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | include/git2wrap/types.hpp | | | ++++++++++++ |
M | source/commit.cpp | | | +++++++++++ |
M | source/repository.cpp | | | ++-- |
M | source/signature.cpp | | | +-------- |
A | source/tree.cpp | | | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 files changed, 234 insertions(+), 13 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
git2wrap
VERSION 0.1.11
VERSION 0.1.12
DESCRIPTION "C++ 20 wrapper for libgit2"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/git2wrap.git"
LANGUAGES CXX
@@ -26,6 +26,7 @@ add_library(
source/repository.cpp
source/revwalk.cpp
source/signature.cpp
source/tree.cpp
)
add_library(git2wrap::git2wrap ALIAS git2wrap_git2wrap)
diff --git a/include/git2wrap/commit.hpp b/include/git2wrap/commit.hpp
@@ -3,6 +3,7 @@
#include <git2.h>
#include "git2wrap/git2wrap_export.hpp"
#include "git2wrap/tree.hpp"
#include "git2wrap/types.hpp"
namespace git2wrap
@@ -32,6 +33,8 @@ public:
commit get_parent(unsigned n = 0) const;
buf get_header_field(const char* field) const;
tree get_tree() const;
private:
commitUPtr m_commit;
repositoryPtr m_repo;
diff --git a/include/git2wrap/repository.hpp b/include/git2wrap/repository.hpp
@@ -35,8 +35,8 @@ public:
unsigned flags,
const char* ceiling_dirs);
object revparse(const char* spec);
commit commit_lookup(const git_oid* objid);
object revparse(const char* spec) const;
commit commit_lookup(const git_oid* objid) const;
branch_iterator branch_begin(git_branch_t list_flags) const;
branch_iterator branch_end() const;
diff --git a/include/git2wrap/tree.hpp b/include/git2wrap/tree.hpp
@@ -0,0 +1,58 @@
#pragma once
#include <git2.h>
#include "git2wrap/git2wrap_export.hpp"
#include "git2wrap/types.hpp"
namespace git2wrap
{
class GIT2WRAP_EXPORT tree
{
public:
tree(git_tree* tre, repositoryPtr repo);
operator bool() const { return m_tree != nullptr; } // NOLINT
tree dup() const;
const oid* get_id() const;
repositoryPtr get_owner() const;
size_t get_entrycount() const;
tree_entry get_entry(const char* name) const;
tree_entry get_entry(size_t idx) const;
tree_entry get_entry(const oid* objid) const;
tree_entry get_entry_path(const char* path) const;
private:
treeUPtr m_tree;
repositoryPtr m_repo;
};
class GIT2WRAP_EXPORT tree_entry
{
public:
tree_entry(const git_tree_entry* entry, repositoryPtr repo);
tree_entry(git_tree_entry* entry, repositoryPtr repo);
operator bool() const { return m_entry != nullptr; } // NOLINT
tree_entry dup() const;
object to_object() const;
tree to_tree() const;
const char* get_name() const;
const oid* get_id() const;
object_t get_type() const;
filemode_t get_filemode() const;
filemode_t get_filemode_raw() const;
auto operator<=>(const tree_entry& rhs);
private:
tree_entryPtr m_entry;
repositoryPtr m_repo;
};
} // namespace git2wrap
diff --git a/include/git2wrap/types.hpp b/include/git2wrap/types.hpp
@@ -27,9 +27,21 @@ CLASS(reference)
CLASS(repository)
CLASS(revwalk)
CLASS(signature)
CLASS(tree)
CLASS(tree_entry)
using oid = git_oid;
using time_t = git_time_t;
using time = git_time;
using object_t = git_object_t;
using filemode_t = git_filemode_t;
static const struct empty_lambda_t // static and const applies to the object!
{
template<typename... T>
void operator()(T&&...) const // NOLINT
{
} // does nothing
} empty_lambda {}; // declare an object which is static and const
} // namespace git2wrap
diff --git a/source/commit.cpp b/source/commit.cpp
@@ -106,4 +106,15 @@ buf commit::get_header_field(const char* field) const
return bufr;
}
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__);
}
return {tre, m_repo};
}
} // namespace git2wrap
diff --git a/source/repository.cpp b/source/repository.cpp
@@ -74,7 +74,7 @@ repository repository::open(const char* path,
return repository(repo);
}
object repository::revparse(const char* spec)
object repository::revparse(const char* spec) const
{
git_object* obj = nullptr;
@@ -85,7 +85,7 @@ object repository::revparse(const char* spec)
return {obj, m_repo};
}
commit repository::commit_lookup(const git_oid* objid)
commit repository::commit_lookup(const git_oid* objid) const
{
git_commit* commit = nullptr;
if (auto err = git_commit_lookup(&commit, m_repo.get(), objid)) {
diff --git a/source/signature.cpp b/source/signature.cpp
@@ -1,14 +1,7 @@
#include "git2wrap/signature.hpp"
#include "git2wrap/error.hpp"
static const struct empty_lambda_t // static and const applies to the object!
{
template<typename... T>
void operator()(T&&...) const // NOLINT
{
} // does nothing
} empty_lambda {}; // declare an object which is static and const
#include "git2wrap/types.hpp"
namespace git2wrap
{
diff --git a/source/tree.cpp b/source/tree.cpp
@@ -0,0 +1,143 @@
#include "git2wrap/tree.hpp"
#include "git2wrap/error.hpp"
#include "git2wrap/object.hpp"
#include "git2wrap/types.hpp"
namespace git2wrap
{
tree::tree(git_tree* tre, repositoryPtr repo)
: m_tree(tre, git_tree_free)
, m_repo(std::move(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__);
}
return {tre, m_repo};
}
const oid* tree::get_id() const
{
return git_tree_id(m_tree.get());
}
repositoryPtr tree::get_owner() const
{
return m_repo;
}
size_t tree::get_entrycount() const
{
return git_tree_entrycount(m_tree.get());
}
tree_entry tree::get_entry(const char* name) const
{
return {git_tree_entry_byname(m_tree.get(), name), m_repo};
}
tree_entry tree::get_entry(size_t idx) const
{
return {git_tree_entry_byindex(m_tree.get(), idx), m_repo};
}
tree_entry tree::get_entry(const oid* objid) const
{
return {git_tree_entry_byid(m_tree.get(), objid), m_repo};
}
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__);
}
return {entry, m_repo};
}
tree_entry::tree_entry(const git_tree_entry* entry, repositoryPtr repo)
: m_entry(const_cast<git_tree_entry*>(entry), empty_lambda) // NOLINT
, m_repo(std::move(repo))
{
}
tree_entry::tree_entry(git_tree_entry* entry, repositoryPtr repo)
: m_entry(entry, git_tree_entry_free)
, m_repo(std::move(repo))
{
}
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__);
}
return {entry, m_repo};
}
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__);
}
return {obj, m_repo};
}
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__);
}
return {reinterpret_cast<git_tree*>(obj), m_repo}; // NOLINT
}
const char* tree_entry::get_name() const
{
return git_tree_entry_name(m_entry.get());
}
const oid* tree_entry::get_id() const
{
return git_tree_entry_id(m_entry.get());
}
object_t tree_entry::get_type() const
{
return git_tree_entry_type(m_entry.get());
}
filemode_t tree_entry::get_filemode() const
{
return git_tree_entry_filemode(m_entry.get());
}
filemode_t tree_entry::get_filemode_raw() const
{
return git_tree_entry_filemode_raw(m_entry.get());
}
auto tree_entry::operator<=>(const tree_entry& rhs)
{
return git_tree_entry_cmp(m_entry.get(), rhs.m_entry.get());
}
} // namespace git2wrap