From b977235b32493dfdbfe83a6f2752a73c2af49c1f Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Tue, 29 Jul 2025 09:36:26 +0200 Subject: [PATCH 01/17] add commit subcommand --- CMakeLists.txt | 4 ++++ src/subcommand/commit_subcommand.cpp | 36 ++++++++++++++++++++++++++++ src/subcommand/commit_subcommand.hpp | 17 +++++++++++++ src/wrapper/commit_wrapper.cpp | 1 - src/wrapper/commit_wrapper.hpp | 1 + src/wrapper/object_wrapper.hpp | 2 ++ src/wrapper/repository_wrapper.cpp | 19 +++++++++++++++ src/wrapper/repository_wrapper.hpp | 2 ++ src/wrapper/signature_wrapper.cpp | 17 +++++++++++++ src/wrapper/signature_wrapper.hpp | 26 ++++++++++++++++++++ test/test_commit.py | 36 ++++++++++++++++++++++++++++ 11 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/subcommand/commit_subcommand.cpp create mode 100644 src/subcommand/commit_subcommand.hpp create mode 100644 src/wrapper/signature_wrapper.cpp create mode 100644 src/wrapper/signature_wrapper.hpp create mode 100644 test/test_commit.py diff --git a/CMakeLists.txt b/CMakeLists.txt index d84f74b..8ebdebe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/subcommand/checkout_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/clone_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/clone_subcommand.hpp + ${GIT2CPP_SOURCE_DIR}/subcommand/commit_subcommand.cpp + ${GIT2CPP_SOURCE_DIR}/subcommand/commit_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp @@ -69,6 +71,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/wrapper/refs_wrapper.hpp ${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.cpp ${GIT2CPP_SOURCE_DIR}/wrapper/repository_wrapper.hpp + ${GIT2CPP_SOURCE_DIR}/wrapper/signature_wrapper.cpp + ${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/wrapper_base.hpp diff --git a/src/subcommand/commit_subcommand.cpp b/src/subcommand/commit_subcommand.cpp new file mode 100644 index 0000000..6431264 --- /dev/null +++ b/src/subcommand/commit_subcommand.cpp @@ -0,0 +1,36 @@ +#include +#include + +#include "commit_subcommand.hpp" +#include "../wrapper/index_wrapper.hpp" +#include "../wrapper/repository_wrapper.hpp" + + +commit_subcommand::commit_subcommand(const libgit2_object&, CLI::App& app) +{ + auto *sub = app.add_subcommand("commit", "Record changes to the repository"); + + sub->add_option("message", m_message, "Commit message"); + + sub->add_flag("-m,--message", m_message_flag, ""); + + sub->callback([this]() { this->run(); }); +}; + + +void commit_subcommand::run() +{ + auto directory = get_current_git_path(); + auto bare = false; + auto repo = repository_wrapper::init(directory, bare); + auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); + + if (!m_message_flag) + { + std::cout << "Please provide a message using the -m flag." << std::endl; + } + else + { + repo.create_commit(author_committer_signatures, m_message); + } +} diff --git a/src/subcommand/commit_subcommand.hpp b/src/subcommand/commit_subcommand.hpp new file mode 100644 index 0000000..f78680b --- /dev/null +++ b/src/subcommand/commit_subcommand.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "../utils/common.hpp" + +class commit_subcommand +{ +public: + + explicit commit_subcommand(const libgit2_object&, CLI::App& app); + void run(); + +private: + bool m_message_flag = true; // TODO: change to false when a message can be provided if the "-m" flag is not provided + std::string m_message; +}; diff --git a/src/wrapper/commit_wrapper.cpp b/src/wrapper/commit_wrapper.cpp index 66fcd4f..bbd48b1 100644 --- a/src/wrapper/commit_wrapper.cpp +++ b/src/wrapper/commit_wrapper.cpp @@ -20,4 +20,3 @@ const git_oid& commit_wrapper::oid() const { return *git_commit_id(p_resource); } - diff --git a/src/wrapper/commit_wrapper.hpp b/src/wrapper/commit_wrapper.hpp index 201cbce..2cf2378 100644 --- a/src/wrapper/commit_wrapper.hpp +++ b/src/wrapper/commit_wrapper.hpp @@ -2,6 +2,7 @@ #include +#include "../wrapper/repository_wrapper.hpp" #include "../wrapper/wrapper_base.hpp" class commit_wrapper : public wrapper_base diff --git a/src/wrapper/object_wrapper.hpp b/src/wrapper/object_wrapper.hpp index b0b86bf..bceab14 100644 --- a/src/wrapper/object_wrapper.hpp +++ b/src/wrapper/object_wrapper.hpp @@ -17,6 +17,8 @@ class object_wrapper : public wrapper_base const git_oid& oid() const; + operator git_commit*() const noexcept; + private: object_wrapper(git_object* obj); diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 6be951d..bbb9fa8 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -107,6 +107,25 @@ commit_wrapper repository_wrapper::find_commit(const git_oid& id) const return commit_wrapper(commit); } +void repository_wrapper::create_commit(const signature_wrapper::author_committer_signatures& author_committer_signatures, + const std::string& message) +{ + git_oid* commit_id; + const char* update_ref = "ḦEAD"; + auto parent = revparse_single(update_ref); + std::size_t parent_count = 0; + const git_commit* parents[1] = {nullptr}; + if (parent) + { + parent_count = 1; + parents[0] = *parent; + } + const char* message_encoding = "UTF-8"; + const git_tree* tree; + throw_if_error(git_commit_create(commit_id, *this, update_ref, author_committer_signatures.first, author_committer_signatures.second, + message_encoding, message.c_str(), tree, parent_count, parents)); +} + annotated_commit_wrapper repository_wrapper::find_annotated_commit(const git_oid& id) const { git_annotated_commit* commit; diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 8d44aed..6c745c4 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -13,6 +13,7 @@ #include "../wrapper/index_wrapper.hpp" #include "../wrapper/object_wrapper.hpp" #include "../wrapper/refs_wrapper.hpp" +#include "../wrapper/signature_wrapper.hpp" #include "../wrapper/wrapper_base.hpp" class repository_wrapper : public wrapper_base @@ -50,6 +51,7 @@ class repository_wrapper : public wrapper_base // Commits commit_wrapper find_commit(std::string_view ref_name = "HEAD") const; commit_wrapper find_commit(const git_oid& id) const; + void create_commit(const signature_wrapper::author_committer_signatures&, const std::string&); // Annotated commits annotated_commit_wrapper find_annotated_commit(const git_oid& id) const; diff --git a/src/wrapper/signature_wrapper.cpp b/src/wrapper/signature_wrapper.cpp new file mode 100644 index 0000000..4bbb7a6 --- /dev/null +++ b/src/wrapper/signature_wrapper.cpp @@ -0,0 +1,17 @@ +#include "../wrapper/repository_wrapper.hpp" +#include "../wrapper/signature_wrapper.hpp" +#include "../utils/git_exception.hpp" + +signature_wrapper::~signature_wrapper() +{ + git_signature_free(p_resource); + p_resource=nullptr; +} + +signature_wrapper::author_committer_signatures signature_wrapper::get_default_signature_from_env(repository_wrapper& rw) +{ + signature_wrapper author; + signature_wrapper committer; + throw_if_error(git_signature_default_from_env(&(author.p_resource), &(committer.p_resource), rw)); + return {std::move(author), std::move(committer)}; +} diff --git a/src/wrapper/signature_wrapper.hpp b/src/wrapper/signature_wrapper.hpp new file mode 100644 index 0000000..150bf62 --- /dev/null +++ b/src/wrapper/signature_wrapper.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include + +#include "../wrapper/wrapper_base.hpp" + +class repository_wrapper; + +class signature_wrapper : public wrapper_base +{ +public: + using author_committer_signatures = std::pair; + + ~signature_wrapper(); + + signature_wrapper(signature_wrapper&&) = default; + signature_wrapper& operator=(signature_wrapper&&) = default; + + static author_committer_signatures get_default_signature_from_env(repository_wrapper&); + +private: + + signature_wrapper() = default; +}; diff --git a/test/test_commit.py b/test/test_commit.py new file mode 100644 index 0000000..aa4cdb4 --- /dev/null +++ b/test/test_commit.py @@ -0,0 +1,36 @@ +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"): + pass + + cmd_add = [git2cpp_path, 'add', "test/mook_file.txt"] + 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) + + assert "Changes to be committed" in p_status.stdout + assert "new file" in p_status.stdout + + cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"] + subprocess.run(cmd_commit, capture_output=True, text=True) + + cmd_status_2 = [git2cpp_path, 'status', "--long"] + p_status_2 = subprocess.run(cmd_status_2, capture_output=True, text=True) + + print(p_status_2.stdout) + + # assert "mook_file" not in p_status.stdout + + os.remove("./test/mook_file.txt") + + # TODO: git reset + + # 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) From 4dbb90d56a89f02c560a9cc09f40fd04eb39cde0 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Tue, 29 Jul 2025 17:37:29 +0200 Subject: [PATCH 02/17] add reset subcommand --- CMakeLists.txt | 2 + src/main.cpp | 4 ++ src/subcommand/commit_subcommand.cpp | 8 ++-- src/subcommand/reset_subcommand.cpp | 61 ++++++++++++++++++++++++++++ src/subcommand/reset_subcommand.hpp | 19 +++++++++ src/wrapper/repository_wrapper.cpp | 6 +++ src/wrapper/repository_wrapper.hpp | 3 +- test/test_commit.py | 2 +- 8 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 src/subcommand/reset_subcommand.cpp create mode 100644 src/subcommand/reset_subcommand.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ebdebe..d9c4aa2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,8 @@ set(GIT2CPP_SRC ${GIT2CPP_SOURCE_DIR}/subcommand/commit_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp + ${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp + ${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp ${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.hpp ${GIT2CPP_SOURCE_DIR}/utils/common.cpp diff --git a/src/main.cpp b/src/main.cpp index ca92d6d..767e61a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,9 @@ #include "subcommand/branch_subcommand.hpp" #include "subcommand/checkout_subcommand.hpp" #include "subcommand/clone_subcommand.hpp" +#include "subcommand/commit_subcommand.hpp" #include "subcommand/init_subcommand.hpp" +#include "subcommand/reset_subcommand.hpp" #include "subcommand/status_subcommand.hpp" int main(int argc, char** argv) @@ -29,6 +31,8 @@ int main(int argc, char** argv) branch_subcommand branch(lg2_obj, app); checkout_subcommand checkout(lg2_obj, app); clone_subcommand clone(lg2_obj, app); + commit_subcommand commit(lg2_obj, app); + reset_subcommand reset(lg2_obj, app); app.require_subcommand(/* min */ 0, /* max */ 1); diff --git a/src/subcommand/commit_subcommand.cpp b/src/subcommand/commit_subcommand.cpp index 6431264..9fcc2a4 100644 --- a/src/subcommand/commit_subcommand.cpp +++ b/src/subcommand/commit_subcommand.cpp @@ -27,10 +27,8 @@ void commit_subcommand::run() if (!m_message_flag) { - std::cout << "Please provide a message using the -m flag." << std::endl; - } - else - { - repo.create_commit(author_committer_signatures, m_message); + throw std::runtime_error("Please provide a message using the -m flag."); } + + repo.create_commit(author_committer_signatures, m_message); } diff --git a/src/subcommand/reset_subcommand.cpp b/src/subcommand/reset_subcommand.cpp new file mode 100644 index 0000000..b862e78 --- /dev/null +++ b/src/subcommand/reset_subcommand.cpp @@ -0,0 +1,61 @@ +#include "reset_subcommand.hpp" +// #include "../wrapper/index_wrapper.hpp" +#include "../wrapper/repository_wrapper.hpp" +#include + +enum class reset_type +{ + GIT_RESET_SOFT = 1, + GIT_RESET_MIXED = 2, + GIT_RESET_HARD = 3 +}; + +reset_subcommand::reset_subcommand(const libgit2_object&, CLI::App& app) +{ + auto *sub = app.add_subcommand("reset", "Reset current HEAD to the specified state"); + + sub->add_option("", m_commit, "The ID of the commit that will become HEAD"); + + sub->add_flag("--soft", m_soft_flag, ""); + sub->add_flag("--mixed", m_mixed_flag, ""); + sub->add_flag("--hard", m_hard_flag, ""); + + sub->callback([this]() { this->run(); }); +}; + + +void reset_subcommand::run() +{ + auto directory = get_current_git_path(); + auto bare = false; + auto repo = repository_wrapper::init(directory, bare); + + auto target = repo.revparse_single(m_commit); + if (!target) + { + throw std::runtime_error("Target revision not found."); + } + + git_checkout_options options; + git_checkout_options_init(&options, GIT_CHECKOUT_OPTIONS_VERSION); + + git_reset_t reset_type; + if (m_soft_flag) + { + reset_type = GIT_RESET_SOFT; + } + if (m_mixed_flag) + { + reset_type = GIT_RESET_MIXED; + } + if (m_hard_flag) + { + reset_type = GIT_RESET_HARD; + if (m_commit.empty()) + { + m_commit = "HEAD"; + } + } + + repo.reset(target.value(), reset_type, options); +} diff --git a/src/subcommand/reset_subcommand.hpp b/src/subcommand/reset_subcommand.hpp new file mode 100644 index 0000000..bfddd43 --- /dev/null +++ b/src/subcommand/reset_subcommand.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include "../utils/common.hpp" + +class reset_subcommand +{ +public: + + explicit reset_subcommand(const libgit2_object&, CLI::App& app); + void run(); + +private: + std::string m_commit; + bool m_soft_flag = false; + bool m_mixed_flag = false; + bool m_hard_flag = false; +}; diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index bbb9fa8..b25f974 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -1,4 +1,5 @@ #include "../utils/git_exception.hpp" +#include "object_wrapper.hpp" #include "../wrapper/repository_wrapper.hpp" repository_wrapper::~repository_wrapper() @@ -149,3 +150,8 @@ void repository_wrapper::set_head_detached(const annotated_commit_wrapper& commi { throw_if_error(git_repository_set_head_detached_from_annotated(*this, commit)); } + +void repository_wrapper::reset(const object_wrapper& target, git_reset_t reset_type, const git_checkout_options& checkout_options) +{ + throw_if_error(git_reset(*this, target, reset_type, &checkout_options)); +} diff --git a/src/wrapper/repository_wrapper.hpp b/src/wrapper/repository_wrapper.hpp index 6c745c4..7258724 100644 --- a/src/wrapper/repository_wrapper.hpp +++ b/src/wrapper/repository_wrapper.hpp @@ -62,9 +62,10 @@ class repository_wrapper : public wrapper_base // Objects std::optional revparse_single(std::string_view spec) const; - // Set head + // Head manipulations void set_head(std::string_view ref_name); void set_head_detached(const annotated_commit_wrapper& commit); + void reset(const object_wrapper& target, git_reset_t reset_type, const git_checkout_options& checkout_options); private: diff --git a/test/test_commit.py b/test/test_commit.py index aa4cdb4..fa83b9f 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -18,7 +18,7 @@ def test_add(git2cpp_path, all_flag): assert "Changes to be committed" in p_status.stdout assert "new file" in p_status.stdout - cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"] + cmd_commit = [git2cpp_path, 'commit', "--soft", "-m", "test commit"] subprocess.run(cmd_commit, capture_output=True, text=True) cmd_status_2 = [git2cpp_path, 'status', "--long"] From da951b1ba29681f370883f2f3c8e7acd79f6d169 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 11:33:24 +0200 Subject: [PATCH 03/17] write index --- src/subcommand/add_subcommand.cpp | 2 ++ src/wrapper/index_wrapper.cpp | 12 ++++++++++++ src/wrapper/index_wrapper.hpp | 3 +++ src/wrapper/object_wrapper.cpp | 5 +++++ src/wrapper/repository_wrapper.cpp | 19 ++++++++++++++++--- test/test_commit.py | 2 ++ 6 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/subcommand/add_subcommand.cpp b/src/subcommand/add_subcommand.cpp index cb2c609..9988d25 100644 --- a/src/subcommand/add_subcommand.cpp +++ b/src/subcommand/add_subcommand.cpp @@ -31,9 +31,11 @@ void add_subcommand::run() if (m_all_flag) { index.add_all(); + index.write(); } else { index.add_entries(m_add_files); + index.write(); } } diff --git a/src/wrapper/index_wrapper.cpp b/src/wrapper/index_wrapper.cpp index 81b1c26..03e8afb 100644 --- a/src/wrapper/index_wrapper.cpp +++ b/src/wrapper/index_wrapper.cpp @@ -32,5 +32,17 @@ void index_wrapper::add_impl(std::vector patterns) { git_strarray_wrapper array{patterns}; throw_if_error(git_index_add_all(*this, array, 0, NULL, NULL)); + // throw_if_error(git_index_write(*this)); +} + +void index_wrapper::write() +{ throw_if_error(git_index_write(*this)); } + +git_oid index_wrapper::write_tree() +{ + git_oid tree_id; + throw_if_error(git_index_write_tree(&tree_id, *this)); + return tree_id; +} diff --git a/src/wrapper/index_wrapper.hpp b/src/wrapper/index_wrapper.hpp index f8c1282..2095fe0 100644 --- a/src/wrapper/index_wrapper.hpp +++ b/src/wrapper/index_wrapper.hpp @@ -20,6 +20,9 @@ class index_wrapper : public wrapper_base static index_wrapper init(repository_wrapper& rw); + void write(); + git_oid write_tree(); + void add_entries(std::vector patterns); void add_all(); diff --git a/src/wrapper/object_wrapper.cpp b/src/wrapper/object_wrapper.cpp index cea6175..bf21361 100644 --- a/src/wrapper/object_wrapper.cpp +++ b/src/wrapper/object_wrapper.cpp @@ -15,3 +15,8 @@ const git_oid& object_wrapper::oid() const { return *git_object_id(*this); } + +object_wrapper::operator git_commit*() const noexcept +{ + return reinterpret_cast(p_resource); +} diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index b25f974..3d0d243 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -1,5 +1,6 @@ #include "../utils/git_exception.hpp" -#include "object_wrapper.hpp" +#include "../wrapper/index_wrapper.hpp" +#include "../wrapper/object_wrapper.hpp" #include "../wrapper/repository_wrapper.hpp" repository_wrapper::~repository_wrapper() @@ -111,7 +112,9 @@ commit_wrapper repository_wrapper::find_commit(const git_oid& id) const void repository_wrapper::create_commit(const signature_wrapper::author_committer_signatures& author_committer_signatures, const std::string& message) { + const char* message_encoding = "UTF-8"; git_oid* commit_id; + const char* update_ref = "ḦEAD"; auto parent = revparse_single(update_ref); std::size_t parent_count = 0; @@ -121,10 +124,18 @@ void repository_wrapper::create_commit(const signature_wrapper::author_committer parent_count = 1; parents[0] = *parent; } - const char* message_encoding = "UTF-8"; - const git_tree* tree; + + git_tree* tree; + index_wrapper index = this->make_index(); + git_oid tree_id = index.write_tree(); + index.write(); + + throw_if_error(git_tree_lookup(&tree, *this, &tree_id)); + throw_if_error(git_commit_create(commit_id, *this, update_ref, author_committer_signatures.first, author_committer_signatures.second, message_encoding, message.c_str(), tree, parent_count, parents)); + + git_tree_free(tree); } annotated_commit_wrapper repository_wrapper::find_annotated_commit(const git_oid& id) const @@ -153,5 +164,7 @@ void repository_wrapper::set_head_detached(const annotated_commit_wrapper& commi void repository_wrapper::reset(const object_wrapper& target, git_reset_t reset_type, const git_checkout_options& checkout_options) { + // TODO: gerer l'index + throw_if_error(git_reset(*this, target, reset_type, &checkout_options)); } diff --git a/test/test_commit.py b/test/test_commit.py index fa83b9f..eef433b 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -31,6 +31,8 @@ def test_add(git2cpp_path, all_flag): os.remove("./test/mook_file.txt") # TODO: git reset + # + # run status + assert # 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) From eef5385e22ef43459c468d9e5e87bca3fe657004 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 14:16:16 +0200 Subject: [PATCH 04/17] fix --- src/subcommand/commit_subcommand.cpp | 8 ++++---- src/subcommand/commit_subcommand.hpp | 4 ++-- src/wrapper/repository_wrapper.cpp | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/subcommand/commit_subcommand.cpp b/src/subcommand/commit_subcommand.cpp index 9fcc2a4..f068e86 100644 --- a/src/subcommand/commit_subcommand.cpp +++ b/src/subcommand/commit_subcommand.cpp @@ -10,9 +10,9 @@ commit_subcommand::commit_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("commit", "Record changes to the repository"); - sub->add_option("message", m_message, "Commit message"); + sub->add_option("commit_message", m_commit_message, "Commit message"); - sub->add_flag("-m,--message", m_message_flag, ""); + sub->add_flag("-m,--message", m_commit_message_flag, ""); sub->callback([this]() { this->run(); }); }; @@ -25,10 +25,10 @@ void commit_subcommand::run() auto repo = repository_wrapper::init(directory, bare); auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); - if (!m_message_flag) + if (!m_commit_message_flag) { throw std::runtime_error("Please provide a message using the -m flag."); } - repo.create_commit(author_committer_signatures, m_message); + repo.create_commit(author_committer_signatures, m_commit_message); } diff --git a/src/subcommand/commit_subcommand.hpp b/src/subcommand/commit_subcommand.hpp index f78680b..2159e04 100644 --- a/src/subcommand/commit_subcommand.hpp +++ b/src/subcommand/commit_subcommand.hpp @@ -12,6 +12,6 @@ class commit_subcommand void run(); private: - bool m_message_flag = true; // TODO: change to false when a message can be provided if the "-m" flag is not provided - std::string m_message; + bool m_commit_message_flag = true; // TODO: change to false when a message can be provided if the "-m" flag is not provided + std::string m_commit_message; }; diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index 3d0d243..a7815ed 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -113,9 +113,9 @@ void repository_wrapper::create_commit(const signature_wrapper::author_committer const std::string& message) { const char* message_encoding = "UTF-8"; - git_oid* commit_id; + git_oid commit_id; - const char* update_ref = "ḦEAD"; + std::string update_ref = "ḦEAD"; auto parent = revparse_single(update_ref); std::size_t parent_count = 0; const git_commit* parents[1] = {nullptr}; @@ -132,7 +132,7 @@ void repository_wrapper::create_commit(const signature_wrapper::author_committer throw_if_error(git_tree_lookup(&tree, *this, &tree_id)); - throw_if_error(git_commit_create(commit_id, *this, update_ref, author_committer_signatures.first, author_committer_signatures.second, + throw_if_error(git_commit_create(&commit_id, *this, update_ref.c_str(), author_committer_signatures.first, author_committer_signatures.second, message_encoding, message.c_str(), tree, parent_count, parents)); git_tree_free(tree); From 0c7c9281fd78d77722ef8f45c60f85c1d5042619 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 15:54:26 +0200 Subject: [PATCH 05/17] small fix --- src/wrapper/repository_wrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wrapper/repository_wrapper.cpp b/src/wrapper/repository_wrapper.cpp index a7815ed..6eb00f4 100644 --- a/src/wrapper/repository_wrapper.cpp +++ b/src/wrapper/repository_wrapper.cpp @@ -115,7 +115,7 @@ void repository_wrapper::create_commit(const signature_wrapper::author_committer const char* message_encoding = "UTF-8"; git_oid commit_id; - std::string update_ref = "ḦEAD"; + std::string update_ref = "HEAD"; auto parent = revparse_single(update_ref); std::size_t parent_count = 0; const git_commit* parents[1] = {nullptr}; From 7ee91abc23cf6310bbb6c22def736a4b8ff9697b Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 16:01:04 +0200 Subject: [PATCH 06/17] test commit --- test/mook_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/mook_file.txt diff --git a/test/mook_file.txt b/test/mook_file.txt new file mode 100644 index 0000000..e69de29 From 8082673a52d0a605fa06e912362e4623570891dd Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 16:02:15 +0200 Subject: [PATCH 07/17] update test --- test/test_commit.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/test/test_commit.py b/test/test_commit.py index eef433b..9bbc7f6 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -18,21 +18,13 @@ def test_add(git2cpp_path, all_flag): assert "Changes to be committed" in p_status.stdout assert "new file" in p_status.stdout - cmd_commit = [git2cpp_path, 'commit', "--soft", "-m", "test commit"] + cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"] subprocess.run(cmd_commit, capture_output=True, text=True) cmd_status_2 = [git2cpp_path, 'status', "--long"] - p_status_2 = subprocess.run(cmd_status_2, capture_output=True, text=True) + subprocess.run(cmd_status_2, capture_output=True, text=True) - print(p_status_2.stdout) + assert "mook_file" not in p_status.stdout - # assert "mook_file" not in p_status.stdout - - os.remove("./test/mook_file.txt") - - # TODO: git reset - # - # run status + assert - - # 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) + cmd_reset = [git2cpp_path, 'reset', "--hard", "HEAD~1"] + subprocess.run(cmd_reset, capture_output=True, text=True) From 516dba51c3ce72499ea26f8c1c3b6aae54b685c4 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 16:03:43 +0200 Subject: [PATCH 08/17] remove test file --- test/mook_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/mook_file.txt diff --git a/test/mook_file.txt b/test/mook_file.txt deleted file mode 100644 index e69de29..0000000 From 1076d427df3e48f281a494ceee496b3a9a879f8a Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 16:06:42 +0200 Subject: [PATCH 09/17] fix test --- test/test_commit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_commit.py b/test/test_commit.py index 9bbc7f6..c9ef529 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -22,9 +22,9 @@ def test_add(git2cpp_path, all_flag): subprocess.run(cmd_commit, capture_output=True, text=True) cmd_status_2 = [git2cpp_path, 'status', "--long"] - subprocess.run(cmd_status_2, capture_output=True, text=True) + p_status_2 = subprocess.run(cmd_status_2, capture_output=True, text=True) - assert "mook_file" not in p_status.stdout + assert "mook_file" not in p_status_2.stdout cmd_reset = [git2cpp_path, 'reset', "--hard", "HEAD~1"] subprocess.run(cmd_reset, capture_output=True, text=True) From 75d16eebe1c47a231a0ceca66c1a807375e0ca87 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 16:39:23 +0200 Subject: [PATCH 10/17] add branch to test --- test/test_commit.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/test_commit.py b/test/test_commit.py index c9ef529..8807c57 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -6,25 +6,35 @@ @pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"]) def test_add(git2cpp_path, all_flag): + checkout_cmd = [git2cpp_path, 'checkout', '-b', 'foregone'] + subprocess.run(checkout_cmd, cwd="test/data/status_data", text=True) + with open("./test/mook_file.txt", "x"): pass cmd_add = [git2cpp_path, 'add', "test/mook_file.txt"] - subprocess.run(cmd_add, capture_output=True, text=True) + subprocess.run(cmd_add, cwd="test/data/status_data", text=True) cmd_status = [git2cpp_path, 'status', "--long"] - p_status = subprocess.run(cmd_status, capture_output=True, text=True) + p_status = subprocess.run(cmd_status, capture_output=True, cwd="test/data/status_data", text=True) assert "Changes to be committed" in p_status.stdout assert "new file" in p_status.stdout cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"] - subprocess.run(cmd_commit, capture_output=True, text=True) + subprocess.run(cmd_commit, cwd="test/data/status_data", text=True) cmd_status_2 = [git2cpp_path, 'status', "--long"] - p_status_2 = subprocess.run(cmd_status_2, capture_output=True, text=True) + p_status_2 = subprocess.run(cmd_status_2, capture_output=True, cwd="test/data/status_data", text=True) assert "mook_file" not in p_status_2.stdout cmd_reset = [git2cpp_path, 'reset', "--hard", "HEAD~1"] - subprocess.run(cmd_reset, capture_output=True, text=True) + subprocess.run(cmd_reset, cwd="test/data/status_data", text=True) + + checkout_cmd.remove('-b') + checkout_cmd[2] = 'main' + subprocess.run(checkout_cmd, cwd="test/data/status_data", text=True) + + del_cmd = [git2cpp_path, 'branch', '-d', 'foregone'] + subprocess.run(del_cmd, cwd="test/data/status_data", text=True) From fee3016e71ee2d566948ad79d639ea3fa1e9f242 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 17:04:13 +0200 Subject: [PATCH 11/17] edit branch name --- test/data/status_data/embedded_git/HEAD | 2 +- test/data/status_data/embedded_git/config | 0 test/data/status_data/embedded_git/index | Bin 294 -> 313 bytes test/data/status_data/embedded_git/logs/HEAD | 13 +++++++++++++ .../logs/refs/heads/commit_test_branch | 1 + .../a3/079a98067bc027a22dc56fe619c5d30f23d3a6 | Bin 0 -> 129 bytes .../cb/a545ef5cc4ddf12a9744b6a49b20dda1ef1d5c | Bin 0 -> 175 bytes .../embedded_git/refs/heads/commit_test_branch | 1 + ...er_test_file_new.txt => other_test_file.txt} | 0 test/data/status_data/random_file.txt | 2 +- test/data/status_data/test_file.txt | 1 + test/mook_file.txt | 0 test/test_commit.py | 6 +++--- 13 files changed, 21 insertions(+), 5 deletions(-) mode change 100755 => 100644 test/data/status_data/embedded_git/config create mode 100644 test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch create mode 100644 test/data/status_data/embedded_git/objects/a3/079a98067bc027a22dc56fe619c5d30f23d3a6 create mode 100644 test/data/status_data/embedded_git/objects/cb/a545ef5cc4ddf12a9744b6a49b20dda1ef1d5c create mode 100644 test/data/status_data/embedded_git/refs/heads/commit_test_branch rename test/data/status_data/{other_test_file_new.txt => other_test_file.txt} (100%) create mode 100644 test/data/status_data/test_file.txt create mode 100644 test/mook_file.txt diff --git a/test/data/status_data/embedded_git/HEAD b/test/data/status_data/embedded_git/HEAD index b870d82..990e32d 100644 --- a/test/data/status_data/embedded_git/HEAD +++ b/test/data/status_data/embedded_git/HEAD @@ -1 +1 @@ -ref: refs/heads/main +ref: refs/heads/commit_test_branch diff --git a/test/data/status_data/embedded_git/config b/test/data/status_data/embedded_git/config old mode 100755 new mode 100644 diff --git a/test/data/status_data/embedded_git/index b/test/data/status_data/embedded_git/index index 4c1df39881ac67517b7bf97760ae293b4719d233..c92204e3865d98cbceb78745b91714f93e3f690c 100644 GIT binary patch delta 190 zcmZ3+w3A82#WTp6fq{Vuh?z6Gj0N=%`OJdRKrsder|mB%s;C+Z=a*!p7R8sO7MH}Q zW#**nl~j~~vb6uYb-RtVn<3mVKi9IY5OY%hQ=j88(%;$gY3jR z`g)QK#tH^pkBzLIbk6OYD!OHrakN8Xyk&Q4@FxFljE1ML2wX|JCA#4U!`8*0SOozW CfIA@o delta 171 zcmdnVw2Vo`#WTp6fq{Vuh?z5@mX(TrD$~e_T5wjf+uaf zd`W6?NqkynPHKE!YPnuXMF~(%M%2>zJQ;FoFdCvQZtlby3k@I}2WVV^EHUwpzBC(y fuAzbf*J_i%nDe~3nk~EEJ-zks<>Aj~y>9>j8T~Fr diff --git a/test/data/status_data/embedded_git/logs/HEAD b/test/data/status_data/embedded_git/logs/HEAD index a996210..b4fd0ec 100644 --- a/test/data/status_data/embedded_git/logs/HEAD +++ b/test/data/status_data/embedded_git/logs/HEAD @@ -1,2 +1,15 @@ 0000000000000000000000000000000000000000 75743dcbd85064226c77a0b862af817838ae0b2e Sandrine Pataut 1750769952 +0200 commit (initial): first commit 75743dcbd85064226c77a0b862af817838ae0b2e ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1750771272 +0200 commit: Second commit +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from main to bla +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from bla to main +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from main to bla +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from bla to main +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from main to bla +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from bla to main +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from main to bla +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887403 +0200 checkout: moving from bla to main +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 checkout: moving from main to commit_test_branch +ee8c4cf874c4f1e3ba755f929fe7811018adee3d cba545ef5cc4ddf12a9744b6a49b20dda1ef1d5c Sandrine Pataut 1753887505 +0200 commit: test commit +cba545ef5cc4ddf12a9744b6a49b20dda1ef1d5c ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 reset: moving to ee8c4cf874c4f1e3ba755f929fe7811018adee3d +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 checkout: moving from commit_test_branch to main +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 checkout: moving from main to commit_test_branch diff --git a/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch b/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch new file mode 100644 index 0000000..6f25389 --- /dev/null +++ b/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 branch: Created from ee8c4cf874c4f1e3ba755f929fe7811018adee3d diff --git a/test/data/status_data/embedded_git/objects/a3/079a98067bc027a22dc56fe619c5d30f23d3a6 b/test/data/status_data/embedded_git/objects/a3/079a98067bc027a22dc56fe619c5d30f23d3a6 new file mode 100644 index 0000000000000000000000000000000000000000..50960b9a285d98ba1d05175867ee2569188580b4 GIT binary patch literal 129 zcmV-{0Dk{?0V^p=O;s>7Hf1n0FfcPQQOGaJNG*ylNi8mkPs_|njn7Lh*DI+gVVJXW zMPc^$udC(1c<$qwzQC($ol_E2OHpE8N`5X_Iaui(XFeOfJi*DE%UKxJb_UE;lPd~@ jDutU1R=D@Qa}N8v&bgAQQyVW$@ej8;rtkm&{mnMHv`;zj literal 0 HcmV?d00001 diff --git a/test/data/status_data/embedded_git/objects/cb/a545ef5cc4ddf12a9744b6a49b20dda1ef1d5c b/test/data/status_data/embedded_git/objects/cb/a545ef5cc4ddf12a9744b6a49b20dda1ef1d5c new file mode 100644 index 0000000000000000000000000000000000000000..5aeeed187c1516c2aec50ef45be35e0b100d511e GIT binary patch literal 175 zcmV;g08syU0j1783IZ_@hGFZRVtP>~$>tA4tgHp~0OMq$3vO1{cz+l5238+D#mmRz zF!T+K$3>e&fGmbYDWVS-Mia=G%+43_Id6 literal 0 HcmV?d00001 diff --git a/test/data/status_data/embedded_git/refs/heads/commit_test_branch b/test/data/status_data/embedded_git/refs/heads/commit_test_branch new file mode 100644 index 0000000..6d6a647 --- /dev/null +++ b/test/data/status_data/embedded_git/refs/heads/commit_test_branch @@ -0,0 +1 @@ +ee8c4cf874c4f1e3ba755f929fe7811018adee3d diff --git a/test/data/status_data/other_test_file_new.txt b/test/data/status_data/other_test_file.txt similarity index 100% rename from test/data/status_data/other_test_file_new.txt rename to test/data/status_data/other_test_file.txt diff --git a/test/data/status_data/random_file.txt b/test/data/status_data/random_file.txt index c0fc9fb..dc430e3 100644 --- a/test/data/status_data/random_file.txt +++ b/test/data/status_data/random_file.txt @@ -1 +1 @@ -That's a random file modified. +That's a random file. diff --git a/test/data/status_data/test_file.txt b/test/data/status_data/test_file.txt new file mode 100644 index 0000000..bdef436 --- /dev/null +++ b/test/data/status_data/test_file.txt @@ -0,0 +1 @@ +This is the first test file. diff --git a/test/mook_file.txt b/test/mook_file.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/test_commit.py b/test/test_commit.py index 8807c57..8548afc 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -5,8 +5,8 @@ @pytest.mark.parametrize("all_flag", ["", "-A", "--all", "--no-ignore-removal"]) -def test_add(git2cpp_path, all_flag): - checkout_cmd = [git2cpp_path, 'checkout', '-b', 'foregone'] +def test_commit(rename_git, git2cpp_path, all_flag): + checkout_cmd = [git2cpp_path, 'checkout', '-b', 'commit_test_branch'] subprocess.run(checkout_cmd, cwd="test/data/status_data", text=True) with open("./test/mook_file.txt", "x"): @@ -36,5 +36,5 @@ def test_add(git2cpp_path, all_flag): checkout_cmd[2] = 'main' subprocess.run(checkout_cmd, cwd="test/data/status_data", text=True) - del_cmd = [git2cpp_path, 'branch', '-d', 'foregone'] + del_cmd = [git2cpp_path, 'branch', '-d', 'commit_test_branch'] subprocess.run(del_cmd, cwd="test/data/status_data", text=True) From 8b9eaf4daf842c9a2b043958d6d992e3924d37fa Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 17:11:29 +0200 Subject: [PATCH 12/17] remove mook test file --- test/mook_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/mook_file.txt diff --git a/test/mook_file.txt b/test/mook_file.txt deleted file mode 100644 index e69de29..0000000 From 516e7263d8f57bd364af96c581473830fa3c8744 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 17:14:46 +0200 Subject: [PATCH 13/17] test commit --- test/mook_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/mook_file.txt diff --git a/test/mook_file.txt b/test/mook_file.txt new file mode 100644 index 0000000..e69de29 From 2ba0ac9033a939f5672b89c965ffcbede059627e Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Wed, 30 Jul 2025 17:17:42 +0200 Subject: [PATCH 14/17] edit file path --- test/data/status_data/embedded_git/config | 0 test/data/status_data/embedded_git/index | Bin 313 -> 294 bytes test/data/status_data/embedded_git/logs/HEAD | 3 +++ .../logs/refs/heads/commit_test_branch | 2 +- test/mook_file.txt | 0 test/test_commit.py | 4 ++-- 6 files changed, 6 insertions(+), 3 deletions(-) mode change 100644 => 100755 test/data/status_data/embedded_git/config delete mode 100644 test/mook_file.txt diff --git a/test/data/status_data/embedded_git/config b/test/data/status_data/embedded_git/config old mode 100644 new mode 100755 diff --git a/test/data/status_data/embedded_git/index b/test/data/status_data/embedded_git/index index c92204e3865d98cbceb78745b91714f93e3f690c..95e66756d042e8caa34a150d0f4b443a8426c817 100644 GIT binary patch delta 60 zcmdnVw2Wy&nwZ%$J^fis8C?*X0R)_O9hz9vFU`iFYp7trCGm0Mn_5%-z%OgB#4A|m K?f4oJ=K}y`yA{6x delta 79 zcmZ3+w3BHM(JiZtqa70C eExS{LH~DX4G(3Gp;7Zah(G5Qswl4m}DhL1s0vyr+ diff --git a/test/data/status_data/embedded_git/logs/HEAD b/test/data/status_data/embedded_git/logs/HEAD index b4fd0ec..193713c 100644 --- a/test/data/status_data/embedded_git/logs/HEAD +++ b/test/data/status_data/embedded_git/logs/HEAD @@ -13,3 +13,6 @@ ee8c4cf874c4f1e3ba755f929fe7811018adee3d cba545ef5cc4ddf12a9744b6a49b20dda1ef1d5 cba545ef5cc4ddf12a9744b6a49b20dda1ef1d5c ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 reset: moving to ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 checkout: moving from commit_test_branch to main ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 checkout: moving from main to commit_test_branch +ee8c4cf874c4f1e3ba755f929fe7811018adee3d 75743dcbd85064226c77a0b862af817838ae0b2e Sandrine Pataut 1753888486 +0200 reset: moving to 75743dcbd85064226c77a0b862af817838ae0b2e +75743dcbd85064226c77a0b862af817838ae0b2e ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753888486 +0200 checkout: moving from commit_test_branch to main +ee8c4cf874c4f1e3ba755f929fe7811018adee3d ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753888486 +0200 checkout: moving from main to commit_test_branch diff --git a/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch b/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch index 6f25389..0d35c1b 100644 --- a/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch +++ b/test/data/status_data/embedded_git/logs/refs/heads/commit_test_branch @@ -1 +1 @@ -0000000000000000000000000000000000000000 ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753887505 +0200 branch: Created from ee8c4cf874c4f1e3ba755f929fe7811018adee3d +0000000000000000000000000000000000000000 ee8c4cf874c4f1e3ba755f929fe7811018adee3d Sandrine Pataut 1753888486 +0200 branch: Created from ee8c4cf874c4f1e3ba755f929fe7811018adee3d diff --git a/test/mook_file.txt b/test/mook_file.txt deleted file mode 100644 index e69de29..0000000 diff --git a/test/test_commit.py b/test/test_commit.py index 8548afc..89381ac 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -9,10 +9,10 @@ def test_commit(rename_git, git2cpp_path, all_flag): checkout_cmd = [git2cpp_path, 'checkout', '-b', 'commit_test_branch'] subprocess.run(checkout_cmd, cwd="test/data/status_data", text=True) - with open("./test/mook_file.txt", "x"): + with open("./test/data/status_data/mook_file.txt", "x"): pass - cmd_add = [git2cpp_path, 'add', "test/mook_file.txt"] + cmd_add = [git2cpp_path, 'add', "mook_file.txt"] subprocess.run(cmd_add, cwd="test/data/status_data", text=True) cmd_status = [git2cpp_path, 'status', "--long"] From 954587d554a993b4514f454768a335a3b552a334 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Thu, 31 Jul 2025 10:57:41 +0200 Subject: [PATCH 15/17] address review comments --- src/subcommand/commit_subcommand.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/subcommand/commit_subcommand.cpp b/src/subcommand/commit_subcommand.cpp index f068e86..a0efc1b 100644 --- a/src/subcommand/commit_subcommand.cpp +++ b/src/subcommand/commit_subcommand.cpp @@ -10,6 +10,8 @@ commit_subcommand::commit_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("commit", "Record changes to the repository"); + // TODO: merge the following option and flag into: sub->add_option("-m,--message", m_commit_message, "Commit message") + // when we have a solution to ask for a message when not using the -m flag. sub->add_option("commit_message", m_commit_message, "Commit message"); sub->add_flag("-m,--message", m_commit_message_flag, ""); @@ -23,12 +25,16 @@ void commit_subcommand::run() auto directory = get_current_git_path(); auto bare = false; auto repo = repository_wrapper::init(directory, bare); - auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); + auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); if (!m_commit_message_flag) { throw std::runtime_error("Please provide a message using the -m flag."); } + if (m_commit_message.empty()) + { + throw std::runtime_error("Please provide a commit message."); + } repo.create_commit(author_committer_signatures, m_commit_message); } From d6c35c3667b7e1cbd01da52af4b339f5ba4a8ad7 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Thu, 31 Jul 2025 11:20:32 +0200 Subject: [PATCH 16/17] edit option --- src/subcommand/commit_subcommand.cpp | 17 +++++++---------- src/subcommand/commit_subcommand.hpp | 1 - 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/subcommand/commit_subcommand.cpp b/src/subcommand/commit_subcommand.cpp index a0efc1b..44d1bd9 100644 --- a/src/subcommand/commit_subcommand.cpp +++ b/src/subcommand/commit_subcommand.cpp @@ -10,11 +10,7 @@ commit_subcommand::commit_subcommand(const libgit2_object&, CLI::App& app) { auto *sub = app.add_subcommand("commit", "Record changes to the repository"); - // TODO: merge the following option and flag into: sub->add_option("-m,--message", m_commit_message, "Commit message") - // when we have a solution to ask for a message when not using the -m flag. - sub->add_option("commit_message", m_commit_message, "Commit message"); - - sub->add_flag("-m,--message", m_commit_message_flag, ""); + sub->add_option("-m,--message", m_commit_message, "Commit message"); sub->callback([this]() { this->run(); }); }; @@ -27,13 +23,14 @@ void commit_subcommand::run() auto repo = repository_wrapper::init(directory, bare); auto author_committer_signatures = signature_wrapper::get_default_signature_from_env(repo); - if (!m_commit_message_flag) - { - throw std::runtime_error("Please provide a message using the -m flag."); - } if (m_commit_message.empty()) { - throw std::runtime_error("Please provide a commit message."); + std::cout << "Please enter a commit message: " << std::endl; + std::getline(std::cin, m_commit_message); + if (m_commit_message.empty()) + { + throw std::runtime_error("Aborting, no commit message specified."); + } } repo.create_commit(author_committer_signatures, m_commit_message); diff --git a/src/subcommand/commit_subcommand.hpp b/src/subcommand/commit_subcommand.hpp index 2159e04..8b29ccb 100644 --- a/src/subcommand/commit_subcommand.hpp +++ b/src/subcommand/commit_subcommand.hpp @@ -12,6 +12,5 @@ class commit_subcommand void run(); private: - bool m_commit_message_flag = true; // TODO: change to false when a message can be provided if the "-m" flag is not provided std::string m_commit_message; }; From 67f57dcb597638826d95d73fcb35bb2d9b0aa8f9 Mon Sep 17 00:00:00 2001 From: Sandrine Pataut Date: Thu, 31 Jul 2025 11:43:54 +0200 Subject: [PATCH 17/17] remove extra space --- src/subcommand/commit_subcommand.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand/commit_subcommand.cpp b/src/subcommand/commit_subcommand.cpp index 44d1bd9..b476100 100644 --- a/src/subcommand/commit_subcommand.cpp +++ b/src/subcommand/commit_subcommand.cpp @@ -25,7 +25,7 @@ void commit_subcommand::run() if (m_commit_message.empty()) { - std::cout << "Please enter a commit message: " << std::endl; + std::cout << "Please enter a commit message:" << std::endl; std::getline(std::cin, m_commit_message); if (m_commit_message.empty()) {