Skip to content

Commit

Permalink
[dnf5] Implement new argument "--show-new-leaves"
Browse files Browse the repository at this point in the history
Shows newly installed leaf packages and packages that became leaves
after a transaction.
  • Loading branch information
jrohel committed Oct 17, 2023
1 parent 18eeb39 commit c5436d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dnf5/include/dnf5/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class Context : public libdnf5::cli::session::Session {

bool get_dump_variables() const { return dump_variables; }

/// Set to true to show newly installed leaf packages and packages that became leaves after a transaction.
void set_show_new_leaves(bool show_new_leaves) { this->show_new_leaves = show_new_leaves; }

bool get_show_new_leaves() const { return show_new_leaves; }

Plugins & get_plugins() { return *plugins; }

libdnf5::Goal * get_goal(bool new_if_not_exist = true);
Expand Down Expand Up @@ -145,6 +150,7 @@ class Context : public libdnf5::cli::session::Session {
bool dump_main_config{false};
std::vector<std::string> dump_repo_config_id_list;
bool dump_variables{false};
bool show_new_leaves{false};

std::unique_ptr<Plugins> plugins;
std::unique_ptr<libdnf5::Goal> goal;
Expand Down
61 changes: 61 additions & 0 deletions dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <libdnf5/logger/global_logger.hpp>
#include <libdnf5/logger/memory_buffer_logger.hpp>
#include <libdnf5/repo/repo_cache.hpp>
#include <libdnf5/rpm/package_query.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>
#include <libdnf5/version.hpp>
#include <string.h>
Expand Down Expand Up @@ -498,6 +499,21 @@ void RootCommand::set_argument_parser() {
});
global_options_group->register_argument(releasever);

{
auto show_new_leaves = parser.add_new_named_arg("show-new-leaves");
show_new_leaves->set_long_name("show-new-leaves");
show_new_leaves->set_description(
"Show newly installed leaf packages and packages that became leaves after a transaction.");
show_new_leaves->set_parse_hook_func([&ctx](
[[maybe_unused]] ArgumentParser::NamedArg * arg,
[[maybe_unused]] const char * option,
[[maybe_unused]] const char * value) {
ctx.set_show_new_leaves(true);
return true;
});
global_options_group->register_argument(show_new_leaves);
}

{
auto debug_solver = parser.add_new_named_arg("debug_solver");
debug_solver->set_long_name("debugsolver");
Expand Down Expand Up @@ -825,6 +841,47 @@ static void dump_variables(Context & context) {
}
}

static void print_new_leaves(Context & context) {
libdnf5::rpm::PackageQuery pkg_query(context.base);
pkg_query.filter_installed();
libdnf5::rpm::PackageQuery pre_trans_leaves_query(pkg_query);
pre_trans_leaves_query.filter_leaves();

// Calculate the new system state (list of installed packages) after a successful transaction.
// Current state plus inbound minus outbound packages.
for (const auto & trans_pkg : context.get_transaction()->get_transaction_packages()) {
if (libdnf5::transaction::transaction_item_action_is_inbound(trans_pkg.get_action())) {
pkg_query.add(trans_pkg.get_package());
} else if (libdnf5::transaction::transaction_item_action_is_outbound(trans_pkg.get_action())) {
pkg_query.remove(trans_pkg.get_package());
}
}

pkg_query.filter_leaves();
pkg_query.difference(pre_trans_leaves_query);

std::set<std::string> pre_trans_leaves_na;
std::set<std::string> new_leaves_na;
for (const auto & package : pre_trans_leaves_query) {
pre_trans_leaves_na.insert(package.get_na());
}
for (const auto & package : pkg_query) {
auto na = package.get_na();
// Filters out new leaves packages with the same name and architecture as previously existing leaves.
if (!pre_trans_leaves_na.contains(na)) {
new_leaves_na.insert(package.get_na());
}
}

if (!new_leaves_na.empty()) {
std::cout << "New leaves:" << std::endl;
for (const auto & leaf_pkg : new_leaves_na) {
std::cout << " " << leaf_pkg << std::endl;
}
std::cout << std::endl;
}
}

} // namespace dnf5


Expand Down Expand Up @@ -992,6 +1049,10 @@ int main(int argc, char * argv[]) try {
return static_cast<int>(libdnf5::cli::ExitCode::SUCCESS);
}

if (context.get_show_new_leaves()) {
dnf5::print_new_leaves(context);
}

dnf5::print_transaction_size_stats(context);

if (base.get_config().get_downloadonly_option().get_value()) {
Expand Down
3 changes: 3 additions & 0 deletions doc/dnf5.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ Following options are applicable in the general context for any ``dnf5`` command
``--setvar=VAR_NAME=VALUE``
| Override a ``DNF5`` variable value, like ``arch``, ``releasever``, etc.
``--show-new-leaves``
| Show newly installed leaf packages and packages that became leaves after a transaction.
``--skip-broken``
| Resolve any dependency problems by removing packages that are causing problems from the transaction.
Expand Down

0 comments on commit c5436d4

Please sign in to comment.