Skip to content

Commit 0dd4d54

Browse files
committed
initial commit
1 parent 18b78d2 commit 0dd4d54

File tree

5 files changed

+73
-30
lines changed

5 files changed

+73
-30
lines changed
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
#ifndef CPPGIT_GIT_HPP
2-
#define CPPGIT_GIT_HPP
1+
#ifndef CPPGIT_GIT2API_HPP
2+
#define CPPGIT_GIT2API_HPP
33

44
#include <tuple>
5-
5+
#include <atomic>
66
namespace cppgit {
77
// =====================================================================================
88
// Class: Git
99
// Description:
1010
// =====================================================================================
11-
class Git {
11+
class Git2API {
1212
public:
1313
// ==================== LIFECYCLE =======================================
14-
Git();
15-
~Git();
14+
Git2API();
15+
Git2API(const Git2API &);
16+
Git2API(Git2API &&) = delete;
17+
Git2API &operator=(const Git2API &) = default;
18+
Git2API &operator=(Git2API &&) = delete;
19+
virtual ~Git2API();
1620
// ==================== ACCESSORS =======================================
21+
[[nodiscard]] static int counter() noexcept;
1722
[[nodiscard]] static std::tuple<int, int, int> version();
1823
// ==================== MUTATORS =======================================
1924
// ==================== OPERATORS =======================================
@@ -23,7 +28,8 @@ class Git {
2328
private:
2429
// ==================== METHODS =======================================
2530
// ==================== DATA MEMBERS =======================================
26-
}; // ----- end of class Git -----
31+
inline static std::atomic<int> m_init_counter{0};
32+
}; // ----- end of class Git2API -----
2733
} // namespace cppgit
2834

2935
#endif

include/cppgit/repository.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CPPGIT_HPP
22
#define CPPGIT_HPP
33

4+
#include "cppgit/git2api.hpp"
45
#include <tuple>
56

67
namespace cppgit {
@@ -9,7 +10,7 @@ namespace cppgit {
910
// Description:
1011
//
1112
// =====================================================================================
12-
class Repository {
13+
class Repository: private Git2API {
1314
public:
1415
// ==================== LIFECYCLE =======================================
1516
Repository();

src/git.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/git2api.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "cppgit/git2api.hpp"
2+
#include "cppgit/exception.hpp"
3+
#include <git2.h>
4+
5+
namespace cppgit {
6+
7+
Git2API::Git2API() {
8+
if (++m_init_counter == 1) {
9+
git_libgit2_init();
10+
}
11+
}
12+
13+
Git2API::Git2API(const Git2API&) {
14+
++m_init_counter;
15+
}
16+
17+
18+
Git2API::~Git2API() {
19+
if (--m_init_counter == 0){
20+
git_libgit2_shutdown();
21+
}
22+
}
23+
24+
int Git2API::counter() noexcept{
25+
return Git2API::m_init_counter;
26+
}
27+
std::tuple<int, int, int> Git2API::version() {
28+
int major = 0, minor = 0, revision = 0;
29+
if (git_libgit2_version(&major, &minor, &revision) != 0) {
30+
//
31+
throw Exception();
32+
}
33+
return {major, minor, revision};
34+
}
35+
36+
} // namespace cppgit

test/src/version_test.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11

2+
#include "cppgit/git2api.hpp"
23
#include "cppgit/version.hpp"
34
#include <doctest/doctest.h>
45
#include <iostream>
56
#include <string>
67

7-
TEST_CASE("Greeter version") {
8+
class Object: private cppgit::Git2API {
9+
10+
};
11+
12+
TEST_CASE("Version") {
813
static_assert(std::string_view(CPPGIT_VERSION) == std::string_view("1.0.0"));
914
CHECK(std::string(CPPGIT_VERSION) == std::string("1.0.0"));
1015
}
16+
17+
TEST_CASE("Initialization Test") {
18+
{
19+
const cppgit::Git2API git_obj1;
20+
{
21+
const cppgit::Git2API git_obj2;
22+
const Object git_obj3;
23+
CHECK(cppgit::Git2API::counter() == 3);
24+
}
25+
CHECK(cppgit::Git2API::counter() == 1);
26+
const cppgit::Git2API git_obj2;
27+
CHECK(cppgit::Git2API::counter() == 2);
28+
}
29+
CHECK(cppgit::Git2API::counter() == 0);
30+
31+
}

0 commit comments

Comments
 (0)