| 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | /* |
| 6 | The digraph command performs queries over unlabelled directed graphs |
| 7 | represented in text form. It is intended to integrate nicely with |
| 8 | typical UNIX command pipelines. |
| 9 | |
| 10 | Usage: |
| 11 | |
| 12 | your-application | digraph [command] |
| 13 | |
| 14 | The supported commands are: |
| 15 | |
| 16 | nodes |
| 17 | the set of all nodes |
| 18 | degree |
| 19 | the in-degree and out-degree of each node |
| 20 | transpose |
| 21 | the reverse of the input edges |
| 22 | preds <node> ... |
| 23 | the set of immediate predecessors of the specified nodes |
| 24 | succs <node> ... |
| 25 | the set of immediate successors of the specified nodes |
| 26 | forward <node> ... |
| 27 | the set of nodes transitively reachable from the specified nodes |
| 28 | reverse <node> ... |
| 29 | the set of nodes that transitively reach the specified nodes |
| 30 | somepath <node> <node> |
| 31 | the list of edges on some arbitrary path from the first node to the second |
| 32 | allpaths <node> <node> |
| 33 | the set of edges on all paths from the first node to the second |
| 34 | sccs |
| 35 | all strongly connected components (one per line) |
| 36 | scc <node> |
| 37 | the set of nodes strongly connected to the specified one |
| 38 | focus <node> |
| 39 | the subgraph containing all directed paths that pass through the specified node |
| 40 | to dot |
| 41 | print the graph in Graphviz dot format (other formats may be supported in the future) |
| 42 | |
| 43 | Input format: |
| 44 | |
| 45 | Each line contains zero or more words. Words are separated by unquoted |
| 46 | whitespace; words may contain Go-style double-quoted portions, allowing spaces |
| 47 | and other characters to be expressed. |
| 48 | |
| 49 | Each word declares a node, and if there are more than one, an edge from the |
| 50 | first to each subsequent one. The graph is provided on the standard input. |
| 51 | |
| 52 | For instance, the following (acyclic) graph specifies a partial order among the |
| 53 | subtasks of getting dressed: |
| 54 | |
| 55 | $ cat clothes.txt |
| 56 | socks shoes |
| 57 | "boxer shorts" pants |
| 58 | pants belt shoes |
| 59 | shirt tie sweater |
| 60 | sweater jacket |
| 61 | hat |
| 62 | |
| 63 | The line "shirt tie sweater" indicates the two edges shirt -> tie and |
| 64 | shirt -> sweater, not shirt -> tie -> sweater. |
| 65 | |
| 66 | Example usage: |
| 67 | |
| 68 | Show which clothes (see above) must be donned before a jacket: |
| 69 | |
| 70 | $ digraph reverse jacket |
| 71 | |
| 72 | Many tools can be persuaded to produce output in digraph format, |
| 73 | as in the following examples. |
| 74 | |
| 75 | Using an import graph produced by go list, show a path that indicates |
| 76 | why the gopls application depends on the cmp package: |
| 77 | |
| 78 | $ go list -f '{{.ImportPath}} {{join .Imports " "}}' -deps golang.org/x/tools/gopls | |
| 79 | digraph somepath golang.org/x/tools/gopls github.com/google/go-cmp/cmp |
| 80 | |
| 81 | Show which packages in x/tools depend, perhaps indirectly, on the callgraph package: |
| 82 | |
| 83 | $ go list -f '{{.ImportPath}} {{join .Imports " "}}' -deps golang.org/x/tools/... | |
| 84 | digraph reverse golang.org/x/tools/go/callgraph |
| 85 | |
| 86 | Visualize the package dependency graph of the current package: |
| 87 | |
| 88 | $ go list -f '{{.ImportPath}} {{join .Imports " "}}' -deps | |
| 89 | digraph to dot | dot -Tpng -o x.png |
| 90 | |
| 91 | Using a module graph produced by go mod, show all dependencies of the current module: |
| 92 | |
| 93 | $ go mod graph | digraph forward $(go list -m) |
| 94 | */ |
| 95 | package main |