Răsfoiți Sursa

Merge pull request #3017 from infosiftr/deps-depth

Add "--depth" flag to "bashbrew children" and "bashbrew parents"
yosifkit 8 ani în urmă
părinte
comite
0b2e3d8baf
2 a modificat fișierele cu 41 adăugiri și 12 ștergeri
  1. 34 11
      bashbrew/go/src/bashbrew/cmd-deps.go
  2. 7 1
      bashbrew/go/src/bashbrew/main.go

+ 34 - 11
bashbrew/go/src/bashbrew/cmd-deps.go

@@ -15,6 +15,11 @@ func cmdParents(c *cli.Context) error {
 	return cmdFamily(true, c)
 }
 
+type topsortDepthNodes struct {
+	depth int
+	nodes []*topsort.Node
+}
+
 func cmdFamily(parents bool, c *cli.Context) error {
 	depsRepos, err := repos(c.Bool("all"), c.Args()...)
 	if err != nil {
@@ -23,6 +28,7 @@ func cmdFamily(parents bool, c *cli.Context) error {
 
 	uniq := c.Bool("uniq")
 	applyConstraints := c.Bool("apply-constraints")
+	depth := c.Int("depth")
 
 	allRepos, err := repos(true)
 	if err != nil {
@@ -85,24 +91,41 @@ func cmdFamily(parents bool, c *cli.Context) error {
 			}
 
 			for _, tag := range r.Tags("", uniq, entry) {
-				nodes := []*topsort.Node{}
+				nodes := []topsortDepthNodes{}
 				if parents {
-					nodes = append(nodes, network.Get(tag).InboundEdges...)
+					nodes = append(nodes, topsortDepthNodes{
+						depth: 1,
+						nodes: network.Get(tag).InboundEdges,
+					})
 				} else {
-					nodes = append(nodes, network.Get(tag).OutboundEdges...)
+					nodes = append(nodes, topsortDepthNodes{
+						depth: 1,
+						nodes: network.Get(tag).OutboundEdges,
+					})
 				}
 				for len(nodes) > 0 {
-					node := nodes[0]
+					depthNodes := nodes[0]
 					nodes = nodes[1:]
-					if seen[node] {
+					if depth > 0 && depthNodes.depth > depth {
 						continue
 					}
-					seen[node] = true
-					fmt.Printf("%s\n", node.Name)
-					if parents {
-						nodes = append(nodes, node.InboundEdges...)
-					} else {
-						nodes = append(nodes, node.OutboundEdges...)
+					for _, node := range depthNodes.nodes {
+						if seen[node] {
+							continue
+						}
+						seen[node] = true
+						fmt.Printf("%s\n", node.Name)
+						if parents {
+							nodes = append(nodes, topsortDepthNodes{
+								depth: depthNodes.depth + 1,
+								nodes: node.InboundEdges,
+							})
+						} else {
+							nodes = append(nodes, topsortDepthNodes{
+								depth: depthNodes.depth + 1,
+								nodes: node.OutboundEdges,
+							})
+						}
 					}
 				}
 			}

+ 7 - 1
bashbrew/go/src/bashbrew/main.go

@@ -172,6 +172,11 @@ func main() {
 			Name:  "apply-constraints",
 			Usage: "apply Constraints as if repos were building",
 		},
+		"depth": cli.IntFlag{
+			Name:  "depth",
+			Value: 0,
+			Usage: "maximum number of levels to traverse (0 for unlimited)",
+		},
 	}
 
 	app.Commands = []cli.Command{
@@ -244,7 +249,6 @@ func main() {
 			Action: cmdPutShared,
 		},
 
-		// TODO --depth flag for children and parents
 		{
 			Name: "children",
 			Aliases: []string{
@@ -255,6 +259,7 @@ func main() {
 			Usage: `print the repos built FROM a given repo or repo:tag`,
 			Flags: []cli.Flag{
 				commonFlags["apply-constraints"],
+				commonFlags["depth"],
 			},
 			Before: subcommandBeforeFactory("children"),
 			Action: cmdOffspring,
@@ -270,6 +275,7 @@ func main() {
 			Usage: `print the repos this repo or repo:tag is FROM`,
 			Flags: []cli.Flag{
 				commonFlags["apply-constraints"],
+				commonFlags["depth"],
 			},
 			Before: subcommandBeforeFactory("parents"),
 			Action: cmdParents,