summaryrefslogtreecommitdiff
path: root/cmdline/apt.cc
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2025-05-19 14:17:23 +0200
committerJulian Andres Klode <jak@debian.org>2025-05-19 15:20:09 +0000
commit7724d170751b3dc1717ba7a08881437cf7151996 (patch)
tree9f76923fbd5b856904a414fa615116940d2afff6 /cmdline/apt.cc
parent2ddc16b9f8edff1329f5ac3e46b0c0cf9bde661d (diff)
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).
Diffstat (limited to 'cmdline/apt.cc')
-rw-r--r--cmdline/apt.cc17
1 files changed, 17 insertions, 0 deletions
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 <apt-pkg/error.h>
#include <apt-pkg/init.h>
#include <apt-pkg/pkgsystem.h>
+#include <apt-pkg/solver3.h>
#include <apt-pkg/strutl.h>
#include <apt-private/private-cmndline.h>
@@ -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<aptDispatchWithHelp> GetCommands() /*{{{*/
{
// advanced commands are left undocumented on purpose
@@ -81,6 +96,8 @@ static std::vector<aptDispatchWithHelp> 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},