Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Annotated commit wrapper
  • Loading branch information
JohanMabille committed Jul 21, 2025
commit 1c88f5f88480d49f08e53623e31fcae1bd5ecca9
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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/annotated_commit_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/annotated_commit_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.cpp
Expand Down
23 changes: 23 additions & 0 deletions src/wrapper/annotated_commit_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../wrapper/annotated_commit_wrapper.hpp"

annotated_commit_wrapper::annotated_commit_wrapper(git_annotated_commit* commit)
: base_type(commit)
{
}

annotated_commit_wrapper::~annotated_commit_wrapper()
{
git_annotated_commit_free(p_resource);
p_resource = nullptr;
}

const git_oid& annotated_commit_wrapper::oid() const
{
return *git_annotated_commit_id(p_resource);
}

std::string_view annotated_commit_wrapper::reference_name() const
{
const char* res = git_annotated_commit_ref(*this);
return res ? res : std::string_view{};
}
29 changes: 29 additions & 0 deletions src/wrapper/annotated_commit_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <string_view>

#include <git2.h>

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

class annotated_commit_wrapper : public wrapper_base<git_annotated_commit>
{
public:

using base_type = wrapper_base<git_annotated_commit>;

~annotated_commit_wrapper();

annotated_commit_wrapper(annotated_commit_wrapper&&) noexcept = default;
annotated_commit_wrapper& operator=(annotated_commit_wrapper&&) noexcept = default;

const git_oid& oid() const;
std::string_view reference_name() const;

private:

annotated_commit_wrapper(git_annotated_commit* commit);

friend class repository_wrapper;
};

10 changes: 3 additions & 7 deletions src/wrapper/commit_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ commit_wrapper::~commit_wrapper()
p_resource = nullptr;
}

/*commit_wrapper commit_wrapper::from_reference_name(const repository_wrapper& repo, const std::string& ref_name)
const git_oid& commit_wrapper::oid() const
{
git_oid oid_parent_commit;
throwIfError(git_reference_name_to_id(&oid_parent_commit, repo, ref_name.c_str()));
return *git_commit_id(p_resource);
}

commit_wrapper cw;
throwIfError(git_commit_lookup(&(cw.p_resource), repo, &oid_parent_commit));
return cw;
}*/
2 changes: 2 additions & 0 deletions src/wrapper/commit_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class commit_wrapper : public wrapper_base<git_commit>
commit_wrapper(commit_wrapper&&) noexcept = default;
commit_wrapper& operator=(commit_wrapper&&) noexcept = default;

const git_oid& oid() const;

private:

commit_wrapper(git_commit* commit);
Expand Down
16 changes: 15 additions & 1 deletion src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ reference_wrapper repository_wrapper::head() const
return reference_wrapper(ref);
}

reference_wrapper repository_wrapper::find_reference(std::string_view ref_name) const
{
git_reference* ref;
throwIfError(git_reference_lookup(&ref, *this, ref_name.data()));
return reference_wrapper(ref);
}

index_wrapper repository_wrapper::make_index()
{
index_wrapper index = index_wrapper::init(*this);
Expand All @@ -46,7 +53,7 @@ branch_wrapper repository_wrapper::create_branch(std::string_view name, const co
return branch_wrapper(branch);
}

branch_wrapper repository_wrapper::find_branch(std::string_view name)
branch_wrapper repository_wrapper::find_branch(std::string_view name) const
{
git_reference* branch = nullptr;
throwIfError(git_branch_lookup(&branch, *this, name.data(), GIT_BRANCH_LOCAL));
Expand Down Expand Up @@ -74,3 +81,10 @@ commit_wrapper repository_wrapper::find_commit(const git_oid& id) const
throwIfError(git_commit_lookup(&commit, *this, &id));
return commit_wrapper(commit);
}

annotated_commit_wrapper repository_wrapper::find_annotated_commit(const git_oid& id) const
{
git_annotated_commit* commit;
throwIfError(git_annotated_commit_lookup(&commit, *this, &id));
return annotated_commit_wrapper(commit);
}
13 changes: 9 additions & 4 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <git2.h>

#include "../wrapper/annotated_commit_wrapper.hpp"
#include "../wrapper/branch_wrapper.hpp"
#include "../wrapper/commit_wrapper.hpp"
#include "../wrapper/index_wrapper.hpp"
Expand All @@ -22,22 +23,26 @@ class repository_wrapper : public wrapper_base<git_repository>
static repository_wrapper init(std::string_view directory, bool bare);
static repository_wrapper open(std::string_view directory);

// References
reference_wrapper head() const;
reference_wrapper find_reference(std::string_view ref_name) const;

// Index
index_wrapper make_index();

// Branches
branch_wrapper create_branch(std::string_view name, bool force);
branch_wrapper create_branch(std::string_view name, const commit_wrapper& commit, bool force);

branch_wrapper find_branch(std::string_view name);

branch_wrapper find_branch(std::string_view name) const;
branch_iterator iterate_branches(git_branch_t type) const;

// Commits

commit_wrapper find_commit(std::string_view ref_name = "HEAD") const;
commit_wrapper find_commit(const git_oid& id) const;

// Annotated commits
annotated_commit_wrapper find_annotated_commit(const git_oid& id) const;

private:

repository_wrapper() = default;
Expand Down