diff --git a/go.mod b/go.mod index 28c1896..aca34ff 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,5 @@ require ( github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.7.0 - github.com/xlab/treeprint v1.0.0 golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 ) diff --git a/go.sum b/go.sum index 2086cae..02a2a85 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,7 @@ github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OI github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -128,6 +129,7 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/karrick/godirwalk v1.15.6 h1:Yf2mmR8TJy+8Fa0SuQVto5SYap6IF7lNVX4Jdl8G1qA= @@ -195,7 +197,9 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= @@ -226,8 +230,6 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= -github.com/xlab/treeprint v1.0.0 h1:J0TkWtiuYgtdlrkkrDLISYBQ92M+X5m4LrIIMKrbDTs= -github.com/xlab/treeprint v1.0.0/go.mod h1:IoImgRak9i3zJyuxOKUP1v4UZd1tMoKkq/Cimt1uhCg= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= diff --git a/pkg/list.go b/pkg/list.go index d0ed708..9410842 100644 --- a/pkg/list.go +++ b/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 ( diff --git a/pkg/tree.go b/pkg/tree.go new file mode 100644 index 0000000..9808884 --- /dev/null +++ b/pkg/tree.go @@ -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 +} diff --git a/pkg/tree_test.go b/pkg/tree_test.go index 6e5e2d6..1da16df 100644 --- a/pkg/tree_test.go +++ b/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 }