Skip to content

Commit

Permalink
versionlock: Refactor for simpler code
Browse files Browse the repository at this point in the history
- Since the add and delete subcommands do not modify any rules but
  merely add or delete a rule, we can detect changes in the ruleset
  simply by comparing the size of the vector before and after the
  operation.
- Simplify exclude implementation leveraging std::any_of
  • Loading branch information
m-blaha authored and jan-kolarik committed Feb 14, 2024
1 parent 2e89ba0 commit eeeedfb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
5 changes: 2 additions & 3 deletions dnf5/commands/versionlock/versionlock_add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ void VersionlockAddCommand::run() {
auto & ctx = get_context();
auto package_sack = ctx.base.get_rpm_package_sack();
auto vl_config = package_sack->get_versionlock_config();
auto orig_size = vl_config.get_packages().size();

const auto comment = format_comment("add");

bool changed{false};
const libdnf5::ResolveSpecSettings settings{
.with_nevra = true, .with_provides = false, .with_filenames = false, .with_binaries = false};
for (const auto & spec : pkg_specs) {
Expand All @@ -118,15 +118,14 @@ void VersionlockAddCommand::run() {
}
versions.emplace(evr);
if (lock_version(vl_config, pkg, comment)) {
changed = true;
std::cout << libdnf5::utils::sformat(_("Adding versionlock on \"{0} = {1}\"."), pkg.get_name(), evr)
<< std::endl;
} else {
std::cerr << libdnf5::utils::sformat(_("Package \"{}\" is already locked."), spec) << std::endl;
}
}
}
if (changed) {
if (vl_config.get_packages().size() != orig_size) {
vl_config.save();
}
}
Expand Down
13 changes: 5 additions & 8 deletions dnf5/commands/versionlock/versionlock_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,30 @@ void VersionlockDeleteCommand::set_argument_parser() {
cmd.register_positional_arg(keys);
}

bool delete_package(libdnf5::rpm::VersionlockConfig & vl_config, std::string_view spec) {
bool changed = false;
auto remove_predicate = [spec, &changed](libdnf5::rpm::VersionlockPackage & pkg) {
void delete_package(libdnf5::rpm::VersionlockConfig & vl_config, std::string_view spec) {
auto remove_predicate = [spec](libdnf5::rpm::VersionlockPackage & pkg) {
if (pkg.is_valid() && pkg.get_name() == spec) {
std::cout << _("Deleting versionlock entry:") << std::endl;
std::cout << pkg.to_string(false, true) << std::endl;
changed = true;
return true;
}
return false;
};

auto & vl_packages = vl_config.get_packages();
vl_packages.erase(std::remove_if(vl_packages.begin(), vl_packages.end(), remove_predicate), vl_packages.end());
return changed;
}

void VersionlockDeleteCommand::run() {
auto & ctx = get_context();
auto package_sack = ctx.base.get_rpm_package_sack();
auto vl_config = package_sack->get_versionlock_config();
auto orig_size = vl_config.get_packages().size();

bool changed{false};
for (const auto & spec : pkg_specs) {
changed = changed || delete_package(vl_config, spec);
delete_package(vl_config, spec);
}
if (changed) {
if (vl_config.get_packages().size() != orig_size) {
vl_config.save();
}
}
Expand Down
27 changes: 9 additions & 18 deletions dnf5/commands/versionlock/versionlock_exclude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,27 @@ bool exclude_versions(
if (!vl_package.is_valid() || vl_package.get_name() != name) {
continue;
}
bool conditions_break = false;
auto vl_conditions = vl_package.get_conditions();
for (const auto & vl_cond : vl_conditions) {
if (vl_cond.get_key() != libdnf5::rpm::VersionlockCondition::Keys::EVR ||
vl_cond.get_comparator() != libdnf5::sack::QueryCmp::NEQ) {
conditions_break = true;
break;
}
}
if (conditions_break) {
if (std::any_of(vl_conditions.begin(), vl_conditions.end(), [](const auto & vl_cond) {
return vl_cond.get_key() != libdnf5::rpm::VersionlockCondition::Keys::EVR ||
vl_cond.get_comparator() != libdnf5::sack::QueryCmp::NEQ;
})) {
continue;
}
// add missing versions and return
bool changed = false;
for (const auto & pkg : packages) {
bool pkg_found = false;
const auto evr = pkg.get_evr();
for (const auto & vl_cond : vl_conditions) {
if (vl_cond.get_value() == evr) {
pkg_found = true;
break;
}
}
if (pkg_found) {
if (std::any_of(vl_conditions.begin(), vl_conditions.end(), [&evr](const auto & vl_cond) {
return vl_cond.get_value() == evr;
})) {
// condition for this evr is already present
continue;
}
changed = true;
vl_package.add_condition(libdnf5::rpm::VersionlockCondition{"evr", "!=", evr});
std::cout << libdnf5::utils::sformat(_("Adding versionlock exclude on \"{0} = {1}\"."), name, evr)
<< std::endl;
changed = true;
}
return changed;
}
Expand Down

0 comments on commit eeeedfb

Please sign in to comment.