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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ set(GIT2CPP_SRC
${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/status_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/strarray_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/strarray_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/tag_wrapper.cpp
${GIT2CPP_SOURCE_DIR}/wrapper/tag_wrapper.hpp
${GIT2CPP_SOURCE_DIR}/wrapper/tree_wrapper.cpp
Expand Down
12 changes: 6 additions & 6 deletions src/subcommand/log_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <termcolor/termcolor.hpp>

#include "../utils/terminal_pager.hpp"
#include "../wrapper/strarray_wrapper.hpp"

log_subcommand::log_subcommand(const libgit2_object&, CLI::App& app)
{
Expand Down Expand Up @@ -87,17 +88,17 @@ namespace
std::vector<std::string> get_tags_for_commit(repository_wrapper& repo, const git_oid& commit_oid)
{
std::vector<std::string> tags;
git_strarray tag_names = {0};
strarray_owned_wrapper tag_names;

if (git_tag_list(&tag_names, repo) != 0)
if (git_tag_list(tag_names, repo) != 0)
{
return tags;
}

for (size_t i = 0; i < tag_names.count; i++)
for (size_t i = 0; i < tag_names.size(); i++)
{
std::string tag_name = tag_names.strings[i];
std::string ref_name = "refs/tags/" + std::string(tag_name);
auto tag_name = std::string(tag_names[i]);
std::string ref_name = "refs/tags/" + tag_name;

reference_wrapper tag_ref = repo.find_reference(ref_name);
object_wrapper peeled = tag_ref.peel<object_wrapper>();
Expand All @@ -108,7 +109,6 @@ namespace
}
}

git_strarray_dispose(&tag_names); // TODO: refactor git_strarray_wrapper to use it here
return tags;
}

Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/push_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <git2/remote.h>

#include "../utils/ansi_code.hpp"
#include "../utils/common.hpp"
#include "../utils/credentials.hpp"
#include "../utils/progress.hpp"
#include "../wasm/scope.hpp"
#include "../wrapper/strarray_wrapper.hpp"

push_subcommand::push_subcommand(const libgit2_object&, CLI::App& app)
{
Expand Down Expand Up @@ -182,7 +182,7 @@ void push_subcommand::run()
{
refspecs_push.push_back("refs/heads/" + refspec);
}
git_strarray_wrapper refspecs_wrapper(refspecs_push);
strarray_view_wrapper refspecs_wrapper(refspecs_push);
git_strarray* refspecs_ptr = refspecs_wrapper;

auto remotes_before_push = get_remotes(repo, remote_name);
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/tag_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ void tag_subcommand::list_tags(repository_wrapper& repo)
std::string pattern = m_tag_name.empty() ? "*" : m_tag_name;
auto tag_names = repo.tag_list_match(pattern);

for (const auto& tag_name : tag_names)
for (size_t i = 0u; i < tag_names.size(); ++i)
{
each_tag(repo, tag_name, m_num_lines);
each_tag(repo, tag_names[i], m_num_lines);
}
}

Expand Down
53 changes: 0 additions & 53 deletions src/utils/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,59 +60,6 @@ status_messages get_status_msg(git_status_t st)
return get_status_msg_map().find(st)->second;
}

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());
}
}

size_t git_strarray_wrapper::size()
{
return m_patterns.size();
}

std::string read_file(const std::string& path)
{
std::ifstream file(path, std::ios::binary);
Expand Down
33 changes: 0 additions & 33 deletions src/utils/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,6 @@ status_messages get_status_msg(git_status_t);

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

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*();

size_t size();

private:

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

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

std::string read_file(const std::string& path);

std::vector<std::string> split_input_at_newlines(std::string_view str);
Expand Down
5 changes: 3 additions & 2 deletions src/wrapper/index_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../utils/common.hpp"
#include "../utils/git_exception.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/strarray_wrapper.hpp"

index_wrapper::~index_wrapper()
{
Expand Down Expand Up @@ -40,7 +41,7 @@ void index_wrapper::add_all()

void index_wrapper::add_impl(std::vector<std::string> patterns)
{
git_strarray_wrapper array{patterns};
strarray_view_wrapper array{patterns};
throw_if_error(git_index_add_all(*this, array, 0, NULL, NULL));
}

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

void index_wrapper::remove_entries(std::vector<std::string> paths)
{
git_strarray_wrapper array{paths};
strarray_view_wrapper array{paths};
throw_if_error(git_index_remove_all(*this, array, NULL, NULL));
}

Expand Down
16 changes: 6 additions & 10 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#include "../utils/git_exception.hpp"
#include "../wrapper/commit_wrapper.hpp"
#include "../wrapper/config_wrapper.hpp"
#include "../wrapper/diff_wrapper.hpp"
#include "../wrapper/index_wrapper.hpp"
#include "../wrapper/object_wrapper.hpp"
#include "../wrapper/remote_wrapper.hpp"
#include "config_wrapper.hpp"
#include "diff_wrapper.hpp"

repository_wrapper::~repository_wrapper()
{
Expand Down Expand Up @@ -651,13 +651,9 @@ repository_wrapper::diff_index_to_workdir(std::optional<index_wrapper> index, gi

// Tags

std::vector<std::string> repository_wrapper::tag_list_match(std::string pattern)
strarray_owned_wrapper repository_wrapper::tag_list_match(std::string pattern)
{
git_strarray tag_names;
throw_if_error(git_tag_list_match(&tag_names, pattern.c_str(), *this));

std::vector<std::string> result(tag_names.strings, tag_names.strings + tag_names.count);

git_strarray_dispose(&tag_names);
return result;
strarray_owned_wrapper tag_names;
throw_if_error(git_tag_list_match(tag_names, pattern.c_str(), *this));
return tag_names;
}
4 changes: 2 additions & 2 deletions src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../wrapper/remote_wrapper.hpp"
#include "../wrapper/revwalk_wrapper.hpp"
#include "../wrapper/signature_wrapper.hpp"
#include "../wrapper/strarray_wrapper.hpp"
#include "../wrapper/tree_wrapper.hpp"
#include "../wrapper/wrapper_base.hpp"

Expand Down Expand Up @@ -134,8 +135,7 @@ class repository_wrapper : public wrapper_base<git_repository>
diff_wrapper diff_index_to_workdir(std::optional<index_wrapper> index, git_diff_options* diffopts);

// Tags
// git_strarray_wrapper tag_list_match(std::string pattern);
std::vector<std::string> tag_list_match(std::string pattern);
strarray_owned_wrapper tag_list_match(std::string pattern);

private:

Expand Down
97 changes: 97 additions & 0 deletions src/wrapper/strarray_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "strarray_wrapper.hpp"

strarray_owned_wrapper::strarray_owned_wrapper()
: m_array{nullptr, 0}
{
}

strarray_owned_wrapper::strarray_owned_wrapper(git_strarray&& arr)
: m_array(std::move(arr))
{
}

strarray_owned_wrapper::strarray_owned_wrapper(strarray_owned_wrapper&& rhs)
: m_array(std::move(rhs.m_array))
{
rhs.m_array = git_strarray{nullptr, 0};
}

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

strarray_owned_wrapper::~strarray_owned_wrapper()
{
git_strarray_dispose(&m_array);
}

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

size_t strarray_owned_wrapper::size() const
{
return m_array.count;
}

std::string strarray_owned_wrapper::operator[](size_t i) const
{
return {m_array.strings[i]};
}

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

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

strarray_view_wrapper& strarray_view_wrapper::operator=(strarray_view_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;
}

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

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

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

void strarray_view_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());
}
}

size_t strarray_view_wrapper::size() const
{
return m_patterns.size();
}
Loading
Loading