explore repository structure, with minimal assumptions about "hg log" output;
authorwenzelm
Sun, 14 May 2017 15:07:13 +0200
changeset 65819 ff3dc9325eaa
parent 65818 94cad7590015
child 65820 a5d4958d0901
explore repository structure, with minimal assumptions about "hg log" output;
src/Pure/General/mercurial.scala
--- a/src/Pure/General/mercurial.scala	Sun May 14 12:50:55 2017 +0200
+++ b/src/Pure/General/mercurial.scala	Sun May 14 15:07:13 2017 +0200
@@ -137,5 +137,26 @@
       hg.command("update",
         opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check
     }
+
+    def length(): Int =
+      identify(options = "-n") match {
+        case Value.Int(n) => n + 1
+        case s => error("Cannot determine repository length from " + quote(s))
+      }
+
+    def graph(): Graph[String, Unit] =
+    {
+      val Node = """^node: (\w{12}) (\w{12}) (\w{12})""".r
+      val log_result =
+        log(template = """node: {node|short} {p1node|short} {p2node|short}\n""",
+          options = "-l " + length())
+      (Graph.string[Unit] /: split_lines(log_result)) {
+        case (graph, Node(x, y, z)) =>
+          val deps = List(y, z).filterNot(s => s.forall(_ == '0'))
+          val graph1 = (graph /: (x :: deps))(_.default_node(_, ()))
+          (graph1 /: deps)({ case (g, dep) => g.add_edge(dep, x) })
+        case (graph, _) => graph
+      }
+    }
   }
 }