2017-09-10 20:36:59 +00:00
|
|
|
#include "command.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "progress-bar.hh"
|
|
|
|
#include "fs-accessor.hh"
|
2018-07-03 10:49:12 +00:00
|
|
|
#include "shared.hh"
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
#include <queue>
|
|
|
|
|
2017-09-10 20:36:59 +00:00
|
|
|
using namespace nix;
|
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
static std::string hilite(const std::string & s, size_t pos, size_t len,
|
|
|
|
const std::string & colour = ANSI_RED)
|
2017-09-10 20:36:59 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
std::string(s, 0, pos)
|
2017-09-11 14:21:27 +00:00
|
|
|
+ colour
|
2017-09-10 20:36:59 +00:00
|
|
|
+ std::string(s, pos, len)
|
|
|
|
+ ANSI_NORMAL
|
|
|
|
+ std::string(s, pos + len);
|
|
|
|
}
|
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
static std::string filterPrintable(const std::string & s)
|
|
|
|
{
|
|
|
|
std::string res;
|
|
|
|
for (char c : s)
|
|
|
|
res += isprint(c) ? c : '.';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-09-10 20:36:59 +00:00
|
|
|
struct CmdWhyDepends : SourceExprCommand
|
|
|
|
{
|
|
|
|
std::string _package, _dependency;
|
2017-09-11 14:21:27 +00:00
|
|
|
bool all = false;
|
2017-09-10 20:36:59 +00:00
|
|
|
|
|
|
|
CmdWhyDepends()
|
|
|
|
{
|
|
|
|
expectArg("package", &_package);
|
|
|
|
expectArg("dependency", &_dependency);
|
2017-09-11 14:21:27 +00:00
|
|
|
|
2020-05-04 20:40:19 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "all",
|
|
|
|
.shortName = 'a',
|
|
|
|
.description = "show all edges in the dependency graph leading from 'package' to 'dependency', rather than just a shortest path",
|
|
|
|
.handler = {&all, true},
|
|
|
|
});
|
2017-09-10 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "show why a package has another package in its closure";
|
|
|
|
}
|
|
|
|
|
|
|
|
Examples examples() override
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
Example{
|
2017-09-14 09:17:32 +00:00
|
|
|
"To show one path through the dependency graph leading from Hello to Glibc:",
|
2017-09-10 20:36:59 +00:00
|
|
|
"nix why-depends nixpkgs.hello nixpkgs.glibc"
|
|
|
|
},
|
2017-09-14 09:17:32 +00:00
|
|
|
Example{
|
|
|
|
"To show all files and paths in the dependency graph leading from Thunderbird to libX11:",
|
|
|
|
"nix why-depends --all nixpkgs.thunderbird nixpkgs.xorg.libX11"
|
|
|
|
},
|
|
|
|
Example{
|
|
|
|
"To show why Glibc depends on itself:",
|
|
|
|
"nix why-depends nixpkgs.glibc nixpkgs.glibc"
|
|
|
|
},
|
2017-09-10 20:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:18:23 +00:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2017-09-10 20:36:59 +00:00
|
|
|
void run(ref<Store> store) override
|
|
|
|
{
|
2019-04-08 14:11:17 +00:00
|
|
|
auto package = parseInstallable(store, _package);
|
2020-07-15 18:22:52 +00:00
|
|
|
auto packagePath = toStorePath(store, Realise::Outputs, operateOn, package);
|
2019-04-08 14:11:17 +00:00
|
|
|
auto dependency = parseInstallable(store, _dependency);
|
2020-07-15 18:22:52 +00:00
|
|
|
auto dependencyPath = toStorePath(store, Realise::Derivation, operateOn, dependency);
|
2020-06-16 12:16:39 +00:00
|
|
|
auto dependencyPathHash = dependencyPath.hashPart();
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePathSet closure;
|
2017-09-10 20:36:59 +00:00
|
|
|
store->computeFSClosure({packagePath}, closure, false, false);
|
|
|
|
|
|
|
|
if (!closure.count(dependencyPath)) {
|
2020-07-15 18:09:50 +00:00
|
|
|
printError("'%s' does not depend on '%s'",
|
|
|
|
store->printStorePath(packagePath),
|
|
|
|
store->printStorePath(dependencyPath));
|
2017-09-10 20:36:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopProgressBar(); // FIXME
|
|
|
|
|
|
|
|
auto accessor = store->getFSAccessor();
|
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
auto const inf = std::numeric_limits<size_t>::max();
|
|
|
|
|
|
|
|
struct Node
|
|
|
|
{
|
2019-12-05 18:11:09 +00:00
|
|
|
StorePath path;
|
|
|
|
StorePathSet refs;
|
|
|
|
StorePathSet rrefs;
|
2017-09-11 14:21:27 +00:00
|
|
|
size_t dist = inf;
|
|
|
|
Node * prev = nullptr;
|
|
|
|
bool queued = false;
|
|
|
|
bool visited = false;
|
|
|
|
};
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
std::map<StorePath, Node> graph;
|
2017-09-11 14:21:27 +00:00
|
|
|
|
|
|
|
for (auto & path : closure)
|
2020-06-16 20:20:18 +00:00
|
|
|
graph.emplace(path, Node { .path = path, .refs = store->queryPathInfo(path)->references });
|
2017-09-11 14:21:27 +00:00
|
|
|
|
|
|
|
// Transpose the graph.
|
|
|
|
for (auto & node : graph)
|
|
|
|
for (auto & ref : node.second.refs)
|
2020-06-16 20:20:18 +00:00
|
|
|
graph.find(ref)->second.rrefs.insert(node.first);
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
/* Run Dijkstra's shortest path algorithm to get the distance
|
|
|
|
of every path in the closure to 'dependency'. */
|
2020-06-16 20:20:18 +00:00
|
|
|
graph.emplace(dependencyPath, Node { .path = dependencyPath, .dist = 0 });
|
2017-09-11 14:21:27 +00:00
|
|
|
|
|
|
|
std::priority_queue<Node *> queue;
|
|
|
|
|
|
|
|
queue.push(&graph.at(dependencyPath));
|
|
|
|
|
|
|
|
while (!queue.empty()) {
|
|
|
|
auto & node = *queue.top();
|
|
|
|
queue.pop();
|
|
|
|
|
|
|
|
for (auto & rref : node.rrefs) {
|
|
|
|
auto & node2 = graph.at(rref);
|
|
|
|
auto dist = node.dist + 1;
|
|
|
|
if (dist < node2.dist) {
|
|
|
|
node2.dist = dist;
|
|
|
|
node2.prev = &node;
|
|
|
|
if (!node2.queued) {
|
|
|
|
node2.queued = true;
|
|
|
|
queue.push(&node2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
/* Print the subgraph of nodes that have 'dependency' in their
|
|
|
|
closure (i.e., that have a non-infinite distance to
|
|
|
|
'dependency'). Print every edge on a path between `package`
|
|
|
|
and `dependency`. */
|
|
|
|
std::function<void(Node &, const string &, const string &)> printNode;
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
struct BailOut { };
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
printNode = [&](Node & node, const string & firstPad, const string & tailPad) {
|
2019-12-05 18:11:09 +00:00
|
|
|
auto pathS = store->printStorePath(node.path);
|
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
assert(node.dist != inf);
|
2020-04-16 11:46:37 +00:00
|
|
|
logger->stdout("%s%s%s%s" ANSI_NORMAL,
|
2017-09-11 14:21:27 +00:00
|
|
|
firstPad,
|
|
|
|
node.visited ? "\e[38;5;244m" : "",
|
2020-01-31 13:06:26 +00:00
|
|
|
firstPad != "" ? "→ " : "",
|
2019-12-05 18:11:09 +00:00
|
|
|
pathS);
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-14 09:17:32 +00:00
|
|
|
if (node.path == dependencyPath && !all
|
|
|
|
&& packagePath != dependencyPath)
|
|
|
|
throw BailOut();
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
if (node.visited) return;
|
|
|
|
node.visited = true;
|
2017-09-10 20:36:59 +00:00
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
/* Sort the references by distance to `dependency` to
|
|
|
|
ensure that the shortest path is printed first. */
|
|
|
|
std::multimap<size_t, Node *> refs;
|
|
|
|
std::set<std::string> hashes;
|
|
|
|
|
|
|
|
for (auto & ref : node.refs) {
|
2017-09-14 09:17:32 +00:00
|
|
|
if (ref == node.path && packagePath != dependencyPath) continue;
|
2017-09-11 14:21:27 +00:00
|
|
|
auto & node2 = graph.at(ref);
|
|
|
|
if (node2.dist == inf) continue;
|
|
|
|
refs.emplace(node2.dist, &node2);
|
2020-06-16 12:16:39 +00:00
|
|
|
hashes.insert(std::string(node2.path.hashPart()));
|
2017-09-11 14:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For each reference, find the files and symlinks that
|
|
|
|
contain the reference. */
|
|
|
|
std::map<std::string, Strings> hits;
|
|
|
|
|
|
|
|
std::function<void(const Path &)> visitPath;
|
|
|
|
|
|
|
|
visitPath = [&](const Path & p) {
|
2017-09-10 20:36:59 +00:00
|
|
|
auto st = accessor->stat(p);
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto p2 = p == pathS ? "/" : std::string(p, pathS.size() + 1);
|
2017-09-11 14:21:27 +00:00
|
|
|
|
|
|
|
auto getColour = [&](const std::string & hash) {
|
|
|
|
return hash == dependencyPathHash ? ANSI_GREEN : ANSI_BLUE;
|
|
|
|
};
|
2017-09-10 20:36:59 +00:00
|
|
|
|
|
|
|
if (st.type == FSAccessor::Type::tDirectory) {
|
|
|
|
auto names = accessor->readDirectory(p);
|
|
|
|
for (auto & name : names)
|
2017-09-11 14:21:27 +00:00
|
|
|
visitPath(p + "/" + name);
|
2017-09-10 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (st.type == FSAccessor::Type::tRegular) {
|
|
|
|
auto contents = accessor->readFile(p);
|
|
|
|
|
2017-09-11 14:21:27 +00:00
|
|
|
for (auto & hash : hashes) {
|
|
|
|
auto pos = contents.find(hash);
|
|
|
|
if (pos != std::string::npos) {
|
2017-09-25 14:59:16 +00:00
|
|
|
size_t margin = 32;
|
2017-09-11 14:21:27 +00:00
|
|
|
auto pos2 = pos >= margin ? pos - margin : 0;
|
|
|
|
hits[hash].emplace_back(fmt("%s: …%s…\n",
|
|
|
|
p2,
|
|
|
|
hilite(filterPrintable(
|
|
|
|
std::string(contents, pos2, pos - pos2 + hash.size() + margin)),
|
2020-06-16 12:16:39 +00:00
|
|
|
pos - pos2, StorePath::HashLen,
|
2017-09-11 14:21:27 +00:00
|
|
|
getColour(hash))));
|
|
|
|
}
|
2017-09-10 20:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (st.type == FSAccessor::Type::tSymlink) {
|
|
|
|
auto target = accessor->readLink(p);
|
2017-09-11 14:21:27 +00:00
|
|
|
|
|
|
|
for (auto & hash : hashes) {
|
|
|
|
auto pos = target.find(hash);
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
hits[hash].emplace_back(fmt("%s -> %s\n", p2,
|
2020-06-16 12:16:39 +00:00
|
|
|
hilite(target, pos, StorePath::HashLen, getColour(hash))));
|
2017-09-10 20:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-14 11:22:32 +00:00
|
|
|
// FIXME: should use scanForReferences().
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
visitPath(pathS);
|
2017-09-11 14:21:27 +00:00
|
|
|
|
2018-07-03 10:49:12 +00:00
|
|
|
RunPager pager;
|
2017-09-11 14:21:27 +00:00
|
|
|
for (auto & ref : refs) {
|
2020-06-16 12:16:39 +00:00
|
|
|
std::string hash(ref.second->path.hashPart());
|
2017-09-11 14:21:27 +00:00
|
|
|
|
|
|
|
bool last = all ? ref == *refs.rbegin() : true;
|
|
|
|
|
|
|
|
for (auto & hit : hits[hash]) {
|
|
|
|
bool first = hit == *hits[hash].begin();
|
2017-09-25 14:59:16 +00:00
|
|
|
std::cout << tailPad
|
2017-09-11 14:21:27 +00:00
|
|
|
<< (first ? (last ? treeLast : treeConn) : (last ? treeNull : treeLine))
|
|
|
|
<< hit;
|
|
|
|
if (!all) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printNode(*ref.second,
|
|
|
|
tailPad + (last ? treeNull : treeLine),
|
|
|
|
tailPad + (last ? treeNull : treeLine));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
printNode(graph.at(packagePath), "", "");
|
|
|
|
} catch (BailOut & ) { }
|
2017-09-10 20:36:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 14:01:35 +00:00
|
|
|
static auto r1 = registerCommand<CmdWhyDepends>("why-depends");
|