mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 23:14:43 +00:00
Add new tree implementation and remove treeprint dependency
This commit is contained in:
49
pkg/list.go
49
pkg/list.go
@@ -4,14 +4,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/xlab/treeprint"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/karrick/godirwalk"
|
||||
)
|
||||
@@ -108,48 +105,8 @@ func OpenAll(paths []string) ([]*Repo, error) {
|
||||
func PrintRepos(repos []*Repo) {
|
||||
root := viper.GetString(KeyReposRoot)
|
||||
|
||||
seg := make([][]string, len(repos))
|
||||
|
||||
t := treeprint.New()
|
||||
t.SetValue(root)
|
||||
|
||||
for i, repo := range repos {
|
||||
path := strings.TrimPrefix(repo.path, root)
|
||||
path = strings.Trim(path, string(filepath.Separator))
|
||||
subpaths := strings.Split(path, string(filepath.Separator))
|
||||
|
||||
seg[i] = make([]string, len(subpaths))
|
||||
|
||||
node := t
|
||||
for j, sub := range subpaths {
|
||||
seg[i][j] = sub
|
||||
|
||||
if i > 0 && seg[i][j] == seg[i-1][j] {
|
||||
node = node.FindLastNode()
|
||||
continue
|
||||
}
|
||||
|
||||
value := seg[i][j]
|
||||
|
||||
// if this is the last segment, it means that's the name of the repository and we need to print its status
|
||||
if j == len(seg[i])-1 {
|
||||
value = value + " " + renderWorktreeStatus(repo)
|
||||
|
||||
}
|
||||
|
||||
node = node.AddBranch(value)
|
||||
if j == len(seg[i])-1 {
|
||||
for _, branch := range repo.Status.Branches {
|
||||
if branch.Name != repo.Status.CurrentBranch {
|
||||
node.AddNode(renderBranchStatus(branch))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(t.String())
|
||||
tree := BuildTree(root, repos)
|
||||
fmt.Println(RenderTree(tree))
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
137
pkg/tree.go
Normal file
137
pkg/tree.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Node represents a node in a repos tree
|
||||
type Node struct {
|
||||
val string
|
||||
depth int // depth is a nesting depth used when rendering a tree, not an depth level of a node inside the tree
|
||||
parent *Node
|
||||
children []*Node
|
||||
repo *Repo
|
||||
}
|
||||
|
||||
// Root creates a new root of a tree
|
||||
func Root(val string) *Node {
|
||||
root := &Node{
|
||||
val: val,
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
// Add adds a child node
|
||||
func (n *Node) Add(val string) *Node {
|
||||
if n.children == nil {
|
||||
n.children = make([]*Node, 0)
|
||||
}
|
||||
|
||||
child := &Node{
|
||||
val: val,
|
||||
parent: n,
|
||||
}
|
||||
n.children = append(n.children, child)
|
||||
return child
|
||||
}
|
||||
|
||||
// GetChild finds a node with val inside this node's children (only 1 level deep).
|
||||
// Returns pointer to found child or nil if node doesn't have any children or doesn't have a child with sought value.
|
||||
func (n *Node) GetChild(val string) *Node {
|
||||
if n.children == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, child := range n.children {
|
||||
if child.val == val {
|
||||
return child
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildTree builds a directory tree of paths to repositories.
|
||||
// Each node represents a directory in the repo path.
|
||||
// Each leaf (final node) contains a pointer to the repo.
|
||||
func BuildTree(root string, repos []*Repo) *Node {
|
||||
tree := Root(root)
|
||||
|
||||
for _, repo := range repos {
|
||||
path := strings.TrimPrefix(repo.path, root)
|
||||
path = strings.Trim(path, string(filepath.Separator))
|
||||
subs := strings.Split(path, string(filepath.Separator))
|
||||
|
||||
// For each path fragment, start at the root of the tree
|
||||
// and check if the fragment exist among the children of the node.
|
||||
// If not, add it to node's children and move to next fragment.
|
||||
// If it does, just move to the next fragment.
|
||||
node := tree
|
||||
for i, sub := range subs {
|
||||
child := node.GetChild(sub)
|
||||
if child == nil {
|
||||
node = node.Add(sub)
|
||||
|
||||
// If that's the last fragment, it's a tree leaf and needs a *Repo attached.
|
||||
if i == len(subs)-1 {
|
||||
node.repo = repo
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
node = child
|
||||
}
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
// RenderTree returns a string representation of repos tree.
|
||||
// It recursively traverses the tree and prints its nodes.
|
||||
// If a node contains multiple children, they are be printed in new lines and indented.
|
||||
// If a node contains only a single child, it is printed in the same line using path separator.
|
||||
// For better readability the first level (repos hosts) is not indented.
|
||||
//
|
||||
// Example:
|
||||
// Following paths:
|
||||
// /repos/github.com/user/repo1
|
||||
// /repos/github.com/user/repo2
|
||||
// /repos/github.com/another/repo
|
||||
//
|
||||
// will render a tree:
|
||||
// /repos/
|
||||
// github.com/
|
||||
// user/
|
||||
// repo1
|
||||
// repo2
|
||||
// another/repo
|
||||
//
|
||||
func RenderTree(node *Node) string {
|
||||
if node.children == nil {
|
||||
// If node is a leaf, print repo name and its status and finish processing this node.
|
||||
return node.val + " " + renderWorktreeStatus(node.repo)
|
||||
}
|
||||
|
||||
shift := ""
|
||||
if node.parent == nil {
|
||||
// If node is a root, print its children on a new line without indentation.
|
||||
shift = "\n"
|
||||
} else if len(node.children) == 1 {
|
||||
// If node has only a single child, print it on the same line as its parent.
|
||||
// Setting node's depth to the same as parent's ensures that its children will be indented only once even if
|
||||
// node's path has multiple levels above.
|
||||
node.depth = node.parent.depth
|
||||
} else {
|
||||
// If node has multiple children, print each of them on a new line
|
||||
// and indent them once relative to the parent
|
||||
node.depth = node.parent.depth + 1
|
||||
shift = "\n" + strings.Repeat("\t", node.depth)
|
||||
}
|
||||
|
||||
val := node.val + string(filepath.Separator)
|
||||
for _, child := range node.children {
|
||||
val += shift + RenderTree(child)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
178
pkg/tree_test.go
178
pkg/tree_test.go
@@ -2,100 +2,104 @@ package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var paths = []string{
|
||||
|
||||
// "/home/grdl/repositories/gitlab.com/grdl/testflux",
|
||||
"/home/grdl/repositories/bitbucket.org/gridarrow/istio",
|
||||
"/home/grdl/repositories/bitbucket.org/grdl/bob",
|
||||
"/home/grdl/repositories/github.com/fboender/multi-git-status",
|
||||
"/home/grdl/repositories/github.com/grdl/git-get",
|
||||
"/home/grdl/repositories/github.com/grdl/testflux",
|
||||
"/home/grdl/repositories/github.com/johanhaleby/kubetail",
|
||||
"/home/grdl/repositories/gitlab.com/grdl/git-get",
|
||||
"/home/grdl/repositories/gitlab.com/grdl/grafana-dashboard-builder",
|
||||
"/home/grdl/repositories/gitlab.com/grdl/dotfiles",
|
||||
}
|
||||
|
||||
func TestTree(t *testing.T) {
|
||||
InitConfig()
|
||||
root := viper.GetString(KeyReposRoot)
|
||||
var tests = []struct {
|
||||
paths []string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
[]string{
|
||||
"root/github.com/grdl/repo1",
|
||||
}, `
|
||||
root/
|
||||
github.com/grdl/repo1
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"root/github.com/grdl/repo1",
|
||||
"root/github.com/grdl/repo2",
|
||||
}, `
|
||||
root/
|
||||
github.com/grdl/
|
||||
repo1
|
||||
repo2
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"root/gitlab.com/grdl/repo1",
|
||||
"root/github.com/grdl/repo1",
|
||||
}, `
|
||||
root/
|
||||
gitlab.com/grdl/repo1
|
||||
github.com/grdl/repo1
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"root/gitlab.com/grdl/repo1",
|
||||
"root/gitlab.com/grdl/repo2",
|
||||
"root/gitlab.com/other/repo1",
|
||||
"root/github.com/grdl/repo1",
|
||||
"root/github.com/grdl/nested/repo2",
|
||||
}, `
|
||||
root/
|
||||
gitlab.com/
|
||||
grdl/
|
||||
repo1
|
||||
repo2
|
||||
other/repo1
|
||||
github.com/grdl/
|
||||
repo1
|
||||
nested/repo2
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"root/gitlab.com/grdl/nested/repo1",
|
||||
"root/gitlab.com/grdl/nested/repo2",
|
||||
"root/gitlab.com/other/repo1",
|
||||
}, `
|
||||
root/
|
||||
gitlab.com/
|
||||
grdl/nested/
|
||||
repo1
|
||||
repo2
|
||||
other/repo1
|
||||
`,
|
||||
},
|
||||
{
|
||||
[]string{
|
||||
"root/gitlab.com/grdl/double/nested/repo1",
|
||||
"root/gitlab.com/grdl/nested/repo2",
|
||||
"root/gitlab.com/other/repo1",
|
||||
}, `
|
||||
root/
|
||||
gitlab.com/
|
||||
grdl/
|
||||
double/nested/repo1
|
||||
nested/repo2
|
||||
other/repo1
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
tree := Root(root)
|
||||
for i, test := range tests {
|
||||
var repos []*Repo
|
||||
for _, path := range test.paths {
|
||||
repos = append(repos, &Repo{path: path})
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
p := strings.TrimPrefix(path, root)
|
||||
p = strings.Trim(p, string(filepath.Separator))
|
||||
subs := strings.Split(p, string(filepath.Separator))
|
||||
tree := BuildTree("root", repos)
|
||||
// Leading and trailing newlines are added to test cases for readability. We also need to add them to the rendering result.
|
||||
got := fmt.Sprintf("\n%s\n", RenderTree(tree))
|
||||
|
||||
node := tree
|
||||
for _, sub := range subs {
|
||||
child := node.GetChild(sub)
|
||||
if child == nil {
|
||||
node = node.Add(sub)
|
||||
continue
|
||||
}
|
||||
node = child
|
||||
if got != test.want {
|
||||
t.Errorf("Failed test case %d, got: %+v; want: %+v", i, got, test.want)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(tree)
|
||||
}
|
||||
|
||||
func process(node *Node, val string) *Node {
|
||||
found := node.GetChild(val)
|
||||
if found == nil {
|
||||
added := node.Add(val)
|
||||
return added
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
val string
|
||||
parent *Node
|
||||
children []*Node
|
||||
}
|
||||
|
||||
func Root(val string) *Node {
|
||||
root := &Node{
|
||||
val: val,
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
// Add adds a child node
|
||||
func (n *Node) Add(val string) *Node {
|
||||
if n.children == nil {
|
||||
n.children = make([]*Node, 0)
|
||||
}
|
||||
|
||||
new := &Node{
|
||||
val: val,
|
||||
parent: n,
|
||||
}
|
||||
n.children = append(n.children, new)
|
||||
return new
|
||||
}
|
||||
|
||||
// GetChild finds a node with val inside this node's children (only 1 level deep).
|
||||
// Returns pointer to found child or nil if node doesn't have any children or doesn't have a child with sought value.
|
||||
func (n *Node) GetChild(val string) *Node {
|
||||
if n.children == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, child := range n.children {
|
||||
if child.val == val {
|
||||
return child
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user