forked from QuantStack/git2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_subcommand.cpp
More file actions
85 lines (74 loc) · 2.54 KB
/
reset_subcommand.cpp
File metadata and controls
85 lines (74 loc) · 2.54 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "reset_subcommand.hpp"
// #include "../wrapper/index_wrapper.hpp"
#include <stdexcept>
#include "../utils/ansi_code.hpp"
#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,
"Leave your working tree files and the index unchanged. For example, if you have no staged changes, you can use "
+ ansi_code::bold + "git reset --soft HEAD~5" + ansi_code::reset + "; " + ansi_code::bold
+ "git commit" + ansi_code::reset
+ " to combine the last 5 commits into 1 commit. This works even with changes in the working tree, which are left untouched, but such usage can lead to confusion."
);
sub->add_flag(
"--mixed",
m_mixed_flag,
"Leave your working directory unchanged. Update the index to match the new HEAD, so nothing will be staged."
);
sub->add_flag(
"--hard",
m_hard_flag,
"Overwrite all files and directories with the version from " + ansi_code::bold + "<commit>"
+ ansi_code::reset + ", and may overwrite untracked files. Tracked files not in "
+ ansi_code::bold + "<commit>" + ansi_code::reset
+ " are removed so that the working tree matches " + ansi_code::bold + "<commit>"
+ ansi_code::reset + ". Update the index to match the new HEAD, so nothing will be staged."
);
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);
}