Skip to content

Commit 4c954bb

Browse files
committed
Refactored git_strarray_wrapper
1 parent e69f21b commit 4c954bb

9 files changed

Lines changed: 185 additions & 97 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ set(GIT2CPP_SRC
149149
${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.hpp
150150
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.cpp
151151
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.hpp
152+
${GIT2CPP_SOURCE_DIR}/wrapper/strarray_wrapper.cpp
153+
${GIT2CPP_SOURCE_DIR}/wrapper/strarray_wrapper.hpp
152154
${GIT2CPP_SOURCE_DIR}/wrapper/tag_wrapper.cpp
153155
${GIT2CPP_SOURCE_DIR}/wrapper/tag_wrapper.hpp
154156
${GIT2CPP_SOURCE_DIR}/wrapper/tree_wrapper.cpp

src/subcommand/log_subcommand.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <termcolor/termcolor.hpp>
1313

1414
#include "../utils/terminal_pager.hpp"
15+
#include "../wrapper/strarray_wrapper.hpp"
1516

1617
log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
1718
{
@@ -87,17 +88,17 @@ namespace
8788
std::vector<std::string> get_tags_for_commit(repository_wrapper& repo, const git_oid& commit_oid)
8889
{
8990
std::vector<std::string> tags;
90-
git_strarray tag_names = {0};
91+
strarray_owned_wrapper tag_names;
9192

92-
if (git_tag_list(&tag_names, repo) != 0)
93+
if (git_tag_list(tag_names, repo) != 0)
9394
{
9495
return tags;
9596
}
9697

97-
for (size_t i = 0; i < tag_names.count; i++)
98+
for (size_t i = 0; i < tag_names.size(); i++)
9899
{
99-
std::string tag_name = tag_names.strings[i];
100-
std::string ref_name = "refs/tags/" + std::string(tag_name);
100+
auto tag_name = std::string(tag_names[i]);
101+
std::string ref_name = "refs/tags/" + tag_name;
101102

102103
reference_wrapper tag_ref = repo.find_reference(ref_name);
103104
object_wrapper peeled = tag_ref.peel<object_wrapper>();
@@ -108,7 +109,6 @@ namespace
108109
}
109110
}
110111

111-
git_strarray_dispose(&tag_names); // TODO: refactor git_strarray_wrapper to use it here
112112
return tags;
113113
}
114114

src/subcommand/push_subcommand.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#include <git2/remote.h>
88

99
#include "../utils/ansi_code.hpp"
10-
#include "../utils/common.hpp"
10+
//#include "../utils/common.hpp"
1111
#include "../utils/credentials.hpp"
1212
#include "../utils/progress.hpp"
1313
#include "../wasm/scope.hpp"
14+
#include "../wrapper/strarray_wrapper.hpp"
1415

1516
push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app)
1617
{
@@ -182,7 +183,7 @@ void push_subcommand::run()
182183
{
183184
refspecs_push.push_back("refs/heads/" + refspec);
184185
}
185-
git_strarray_wrapper refspecs_wrapper(refspecs_push);
186+
strarray_view_wrapper refspecs_wrapper(refspecs_push);
186187
git_strarray* refspecs_ptr = refspecs_wrapper;
187188

188189
auto remotes_before_push = get_remotes(repo, remote_name);

src/utils/common.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,59 +60,6 @@ status_messages get_status_msg(git_status_t st)
6060
return get_status_msg_map().find(st)->second;
6161
}
6262

63-
git_strarray_wrapper::git_strarray_wrapper(std::vector<std::string> patterns)
64-
: m_patterns(std::move(patterns))
65-
{
66-
init_str_array();
67-
}
68-
69-
git_strarray_wrapper::git_strarray_wrapper(git_strarray_wrapper&& rhs)
70-
: m_patterns(std::move(rhs.m_patterns))
71-
{
72-
init_str_array();
73-
rhs.reset_str_array();
74-
}
75-
76-
git_strarray_wrapper& git_strarray_wrapper::operator=(git_strarray_wrapper&& rhs)
77-
{
78-
using std::swap;
79-
swap(m_patterns, rhs.m_patterns);
80-
swap(m_array.strings, rhs.m_array.strings);
81-
swap(m_array.count, rhs.m_array.count);
82-
return *this;
83-
}
84-
85-
git_strarray_wrapper::~git_strarray_wrapper()
86-
{
87-
reset_str_array();
88-
}
89-
90-
git_strarray_wrapper::operator git_strarray*()
91-
{
92-
return &m_array;
93-
}
94-
95-
void git_strarray_wrapper::reset_str_array()
96-
{
97-
delete[] m_array.strings;
98-
m_array = {nullptr, 0};
99-
}
100-
101-
void git_strarray_wrapper::init_str_array()
102-
{
103-
m_array.strings = new char*[m_patterns.size()];
104-
m_array.count = m_patterns.size();
105-
for (size_t i = 0; i < m_patterns.size(); ++i)
106-
{
107-
m_array.strings[i] = const_cast<char*>(m_patterns[i].c_str());
108-
}
109-
}
110-
111-
size_t git_strarray_wrapper::size()
112-
{
113-
return m_patterns.size();
114-
}
115-
11663
std::string read_file(const std::string& path)
11764
{
11865
std::ifstream file(path, std::ios::binary);

src/utils/common.hpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,6 @@ status_messages get_status_msg(git_status_t);
4040

4141
using stream_colour_fn = std::ostream& (*) (std::ostream&);
4242

43-
class git_strarray_wrapper
44-
{
45-
public:
46-
47-
git_strarray_wrapper()
48-
: m_patterns{}
49-
, m_array{nullptr, 0}
50-
{
51-
}
52-
53-
git_strarray_wrapper(std::vector<std::string> patterns);
54-
55-
git_strarray_wrapper(const git_strarray_wrapper&) = delete;
56-
git_strarray_wrapper& operator=(const git_strarray_wrapper&) = delete;
57-
58-
git_strarray_wrapper(git_strarray_wrapper&& rhs);
59-
git_strarray_wrapper& operator=(git_strarray_wrapper&&);
60-
61-
~git_strarray_wrapper();
62-
63-
operator git_strarray*();
64-
65-
size_t size();
66-
67-
private:
68-
69-
std::vector<std::string> m_patterns;
70-
git_strarray m_array;
71-
72-
void reset_str_array();
73-
void init_str_array();
74-
};
75-
7643
std::string read_file(const std::string& path);
7744

7845
std::vector<std::string> split_input_at_newlines(std::string_view str);

src/wrapper/index_wrapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../utils/common.hpp"
1010
#include "../utils/git_exception.hpp"
1111
#include "../wrapper/repository_wrapper.hpp"
12+
#include "../wrapper/strarray_wrapper.hpp"
1213

1314
index_wrapper::~index_wrapper()
1415
{
@@ -40,7 +41,7 @@ void index_wrapper::add_all()
4041

4142
void index_wrapper::add_impl(std::vector<std::string> patterns)
4243
{
43-
git_strarray_wrapper array{patterns};
44+
strarray_view_wrapper array{patterns};
4445
throw_if_error(git_index_add_all(*this, array, 0, NULL, NULL));
4546
}
4647

@@ -51,7 +52,7 @@ void index_wrapper::remove_entry(const std::string& path)
5152

5253
void index_wrapper::remove_entries(std::vector<std::string> paths)
5354
{
54-
git_strarray_wrapper array{paths};
55+
strarray_view_wrapper array{paths};
5556
throw_if_error(git_index_remove_all(*this, array, NULL, NULL));
5657
}
5758

src/wrapper/repository_wrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class repository_wrapper : public wrapper_base<git_repository>
134134
diff_wrapper diff_index_to_workdir(std::optional<index_wrapper> index, git_diff_options* diffopts);
135135

136136
// Tags
137-
// git_strarray_wrapper tag_list_match(std::string pattern);
137+
// strarray_view_wrapper tag_list_match(std::string pattern);
138138
std::vector<std::string> tag_list_match(std::string pattern);
139139

140140
private:

src/wrapper/strarray_wrapper.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "strarray_wrapper.hpp"
2+
3+
strarray_owned_wrapper::strarray_owned_wrapper()
4+
: m_array{nullptr, 0}
5+
{
6+
}
7+
8+
strarray_owned_wrapper::strarray_owned_wrapper(git_strarray&& arr)
9+
: m_array(std::move(arr))
10+
{
11+
}
12+
13+
strarray_owned_wrapper::strarray_owned_wrapper(strarray_owned_wrapper&& rhs)
14+
: m_array(std::move(rhs.m_array))
15+
{
16+
rhs.m_array = git_strarray{nullptr, 0};
17+
}
18+
19+
strarray_owned_wrapper& strarray_owned_wrapper::operator=(strarray_owned_wrapper&& rhs)
20+
{
21+
std::swap(m_array.strings, rhs.m_array.strings);
22+
std::swap(m_array.count, rhs.m_array.count);
23+
return *this;
24+
}
25+
26+
strarray_owned_wrapper::~strarray_owned_wrapper()
27+
{
28+
git_strarray_dispose(&m_array);
29+
}
30+
31+
strarray_owned_wrapper::operator git_strarray*()
32+
{
33+
return &m_array;
34+
}
35+
36+
size_t strarray_owned_wrapper::size() const
37+
{
38+
return m_array.count;
39+
}
40+
41+
std::string_view strarray_owned_wrapper::operator[](size_t i) const
42+
{
43+
return { m_array.strings[i] };
44+
}
45+
46+
strarray_view_wrapper::strarray_view_wrapper(std::vector<std::string> patterns)
47+
: m_patterns(std::move(patterns))
48+
{
49+
init_str_array();
50+
}
51+
52+
strarray_view_wrapper::strarray_view_wrapper(strarray_view_wrapper&& rhs)
53+
: m_patterns(std::move(rhs.m_patterns))
54+
{
55+
init_str_array();
56+
rhs.reset_str_array();
57+
}
58+
59+
strarray_view_wrapper& strarray_view_wrapper::operator=(strarray_view_wrapper&& rhs)
60+
{
61+
using std::swap;
62+
swap(m_patterns, rhs.m_patterns);
63+
swap(m_array.strings, rhs.m_array.strings);
64+
swap(m_array.count, rhs.m_array.count);
65+
return *this;
66+
}
67+
68+
strarray_view_wrapper::~strarray_view_wrapper()
69+
{
70+
reset_str_array();
71+
}
72+
73+
strarray_view_wrapper::operator git_strarray*()
74+
{
75+
return &m_array;
76+
}
77+
78+
void strarray_view_wrapper::reset_str_array()
79+
{
80+
delete[] m_array.strings;
81+
m_array = {nullptr, 0};
82+
}
83+
84+
void strarray_view_wrapper::init_str_array()
85+
{
86+
m_array.strings = new char*[m_patterns.size()];
87+
m_array.count = m_patterns.size();
88+
for (size_t i = 0; i < m_patterns.size(); ++i)
89+
{
90+
m_array.strings[i] = const_cast<char*>(m_patterns[i].c_str());
91+
}
92+
}
93+
94+
size_t strarray_view_wrapper::size() const
95+
{
96+
return m_patterns.size();
97+
}
98+
99+

src/wrapper/strarray_wrapper.hpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <string_view>
5+
#include <vector>
6+
7+
#include <git2.h>
8+
9+
// Wrapper of git_strarray that frees the contained
10+
// strings (i.e. calls git_strarray_dispose) upon destruction.
11+
class strarray_owned_wrapper
12+
{
13+
public:
14+
15+
strarray_owned_wrapper();
16+
explicit strarray_owned_wrapper(git_strarray&& arr);
17+
18+
strarray_owned_wrapper(const strarray_owned_wrapper&) = delete;
19+
strarray_owned_wrapper operator=(const strarray_owned_wrapper&) = delete;
20+
21+
strarray_owned_wrapper(strarray_owned_wrapper&& rhs);
22+
strarray_owned_wrapper& operator=(strarray_owned_wrapper&& rhs);
23+
24+
~strarray_owned_wrapper();
25+
26+
operator git_strarray*();
27+
28+
size_t size() const;
29+
30+
std::string_view operator[](size_t i) const;
31+
32+
private:
33+
34+
git_strarray m_array;
35+
};
36+
37+
// Wrapper of git_strarray containing pointers to strings
38+
// stored in a stnadard container. Does not free them upon
39+
// destruction.
40+
class strarray_view_wrapper
41+
{
42+
public:
43+
44+
strarray_view_wrapper()
45+
: m_patterns{}
46+
, m_array{nullptr, 0}
47+
{
48+
}
49+
50+
strarray_view_wrapper(std::vector<std::string> patterns);
51+
52+
strarray_view_wrapper(const strarray_view_wrapper&) = delete;
53+
strarray_view_wrapper& operator=(const strarray_view_wrapper&) = delete;
54+
55+
strarray_view_wrapper(strarray_view_wrapper&& rhs);
56+
strarray_view_wrapper& operator=(strarray_view_wrapper&& rhs);
57+
58+
~strarray_view_wrapper();
59+
60+
operator git_strarray*();
61+
62+
size_t size() const;
63+
64+
private:
65+
66+
std::vector<std::string> m_patterns;
67+
git_strarray m_array;
68+
69+
void reset_str_array();
70+
void init_str_array();
71+
};

0 commit comments

Comments
 (0)