Skip to content

Commit c038e3f

Browse files
authored
Commit wrapper (#18)
* Mover wrapper_base to a dedicated file * Skeleton for commit_wrapper * noexcept pass
1 parent 78f7356 commit c038e3f

10 files changed

+96
-43
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ set(GIT2CPP_SRC
4949
${GIT2CPP_SOURCE_DIR}/utils/common.hpp
5050
${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp
5151
${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp
52+
${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.cpp
53+
${GIT2CPP_SOURCE_DIR}/wrapper/commit_wrapper.hpp
5254
${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.cpp
5355
${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.hpp
5456
${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.cpp
@@ -57,6 +59,7 @@ set(GIT2CPP_SRC
5759
${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.hpp
5860
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.cpp
5961
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.hpp
62+
${GIT2CPP_SOURCE_DIR}/wrapper/wrapper_base.hpp
6063
${GIT2CPP_SOURCE_DIR}/main.cpp
6164
${GIT2CPP_SOURCE_DIR}/version.hpp
6265
)

src/utils/common.hpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,6 @@ class noncopyable_nonmovable
1919
~noncopyable_nonmovable() = default;
2020
};
2121

22-
template <class T>
23-
class wrapper_base
24-
{
25-
public:
26-
using resource_type = T;
27-
28-
wrapper_base(const wrapper_base&) = delete;
29-
wrapper_base& operator=(const wrapper_base&) = delete;
30-
31-
wrapper_base(wrapper_base&& rhs)
32-
: p_resource(rhs.p_resource)
33-
{
34-
rhs.p_resource = nullptr;
35-
}
36-
wrapper_base& operator=(wrapper_base&& rhs)
37-
{
38-
std::swap(p_resource, rhs.p_resource);
39-
return *this;
40-
}
41-
42-
operator resource_type*() const noexcept
43-
{
44-
return p_resource;
45-
}
46-
47-
protected:
48-
// Allocation and deletion of p_resource must be handled by inheriting class.
49-
wrapper_base() = default;
50-
~wrapper_base() = default;
51-
resource_type* p_resource = nullptr;
52-
};
53-
5422
class libgit2_object : private noncopyable_nonmovable
5523
{
5624
public:

src/wrapper/commit_wrapper.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "commit_wrapper.hpp"
2+
#include "../utils/git_exception.hpp"
3+
4+
commit_wrapper::~commit_wrapper()
5+
{
6+
git_commit_free(p_resource);
7+
p_resource = nullptr;
8+
}
9+
10+
11+
commit_wrapper commit_wrapper::last_commit(const repository_wrapper& repo, const std::string& ref_name)
12+
{
13+
git_oid oid_parent_commit;
14+
throwIfError(git_reference_name_to_id(&oid_parent_commit, repo, ref_name.c_str()));
15+
16+
commit_wrapper cw;
17+
throwIfError(git_commit_lookup(&(cw.p_resource), repo, &oid_parent_commit));
18+
return cw;
19+
}

src/wrapper/commit_wrapper.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include <git2.h>
6+
7+
#include "../wrapper/repository_wrapper.hpp"
8+
#include "../wrapper/wrapper_base.hpp"
9+
10+
class commit_wrapper : public wrapper_base<git_commit>
11+
{
12+
public:
13+
14+
~commit_wrapper();
15+
16+
commit_wrapper(commit_wrapper&&) noexcept = default;
17+
commit_wrapper& operator=(commit_wrapper&&) noexcept = default;
18+
19+
static commit_wrapper
20+
last_commit(const repository_wrapper& repo, const std::string& ref_name = "HEAD");
21+
22+
private:
23+
24+
commit_wrapper() = default;
25+
};

src/wrapper/index_wrapper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "index_wrapper.hpp"
2+
#include "../utils/common.hpp"
23
#include "../utils/git_exception.hpp"
34
#include "../wrapper/repository_wrapper.hpp"
45

src/wrapper/index_wrapper.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <git2.h>
77

8-
#include "../utils/common.hpp"
8+
#include "../wrapper/wrapper_base.hpp"
99

1010
class repository_wrapper;
1111

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

1616
~index_wrapper();
1717

18-
index_wrapper(index_wrapper&&) = default;
19-
index_wrapper& operator=(index_wrapper&&) = default;
18+
index_wrapper(index_wrapper&&) noexcept = default;
19+
index_wrapper& operator=(index_wrapper&&) noexcept = default;
2020

2121
static index_wrapper init(repository_wrapper& rw);
2222

src/wrapper/refs_wrapper.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#pragma once
22

3-
// #include <string>
3+
#include <string>
44

55
#include <git2.h>
66

77
#include "../wrapper/repository_wrapper.hpp"
8+
#include "../wrapper/wrapper_base.hpp"
89

910
class reference_wrapper : public wrapper_base<git_reference>
1011
{
1112
public:
1213

1314
~reference_wrapper();
1415

15-
reference_wrapper(reference_wrapper&&) = default;
16-
reference_wrapper& operator=(reference_wrapper&&) = default;
16+
reference_wrapper(reference_wrapper&&) noexcept = default;
17+
reference_wrapper& operator=(reference_wrapper&&) noexcept = default;
1718

1819
static std::string get_ref_name(const repository_wrapper& repo);
1920

src/wrapper/repository_wrapper.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
#include <git2.h>
66

7-
#include "../utils/common.hpp"
87
#include "../wrapper/index_wrapper.hpp"
8+
#include "../wrapper/wrapper_base.hpp"
99

1010
class repository_wrapper : public wrapper_base<git_repository>
1111
{
1212
public:
1313

1414
~repository_wrapper();
1515

16-
repository_wrapper(repository_wrapper&&) = default;
17-
repository_wrapper& operator=(repository_wrapper&&) = default;
16+
repository_wrapper(repository_wrapper&&) noexcept = default;
17+
repository_wrapper& operator=(repository_wrapper&&) noexcept = default;
1818

1919
static repository_wrapper init(const std::string& directory, bool bare);
2020
static repository_wrapper open(const std::string& directory);

src/wrapper/status_wrapper.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <git2.h>
77

88
#include "../wrapper/repository_wrapper.hpp"
9+
#include "../wrapper/wrapper_base.hpp"
910

1011
class status_list_wrapper : public wrapper_base<git_status_list>
1112
{
@@ -14,8 +15,8 @@ class status_list_wrapper : public wrapper_base<git_status_list>
1415

1516
~status_list_wrapper();
1617

17-
status_list_wrapper(status_list_wrapper&&) = default;
18-
status_list_wrapper& operator=(status_list_wrapper&&) = default;
18+
status_list_wrapper(status_list_wrapper&&) noexcept = default;
19+
status_list_wrapper& operator=(status_list_wrapper&&) noexcept = default;
1920

2021
static status_list_wrapper status_list(const repository_wrapper& wrapper);
2122

src/wrapper/wrapper_base.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
4+
template <class T>
5+
class wrapper_base
6+
{
7+
public:
8+
using resource_type = T;
9+
10+
wrapper_base(const wrapper_base&) = delete;
11+
wrapper_base& operator=(const wrapper_base&) = delete;
12+
13+
wrapper_base(wrapper_base&& rhs) noexcept
14+
: p_resource(rhs.p_resource)
15+
{
16+
rhs.p_resource = nullptr;
17+
}
18+
wrapper_base& operator=(wrapper_base&& rhs) noexcept
19+
{
20+
std::swap(p_resource, rhs.p_resource);
21+
return *this;
22+
}
23+
24+
operator resource_type*() const noexcept
25+
{
26+
return p_resource;
27+
}
28+
29+
protected:
30+
// Allocation and deletion of p_resource must be handled by inheriting class.
31+
wrapper_base() = default;
32+
~wrapper_base() = default;
33+
34+
resource_type* p_resource = nullptr;
35+
};

0 commit comments

Comments
 (0)