about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/globals.hh3
-rw-r--r--src/nix-help.txt1
-rw-r--r--src/nix.cc15
-rw-r--r--src/store.cc8
-rw-r--r--src/store.hh4
5 files changed, 29 insertions, 2 deletions
diff --git a/src/globals.hh b/src/globals.hh
index 910e47e01cdd..816cb4766efb 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 a51018ea1930..ceff114ae507 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 9907f5c74da6..9bbbf4ae8c85 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 69532071310f..3e755a0d1a9a 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 abf874543164..7851b1e3d874 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);