Skip to content

Commit 1c88f5f

Browse files
committed
Annotated commit wrapper
1 parent 854f926 commit 1c88f5f

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ set(GIT2CPP_SRC
5151
${GIT2CPP_SOURCE_DIR}/utils/common.hpp
5252
${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp
5353
${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp
54+
${GIT2CPP_SOURCE_DIR}/wrapper/annotated_commit_wrapper.cpp
55+
${GIT2CPP_SOURCE_DIR}/wrapper/annotated_commit_wrapper.hpp
5456
${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.cpp
5557
${GIT2CPP_SOURCE_DIR}/wrapper/branch_wrapper.hpp
5658
${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.cpp
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "../wrapper/annotated_commit_wrapper.hpp"
2+
3+
annotated_commit_wrapper::annotated_commit_wrapper(git_annotated_commit* commit)
4+
: base_type(commit)
5+
{
6+
}
7+
8+
annotated_commit_wrapper::~annotated_commit_wrapper()
9+
{
10+
git_annotated_commit_free(p_resource);
11+
p_resource = nullptr;
12+
}
13+
14+
const git_oid& annotated_commit_wrapper::oid() const
15+
{
16+
return *git_annotated_commit_id(p_resource);
17+
}
18+
19+
std::string_view annotated_commit_wrapper::reference_name() const
20+
{
21+
const char* res = git_annotated_commit_ref(*this);
22+
return res ? res : std::string_view{};
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
5+
#include <git2.h>
6+
7+
#include "../wrapper/wrapper_base.hpp"
8+
9+
class annotated_commit_wrapper : public wrapper_base<git_annotated_commit>
10+
{
11+
public:
12+
13+
using base_type = wrapper_base<git_annotated_commit>;
14+
15+
~annotated_commit_wrapper();
16+
17+
annotated_commit_wrapper(annotated_commit_wrapper&&) noexcept = default;
18+
annotated_commit_wrapper& operator=(annotated_commit_wrapper&&) noexcept = default;
19+
20+
const git_oid& oid() const;
21+
std::string_view reference_name() const;
22+
23+
private:
24+
25+
annotated_commit_wrapper(git_annotated_commit* commit);
26+
27+
friend class repository_wrapper;
28+
};
29+

src/wrapper/commit_wrapper.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ commit_wrapper::~commit_wrapper()
1111
p_resource = nullptr;
1212
}
1313

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

19-
commit_wrapper cw;
20-
throwIfError(git_commit_lookup(&(cw.p_resource), repo, &oid_parent_commit));
21-
return cw;
22-
}*/

src/wrapper/commit_wrapper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class commit_wrapper : public wrapper_base<git_commit>
1515
commit_wrapper(commit_wrapper&&) noexcept = default;
1616
commit_wrapper& operator=(commit_wrapper&&) noexcept = default;
1717

18+
const git_oid& oid() const;
19+
1820
private:
1921

2022
commit_wrapper(git_commit* commit);

src/wrapper/repository_wrapper.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ reference_wrapper repository_wrapper::head() const
2828
return reference_wrapper(ref);
2929
}
3030

31+
reference_wrapper repository_wrapper::find_reference(std::string_view ref_name) const
32+
{
33+
git_reference* ref;
34+
throwIfError(git_reference_lookup(&ref, *this, ref_name.data()));
35+
return reference_wrapper(ref);
36+
}
37+
3138
index_wrapper repository_wrapper::make_index()
3239
{
3340
index_wrapper index = index_wrapper::init(*this);
@@ -46,7 +53,7 @@ branch_wrapper repository_wrapper::create_branch(std::string_view name, const co
4653
return branch_wrapper(branch);
4754
}
4855

49-
branch_wrapper repository_wrapper::find_branch(std::string_view name)
56+
branch_wrapper repository_wrapper::find_branch(std::string_view name) const
5057
{
5158
git_reference* branch = nullptr;
5259
throwIfError(git_branch_lookup(&branch, *this, name.data(), GIT_BRANCH_LOCAL));
@@ -74,3 +81,10 @@ commit_wrapper repository_wrapper::find_commit(const git_oid& id) const
7481
throwIfError(git_commit_lookup(&commit, *this, &id));
7582
return commit_wrapper(commit);
7683
}
84+
85+
annotated_commit_wrapper repository_wrapper::find_annotated_commit(const git_oid& id) const
86+
{
87+
git_annotated_commit* commit;
88+
throwIfError(git_annotated_commit_lookup(&commit, *this, &id));
89+
return annotated_commit_wrapper(commit);
90+
}

src/wrapper/repository_wrapper.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <git2.h>
66

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

26+
// References
2527
reference_wrapper head() const;
28+
reference_wrapper find_reference(std::string_view ref_name) const;
2629

30+
// Index
2731
index_wrapper make_index();
2832

33+
// Branches
2934
branch_wrapper create_branch(std::string_view name, bool force);
3035
branch_wrapper create_branch(std::string_view name, const commit_wrapper& commit, bool force);
31-
32-
branch_wrapper find_branch(std::string_view name);
33-
36+
branch_wrapper find_branch(std::string_view name) const;
3437
branch_iterator iterate_branches(git_branch_t type) const;
3538

3639
// Commits
37-
3840
commit_wrapper find_commit(std::string_view ref_name = "HEAD") const;
3941
commit_wrapper find_commit(const git_oid& id) const;
4042

43+
// Annotated commits
44+
annotated_commit_wrapper find_annotated_commit(const git_oid& id) const;
45+
4146
private:
4247

4348
repository_wrapper() = default;

0 commit comments

Comments
 (0)