From 26a846b02b18d5ab3a89ea01a981b386bbf61fcb Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 9 Jul 2025 10:08:55 +0200 Subject: [PATCH 01/10] add add subcommand --- src/main.cpp | 2 ++ src/subcommand/add_subcommand.cpp | 36 ++++++++++++++++++++++++++++ src/subcommand/add_subcommand.hpp | 16 +++++++++++++ src/subcommand/status_subcommand.cpp | 7 +----- src/version.hpp | 2 +- src/wrapper/index_wrapper.cpp | 29 ++++++++++++++++++++++ src/wrapper/index_wrapper.hpp | 27 +++++++++++++++++++++ src/wrapper/repository_wrapper.cpp | 9 +++++-- src/wrapper/repository_wrapper.hpp | 2 ++ 9 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 src/subcommand/add_subcommand.cpp create mode 100644 src/subcommand/add_subcommand.hpp create mode 100644 src/wrapper/index_wrapper.cpp create mode 100644 src/wrapper/index_wrapper.hpp diff --git a/src/main.cpp b/src/main.cpp index f5ce04a..64d2b72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "utils/git_exception.hpp" #include "version.hpp" +#include "subcommand/add_subcommand.hpp" #include "subcommand/init_subcommand.hpp" #include "subcommand/status_subcommand.hpp" @@ -21,6 +22,7 @@ int main(int argc, char** argv) // Sub commands init_subcommand init(lg2_obj, app); status_subcommand status(lg2_obj, app); + add_subcommand add(lg2_obj, app); app.parse(argc, argv); diff --git a/src/subcommand/add_subcommand.cpp b/src/subcommand/add_subcommand.cpp new file mode 100644 index 0000000..e48236b --- /dev/null +++ b/src/subcommand/add_subcommand.cpp @@ -0,0 +1,36 @@ +#include + +#include "add_subcommand.hpp" +#include "../wrapper/index_wrapper.hpp" +#include "../wrapper/repository_wrapper.hpp" + + +add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app) +{ + auto *sub = app.add_subcommand("add", "Add file contents to the index"); + + sub->add_flag("-A,--all,--no-ignore-removal", all_flag, ""); + // sub->add_flag("-n,--dryrun", dryrun_flag, ""); + // sub->add_flag("-u,--update", update_flag, ""); + // sub->add_flag("-v,--verbose", verbose_flag, ""); + + sub->callback([this]() { this->run(); }); +}; + +void add_subcommand::run() +{ + auto directory = get_current_git_path(); + auto bare = false; + auto repo = repository_wrapper::init(directory, bare); + + index_wrapper index = repo.get_index(); + + if (all_flag) + { + index.add_all(); + } + // else + // { + // index.add_entry(); + // } +} diff --git a/src/subcommand/add_subcommand.hpp b/src/subcommand/add_subcommand.hpp new file mode 100644 index 0000000..76ec774 --- /dev/null +++ b/src/subcommand/add_subcommand.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +#include "../utils/common.hpp" + +class add_subcommand +{ +public: + + explicit add_subcommand(const libgit2_object&, CLI::App& app); + void run(); + +private: + bool all_flag = false; +}; diff --git a/src/subcommand/status_subcommand.cpp b/src/subcommand/status_subcommand.cpp index 0133893..d1f4fa6 100644 --- a/src/subcommand/status_subcommand.cpp +++ b/src/subcommand/status_subcommand.cpp @@ -13,11 +13,6 @@ status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("status", "Show modified files in working directory, staged for your next commit"); - // Displays paths that have differences between the index file and the current HEAD commit, - // paths that have differences between the working tree and the index file, and paths in the - // working tree that are not tracked by Git (and are not ignored by gitignore[5]). - // The first are what you would commit by running git commit; - // the second and third are what you could commit by running git add before running git commit. sub->add_flag("-s,--short", short_flag, "Give the output in the short-format."); sub->add_flag("--long", long_flag, "Give the output in the long-format. This is the default."); @@ -152,7 +147,7 @@ void print_entries(std::vector entries_to_print) } } -void print_not_tracked(std::vector entries_to_print, const std::set& tracked_dir_set, +void print_not_tracked(const std::vector& entries_to_print, const std::set& tracked_dir_set, std::set& untracked_dir_set) { std::vector not_tracked_entries_to_print{}; diff --git a/src/version.hpp b/src/version.hpp index 5a0c737..30c9998 100644 --- a/src/version.hpp +++ b/src/version.hpp @@ -2,7 +2,7 @@ #define GIT2CPP_VERSION_MAJOR 0 #define GIT2CPP_VERSION_MINOR 0 -#define GIT2CPP_VERSION_PATCH 1 +#define GIT2CPP_VERSION_PATCH 2 // e.g. ".rc0" #define GIT2CPP_VERSION_SUFFIX diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp new file mode 100644 index 0000000..bb88278 --- /dev/null +++ b/src/wrapper/index_wrapper.cpp @@ -0,0 +1,29 @@ +#include "index_wrapper.hpp" +#include "../utils/git_exception.hpp" +#include "../wrapper/repository_wrapper.hpp" + +index_wrapper::~index_wrapper() +{ + git_index_free(p_resource); + p_resource=nullptr; +} + +index_wrapper index_wrapper::init(repository_wrapper& rw) +{ + index_wrapper index; + throwIfError(git_repository_index(&(index.p_resource), rw)); + return index; +} + +void index_wrapper::add_entry(const git_index_entry* entry) +{ + index_wrapper index; + throwIfError(git_index_add(index, entry)); +} + +void index_wrapper::add_all() +{ + index_wrapper index; + git_strarray array = {0}; // array of strings, array of path patterns + throwIfError(git_index_add_all(index, &array, 0, NULL, NULL)); +} diff --git a/src/wrapper/index_wrapper.hpp b/src/wrapper/index_wrapper.hpp new file mode 100644 index 0000000..8e5f2ef --- /dev/null +++ b/src/wrapper/index_wrapper.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "../utils/common.hpp" + +class repository_wrapper; + +class index_wrapper : public wrapper_base +{ +public: + + ~index_wrapper(); + + index_wrapper(index_wrapper&&) = default; + index_wrapper& operator=(index_wrapper&&) = default; + + static index_wrapper init(repository_wrapper& rw); + + void add_entry(const git_index_entry* entry); + void add_all(); + + +private: + + index_wrapper() = default; +}; diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index b27adc4..e43da5d 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -1,6 +1,5 @@ -#include "../utils/git_exception.hpp" #include "repository_wrapper.hpp" - +#include "../utils/git_exception.hpp" repository_wrapper::~repository_wrapper() { @@ -21,3 +20,9 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b throwIfError(git_repository_init(&(rw.p_resource), directory.c_str(), bare)); return rw; } + +index_wrapper repository_wrapper::get_index() const +{ + repository_wrapper rw; + index_wrapper::init(rw); +} diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 2836636..2cbe471 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -5,6 +5,7 @@ #include #include "../utils/common.hpp" +#include "../wrapper/index_wrapper.hpp" class repository_wrapper : public wrapper_base { @@ -17,6 +18,7 @@ class repository_wrapper : public wrapper_base static repository_wrapper init(const std::string& directory, bool bare); static repository_wrapper open(const std::string& directory); + index_wrapper get_index() const; private: From 39dc8476fd9921138d4fab3b99191106d54d57a9 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 9 Jul 2025 15:10:40 +0200 Subject: [PATCH 02/10] fix cmake --- .gitignore | 2 ++ CMakeLists.txt | 5 ++++- README.md | 2 +- dev-environment.yml | 2 +- src/utils/meson.build | 4 ---- src/wrapper/repository_wrapper.cpp | 3 ++- 6 files changed, 10 insertions(+), 8 deletions(-) delete mode 100644 src/utils/meson.build diff --git a/.gitignore b/.gitignore index 3d43339..e4f7ae8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build/ __pycache__/ +.cache/ +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index b97b9c2..824aad9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,8 @@ find_package(libgit2) # ===== set(GIT2CPP_SRC + ${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.cpp + ${GIT2CPP_SOURCE_DIR}/subcommand/add_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp @@ -47,6 +49,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/utils/common.hpp ${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp ${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp + ${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.cpp + ${GIT2CPP_SOURCE_DIR}/wrapper/index_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.cpp ${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.cpp @@ -59,4 +63,3 @@ set(GIT2CPP_SRC add_executable(git2cpp ${GIT2CPP_SRC}) target_link_libraries(git2cpp PRIVATE libgit2::libgit2package) - diff --git a/README.md b/README.md index a03afe0..64c6743 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Developer's workflow using `micromamba` to manage the dependencies: ```bash micromamba create -f dev-environment.yml micromamba activate git2cpp-dev -cmake -Bbuild $CMAKE_INSALL_PREFIX=$CONDA_PREFIX +cmake -Bbuild -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX cd build make -j8 ``` diff --git a/dev-environment.yml b/dev-environment.yml index 80a86dd..04f465e 100644 --- a/dev-environment.yml +++ b/dev-environment.yml @@ -4,7 +4,7 @@ channels: dependencies: - cli11 - libgit2 - - meson + - cmake - pkg-config - python - pytest diff --git a/src/utils/meson.build b/src/utils/meson.build deleted file mode 100644 index 5d728df..0000000 --- a/src/utils/meson.build +++ /dev/null @@ -1,4 +0,0 @@ -utils_files = files([ - 'common.cpp', - 'git_exception.cpp', -]) diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index e43da5d..421e4b4 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -24,5 +24,6 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b index_wrapper repository_wrapper::get_index() const { repository_wrapper rw; - index_wrapper::init(rw); + index_wrapper index = index_wrapper::init(rw); + return index; } From 65485bebbd9f0843d9ff4d5ec2af5c75d78e2f61 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Fri, 11 Jul 2025 15:55:05 +0200 Subject: [PATCH 03/10] edit add subcommand --- src/subcommand/add_subcommand.cpp | 18 +++++++++++++----- src/subcommand/add_subcommand.hpp | 1 + src/wrapper/index_wrapper.cpp | 6 ++---- src/wrapper/repository_wrapper.cpp | 5 ++--- src/wrapper/repository_wrapper.hpp | 2 +- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/subcommand/add_subcommand.cpp b/src/subcommand/add_subcommand.cpp index e48236b..1d349ca 100644 --- a/src/subcommand/add_subcommand.cpp +++ b/src/subcommand/add_subcommand.cpp @@ -9,6 +9,8 @@ add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("add", "Add file contents to the index"); + sub->add_option("files", add_files, "Files to add"); + sub->add_flag("-A,--all,--no-ignore-removal", all_flag, ""); // sub->add_flag("-n,--dryrun", dryrun_flag, ""); // sub->add_flag("-u,--update", update_flag, ""); @@ -17,20 +19,26 @@ add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app) sub->callback([this]() { this->run(); }); }; + void add_subcommand::run() { auto directory = get_current_git_path(); auto bare = false; auto repo = repository_wrapper::init(directory, bare); - index_wrapper index = repo.get_index(); + index_wrapper index = repo.make_index(); if (all_flag) { index.add_all(); } - // else - // { - // index.add_entry(); - // } + else + { + for (const auto& path : add_files) + { + git_index_entry* entry; + entry->path = path.c_str(); + index.add_entry(entry); + } + } } diff --git a/src/subcommand/add_subcommand.hpp b/src/subcommand/add_subcommand.hpp index 76ec774..6d79a48 100644 --- a/src/subcommand/add_subcommand.hpp +++ b/src/subcommand/add_subcommand.hpp @@ -13,4 +13,5 @@ class add_subcommand private: bool all_flag = false; + std::vector add_files; }; diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index bb88278..8a763c7 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -17,13 +17,11 @@ index_wrapper index_wrapper::init(repository_wrapper& rw) void index_wrapper::add_entry(const git_index_entry* entry) { - index_wrapper index; - throwIfError(git_index_add(index, entry)); + throwIfError(git_index_add(*this, entry)); } void index_wrapper::add_all() { - index_wrapper index; git_strarray array = {0}; // array of strings, array of path patterns - throwIfError(git_index_add_all(index, &array, 0, NULL, NULL)); + throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL)); } diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 421e4b4..10dc0be 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -21,9 +21,8 @@ repository_wrapper repository_wrapper::init(const std::string& directory, bool b return rw; } -index_wrapper repository_wrapper::get_index() const +index_wrapper repository_wrapper::make_index() { - repository_wrapper rw; - index_wrapper index = index_wrapper::init(rw); + index_wrapper index = index_wrapper::init(*this); return index; } diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 2cbe471..dd0b8f7 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -18,7 +18,7 @@ class repository_wrapper : public wrapper_base static repository_wrapper init(const std::string& directory, bool bare); static repository_wrapper open(const std::string& directory); - index_wrapper get_index() const; + index_wrapper make_index(); private: From 7e5ea3a3ba70e02a05bbf00f87c60e631eb9de12 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Tue, 15 Jul 2025 16:47:30 +0200 Subject: [PATCH 04/10] fix 'add --all' --- src/utils/common.hpp | 2 +- src/wrapper/index_wrapper.cpp | 6 ++++- test/data/status_data/embeded_git/config | 0 test/test_add.py | 29 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) mode change 100644 => 100755 test/data/status_data/embeded_git/config create mode 100644 test/test_add.py diff --git a/src/utils/common.hpp b/src/utils/common.hpp index c19b1b3..afc388b 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -36,7 +36,7 @@ class wrapper_base wrapper_base& operator=(wrapper_base&& rhs) { std::swap(p_resource, rhs.p_resource); - return this; + return *this; } operator resource_type*() const noexcept diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index 8a763c7..9c84798 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -2,6 +2,8 @@ #include "../utils/git_exception.hpp" #include "../wrapper/repository_wrapper.hpp" +#include + index_wrapper::~index_wrapper() { git_index_free(p_resource); @@ -22,6 +24,8 @@ void index_wrapper::add_entry(const git_index_entry* entry) void index_wrapper::add_all() { - git_strarray array = {0}; // array of strings, array of path patterns + const char* patterns[] = {"."}; + git_strarray array{(char**)patterns, 1}; throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL)); + throwIfError(git_index_write(*this)); } diff --git a/test/data/status_data/embeded_git/config b/test/data/status_data/embeded_git/config old mode 100644 new mode 100755 diff --git a/test/test_add.py b/test/test_add.py new file mode 100644 index 0000000..e3d67d7 --- /dev/null +++ b/test/test_add.py @@ -0,0 +1,29 @@ +import os +import subprocess + +import pytest + + +@pytest.mark.parametrize("all_flag", ["-A", "--all", "--no-ignore-removal"]) +def test_add(git2cpp_path, all_flag): + with open("./test/mook_file.txt", "x") as f: + pass + f.close() + + cmd_add = [git2cpp_path, 'add'] + if all_flag != "": + cmd_add.append(all_flag) + subprocess.run(cmd_add, capture_output=True, text=True) + + cmd_status = [git2cpp_path, 'status', "--long"] + p_status = subprocess.run(cmd_status, capture_output=True, text=True) + + print(p_status.stdout) + # assert "Changes to be committed" in p.stdout + # assert "Changes not staged for commit" in p.stdout + # assert "Untracked files" in p.stdout + # assert "new file" in p.stdout + # assert "deleted" in p.stdout + # assert "modified" in p.stdout + # + os.remove("./test/mook_file.txt") From ed4fe0eb5a4fee17e191d3ad6dd085472864e221 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Tue, 15 Jul 2025 17:47:08 +0200 Subject: [PATCH 05/10] fix add_entries --- src/subcommand/add_subcommand.cpp | 7 +------ src/wrapper/index_wrapper.cpp | 17 +++++++++++++---- src/wrapper/index_wrapper.hpp | 6 +++++- test/test_add.py | 10 +++++++++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/subcommand/add_subcommand.cpp b/src/subcommand/add_subcommand.cpp index 1d349ca..03cfe56 100644 --- a/src/subcommand/add_subcommand.cpp +++ b/src/subcommand/add_subcommand.cpp @@ -34,11 +34,6 @@ void add_subcommand::run() } else { - for (const auto& path : add_files) - { - git_index_entry* entry; - entry->path = path.c_str(); - index.add_entry(entry); - } + index.add_entry(add_files); } } diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index 9c84798..9268681 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -3,6 +3,7 @@ #include "../wrapper/repository_wrapper.hpp" #include +#include index_wrapper::~index_wrapper() { @@ -17,15 +18,23 @@ index_wrapper index_wrapper::init(repository_wrapper& rw) return index; } -void index_wrapper::add_entry(const git_index_entry* entry) +void index_wrapper::add_entries(std::vector patterns) { - throwIfError(git_index_add(*this, entry)); + add_impl(std::move(patterns)); } void index_wrapper::add_all() { - const char* patterns[] = {"."}; - git_strarray array{(char**)patterns, 1}; + add_impl({{"."}}); +} + +void index_wrapper::add_impl(std::vector patterns) +{ + git_strarray array{new char*[patterns.size()], patterns.size()}; + for (size_t i=0; i(patterns[i].c_str()); + } throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL)); throwIfError(git_index_write(*this)); } diff --git a/src/wrapper/index_wrapper.hpp b/src/wrapper/index_wrapper.hpp index 8e5f2ef..f478e48 100644 --- a/src/wrapper/index_wrapper.hpp +++ b/src/wrapper/index_wrapper.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include + #include #include "../utils/common.hpp" @@ -17,11 +20,12 @@ class index_wrapper : public wrapper_base static index_wrapper init(repository_wrapper& rw); - void add_entry(const git_index_entry* entry); + void add_entries(std::vector patterns); void add_all(); private: index_wrapper() = default; + void add_impl(std::vector patterns); }; diff --git a/test/test_add.py b/test/test_add.py index e3d67d7..cbf07c8 100644 --- a/test/test_add.py +++ b/test/test_add.py @@ -4,15 +4,21 @@ import pytest -@pytest.mark.parametrize("all_flag", ["-A", "--all", "--no-ignore-removal"]) +@pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"]) def test_add(git2cpp_path, all_flag): with open("./test/mook_file.txt", "x") as f: pass f.close() + with open("./test/mook_file_2.txt", "x") as f: + pass + f.close() + cmd_add = [git2cpp_path, 'add'] if all_flag != "": cmd_add.append(all_flag) + else: + cmd_add.append("test/mook_file.txt") subprocess.run(cmd_add, capture_output=True, text=True) cmd_status = [git2cpp_path, 'status', "--long"] @@ -27,3 +33,5 @@ def test_add(git2cpp_path, all_flag): # assert "modified" in p.stdout # os.remove("./test/mook_file.txt") + os.remove("./test/mook_file_2.txt") + subprocess.run(cmd_add, capture_output=True, text=True) From ccb3077b497a19de52c8ffbf2e792529d0c2234c Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Tue, 15 Jul 2025 17:51:20 +0200 Subject: [PATCH 06/10] update test --- test/test_add.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/test_add.py b/test/test_add.py index cbf07c8..56298f2 100644 --- a/test/test_add.py +++ b/test/test_add.py @@ -24,14 +24,13 @@ def test_add(git2cpp_path, all_flag): cmd_status = [git2cpp_path, 'status', "--long"] p_status = subprocess.run(cmd_status, capture_output=True, text=True) - print(p_status.stdout) - # assert "Changes to be committed" in p.stdout - # assert "Changes not staged for commit" in p.stdout - # assert "Untracked files" in p.stdout - # assert "new file" in p.stdout - # assert "deleted" in p.stdout - # assert "modified" in p.stdout - # + assert "Changes to be committed" in p_status.stdout + assert "new file" in p_status.stdout + if all_flag != "": + assert "Untracked files" not in p_status.stdout + else: + assert "Untracked files" in p_status.stdout + os.remove("./test/mook_file.txt") os.remove("./test/mook_file_2.txt") - subprocess.run(cmd_add, capture_output=True, text=True) + subprocess.run(cmd_add, capture_output=True, text=True) # TODO: replace with a reset when implemented ? From 75f8c47c929e1f467806f8d0eb1654db5266eba3 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 16 Jul 2025 15:27:47 +0200 Subject: [PATCH 07/10] fix typo --- src/subcommand/add_subcommand.cpp | 2 +- src/wrapper/index_wrapper.cpp | 1 - test/test_add.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/subcommand/add_subcommand.cpp b/src/subcommand/add_subcommand.cpp index 03cfe56..4c8e22a 100644 --- a/src/subcommand/add_subcommand.cpp +++ b/src/subcommand/add_subcommand.cpp @@ -34,6 +34,6 @@ void add_subcommand::run() } else { - index.add_entry(add_files); + index.add_entries(add_files); } } diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index 9268681..d551c0c 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -2,7 +2,6 @@ #include "../utils/git_exception.hpp" #include "../wrapper/repository_wrapper.hpp" -#include #include index_wrapper::~index_wrapper() diff --git a/test/test_add.py b/test/test_add.py index 56298f2..0255226 100644 --- a/test/test_add.py +++ b/test/test_add.py @@ -33,4 +33,4 @@ def test_add(git2cpp_path, all_flag): os.remove("./test/mook_file.txt") os.remove("./test/mook_file_2.txt") - subprocess.run(cmd_add, capture_output=True, text=True) # TODO: replace with a reset when implemented ? + subprocess.run(cmd_add, capture_output=True, text=True) From d232da9ff04bd28d33da62fa4f88c0978f5d284f Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Thu, 17 Jul 2025 10:06:57 +0200 Subject: [PATCH 08/10] integrate git_strarray wrapper --- src/wrapper/index_wrapper.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index d551c0c..8a77196 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -29,11 +29,7 @@ void index_wrapper::add_all() void index_wrapper::add_impl(std::vector patterns) { - git_strarray array{new char*[patterns.size()], patterns.size()}; - for (size_t i=0; i(patterns[i].c_str()); - } - throwIfError(git_index_add_all(*this, &array, 0, NULL, NULL)); + git_strarray_wrapper array=git_strarray_wrapper(patterns); + throwIfError(git_index_add_all(*this, array, 0, NULL, NULL)); throwIfError(git_index_write(*this)); } From 893eec9ec6bdb689c13f7f0343cb2bbde69b7cee Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Thu, 17 Jul 2025 10:53:35 +0200 Subject: [PATCH 09/10] address review comments --- src/subcommand/add_subcommand.cpp | 8 ++++---- src/subcommand/add_subcommand.hpp | 4 ++-- src/subcommand/init_subcommand.cpp | 6 +++--- src/subcommand/init_subcommand.hpp | 4 ++-- src/subcommand/status_subcommand.cpp | 12 ++++++------ src/subcommand/status_subcommand.hpp | 6 +++--- src/wrapper/index_wrapper.cpp | 2 +- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/subcommand/add_subcommand.cpp b/src/subcommand/add_subcommand.cpp index 4c8e22a..cb2c609 100644 --- a/src/subcommand/add_subcommand.cpp +++ b/src/subcommand/add_subcommand.cpp @@ -9,9 +9,9 @@ add_subcommand::add_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("add", "Add file contents to the index"); - sub->add_option("files", add_files, "Files to add"); + sub->add_option("files", m_add_files, "Files to add"); - sub->add_flag("-A,--all,--no-ignore-removal", all_flag, ""); + sub->add_flag("-A,--all,--no-ignore-removal", m_all_flag, ""); // sub->add_flag("-n,--dryrun", dryrun_flag, ""); // sub->add_flag("-u,--update", update_flag, ""); // sub->add_flag("-v,--verbose", verbose_flag, ""); @@ -28,12 +28,12 @@ void add_subcommand::run() index_wrapper index = repo.make_index(); - if (all_flag) + if (m_all_flag) { index.add_all(); } else { - index.add_entries(add_files); + index.add_entries(m_add_files); } } diff --git a/src/subcommand/add_subcommand.hpp b/src/subcommand/add_subcommand.hpp index 6d79a48..6f31dff 100644 --- a/src/subcommand/add_subcommand.hpp +++ b/src/subcommand/add_subcommand.hpp @@ -12,6 +12,6 @@ class add_subcommand void run(); private: - bool all_flag = false; - std::vector add_files; + bool m_all_flag = false; + std::vector m_add_files; }; diff --git a/src/subcommand/init_subcommand.cpp b/src/subcommand/init_subcommand.cpp index 79d6807..2a3a703 100644 --- a/src/subcommand/init_subcommand.cpp +++ b/src/subcommand/init_subcommand.cpp @@ -6,10 +6,10 @@ init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("init", "Explanation of init here"); - sub->add_flag("--bare", bare, "info about bare arg"); + sub->add_flag("--bare", m_bare, "info about bare arg"); // If directory not specified, uses cwd. - sub->add_option("directory", directory, "info about directory arg") + sub->add_option("directory", m_directory, "info about directory arg") ->check(CLI::ExistingDirectory | CLI::NonexistentPath) ->default_val(get_current_git_path()); @@ -18,5 +18,5 @@ init_subcommand::init_subcommand(const libgit2_object&, CLI::App& app) void init_subcommand::run() { - repository_wrapper::init(directory, bare); + repository_wrapper::init(m_directory, m_bare); } diff --git a/src/subcommand/init_subcommand.hpp b/src/subcommand/init_subcommand.hpp index ef39e5a..6086492 100644 --- a/src/subcommand/init_subcommand.hpp +++ b/src/subcommand/init_subcommand.hpp @@ -14,6 +14,6 @@ class init_subcommand void run(); private: - bool bare; - std::string directory; + bool m_bare; + std::string m_directory; }; diff --git a/src/subcommand/status_subcommand.cpp b/src/subcommand/status_subcommand.cpp index d1f4fa6..2da0780 100644 --- a/src/subcommand/status_subcommand.cpp +++ b/src/subcommand/status_subcommand.cpp @@ -14,13 +14,13 @@ status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("status", "Show modified files in working directory, staged for your next commit"); - sub->add_flag("-s,--short", short_flag, "Give the output in the short-format."); - sub->add_flag("--long", long_flag, "Give the output in the long-format. This is the default."); + sub->add_flag("-s,--short", m_short_flag, "Give the output in the short-format."); + sub->add_flag("--long", m_long_flag, "Give the output in the long-format. This is the default."); // sub->add_flag("--porcelain[=]", porcelain, "Give the output in an easy-to-parse format for scripts. // This is similar to the short output, but will remain stable across Git versions and regardless of user configuration. // See below for details. The version parameter is used to specify the format version. This is optional and defaults // to the original version v1 format."); - sub->add_flag("-b,--branch", branch_flag, "Show the branch and tracking info even in short-format."); + sub->add_flag("-b,--branch", m_branch_flag, "Show the branch and tracking info even in short-format."); sub->callback([this]() { this->run(); }); }; @@ -194,11 +194,11 @@ void status_subcommand::run() std::vector ignored_to_print{}; output_format of = output_format::DEFAULT; - if (short_flag) + if (m_short_flag) { of = output_format::SHORT; } - if (long_flag) + if (m_long_flag) { of = output_format::LONG; } @@ -215,7 +215,7 @@ void status_subcommand::run() } else { - if (branch_flag) + if (m_branch_flag) { std::cout << "## " << branch_name << std::endl; } diff --git a/src/subcommand/status_subcommand.hpp b/src/subcommand/status_subcommand.hpp index 08b2893..ae259a9 100644 --- a/src/subcommand/status_subcommand.hpp +++ b/src/subcommand/status_subcommand.hpp @@ -12,7 +12,7 @@ class status_subcommand void run(); private: - bool branch_flag = false; - bool long_flag = false; - bool short_flag = false; + bool m_branch_flag = false; + bool m_long_flag = false; + bool m_short_flag = false; }; diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index 8a77196..c7c02ec 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -29,7 +29,7 @@ void index_wrapper::add_all() void index_wrapper::add_impl(std::vector patterns) { - git_strarray_wrapper array=git_strarray_wrapper(patterns); + git_strarray_wrapper array{patterns}; throwIfError(git_index_add_all(*this, array, 0, NULL, NULL)); throwIfError(git_index_write(*this)); } From 3e8c3d086008a44e13fe3d6091f6f9726055a107 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Thu, 17 Jul 2025 15:17:19 +0200 Subject: [PATCH 10/10] address review comments --- test/test_add.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_add.py b/test/test_add.py index 0255226..b762121 100644 --- a/test/test_add.py +++ b/test/test_add.py @@ -8,11 +8,9 @@ def test_add(git2cpp_path, all_flag): with open("./test/mook_file.txt", "x") as f: pass - f.close() with open("./test/mook_file_2.txt", "x") as f: pass - f.close() cmd_add = [git2cpp_path, 'add'] if all_flag != "": @@ -33,4 +31,6 @@ def test_add(git2cpp_path, all_flag): os.remove("./test/mook_file.txt") os.remove("./test/mook_file_2.txt") + + # undo the add, to leave the test directory at the end the same as it was at the start subprocess.run(cmd_add, capture_output=True, text=True)