Skip to content
Prev Previous commit
Next Next commit
add reset subcommand
  • Loading branch information
SandrineP committed Jul 29, 2025
commit 4dbb90d56a89f02c560a9cc09f40fd04eb39cde0
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down
8 changes: 3 additions & 5 deletions src/subcommand/commit_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
61 changes: 61 additions & 0 deletions src/subcommand/reset_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "reset_subcommand.hpp"
// #include "../wrapper/index_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include <stdexcept>

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("<commit>", 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);
}
19 changes: 19 additions & 0 deletions src/subcommand/reset_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <CLI/CLI.hpp>

#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;
};
6 changes: 6 additions & 0 deletions src/wrapper/repository_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../utils/git_exception.hpp"
#include "object_wrapper.hpp"
#include "../wrapper/repository_wrapper.hpp"

repository_wrapper::~repository_wrapper()
Expand Down Expand Up @@ -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));
}
3 changes: 2 additions & 1 deletion src/wrapper/repository_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ class repository_wrapper : public wrapper_base<git_repository>
// Objects
std::optional<object_wrapper> 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:

Expand Down
2 changes: 1 addition & 1 deletion test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down