Skip to content

Commit

Permalink
Add history rollback command
Browse files Browse the repository at this point in the history
  • Loading branch information
kontura authored and evan-goode committed Jul 2, 2024
1 parent 056fd87 commit c4d5bbf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dnf5/commands/history/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void HistoryCommand::register_subcommands() {
cmd.register_group(software_management_commands_group);
register_subcommand(std::make_unique<HistoryUndoCommand>(get_context()), software_management_commands_group);
// register_subcommand(std::make_unique<HistoryRedoCommand>(get_context()), software_management_commands_group);
// register_subcommand(std::make_unique<HistoryRollbackCommand>(get_context()), software_management_commands_group);
register_subcommand(std::make_unique<HistoryRollbackCommand>(get_context()), software_management_commands_group);
register_subcommand(std::make_unique<HistoryStoreCommand>(get_context()), software_management_commands_group);
}

Expand Down
53 changes: 52 additions & 1 deletion dnf5/commands/history/history_rollback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,65 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "history_rollback.hpp"

#include "arguments.hpp"
#include "dnf5/shared_options.hpp"
#include "transaction_id.hpp"

#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>

namespace dnf5 {

using namespace libdnf5::cli;

void HistoryRollbackCommand::set_argument_parser() {
get_argument_parser_command()->set_description("Undo all transactions performed after the specified transaction");
transaction_specs = std::make_unique<TransactionSpecArguments>(*this, 1);
auto & ctx = get_context();
transaction_specs->get_arg()->set_complete_hook_func(create_history_id_autocomplete(ctx));
auto skip_unavailable = std::make_unique<SkipUnavailableOption>(*this);
ignore_extras = std::make_unique<IgnoreExtrasOption>(*this);
ignore_installed = std::make_unique<IgnoreInstalledOption>(*this);
}

void HistoryRollbackCommand::configure() {
auto & context = get_context();
context.set_load_system_repo(true);
context.set_load_available_repos(Context::LoadAvailableRepos::ENABLED);
}

void HistoryRollbackCommand::run() {}
void HistoryRollbackCommand::run() {
auto ts_specs = transaction_specs->get_value();
libdnf5::transaction::TransactionHistory history(get_context().get_base());
std::vector<libdnf5::transaction::Transaction> transactions;
std::vector<libdnf5::transaction::Transaction> target_trans;

target_trans = list_transactions_from_specs(history, ts_specs);

if (target_trans.size() < 1) {
throw libdnf5::cli::CommandExitError(1, M_("No matching transaction ID found, exactly one required."));
}

if (target_trans.size() > 1) {
throw libdnf5::cli::CommandExitError(1, M_("Matched more than one transaction ID, exactly one required."));
}

auto max_id = history.list_transaction_ids().back();

int64_t target_id = target_trans[0].get_id() + 1;
if (target_id <= max_id) {
transactions = history.list_transactions(target_id, max_id);
}

auto goal = get_context().get_goal();
// To enable removal of dependency packages not present in the selected transactions
// it requires allow_erasing. This will inform the user that additional removes
// are required and the transaction won't proceed without --ignore-extras.
goal->set_allow_erasing(true);

auto settings = libdnf5::GoalJobSettings();
settings.set_ignore_extras(ignore_extras->get_value());
settings.set_ignore_installed(ignore_installed->get_value());
goal->add_revert_transactions(transactions, settings);
}

} // namespace dnf5
8 changes: 8 additions & 0 deletions dnf5/commands/history/history_rollback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#ifndef DNF5_COMMANDS_HISTORY_HISTORY_ROLLBACK_HPP
#define DNF5_COMMANDS_HISTORY_HISTORY_ROLLBACK_HPP

#include "commands/history/arguments.hpp"

#include <dnf5/context.hpp>


Expand All @@ -31,7 +33,13 @@ class HistoryRollbackCommand : public Command {
public:
explicit HistoryRollbackCommand(Context & context) : Command(context, "rollback") {}
void set_argument_parser() override;
void configure() override;
void run() override;

private:
std::unique_ptr<TransactionSpecArguments> transaction_specs{nullptr};
std::unique_ptr<libdnf5::cli::session::BoolOption> ignore_extras{nullptr};
std::unique_ptr<libdnf5::cli::session::BoolOption> ignore_installed{nullptr};
};


Expand Down

0 comments on commit c4d5bbf

Please sign in to comment.