Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,51 @@ std::string get_current_git_path()
// sub->add_option("directory", directory, "info about directory arg")
// ->check(CLI::ExistingDirectory | CLI::NonexistentPath)
// ->default_val(std::filesystem::current_path());

git_strarray_wrapper::git_strarray_wrapper(std::vector<std::string> patterns)
: m_patterns(std::move(patterns))
{
init_str_array();
}

git_strarray_wrapper::git_strarray_wrapper(git_strarray_wrapper&& rhs)
: m_patterns(std::move(rhs.m_patterns))
{
init_str_array();
rhs.reset_str_array();
}

git_strarray_wrapper& git_strarray_wrapper::operator=(git_strarray_wrapper&& rhs)
{
using std::swap;
swap(m_patterns, rhs.m_patterns);
swap(m_array.strings, rhs.m_array.strings);
swap(m_array.count, rhs.m_array.count);
return *this;
}

git_strarray_wrapper::~git_strarray_wrapper()
{
reset_str_array();
}

git_strarray_wrapper::operator git_strarray*()
{
return &m_array;
}

void git_strarray_wrapper::reset_str_array()
{
delete[] m_array.strings;
m_array={nullptr, 0};
}

void git_strarray_wrapper::init_str_array()
{
m_array.strings = new char*[m_patterns.size()];
m_array.count = m_patterns.size();
for (size_t i=0; i<m_patterns.size(); ++i)
{
m_array.strings[i] = const_cast<char*>(m_patterns[i].c_str());
}
}
30 changes: 30 additions & 0 deletions src/utils/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <string>
#include <utility>
#include <vector>

#include <git2.h>

class noncopyable_nonmovable
{
Expand Down Expand Up @@ -57,3 +60,30 @@ class libgit2_object : private noncopyable_nonmovable
};

std::string get_current_git_path();

class git_strarray_wrapper
{
public:
git_strarray_wrapper()
: m_patterns{}
, m_array{nullptr, 0}
{}
git_strarray_wrapper(std::vector<std::string> patterns);

git_strarray_wrapper(const git_strarray_wrapper&) = delete;
git_strarray_wrapper& operator=(const git_strarray_wrapper&) = delete;

git_strarray_wrapper(git_strarray_wrapper&& rhs);
git_strarray_wrapper& operator=(git_strarray_wrapper&&);

~git_strarray_wrapper();

operator git_strarray*();

private:
std::vector<std::string> m_patterns;
git_strarray m_array;

void reset_str_array();
void init_str_array();
};