Skip to content

Commit wrapper #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
32 changes: 0 additions & 32 deletions src/utils/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,6 @@ class noncopyable_nonmovable
~noncopyable_nonmovable() = default;
};

template <class T>
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:
Expand Down
19 changes: 19 additions & 0 deletions src/wrapper/commit_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -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));
Comment on lines +14 to +17
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This call relies on the implicit conversion from repository_wrapper to git_repository*. For readability and to avoid confusion, consider providing an explicit accessor (e.g. repo.get()) or using static_cast<git_repository*>(repo).

Suggested change
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));
throwIfError(git_reference_name_to_id(&oid_parent_commit, repo.get(), ref_name.c_str()));
commit_wrapper cw;
throwIfError(git_commit_lookup(&(cw.p_resource), repo.get(), &oid_parent_commit));

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implicit conversion is done on purpose.

return cw;
}
25 changes: 25 additions & 0 deletions src/wrapper/commit_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <string>

#include <git2.h>

#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/wrapper_base.hpp"

class commit_wrapper : public wrapper_base<git_commit>
{
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;
};
1 change: 1 addition & 0 deletions src/wrapper/index_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "index_wrapper.hpp"
#include "../utils/common.hpp"
#include "../utils/git_exception.hpp"
#include "../wrapper/repository_wrapper.hpp"

Expand Down
6 changes: 3 additions & 3 deletions src/wrapper/index_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <git2.h>

#include "../utils/common.hpp"
#include "../wrapper/wrapper_base.hpp"

class repository_wrapper;

Expand All @@ -15,8 +15,8 @@ class index_wrapper : public wrapper_base<git_index>

~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);

Expand Down
7 changes: 4 additions & 3 deletions src/wrapper/refs_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#pragma once

// #include <string>
#include <string>

#include <git2.h>

#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/wrapper_base.hpp"

class reference_wrapper : public wrapper_base<git_reference>
{
public:

~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);

Expand Down
6 changes: 3 additions & 3 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

#include <git2.h>

#include "../utils/common.hpp"
#include "../wrapper/index_wrapper.hpp"
#include "../wrapper/wrapper_base.hpp"

class repository_wrapper : public wrapper_base<git_repository>
{
public:

~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);
Expand Down
5 changes: 3 additions & 2 deletions src/wrapper/status_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <git2.h>

#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/wrapper_base.hpp"

class status_list_wrapper : public wrapper_base<git_status_list>
{
Expand All @@ -14,8 +15,8 @@ class status_list_wrapper : public wrapper_base<git_status_list>

~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);

Expand Down
35 changes: 35 additions & 0 deletions src/wrapper/wrapper_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once


template <class T>
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;
};