mirror of
https://github.com/grdl/git-get.git
synced 2026-02-08 15:09:19 +00:00
Load repos status simultaneously with goroutines
Also, refactor printer interface.
This commit is contained in:
@@ -4,13 +4,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DumpRepo is a git repository printable into a dump file.
|
||||
type DumpRepo interface {
|
||||
Path() string
|
||||
Remote() (string, error)
|
||||
CurrentBranch() (string, error)
|
||||
}
|
||||
|
||||
// DumpPrinter prints a list of repos in a dump file format.
|
||||
type DumpPrinter struct{}
|
||||
|
||||
@@ -21,20 +14,14 @@ func NewDumpPrinter() *DumpPrinter {
|
||||
|
||||
// Print generates a list of repos URLs. Each line contains a URL and, if applicable, a currently checked out branch name.
|
||||
// It's a way to dump all repositories managed by git-get and is supposed to be consumed by `git get --dump`.
|
||||
func (p *DumpPrinter) Print(repos []DumpRepo) string {
|
||||
func (p *DumpPrinter) Print(repos []Printable) string {
|
||||
var str strings.Builder
|
||||
|
||||
for i, r := range repos {
|
||||
url, err := r.Remote()
|
||||
if err != nil {
|
||||
continue
|
||||
// TODO: handle error?
|
||||
}
|
||||
str.WriteString(r.Remote())
|
||||
|
||||
str.WriteString(url)
|
||||
|
||||
current, err := r.CurrentBranch()
|
||||
if err != nil || current != detached {
|
||||
// TODO: if head is detached maybe we should get the revision it points to in case it's a tag
|
||||
if current := r.Current(); current != "" && current != head {
|
||||
str.WriteString(" " + current)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package print
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -14,37 +15,38 @@ func NewFlatPrinter() *FlatPrinter {
|
||||
}
|
||||
|
||||
// Print generates a flat list of repositories and their statuses - each repo in new line with full path.
|
||||
func (p *FlatPrinter) Print(repos []Repo) string {
|
||||
func (p *FlatPrinter) Print(repos []Printable) string {
|
||||
var str strings.Builder
|
||||
|
||||
for _, r := range repos {
|
||||
str.WriteString(fmt.Sprintf("\n%s %s", r.Path(), printCurrentBranchLine(r)))
|
||||
for i, r := range repos {
|
||||
str.WriteString(strings.TrimSuffix(r.Path(), string(os.PathSeparator)))
|
||||
str.WriteString(" " + blue(r.Current()))
|
||||
|
||||
branches, err := r.Branches()
|
||||
if err != nil {
|
||||
str.WriteString(printErr(err))
|
||||
continue
|
||||
current := r.BranchStatus(r.Current())
|
||||
worktree := r.WorkTreeStatus()
|
||||
|
||||
if worktree != "" {
|
||||
worktree = fmt.Sprintf("[ %s ]", worktree)
|
||||
}
|
||||
|
||||
current, err := r.CurrentBranch()
|
||||
if err != nil {
|
||||
str.WriteString(printErr(err))
|
||||
continue
|
||||
if worktree == "" && current == "" {
|
||||
str.WriteString(" " + green("ok"))
|
||||
} else {
|
||||
str.WriteString(" " + strings.Join([]string{yellow(current), red(worktree)}, " "))
|
||||
}
|
||||
|
||||
for _, branch := range branches {
|
||||
// Don't print the status of the current branch. It was already printed above.
|
||||
if branch == current {
|
||||
continue
|
||||
for _, branch := range r.Branches() {
|
||||
status := r.BranchStatus(branch)
|
||||
if status == "" {
|
||||
status = green("ok")
|
||||
}
|
||||
|
||||
status, err := printBranchStatus(r, branch)
|
||||
if err != nil {
|
||||
status = printErr(err)
|
||||
}
|
||||
indent := strings.Repeat(" ", len(r.Path())-1)
|
||||
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, blue(branch), yellow(status)))
|
||||
}
|
||||
|
||||
indent := strings.Repeat(" ", len(r.Path()))
|
||||
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, printBranchName(branch), status))
|
||||
if i < len(repos)-1 {
|
||||
str.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,148 +1,35 @@
|
||||
package print
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
head = "HEAD"
|
||||
)
|
||||
|
||||
// Printable represents a repository which status can be printed
|
||||
type Printable interface {
|
||||
Path() string
|
||||
Current() string
|
||||
Branches() []string
|
||||
BranchStatus(string) string
|
||||
WorkTreeStatus() string
|
||||
Remote() string
|
||||
Errors() []string
|
||||
}
|
||||
|
||||
// TODO: not sure if this works on windows. See https://github.com/mattn/go-colorable
|
||||
const (
|
||||
colorRed = "\033[1;31m%s\033[0m"
|
||||
colorGreen = "\033[1;32m%s\033[0m"
|
||||
colorBlue = "\033[1;34m%s\033[0m"
|
||||
colorYellow = "\033[1;33m%s\033[0m"
|
||||
)
|
||||
|
||||
const (
|
||||
untracked = "untracked"
|
||||
uncommitted = "uncommitted"
|
||||
ahead = "ahead"
|
||||
behind = "behind"
|
||||
noUpstream = "no upstream"
|
||||
ok = "ok"
|
||||
detached = "detached"
|
||||
head = "HEAD"
|
||||
)
|
||||
|
||||
// Repo is a git repository
|
||||
// TODO: maybe branch should be a separate interface
|
||||
type Repo interface {
|
||||
Path() string
|
||||
Branches() ([]string, error)
|
||||
CurrentBranch() (string, error)
|
||||
Upstream(branch string) (string, error)
|
||||
AheadBehind(branch string, upstream string) (int, int, error)
|
||||
Uncommitted() (int, error)
|
||||
Untracked() (int, error)
|
||||
func red(str string) string {
|
||||
return fmt.Sprintf("\033[1;31m%s\033[0m", str)
|
||||
}
|
||||
|
||||
// // Printer provides a way to print a list of repos and their statuses
|
||||
// type Printer interface {
|
||||
// Print(root string, repos []Repo) string
|
||||
// }
|
||||
|
||||
// prints status of currently checked out branch and the work tree.
|
||||
// The format is: branch_name branch_status [ worktree_status ]
|
||||
// Eg: master 1 head 2 behind [ 1 uncommitted ]
|
||||
func printCurrentBranchLine(r Repo) string {
|
||||
var res []string
|
||||
|
||||
current, err := r.CurrentBranch()
|
||||
if err != nil {
|
||||
return printErr(err)
|
||||
}
|
||||
|
||||
// if current head is detached don't print its status
|
||||
if current == head {
|
||||
return fmt.Sprintf(colorYellow, detached)
|
||||
}
|
||||
|
||||
status, err := printBranchStatus(r, current)
|
||||
if err != nil {
|
||||
return printErr(err)
|
||||
}
|
||||
|
||||
worktree, err := printWorkTreeStatus(r)
|
||||
if err != nil {
|
||||
return printErr(err)
|
||||
}
|
||||
|
||||
res = append(res, printBranchName(current))
|
||||
|
||||
// if worktree is not clean and branch is ok then it shouldn't be ok
|
||||
if worktree != "" && strings.Contains(status, ok) {
|
||||
res = append(res, worktree)
|
||||
} else {
|
||||
res = append(res, status)
|
||||
res = append(res, worktree)
|
||||
}
|
||||
|
||||
return strings.Join(res, " ")
|
||||
func green(str string) string {
|
||||
return fmt.Sprintf("\033[1;32m%s\033[0m", str)
|
||||
}
|
||||
|
||||
func printBranchName(branch string) string {
|
||||
return fmt.Sprintf(colorBlue, branch)
|
||||
func blue(str string) string {
|
||||
return fmt.Sprintf("\033[1;34m%s\033[0m", str)
|
||||
}
|
||||
|
||||
func printBranchStatus(r Repo, branch string) (string, error) {
|
||||
var res []string
|
||||
upstream, err := r.Upstream(branch)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if upstream == "" {
|
||||
return fmt.Sprintf(colorYellow, noUpstream), nil
|
||||
}
|
||||
|
||||
a, b, err := r.AheadBehind(branch, upstream)
|
||||
if err != nil {
|
||||
return printErr(err), nil
|
||||
}
|
||||
|
||||
if a == 0 && b == 0 {
|
||||
return fmt.Sprintf(colorGreen, ok), nil
|
||||
}
|
||||
|
||||
if a != 0 {
|
||||
res = append(res, fmt.Sprintf(colorYellow, fmt.Sprintf("%d %s", a, ahead)))
|
||||
}
|
||||
if b != 0 {
|
||||
res = append(res, fmt.Sprintf(colorYellow, fmt.Sprintf("%d %s", b, behind)))
|
||||
}
|
||||
|
||||
return strings.Join(res, " "), nil
|
||||
}
|
||||
|
||||
func printWorkTreeStatus(r Repo) (string, error) {
|
||||
uc, err := r.Uncommitted()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ut, err := r.Untracked()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if uc == 0 && ut == 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
var res []string
|
||||
res = append(res, "[")
|
||||
if uc != 0 {
|
||||
res = append(res, fmt.Sprintf(colorRed, fmt.Sprintf("%d %s", uc, uncommitted)))
|
||||
}
|
||||
if ut != 0 {
|
||||
res = append(res, fmt.Sprintf(colorRed, fmt.Sprintf("%d %s", ut, untracked)))
|
||||
}
|
||||
|
||||
res = append(res, "]")
|
||||
|
||||
return strings.Join(res, " "), nil
|
||||
}
|
||||
|
||||
func printErr(err error) string {
|
||||
return fmt.Sprintf(colorRed, err.Error())
|
||||
func yellow(str string) string {
|
||||
return fmt.Sprintf("\033[1;33m%s\033[0m", str)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func NewTreePrinter() *TreePrinter {
|
||||
}
|
||||
|
||||
// Print generates a tree view of repos and their statuses.
|
||||
func (p *TreePrinter) Print(root string, repos []Repo) string {
|
||||
func (p *TreePrinter) Print(root string, repos []Printable) string {
|
||||
if len(repos) == 0 {
|
||||
return fmt.Sprintf("There are no git repos under %s", root)
|
||||
}
|
||||
@@ -37,7 +37,7 @@ type Node struct {
|
||||
val string
|
||||
parent *Node
|
||||
children []*Node
|
||||
repo Repo
|
||||
repo Printable
|
||||
}
|
||||
|
||||
// Root creates a new root of a tree.
|
||||
@@ -81,7 +81,7 @@ func (n *Node) GetChild(val string) *Node {
|
||||
// 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 {
|
||||
func buildTree(root string, repos []Printable) *Node {
|
||||
tree := Root(root)
|
||||
|
||||
for _, r := range repos {
|
||||
@@ -115,32 +115,26 @@ func buildTree(root string, repos []Repo) *Node {
|
||||
func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
|
||||
if node.children == nil {
|
||||
r := node.repo
|
||||
tp.SetValue(node.val + " " + printCurrentBranchLine(r))
|
||||
current := r.BranchStatus(r.Current())
|
||||
worktree := r.WorkTreeStatus()
|
||||
|
||||
branches, err := r.Branches()
|
||||
if err != nil {
|
||||
tp.AddNode(printErr(err))
|
||||
return
|
||||
if worktree != "" {
|
||||
worktree = fmt.Sprintf("[ %s ]", worktree)
|
||||
}
|
||||
|
||||
current, err := r.CurrentBranch()
|
||||
if err != nil {
|
||||
tp.AddNode(printErr(err))
|
||||
return
|
||||
if worktree == "" && current == "" {
|
||||
tp.SetValue(node.val + " " + blue(r.Current()) + " " + green("ok"))
|
||||
} else {
|
||||
tp.SetValue(node.val + " " + blue(r.Current()) + " " + strings.Join([]string{yellow(current), red(worktree)}, " "))
|
||||
}
|
||||
|
||||
for _, branch := range branches {
|
||||
// Don't print the status of the current branch. It was already printed above.
|
||||
if branch == current {
|
||||
continue
|
||||
for _, branch := range r.Branches() {
|
||||
status := r.BranchStatus(branch)
|
||||
if status == "" {
|
||||
status = green("ok")
|
||||
}
|
||||
|
||||
status, err := printBranchStatus(r, branch)
|
||||
if err != nil {
|
||||
tp.AddNode(printErr(err))
|
||||
continue
|
||||
}
|
||||
tp.AddNode(printBranchName(branch) + " " + status)
|
||||
tp.AddNode(blue(branch) + " " + yellow(status))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user