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). --- apt-pkg/solver3.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'apt-pkg/solver3.cc') diff --git a/apt-pkg/solver3.cc b/apt-pkg/solver3.cc index 620156ac3..abc0af314 100644 --- a/apt-pkg/solver3.cc +++ b/apt-pkg/solver3.cc @@ -1267,3 +1267,23 @@ bool APT::Solver::ToDepCache(pkgDepCache &depcache) const } return true; } + +// Command-line +std::string APT::Solver::InternalCliWhy(pkgDepCache &cache, pkgCache::PkgIterator pkg, bool decision) +{ + APT::Solver solver(cache.GetCache(), cache.GetPolicy(), EDSP::Request::Flags(0)); + // In case nothing has a positive dependency on pkg it may not actually be discovered in a `why-not` + // scenario, so make sure we discover it explicitly. + solver.Discover(Var(pkg)); + if (not solver.FromDepCache(cache) || not solver.Solve()) + return ""; + std::unordered_set seen; + std::string buf; + if (solver[Var(pkg)].decision == Decision::NONE) + return strprintf(buf, "%s is undecided\n", pkg.FullName().c_str()), buf; + if (decision && solver[Var(pkg)].decision != Decision::MUST) + return strprintf(buf, "%s is not actually marked for install\n", pkg.FullName().c_str()), buf; + if (not decision && solver[Var(pkg)].decision == Decision::MUST) + return strprintf(buf, "%s is actually marked for install\n", pkg.FullName().c_str()), buf; + return solver.LongWhyStr(Var(pkg), decision, solver[Var(pkg)].reason, "", seen); +} -- cgit v1.2.3-70-g09d2