Skip to content

Commit 7eaba8d

Browse files
committed
refactoring v2
1 parent a7b747d commit 7eaba8d

File tree

7 files changed

+89
-34
lines changed

7 files changed

+89
-34
lines changed

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "src/utils/git_exception.hpp"
66
#include "version.hpp"
77
#include "subcommand/init_subcommand.hpp"
8-
// #include "subcommand/status_subcommand.hpp"
8+
#include "subcommand/status_subcommand.hpp"
99

1010
int main(int argc, char** argv)
1111
{
@@ -19,7 +19,7 @@ int main(int argc, char** argv)
1919
auto version = app.add_flag("-v,--version", "Show version");
2020

2121
// Sub commands
22-
InitSubcommand init(lg2_obj, app);
22+
init_subcommand init(lg2_obj, app);
2323

2424
app.parse(argc, argv);
2525

src/subcommand/init_subcommand.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#include <filesystem>
1+
// #include <filesystem>
22
#include "init_subcommand.hpp"
3+
#include "src/wrapper/repository_wrapper.hpp"
34

4-
InitSubcommand::InitSubcommand(const libgit2_object&, CLI::App& app)
5+
init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app)
56
{
67
auto *sub = app.add_subcommand("init", "Explanation of init here");
78

@@ -10,12 +11,12 @@ InitSubcommand::InitSubcommand(const libgit2_object&, CLI::App& app)
1011
// If directory not specified, uses cwd.
1112
sub->add_option("directory", directory, "info about directory arg")
1213
->check(CLI::ExistingDirectory | CLI::NonexistentPath)
13-
->default_val(std::filesystem::current_path());
14+
->default_val(get_current_git_path());
1415

1516
sub->callback([this]() { this->run(); });
1617
}
1718

18-
void InitSubcommand::run()
19+
void init_subcommand::run()
1920
{
20-
RepositoryWrapper::init(directory, bare);
21+
repository_wrapper::init(directory, bare);
2122
}

src/subcommand/init_subcommand.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#pragma once
22

3-
#include <CLI/CLI.hpp>
43
#include <string>
54

6-
#include "src/wrapper/repository_wrapper.hpp"
5+
#include <CLI/CLI.hpp>
6+
7+
#include "../utils/common.hpp"
78

8-
class InitSubcommand
9+
class init_subcommand
910
{
1011
public:
1112

12-
explicit InitSubcommand(const libgit2_object&, CLI::App& app);
13+
explicit init_subcommand(const libgit2_object&, CLI::App& app);
1314
void run();
1415

1516
private:

src/utils/common.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <filesystem>
2+
13
#include <git2.h>
24

35
#include "common.hpp"
@@ -11,3 +13,13 @@ libgit2_object::~libgit2_object()
1113
{
1214
git_libgit2_shutdown();
1315
}
16+
17+
std::string get_current_git_path()
18+
{
19+
return std::filesystem::current_path(); // TODO: make sure that it goes to the root
20+
}
21+
22+
// // If directory not specified, uses cwd.
23+
// sub->add_option("directory", directory, "info about directory arg")
24+
// ->check(CLI::ExistingDirectory | CLI::NonexistentPath)
25+
// ->default_val(std::filesystem::current_path());

src/utils/common.hpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <utility>
5+
16
class noncopiable_nonmovable
27
{
38
public:
49
noncopiable_nonmovable(const noncopiable_nonmovable&) = delete;
510
noncopiable_nonmovable& operator=(const noncopiable_nonmovable&) = delete;
6-
noncopiable_nonmovable(noncopiable_nonmovable&) = delete;
11+
noncopiable_nonmovable(noncopiable_nonmovable&&) = delete;
712
noncopiable_nonmovable& operator=(noncopiable_nonmovable&&) = delete;
813

914
protected:
1015
noncopiable_nonmovable() = default;
1116
~noncopiable_nonmovable() = default;
1217
};
1318

19+
template <class T>
20+
class wrapper_base
21+
{
22+
public:
23+
using ressource_type = T;
24+
25+
wrapper_base(const wrapper_base&) = delete;
26+
wrapper_base& operator=(const wrapper_base&) = delete;
27+
28+
wrapper_base(wrapper_base&& rhs)
29+
: p_ressource(rhs.p_ressource)
30+
{
31+
rhs.p_ressource = nullptr;
32+
}
33+
wrapper_base& operator=(wrapper_base&& rhs)
34+
{
35+
std::swap(p_ressource, rhs.p_ressource);
36+
return this;
37+
}
38+
39+
operator ressource_type*() const noexcept
40+
{
41+
return p_ressource;
42+
}
43+
44+
protected:
45+
// Allocation and deletion of p_ressource must be handled by inheriting class.
46+
wrapper_base() = default;
47+
~wrapper_base() = default;
48+
ressource_type* p_ressource = nullptr;
49+
};
50+
1451
class libgit2_object : private noncopiable_nonmovable
1552
{
1653
public:
1754

1855
libgit2_object();
1956
~libgit2_object();
2057
};
58+
59+
std::string get_current_git_path();

src/wrapper/repository_wrapper.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
#include "src/utils/git_exception.hpp"
1+
#include "../utils/git_exception.hpp"
22
#include "repository_wrapper.hpp"
33

44

5-
RepositoryWrapper::RepositoryWrapper()
6-
: p_repo(nullptr)
7-
{}
8-
9-
RepositoryWrapper::~RepositoryWrapper()
5+
repository_wrapper::~repository_wrapper()
106
{
11-
git_repository_free(p_repo);
12-
p_repo=nullptr;
7+
git_repository_free(p_ressource);
8+
p_ressource=nullptr;
139
}
1410

15-
// RepositoryWrapper::RepositoryWrapper open(const std::string path)
16-
// {
17-
//
18-
// };
11+
repository_wrapper repository_wrapper::open(const std::string& directory)
12+
{
13+
repository_wrapper rw;
14+
throwIfError(git_repository_open(&(rw.p_ressource), directory.c_str()));
15+
return rw;
16+
}
1917

20-
void RepositoryWrapper::init(const std::string& directory, bool bare)
18+
repository_wrapper repository_wrapper::init(const std::string& directory, bool bare)
2119
{
22-
RepositoryWrapper rw;
23-
throwIfError(git_repository_init(&(rw.p_repo), directory.c_str(), bare));
20+
repository_wrapper rw;
21+
throwIfError(git_repository_init(&(rw.p_ressource), directory.c_str(), bare));
22+
return rw;
2423
}

src/wrapper/repository_wrapper.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
#pragma once
22

3-
#include <git2.h>
43
#include <string>
54

5+
#include <git2.h>
6+
67
#include "../utils/common.hpp"
78

8-
class RepositoryWrapper : private noncopiable_nonmovable
9+
class repository_wrapper : public wrapper_base<git_repository>
910
{
1011
public:
1112

12-
~RepositoryWrapper();
13+
~repository_wrapper();
14+
15+
repository_wrapper(repository_wrapper&&) = default;
16+
repository_wrapper& operator=(repository_wrapper&&) = default;
1317

14-
// static RepositoryWrapper open(const std::string path);
15-
static void init(const std::string& directory, bool bare);
18+
static repository_wrapper init(const std::string& directory, bool bare);
19+
static repository_wrapper open(const std::string& directory);
1620

1721
private:
1822

19-
RepositoryWrapper();
20-
git_repository* p_repo;
23+
repository_wrapper() = default;
2124
};

0 commit comments

Comments
 (0)