Skip to content

Commit 9e04c96

Browse files
committed
add git_strarray wrapper
1 parent 7ba5a9c commit 9e04c96

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/utils/common.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ std::string get_current_git_path()
2323
// sub->add_option("directory", directory, "info about directory arg")
2424
// ->check(CLI::ExistingDirectory | CLI::NonexistentPath)
2525
// ->default_val(std::filesystem::current_path());
26+
27+
git_strarray git_strarray_wrapper::init_str_array()
28+
{
29+
git_strarray_wrapper aw;
30+
git_strarray array{new char*[aw.m_patterns.size()], aw.m_patterns.size()};
31+
for (size_t i=0; i<aw.m_patterns.size(); ++i)
32+
{
33+
array.strings[i] = const_cast<char*>(aw.m_patterns[i].c_str());
34+
}
35+
return array;
36+
}

src/utils/common.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#include <string>
44
#include <utility>
5+
#include <vector>
6+
7+
#include <git2.h>
58

69
class noncopyable_nonmovable
710
{
@@ -57,3 +60,42 @@ class libgit2_object : private noncopyable_nonmovable
5760
};
5861

5962
std::string get_current_git_path();
63+
64+
class git_strarray_wrapper
65+
{
66+
public:
67+
git_strarray_wrapper()
68+
: m_patterns{}
69+
, m_array{nullptr, 0}
70+
{}
71+
git_strarray_wrapper(std::vector<std::string> m_patterns)
72+
: m_patterns(std::move(m_patterns))
73+
{
74+
init_str_array();
75+
}
76+
77+
git_strarray_wrapper(const git_strarray_wrapper&) = delete;
78+
git_strarray_wrapper& operator=(const git_strarray_wrapper&) = delete;
79+
80+
git_strarray_wrapper(git_strarray_wrapper&& rhs)
81+
: m_patterns(std::move(rhs.m_patterns))
82+
{
83+
init_str_array();
84+
}
85+
86+
~git_strarray_wrapper()
87+
{
88+
delete[] m_array.strings;
89+
}
90+
91+
operator git_strarray*()
92+
{
93+
return &m_array;
94+
}
95+
96+
static git_strarray init_str_array();
97+
98+
protected:
99+
std::vector<std::string> m_patterns;
100+
git_strarray m_array;
101+
};

0 commit comments

Comments
 (0)