From 7724d170751b3dc1717ba7a08881437cf7151996 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 19 May 2025 14:17:23 +0200 Subject: Introduce apt why, apt why-not These are implemented somewhat differently from aptitudes why and why-not commands: They produce the actual solver trace for why a particular decision has been taken. For the why-not case, we need to explicitly discover our specified package, as if nothing else depends on it in our graph, it would otherwise always be undiscovered and conflicts not detected (see e.g. level-3 in the test). --- cmdline/apt.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'cmdline/apt.cc') diff --git a/cmdline/apt.cc b/cmdline/apt.cc index af8a95fa5..a876085ce 100644 --- a/cmdline/apt.cc +++ b/cmdline/apt.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -53,6 +54,20 @@ static bool ShowHelp(CommandLine &) /*{{{*/ return true; } /*}}}*/ + +static bool DoWhy(CommandLine &CmdL) /*{{{*/ +{ + pkgCacheFile CacheFile; + APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1); + bool const decision = strcmp(CmdL.FileList[0], "why") == 0; + if (pkgset.size() != 1) + return _error->PendingError() ? false : _error->Error("Only a single argument is supported at this time."); + if (unlikely(not CacheFile.BuildDepCache())) + return false; + for (auto pkg : pkgset) + std::cout << APT::Solver::InternalCliWhy(CacheFile, pkg, decision) << std::flush; + return not _error->PendingError(); +} static std::vector GetCommands() /*{{{*/ { // advanced commands are left undocumented on purpose @@ -81,6 +96,8 @@ static std::vector GetCommands() /*{{{*/ {"modernize-sources", &ModernizeSources, _("modernize .list files to .sources files")}, {"moo", &DoMoo, nullptr}, {"satisfy", &DoBuildDep, _("satisfy dependency strings")}, + {"why", &DoWhy, _("produce a reason trace for the current state of the package")}, + {"why-not", &DoWhy, _("produce a reason trace for the current state of the package")}, // for compat with muscle memory {"dist-upgrade", &DoDistUpgrade, nullptr}, -- cgit v1.2.3-70-g09d2