From 9e04c96990c1b4d3d0b46f3657141b7a914be2d7 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 16 Jul 2025 15:42:58 +0200 Subject: [PATCH 1/4] add git_strarray wrapper --- src/utils/common.cpp | 11 +++++++++++ src/utils/common.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/utils/common.cpp b/src/utils/common.cpp index 908d9b1..70114e7 100644 --- a/src/utils/common.cpp +++ b/src/utils/common.cpp @@ -23,3 +23,14 @@ 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 git_strarray_wrapper::init_str_array() +{ + git_strarray_wrapper aw; + git_strarray array{new char*[aw.m_patterns.size()], aw.m_patterns.size()}; + for (size_t i=0; i(aw.m_patterns[i].c_str()); + } + return array; +} diff --git a/src/utils/common.hpp b/src/utils/common.hpp index ade2158..701fae2 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -2,6 +2,9 @@ #include #include +#include + +#include class noncopyable_nonmovable { @@ -57,3 +60,42 @@ 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 m_patterns) + : m_patterns(std::move(m_patterns)) + { + init_str_array(); + } + + git_strarray_wrapper(const git_strarray_wrapper&) = delete; + git_strarray_wrapper& operator=(const git_strarray_wrapper&) = delete; + + git_strarray_wrapper(git_strarray_wrapper&& rhs) + : m_patterns(std::move(rhs.m_patterns)) + { + init_str_array(); + } + + ~git_strarray_wrapper() + { + delete[] m_array.strings; + } + + operator git_strarray*() + { + return &m_array; + } + + static git_strarray init_str_array(); + +protected: + std::vector m_patterns; + git_strarray m_array; +}; From c6924541fdb1653d4a9a8b8eed2c80d7b5fc2bc1 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 16 Jul 2025 16:10:02 +0200 Subject: [PATCH 2/4] move implementation to cpp file --- src/utils/common.cpp | 25 +++++++++++++++++++++++-- src/utils/common.hpp | 28 +++++++--------------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/utils/common.cpp b/src/utils/common.cpp index 70114e7..d8e2602 100644 --- a/src/utils/common.cpp +++ b/src/utils/common.cpp @@ -24,7 +24,29 @@ std::string get_current_git_path() // ->check(CLI::ExistingDirectory | CLI::NonexistentPath) // ->default_val(std::filesystem::current_path()); -git_strarray git_strarray_wrapper::init_str_array() +git_strarray_wrapper::git_strarray_wrapper(std::vector 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; +} + +git_strarray_wrapper::operator git_strarray*() +{ + return &m_array; +} + +void git_strarray_wrapper::init_str_array() { git_strarray_wrapper aw; git_strarray array{new char*[aw.m_patterns.size()], aw.m_patterns.size()}; @@ -32,5 +54,4 @@ git_strarray git_strarray_wrapper::init_str_array() { array.strings[i] = const_cast(aw.m_patterns[i].c_str()); } - return array; } diff --git a/src/utils/common.hpp b/src/utils/common.hpp index 701fae2..4ba481d 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -68,34 +68,20 @@ class git_strarray_wrapper : m_patterns{} , m_array{nullptr, 0} {} - git_strarray_wrapper(std::vector m_patterns) - : m_patterns(std::move(m_patterns)) - { - init_str_array(); - } + git_strarray_wrapper(std::vector 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) - : m_patterns(std::move(rhs.m_patterns)) - { - init_str_array(); - } + git_strarray_wrapper(git_strarray_wrapper&& rhs); - ~git_strarray_wrapper() - { - delete[] m_array.strings; - } + ~git_strarray_wrapper(); - operator git_strarray*() - { - return &m_array; - } - - static git_strarray init_str_array(); + operator git_strarray*(); -protected: +private: std::vector m_patterns; git_strarray m_array; + + void init_str_array(); }; From 84563e22ad2fdd171dee47ecb6bbb17b7403dd16 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 16 Jul 2025 16:12:59 +0200 Subject: [PATCH 3/4] small fix --- src/utils/common.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/utils/common.cpp b/src/utils/common.cpp index d8e2602..16fcf5b 100644 --- a/src/utils/common.cpp +++ b/src/utils/common.cpp @@ -48,10 +48,9 @@ git_strarray_wrapper::operator git_strarray*() void git_strarray_wrapper::init_str_array() { - git_strarray_wrapper aw; - git_strarray array{new char*[aw.m_patterns.size()], aw.m_patterns.size()}; - for (size_t i=0; i(aw.m_patterns[i].c_str()); + array.strings[i] = const_cast(m_patterns[i].c_str()); } } From 5e25e270d2292fa246e396b029b66dd5995081da Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 16 Jul 2025 17:44:23 +0200 Subject: [PATCH 4/4] address review comments --- src/utils/common.cpp | 27 ++++++++++++++++++++++----- src/utils/common.hpp | 4 +++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/utils/common.cpp b/src/utils/common.cpp index 16fcf5b..e5d97f4 100644 --- a/src/utils/common.cpp +++ b/src/utils/common.cpp @@ -24,8 +24,8 @@ std::string get_current_git_path() // ->check(CLI::ExistingDirectory | CLI::NonexistentPath) // ->default_val(std::filesystem::current_path()); -git_strarray_wrapper::git_strarray_wrapper(std::vector m_patterns) - : m_patterns(std::move(m_patterns)) +git_strarray_wrapper::git_strarray_wrapper(std::vector patterns) + : m_patterns(std::move(patterns)) { init_str_array(); } @@ -34,11 +34,21 @@ 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() { - delete[] m_array.strings; + reset_str_array(); } git_strarray_wrapper::operator git_strarray*() @@ -46,11 +56,18 @@ 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() { - git_strarray array{new char*[m_patterns.size()], m_patterns.size()}; + m_array.strings = new char*[m_patterns.size()]; + m_array.count = m_patterns.size(); for (size_t i=0; i(m_patterns[i].c_str()); + m_array.strings[i] = const_cast(m_patterns[i].c_str()); } } diff --git a/src/utils/common.hpp b/src/utils/common.hpp index 4ba481d..c19b1b3 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -68,12 +68,13 @@ class git_strarray_wrapper : m_patterns{} , m_array{nullptr, 0} {} - git_strarray_wrapper(std::vector m_patterns); + git_strarray_wrapper(std::vector 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(); @@ -83,5 +84,6 @@ class git_strarray_wrapper std::vector m_patterns; git_strarray m_array; + void reset_str_array(); void init_str_array(); };