Skip to content

Add git_strarray wrapper #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 31 additions & 0 deletions src/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,34 @@ 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> m_patterns)
: m_patterns(std::move(m_patterns))
{
init_str_array();
}

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

git_strarray_wrapper::~git_strarray_wrapper()
{
delete[] m_array.strings;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The destructor should also reset the pointer to nullptr after its deletion, and set m_array.size to 0, so many calls to the destructor are harmless.

This could be moved in a private reset_str_array method that could be called from the destructor and other methods.

}

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

void git_strarray_wrapper::init_str_array()
{
git_strarray array{new char*[m_patterns.size()], m_patterns.size()};
for (size_t i=0; i<m_patterns.size(); ++i)
{
array.strings[i] = const_cast<char*>(m_patterns[i].c_str());
}
}
28 changes: 28 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,28 @@ 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> m_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*();

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

void init_str_array();
};