Skip to content

Commit 4dbb90d

Browse files
committed
add reset subcommand
1 parent b977235 commit 4dbb90d

File tree

8 files changed

+98
-7
lines changed

8 files changed

+98
-7
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ set(GIT2CPP_SRC
5151
${GIT2CPP_SOURCE_DIR}/subcommand/commit_subcommand.hpp
5252
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.cpp
5353
${GIT2CPP_SOURCE_DIR}/subcommand/init_subcommand.hpp
54+
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.cpp
55+
${GIT2CPP_SOURCE_DIR}/subcommand/reset_subcommand.hpp
5456
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.cpp
5557
${GIT2CPP_SOURCE_DIR}/subcommand/status_subcommand.hpp
5658
${GIT2CPP_SOURCE_DIR}/utils/common.cpp

src/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include "subcommand/branch_subcommand.hpp"
99
#include "subcommand/checkout_subcommand.hpp"
1010
#include "subcommand/clone_subcommand.hpp"
11+
#include "subcommand/commit_subcommand.hpp"
1112
#include "subcommand/init_subcommand.hpp"
13+
#include "subcommand/reset_subcommand.hpp"
1214
#include "subcommand/status_subcommand.hpp"
1315

1416
int main(int argc, char** argv)
@@ -29,6 +31,8 @@ int main(int argc, char** argv)
2931
branch_subcommand branch(lg2_obj, app);
3032
checkout_subcommand checkout(lg2_obj, app);
3133
clone_subcommand clone(lg2_obj, app);
34+
commit_subcommand commit(lg2_obj, app);
35+
reset_subcommand reset(lg2_obj, app);
3236

3337
app.require_subcommand(/* min */ 0, /* max */ 1);
3438

src/subcommand/commit_subcommand.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ void commit_subcommand::run()
2727

2828
if (!m_message_flag)
2929
{
30-
std::cout << "Please provide a message using the -m flag." << std::endl;
31-
}
32-
else
33-
{
34-
repo.create_commit(author_committer_signatures, m_message);
30+
throw std::runtime_error("Please provide a message using the -m flag.");
3531
}
32+
33+
repo.create_commit(author_committer_signatures, m_message);
3634
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "reset_subcommand.hpp"
2+
// #include "../wrapper/index_wrapper.hpp"
3+
#include "../wrapper/repository_wrapper.hpp"
4+
#include <stdexcept>
5+
6+
enum class reset_type
7+
{
8+
GIT_RESET_SOFT = 1,
9+
GIT_RESET_MIXED = 2,
10+
GIT_RESET_HARD = 3
11+
};
12+
13+
reset_subcommand::reset_subcommand(const libgit2_object&, CLI::App& app)
14+
{
15+
auto *sub = app.add_subcommand("reset", "Reset current HEAD to the specified state");
16+
17+
sub->add_option("<commit>", m_commit, "The ID of the commit that will become HEAD");
18+
19+
sub->add_flag("--soft", m_soft_flag, "");
20+
sub->add_flag("--mixed", m_mixed_flag, "");
21+
sub->add_flag("--hard", m_hard_flag, "");
22+
23+
sub->callback([this]() { this->run(); });
24+
};
25+
26+
27+
void reset_subcommand::run()
28+
{
29+
auto directory = get_current_git_path();
30+
auto bare = false;
31+
auto repo = repository_wrapper::init(directory, bare);
32+
33+
auto target = repo.revparse_single(m_commit);
34+
if (!target)
35+
{
36+
throw std::runtime_error("Target revision not found.");
37+
}
38+
39+
git_checkout_options options;
40+
git_checkout_options_init(&options, GIT_CHECKOUT_OPTIONS_VERSION);
41+
42+
git_reset_t reset_type;
43+
if (m_soft_flag)
44+
{
45+
reset_type = GIT_RESET_SOFT;
46+
}
47+
if (m_mixed_flag)
48+
{
49+
reset_type = GIT_RESET_MIXED;
50+
}
51+
if (m_hard_flag)
52+
{
53+
reset_type = GIT_RESET_HARD;
54+
if (m_commit.empty())
55+
{
56+
m_commit = "HEAD";
57+
}
58+
}
59+
60+
repo.reset(target.value(), reset_type, options);
61+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <CLI/CLI.hpp>
4+
5+
#include "../utils/common.hpp"
6+
7+
class reset_subcommand
8+
{
9+
public:
10+
11+
explicit reset_subcommand(const libgit2_object&, CLI::App& app);
12+
void run();
13+
14+
private:
15+
std::string m_commit;
16+
bool m_soft_flag = false;
17+
bool m_mixed_flag = false;
18+
bool m_hard_flag = false;
19+
};

src/wrapper/repository_wrapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../utils/git_exception.hpp"
2+
#include "object_wrapper.hpp"
23
#include "../wrapper/repository_wrapper.hpp"
34

45
repository_wrapper::~repository_wrapper()
@@ -149,3 +150,8 @@ void repository_wrapper::set_head_detached(const annotated_commit_wrapper& commi
149150
{
150151
throw_if_error(git_repository_set_head_detached_from_annotated(*this, commit));
151152
}
153+
154+
void repository_wrapper::reset(const object_wrapper& target, git_reset_t reset_type, const git_checkout_options& checkout_options)
155+
{
156+
throw_if_error(git_reset(*this, target, reset_type, &checkout_options));
157+
}

src/wrapper/repository_wrapper.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ class repository_wrapper : public wrapper_base<git_repository>
6262
// Objects
6363
std::optional<object_wrapper> revparse_single(std::string_view spec) const;
6464

65-
// Set head
65+
// Head manipulations
6666
void set_head(std::string_view ref_name);
6767
void set_head_detached(const annotated_commit_wrapper& commit);
68+
void reset(const object_wrapper& target, git_reset_t reset_type, const git_checkout_options& checkout_options);
6869

6970
private:
7071

test/test_commit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_add(git2cpp_path, all_flag):
1818
assert "Changes to be committed" in p_status.stdout
1919
assert "new file" in p_status.stdout
2020

21-
cmd_commit = [git2cpp_path, 'commit', "-m", "test commit"]
21+
cmd_commit = [git2cpp_path, 'commit', "--soft", "-m", "test commit"]
2222
subprocess.run(cmd_commit, capture_output=True, text=True)
2323

2424
cmd_status_2 = [git2cpp_path, 'status', "--long"]

0 commit comments

Comments
 (0)