6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 23:14:43 +00:00

Indicate if error occured during status loading and print the list of errors

This commit is contained in:
Grzegorz Dlugoszewski
2020-07-27 16:17:13 +02:00
parent d660a73c7f
commit b2d1d773f7
7 changed files with 107 additions and 59 deletions

View File

@@ -17,7 +17,7 @@ func NewDumpPrinter() *DumpPrinter {
func (p *DumpPrinter) Print(repos []Printable) string {
var str strings.Builder
for i, r := range repos {
for _, r := range repos {
str.WriteString(r.Remote())
// TODO: if head is detached maybe we should get the revision it points to in case it's a tag
@@ -25,9 +25,7 @@ func (p *DumpPrinter) Print(repos []Printable) string {
str.WriteString(" " + current)
}
if i < len(repos)-1 {
str.WriteString("\n")
}
str.WriteString("\n")
}
return str.String()

View File

@@ -18,8 +18,14 @@ func NewFlatPrinter() *FlatPrinter {
func (p *FlatPrinter) Print(repos []Printable) string {
var str strings.Builder
for i, r := range repos {
for _, r := range repos {
str.WriteString(strings.TrimSuffix(r.Path(), string(os.PathSeparator)))
if len(r.Errors()) > 0 {
str.WriteString(" " + red("error") + "\n")
continue
}
str.WriteString(" " + blue(r.Current()))
current := r.BranchStatus(r.Current())
@@ -45,10 +51,8 @@ func (p *FlatPrinter) Print(repos []Printable) string {
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, blue(branch), yellow(status)))
}
if i < len(repos)-1 {
str.WriteString("\n")
}
str.WriteString("\n")
}
return str.String()
return str.String() + Errors(repos)
}

View File

@@ -1,6 +1,9 @@
package print
import "fmt"
import (
"fmt"
"strings"
)
const (
head = "HEAD"
@@ -17,6 +20,28 @@ type Printable interface {
Errors() []string
}
// Errors returns a printable list of errors from the slice of Printables or an empty string if there are no errors.
// It's meant to be appended at the end of Print() result.
func Errors(repos []Printable) string {
errors := []string{}
for _, repo := range repos {
for _, err := range repo.Errors() {
errors = append(errors, err)
}
}
if len(errors) == 0 {
return ""
}
var str strings.Builder
str.WriteString(red("\nOops, errors happened when loading repository status:\n"))
str.WriteString(strings.Join(errors, "\n"))
return str.String()
}
// TODO: not sure if this works on windows. See https://github.com/mattn/go-colorable
func red(str string) string {
return fmt.Sprintf("\033[1;31m%s\033[0m", str)

View File

@@ -29,7 +29,7 @@ func (p *TreePrinter) Print(root string, repos []Printable) string {
p.printTree(tree, tp)
return tp.String()
return tp.String() + Errors(repos)
}
// Node represents a path fragment in repos tree.
@@ -114,34 +114,11 @@ func buildTree(root string, repos []Printable) *Node {
return tree
}
// printTree renders the repo tree by recursively traversing the tree nodes.
// If a node doesn't have any children, it's a leaf node containing the repo status.
func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
if node.children == nil {
r := node.repo
current := r.BranchStatus(r.Current())
worktree := r.WorkTreeStatus()
if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", worktree)
}
var str strings.Builder
if worktree == "" && current == "" {
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(r.Current()), green("ok")))
} else {
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(r.Current()), strings.Join([]string{yellow(current), red(worktree)}, " ")))
}
for _, branch := range r.Branches() {
status := r.BranchStatus(branch)
if status == "" {
status = green("ok")
}
str.WriteString(fmt.Sprintf("\n%s%s %s", indentation(node), blue(branch), yellow(status)))
}
tp.SetValue(str.String())
tp.SetValue(printLeaf(node))
}
for _, child := range node.children {
@@ -150,6 +127,42 @@ func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
}
}
func printLeaf(node *Node) string {
r := node.repo
// If any errors happened during status loading, don't print the status but "error" instead.
// Actual error messages are printed in bulk below the tree.
if len(r.Errors()) > 0 {
return fmt.Sprintf("%s %s", node.val, red("error"))
}
current := r.BranchStatus(r.Current())
worktree := r.WorkTreeStatus()
if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", worktree)
}
var str strings.Builder
if worktree == "" && current == "" {
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(r.Current()), green("ok")))
} else {
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(r.Current()), strings.Join([]string{yellow(current), red(worktree)}, " ")))
}
for _, branch := range r.Branches() {
status := r.BranchStatus(branch)
if status == "" {
status = green("ok")
}
str.WriteString(fmt.Sprintf("\n%s%s %s", indentation(node), blue(branch), yellow(status)))
}
return str.String()
}
// indentation generates a correct indentation for the branches row to match the links to lower rows.
// It traverses the tree "upwards" and checks if a parent node is the youngest one (ie, there are no more sibling at the same level).
// If it is, it means that level should be indented with empty spaces because there is nothing to link to anymore.