-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreset_subcommand.cpp
More file actions
65 lines (54 loc) · 1.45 KB
/
reset_subcommand.cpp
File metadata and controls
65 lines (54 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "reset_subcommand.hpp"
// #include "../wrapper/index_wrapper.hpp"
#include <stdexcept>
#include "../wrapper/repository_wrapper.hpp"
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 repo = repository_wrapper::open(directory);
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);
}