From 1d61e473c88568fae7ef5edebc77acd53e4126f9 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 10 Oct 2003 15:25:21 +0000 Subject: [PATCH] * New query `nix --query --predecessors' to print the predecessors of a Nix expression. --- src/globals.hh | 3 ++- src/nix-help.txt | 1 + src/nix.cc | 15 ++++++++++++++- src/store.cc | 8 ++++++++ src/store.hh | 4 ++++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/globals.hh b/src/globals.hh index 910e47e01..816cb4766 100644 --- a/src/globals.hh +++ b/src/globals.hh @@ -35,7 +35,8 @@ extern TableId dbSuccessors; /* dbSuccessorsRev :: Path -> [Path] - The reverse mapping of dbSuccessors. + The reverse mapping of dbSuccessors (i.e., it stores the + predecessors of a Nix expression). */ extern TableId dbSuccessorsRev; diff --git a/src/nix-help.txt b/src/nix-help.txt index a51018ea1..ceff114ae 100644 --- a/src/nix-help.txt +++ b/src/nix-help.txt @@ -24,6 +24,7 @@ Query flags: --list / -l: query the output paths (roots) of a Nix expression (default) --requisites / -r: print all paths necessary to realise expression --generators / -g: find expressions producing a subset of given ids + --predecessors: print predecessors of a Nix expression --graph: print a dot graph rooted at given ids Options: diff --git a/src/nix.cc b/src/nix.cc index 9907f5c74..9bbbf4ae8 100644 --- a/src/nix.cc +++ b/src/nix.cc @@ -73,7 +73,7 @@ Path maybeNormalise(const Path & ne, bool normalise) /* Perform various sorts of queries. */ static void opQuery(Strings opFlags, Strings opArgs) { - enum { qList, qRequisites, qGenerators, qGraph + enum { qList, qRequisites, qGenerators, qPredecessors, qGraph } query = qList; bool normalise = false; bool includeExprs = true; @@ -84,6 +84,7 @@ static void opQuery(Strings opFlags, Strings opArgs) if (*i == "--list" || *i == "-l") query = qList; else if (*i == "--requisites" || *i == "-r") query = qRequisites; else if (*i == "--generators" || *i == "-g") query = qGenerators; + else if (*i == "--predecessors") query = qPredecessors; else if (*i == "--graph") query = qGraph; else if (*i == "--normalise" || *i == "-n") normalise = true; else if (*i == "--exclude-exprs") includeExprs = false; @@ -139,6 +140,18 @@ static void opQuery(Strings opFlags, Strings opArgs) } #endif + case qPredecessors: { + for (Strings::iterator i = opArgs.begin(); + i != opArgs.end(); i++) + { + Paths preds = queryPredecessors(checkPath(*i)); + for (Paths::iterator j = preds.begin(); + j != preds.end(); j++) + cout << format("%s\n") % *j; + } + break; + } + case qGraph: { PathSet roots; for (Strings::iterator i = opArgs.begin(); diff --git a/src/store.cc b/src/store.cc index 695320713..3e755a0d1 100644 --- a/src/store.cc +++ b/src/store.cc @@ -104,6 +104,14 @@ void registerSuccessor(const Transaction & txn, } +Paths queryPredecessors(const Path & sucPath) +{ + Paths revs; + nixDB.queryStrings(noTxn, dbSuccessorsRev, sucPath, revs); + return revs; +} + + void registerSubstitute(const Path & srcPath, const Path & subPath) { Transaction txn(nixDB); diff --git a/src/store.hh b/src/store.hh index abf874543..7851b1e3d 100644 --- a/src/store.hh +++ b/src/store.hh @@ -22,6 +22,10 @@ void copyPath(const Path & src, const Path & dst); void registerSuccessor(const Transaction & txn, const Path & srcPath, const Path & sucPath); +/* Return the predecessors of the Nix expression stored at the given + path. */ +Paths queryPredecessors(const Path & sucPath); + /* Register a substitute. */ void registerSubstitute(const Path & srcPath, const Path & subPath);