|
| 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 | +} |
0 commit comments