mirror of
https://github.com/grdl/git-get.git
synced 2026-02-10 16:04:16 +00:00
Load repos status simultaneously with goroutines
Also, refactor printer interface.
This commit is contained in:
@@ -110,7 +110,7 @@ https://github.com/grdl/testsite master
|
|||||||
|
|
||||||
Dump file format is simply:
|
Dump file format is simply:
|
||||||
- Each repo URL on a separate line.
|
- Each repo URL on a separate line.
|
||||||
- Each URL can have a suffix with a branch or tag name to check out after cloning. Without that suffix, repository HEAD is used (usually it's `master`).
|
- Each URL can have a space-separated suffix with a branch or tag name to check out after cloning. Without that suffix, repository HEAD is cloned (usually it's `master`).
|
||||||
|
|
||||||
Example dump file content:
|
Example dump file content:
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@@ -98,8 +97,6 @@ func (r *RepoFinder) Find() ([]string, error) {
|
|||||||
return nil, fmt.Errorf("no git repos found in root path %s", r.root)
|
return nil, fmt.Errorf("no git repos found in root path %s", r.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(r.repos)
|
|
||||||
|
|
||||||
return r.repos, nil
|
return r.repos, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
68
pkg/list.go
68
pkg/list.go
@@ -3,14 +3,12 @@ package pkg
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git-get/pkg/cfg"
|
"git-get/pkg/cfg"
|
||||||
"git-get/pkg/git"
|
|
||||||
"git-get/pkg/io"
|
"git-get/pkg/io"
|
||||||
"git-get/pkg/print"
|
"git-get/pkg/print"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var repos []string
|
|
||||||
|
|
||||||
// ListCfg provides configuration for the List command.
|
// ListCfg provides configuration for the List command.
|
||||||
type ListCfg struct {
|
type ListCfg struct {
|
||||||
Fetch bool
|
Fetch bool
|
||||||
@@ -25,50 +23,52 @@ func List(c *ListCfg) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: we should open, fetch and read status of each repo in separate goroutine
|
loaded := loadAll(paths)
|
||||||
var repos []git.Repo
|
|
||||||
for _, path := range paths {
|
|
||||||
repo, err := git.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
// TODO: how should we handle it?
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.Fetch {
|
printables := make([]print.Printable, len(loaded))
|
||||||
err := repo.Fetch()
|
for i := range loaded {
|
||||||
if err != nil {
|
printables[i] = loaded[i]
|
||||||
// TODO: handle error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
repos = append(repos, *repo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch c.Output {
|
switch c.Output {
|
||||||
case cfg.OutFlat:
|
case cfg.OutFlat:
|
||||||
printables := make([]print.Repo, len(repos))
|
|
||||||
for i := range repos {
|
|
||||||
printables[i] = &repos[i]
|
|
||||||
}
|
|
||||||
fmt.Println(print.NewFlatPrinter().Print(printables))
|
fmt.Println(print.NewFlatPrinter().Print(printables))
|
||||||
|
|
||||||
case cfg.OutTree:
|
case cfg.OutTree:
|
||||||
printables := make([]print.Repo, len(repos))
|
|
||||||
for i := range repos {
|
|
||||||
printables[i] = &repos[i]
|
|
||||||
}
|
|
||||||
fmt.Println(print.NewTreePrinter().Print(c.Root, printables))
|
fmt.Println(print.NewTreePrinter().Print(c.Root, printables))
|
||||||
|
|
||||||
case cfg.OutDump:
|
case cfg.OutDump:
|
||||||
printables := make([]print.DumpRepo, len(repos))
|
|
||||||
for i := range repos {
|
|
||||||
printables[i] = &repos[i]
|
|
||||||
}
|
|
||||||
fmt.Println(print.NewDumpPrinter().Print(printables))
|
fmt.Println(print.NewDumpPrinter().Print(printables))
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid --out flag; allowed values: [%s]", strings.Join(cfg.AllowedOut, ", "))
|
return fmt.Errorf("invalid --out flag; allowed values: [%s]", strings.Join(cfg.AllowedOut, ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadAll runs a separate goroutine to open, fetch (if asked to) and load status of git repo
|
||||||
|
func loadAll(paths []string) []*Loaded {
|
||||||
|
var ll []*Loaded
|
||||||
|
|
||||||
|
loadedChan := make(chan *Loaded)
|
||||||
|
|
||||||
|
for _, path := range paths {
|
||||||
|
go func(path string) {
|
||||||
|
loadedChan <- Load(path)
|
||||||
|
}(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
for l := range loadedChan {
|
||||||
|
ll = append(ll, l)
|
||||||
|
|
||||||
|
// Close the channell when loaded all paths
|
||||||
|
if len(ll) == len(paths) {
|
||||||
|
close(loadedChan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort the loaded slice by path
|
||||||
|
sort.Slice(ll, func(i, j int) bool {
|
||||||
|
return strings.Compare(ll[i].path, ll[j].path) < 0
|
||||||
|
})
|
||||||
|
|
||||||
|
return ll
|
||||||
|
}
|
||||||
|
|||||||
173
pkg/load.go
Normal file
173
pkg/load.go
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git-get/pkg/git"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Loaded represents a repository which status is Loaded from disk and stored for printing.
|
||||||
|
type Loaded struct {
|
||||||
|
path string
|
||||||
|
current string
|
||||||
|
branches map[string]string // key: branch name, value: branch status
|
||||||
|
worktree string
|
||||||
|
remote string
|
||||||
|
errors []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load reads status of a repository at a given path.
|
||||||
|
func Load(path string) *Loaded {
|
||||||
|
loaded := &Loaded{
|
||||||
|
path: path,
|
||||||
|
branches: make(map[string]string),
|
||||||
|
errors: make([]string, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := git.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
loaded.errors = append(loaded.errors, err.Error())
|
||||||
|
return loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
loaded.current, err = repo.CurrentBranch()
|
||||||
|
if err != nil {
|
||||||
|
loaded.errors = append(loaded.errors, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
var errs []error
|
||||||
|
loaded.branches, errs = loadBranches(repo)
|
||||||
|
for _, err := range errs {
|
||||||
|
loaded.errors = append(loaded.errors, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
loaded.worktree, err = loadWorkTree(repo)
|
||||||
|
if err != nil {
|
||||||
|
loaded.errors = append(loaded.errors, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
loaded.remote, err = repo.Remote()
|
||||||
|
if err != nil {
|
||||||
|
loaded.errors = append(loaded.errors, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return loaded
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadBranches(r *git.Repo) (map[string]string, []error) {
|
||||||
|
statuses := make(map[string]string)
|
||||||
|
errors := make([]error, 0)
|
||||||
|
|
||||||
|
branches, err := r.Branches()
|
||||||
|
if err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
return statuses, errors
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, branch := range branches {
|
||||||
|
status, err := loadBranchStatus(r, branch)
|
||||||
|
statuses[branch] = status
|
||||||
|
if err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return statuses, errors
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadBranchStatus(r *git.Repo, branch string) (string, error) {
|
||||||
|
upstream, err := r.Upstream(branch)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if upstream == "" {
|
||||||
|
return "no upstream", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ahead, behind, err := r.AheadBehind(branch, upstream)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ahead == 0 && behind == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var res []string
|
||||||
|
if ahead != 0 {
|
||||||
|
res = append(res, fmt.Sprintf("%d ahead", ahead))
|
||||||
|
}
|
||||||
|
if behind != 0 {
|
||||||
|
res = append(res, fmt.Sprintf("%d behind", behind))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(res, " "), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadWorkTree(r *git.Repo) (string, error) {
|
||||||
|
uncommitted, err := r.Uncommitted()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
untracked, err := r.Untracked()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if uncommitted == 0 && untracked == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var res []string
|
||||||
|
if uncommitted != 0 {
|
||||||
|
res = append(res, fmt.Sprintf("%d uncommitted", uncommitted))
|
||||||
|
}
|
||||||
|
if untracked != 0 {
|
||||||
|
res = append(res, fmt.Sprintf("%d untracked", untracked))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(res, " "), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path returns path to a repository.
|
||||||
|
func (r *Loaded) Path() string {
|
||||||
|
return r.path
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current returns the name of currently checked out branch (or tag or detached HEAD).
|
||||||
|
func (r *Loaded) Current() string {
|
||||||
|
return r.current
|
||||||
|
}
|
||||||
|
|
||||||
|
// Branches returns a list of all branches names except the currently checked out one. Use Current() to get its name.
|
||||||
|
func (r *Loaded) Branches() []string {
|
||||||
|
var branches []string
|
||||||
|
for b := range r.branches {
|
||||||
|
if b != r.current {
|
||||||
|
branches = append(branches, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return branches
|
||||||
|
}
|
||||||
|
|
||||||
|
// BranchStatus returns status of a given branch
|
||||||
|
func (r *Loaded) BranchStatus(branch string) string {
|
||||||
|
return r.branches[branch]
|
||||||
|
}
|
||||||
|
|
||||||
|
// WorkTreeStatus returns status of a worktree
|
||||||
|
func (r *Loaded) WorkTreeStatus() string {
|
||||||
|
return r.worktree
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remote returns URL to remote repository
|
||||||
|
func (r *Loaded) Remote() string {
|
||||||
|
return r.remote
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errors is a slice of errors that occurred when loading repo status
|
||||||
|
func (r *Loaded) Errors() []string {
|
||||||
|
return r.errors
|
||||||
|
}
|
||||||
@@ -4,13 +4,6 @@ import (
|
|||||||
"strings"
|
"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.
|
// DumpPrinter prints a list of repos in a dump file format.
|
||||||
type DumpPrinter struct{}
|
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.
|
// 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`.
|
// 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
|
var str strings.Builder
|
||||||
|
|
||||||
for i, r := range repos {
|
for i, r := range repos {
|
||||||
url, err := r.Remote()
|
str.WriteString(r.Remote())
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
// TODO: handle error?
|
|
||||||
}
|
|
||||||
|
|
||||||
str.WriteString(url)
|
// 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 {
|
||||||
current, err := r.CurrentBranch()
|
|
||||||
if err != nil || current != detached {
|
|
||||||
str.WriteString(" " + current)
|
str.WriteString(" " + current)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package print
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"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.
|
// 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
|
var str strings.Builder
|
||||||
|
|
||||||
for _, r := range repos {
|
for i, r := range repos {
|
||||||
str.WriteString(fmt.Sprintf("\n%s %s", r.Path(), printCurrentBranchLine(r)))
|
str.WriteString(strings.TrimSuffix(r.Path(), string(os.PathSeparator)))
|
||||||
|
str.WriteString(" " + blue(r.Current()))
|
||||||
|
|
||||||
branches, err := r.Branches()
|
current := r.BranchStatus(r.Current())
|
||||||
if err != nil {
|
worktree := r.WorkTreeStatus()
|
||||||
str.WriteString(printErr(err))
|
|
||||||
continue
|
if worktree != "" {
|
||||||
|
worktree = fmt.Sprintf("[ %s ]", worktree)
|
||||||
}
|
}
|
||||||
|
|
||||||
current, err := r.CurrentBranch()
|
if worktree == "" && current == "" {
|
||||||
if err != nil {
|
str.WriteString(" " + green("ok"))
|
||||||
str.WriteString(printErr(err))
|
} else {
|
||||||
continue
|
str.WriteString(" " + strings.Join([]string{yellow(current), red(worktree)}, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, branch := range branches {
|
for _, branch := range r.Branches() {
|
||||||
// Don't print the status of the current branch. It was already printed above.
|
status := r.BranchStatus(branch)
|
||||||
if branch == current {
|
if status == "" {
|
||||||
continue
|
status = green("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := printBranchStatus(r, branch)
|
indent := strings.Repeat(" ", len(r.Path())-1)
|
||||||
if err != nil {
|
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, blue(branch), yellow(status)))
|
||||||
status = printErr(err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
indent := strings.Repeat(" ", len(r.Path()))
|
if i < len(repos)-1 {
|
||||||
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, printBranchName(branch), status))
|
str.WriteString("\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,148 +1,35 @@
|
|||||||
package print
|
package print
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"strings"
|
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
|
// TODO: not sure if this works on windows. See https://github.com/mattn/go-colorable
|
||||||
const (
|
func red(str string) string {
|
||||||
colorRed = "\033[1;31m%s\033[0m"
|
return fmt.Sprintf("\033[1;31m%s\033[0m", str)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Printer provides a way to print a list of repos and their statuses
|
func green(str string) string {
|
||||||
// type Printer interface {
|
return fmt.Sprintf("\033[1;32m%s\033[0m", str)
|
||||||
// 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 printBranchName(branch string) string {
|
func blue(str string) string {
|
||||||
return fmt.Sprintf(colorBlue, branch)
|
return fmt.Sprintf("\033[1;34m%s\033[0m", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printBranchStatus(r Repo, branch string) (string, error) {
|
func yellow(str string) string {
|
||||||
var res []string
|
return fmt.Sprintf("\033[1;33m%s\033[0m", str)
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func NewTreePrinter() *TreePrinter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print generates a tree view of repos and their statuses.
|
// 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 {
|
if len(repos) == 0 {
|
||||||
return fmt.Sprintf("There are no git repos under %s", root)
|
return fmt.Sprintf("There are no git repos under %s", root)
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ type Node struct {
|
|||||||
val string
|
val string
|
||||||
parent *Node
|
parent *Node
|
||||||
children []*Node
|
children []*Node
|
||||||
repo Repo
|
repo Printable
|
||||||
}
|
}
|
||||||
|
|
||||||
// Root creates a new root of a tree.
|
// 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.
|
// buildTree builds a directory tree of paths to repositories.
|
||||||
// Each node represents a directory in the repo path.
|
// Each node represents a directory in the repo path.
|
||||||
// Each leaf (final node) contains a pointer to the repo.
|
// 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)
|
tree := Root(root)
|
||||||
|
|
||||||
for _, r := range repos {
|
for _, r := range repos {
|
||||||
@@ -115,32 +115,26 @@ func buildTree(root string, repos []Repo) *Node {
|
|||||||
func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
|
func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
|
||||||
if node.children == nil {
|
if node.children == nil {
|
||||||
r := node.repo
|
r := node.repo
|
||||||
tp.SetValue(node.val + " " + printCurrentBranchLine(r))
|
current := r.BranchStatus(r.Current())
|
||||||
|
worktree := r.WorkTreeStatus()
|
||||||
|
|
||||||
branches, err := r.Branches()
|
if worktree != "" {
|
||||||
if err != nil {
|
worktree = fmt.Sprintf("[ %s ]", worktree)
|
||||||
tp.AddNode(printErr(err))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
current, err := r.CurrentBranch()
|
if worktree == "" && current == "" {
|
||||||
if err != nil {
|
tp.SetValue(node.val + " " + blue(r.Current()) + " " + green("ok"))
|
||||||
tp.AddNode(printErr(err))
|
} else {
|
||||||
return
|
tp.SetValue(node.val + " " + blue(r.Current()) + " " + strings.Join([]string{yellow(current), red(worktree)}, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, branch := range branches {
|
for _, branch := range r.Branches() {
|
||||||
// Don't print the status of the current branch. It was already printed above.
|
status := r.BranchStatus(branch)
|
||||||
if branch == current {
|
if status == "" {
|
||||||
continue
|
status = green("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
status, err := printBranchStatus(r, branch)
|
tp.AddNode(blue(branch) + " " + yellow(status))
|
||||||
if err != nil {
|
|
||||||
tp.AddNode(printErr(err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
tp.AddNode(printBranchName(branch) + " " + status)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user