Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/subcommand/push_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include <git2/remote.h>

#include "../utils/ansi_code.hpp"
#include "../utils/common.hpp"
// #include "../utils/common.hpp"
Comment thread
JohanMabille marked this conversation as resolved.
#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 +183,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
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
2 changes: 1 addition & 1 deletion src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,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);
// strarray_view_wrapper tag_list_match(std::string pattern);
std::vector<std::string> tag_list_match(std::string pattern);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wasn't this a placeholder to be replaced by the line above after this refactoring ?


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_view 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();
}
71 changes: 71 additions & 0 deletions src/wrapper/strarray_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <string>
#include <string_view>
#include <vector>

#include <git2.h>

// Wrapper of git_strarray that frees the contained
// strings (i.e. calls git_strarray_dispose) upon destruction.
class strarray_owned_wrapper
{
public:

strarray_owned_wrapper();
explicit strarray_owned_wrapper(git_strarray&& arr);

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

strarray_owned_wrapper(strarray_owned_wrapper&& rhs);
strarray_owned_wrapper& operator=(strarray_owned_wrapper&& rhs);

~strarray_owned_wrapper();

operator git_strarray*();

size_t size() const;

std::string_view operator[](size_t i) const;

private:

git_strarray m_array;
};

// Wrapper of git_strarray containing pointers to strings
// stored in a stnadard container. Does not free them upon
// destruction.
class strarray_view_wrapper
{
public:

strarray_view_wrapper()
: m_patterns{}
, m_array{nullptr, 0}
{
}

strarray_view_wrapper(std::vector<std::string> patterns);

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

strarray_view_wrapper(strarray_view_wrapper&& rhs);
strarray_view_wrapper& operator=(strarray_view_wrapper&& rhs);

~strarray_view_wrapper();

operator git_strarray*();

size_t size() const;

private:

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

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