6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 20:19:42 +00:00

Remove gogit and major refactoring (#2)

* Fix typo in readme

* Reimplement all git methods without go-git

* Rename repo pkg to git, add gitconfig methods

* Improve tests for configuration reading

* Rename package file to io and move RepoFinder there

* Refactor printers

- Remove smart printer
- Decouple printers from git repos with interfaces
- Update printer functions
- Remove unnecessary flags
- Add better remote URL detection

* Update readme and go.mod

* Add author to git commit in tests

Otherwise tests will fail in CI.

* Install git before running tests and don't use cgo

* Add better error message, revert installing git

* Ensure commit message is in quotes

* Set up git config before running tests
This commit is contained in:
Grzegorz Dlugoszewski
2020-06-24 23:54:44 +02:00
committed by GitHub
parent 2ef739ea49
commit 8c132cdafa
26 changed files with 1452 additions and 1648 deletions

View File

@@ -1,31 +1,40 @@
package print
import (
"git-get/pkg/repo"
"strings"
)
// DumpPrinter implements Printer interface and provides method for printing list of repos in dump file format.
// 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{}
// NewDumpPrinter creates a DumpPrinter.
func NewDumpPrinter() *DumpPrinter {
return &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(_ string, repos []*repo.Repo) string {
func (p *DumpPrinter) Print(repos []DumpRepo) string {
var str strings.Builder
for i, r := range repos {
remotes, err := r.Remotes()
if err != nil || len(remotes) == 0 {
url, err := r.Remote()
if err != nil {
continue
// TODO: handle error?
}
// TODO: Needs work. Right now we're just assuming the first remote is the origin one and the one from which the current branch is checked out.
url := remotes[0].Config().URLs[0]
current := r.Status.CurrentBranch
str.WriteString(url)
if current != repo.StatusDetached && current != repo.StatusUnknown {
current, err := r.CurrentBranch()
if err != nil || current != detached {
str.WriteString(" " + current)
}

View File

@@ -2,32 +2,49 @@ package print
import (
"fmt"
"git-get/pkg/repo"
"path/filepath"
"strings"
)
// FlatPrinter implements Printer interface and provides method for printing list of repos in flat format.
// FlatPrinter prints a list of repos in a flat format.
type FlatPrinter struct{}
// NewFlatPrinter creates a FlatPrinter.
func NewFlatPrinter() *FlatPrinter {
return &FlatPrinter{}
}
// Print generates a flat list of repositories and their statuses - each repo in new line with full path.
func (p *FlatPrinter) Print(root string, repos []*repo.Repo) string {
func (p *FlatPrinter) Print(repos []Repo) string {
var str strings.Builder
for _, r := range repos {
path := strings.TrimPrefix(r.Path, root)
path = strings.Trim(path, string(filepath.Separator))
str.WriteString(fmt.Sprintf("\n%s %s", r.Path(), printCurrentBranchLine(r)))
str.WriteString(fmt.Sprintf("\n%s %s", path, printWorktreeStatus(r)))
branches, err := r.Branches()
if err != nil {
str.WriteString(printErr(err))
continue
}
for _, branch := range r.Status.Branches {
current, err := r.CurrentBranch()
if err != nil {
str.WriteString(printErr(err))
continue
}
for _, branch := range branches {
// Don't print the status of the current branch. It was already printed above.
if branch.Name == r.Status.CurrentBranch {
if branch == current {
continue
}
indent := strings.Repeat(" ", len(path))
str.WriteString(fmt.Sprintf("\n%s %s", indent, printBranchStatus(branch)))
status, err := printBranchStatus(r, branch)
if err != nil {
status = printErr(err)
}
indent := strings.Repeat(" ", len(r.Path()))
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, printBranchName(branch), status))
}
}

View File

@@ -2,85 +2,147 @@ package print
import (
"fmt"
"git-get/pkg/repo"
"strings"
)
// Printer prints list of repos and their statuses
type Printer interface {
Print(root string, repos []*repo.Repo) 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"
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"
)
func printWorktreeStatus(r *repo.Repo) string {
clean := true
var status []string
const (
untracked = "untracked"
uncommitted = "uncommitted"
ahead = "ahead"
behind = "behind"
noUpstream = "no upstream"
ok = "ok"
detached = "detached"
head = "HEAD"
)
// if current branch status can't be found it's probably a detached head
// TODO: what if current HEAD points to a tag?
if current := r.CurrentBranchStatus(); current == nil {
status = append(status, fmt.Sprintf(ColorYellow, r.Status.CurrentBranch))
// 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
// 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 uncomitted ]
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 {
status = append(status, printBranchStatus(current))
res = append(res, status)
res = append(res, worktree)
}
// TODO: this is ugly
// unset clean flag to use it to render braces around worktree status and remove "ok" from branch status if it's there
if r.Status.HasUncommittedChanges || r.Status.HasUntrackedFiles {
clean = false
}
if !clean {
status[len(status)-1] = strings.TrimSuffix(status[len(status)-1], repo.StatusOk)
status = append(status, "[")
}
if r.Status.HasUntrackedFiles {
status = append(status, fmt.Sprintf(ColorRed, repo.StatusUntracked))
}
if r.Status.HasUncommittedChanges {
status = append(status, fmt.Sprintf(ColorRed, repo.StatusUncommitted))
}
if !clean {
status = append(status, "]")
}
return strings.Join(status, " ")
return strings.Join(res, " ")
}
func printBranchStatus(branch *repo.BranchStatus) string {
// ok indicates that the branch has upstream and is not ahead or behind it
ok := true
var status []string
status = append(status, fmt.Sprintf(ColorBlue, branch.Name))
if branch.Upstream == "" {
ok = false
status = append(status, fmt.Sprintf(ColorYellow, repo.StatusNoUpstream))
}
if branch.Behind != 0 {
ok = false
status = append(status, fmt.Sprintf(ColorYellow, fmt.Sprintf("%d %s", branch.Behind, repo.StatusBehind)))
}
if branch.Ahead != 0 {
ok = false
status = append(status, fmt.Sprintf(ColorYellow, fmt.Sprintf("%d %s", branch.Ahead, repo.StatusAhead)))
}
if ok {
status = append(status, fmt.Sprintf(ColorGreen, repo.StatusOk))
}
return strings.Join(status, " ")
func printBranchName(branch string) string {
return fmt.Sprintf(colorBlue, branch)
}
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())
}

View File

@@ -1,98 +0,0 @@
package print
import (
"git-get/pkg/repo"
"path/filepath"
"strings"
)
// SmartPrinter implements Printer interface and provides methods for printing repos and their statuses.
// It's "smart" because it automatically folds branches which only have a single child and indents branches with many children.
type SmartPrinter struct {
// length is the size (number of chars) of the currently processed line.
// It's used to correctly indent the lines with branches status.
length int
}
// Print generates a list of repositories and their statuses.
func (p *SmartPrinter) Print(root string, repos []*repo.Repo) string {
tree := buildTree(root, repos)
return p.printSmartTree(tree)
}
// printSmartTree 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 (p *SmartPrinter) printSmartTree(node *Node) string {
if node.children == nil {
// If node is a leaf, print repo name and its status and finish processing this node.
value := node.val
// TODO: Ugly
// If this is called from tests the repo will be nil and we should return just the name without the status.
if node.repo.Repository == nil {
return value
}
value += " " + printWorktreeStatus(node.repo)
// Print the status of each branch on a new line, indented to match the position of the current branch name.
indent := "\n" + strings.Repeat(" ", p.length+len(node.val))
for _, branch := range node.repo.Status.Branches {
// Don't print the status of the current branch. It was already printed above.
if branch.Name == node.repo.Status.CurrentBranch {
continue
}
value += indent + printBranchStatus(branch)
}
return value
}
val := node.val + string(filepath.Separator)
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
p.length += len(val)
} 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(" ", node.depth)
p.length = 0
}
for _, child := range node.children {
p.length += len(shift)
val += shift + p.printSmartTree(child)
p.length = 0
}
return val
}

View File

@@ -1,20 +1,29 @@
package print
import (
"git-get/pkg/repo"
"fmt"
"path/filepath"
"strings"
"github.com/xlab/treeprint"
)
// TreePrinter implements Printer interface and provides methods for printing repos and their statuses.
type TreePrinter struct{}
// TreePrinter prints list of repos in a directory tree format.
type TreePrinter struct {
}
// NewTreePrinter creates a TreePrinter.
func NewTreePrinter() *TreePrinter {
return &TreePrinter{}
}
// Print generates a tree view of repos and their statuses.
func (p *TreePrinter) Print(root string, repos []*repo.Repo) string {
tree := buildTree(root, repos)
func (p *TreePrinter) Print(root string, repos []Repo) string {
if len(repos) == 0 {
return fmt.Sprintf("There are no git repos under %s", root)
}
tree := buildTree(root, repos)
tp := treeprint.New()
tp.SetValue(root)
@@ -23,16 +32,15 @@ func (p *TreePrinter) Print(root string, repos []*repo.Repo) string {
return tp.String()
}
// Node represents a node (ie. path fragment) in a repos tree.
// Node represents a path fragment in repos tree.
type Node struct {
val string
depth int // depth is a nesting depth used when rendering a smart tree, not a depth level of a tree node.
parent *Node
children []*Node
repo *repo.Repo
repo Repo
}
// Root creates a new root of a tree
// Root creates a new root of a tree.
func Root(val string) *Node {
root := &Node{
val: val,
@@ -40,7 +48,7 @@ func Root(val string) *Node {
return root
}
// Add adds a child node
// Add adds a child node with given value to a current node.
func (n *Node) Add(val string) *Node {
if n.children == nil {
n.children = make([]*Node, 0)
@@ -73,11 +81,11 @@ 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.Repo) *Node {
func buildTree(root string, repos []Repo) *Node {
tree := Root(root)
for _, r := range repos {
path := strings.TrimPrefix(r.Path, root)
path := strings.TrimPrefix(r.Path(), root)
path = strings.Trim(path, string(filepath.Separator))
subs := strings.Split(path, string(filepath.Separator))
@@ -106,16 +114,34 @@ func buildTree(root string, repos []*repo.Repo) *Node {
func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
if node.children == nil {
tp.SetValue(node.val + " " + printWorktreeStatus(node.repo))
r := node.repo
tp.SetValue(node.val + " " + printCurrentBranchLine(r))
for _, branch := range node.repo.Status.Branches {
// Don't print the status of the current branch. It was already printed above.
if branch.Name == node.repo.Status.CurrentBranch {
continue
}
tp.AddNode(printBranchStatus(branch))
branches, err := r.Branches()
if err != nil {
tp.AddNode(printErr(err))
return
}
current, err := r.CurrentBranch()
if err != nil {
tp.AddNode(printErr(err))
return
}
for _, branch := range branches {
// Don't print the status of the current branch. It was already printed above.
if branch == current {
continue
}
status, err := printBranchStatus(r, branch)
if err != nil {
tp.AddNode(printErr(err))
continue
}
tp.AddNode(printBranchName(branch) + " " + status)
}
}
for _, child := range node.children {

View File

@@ -1,108 +0,0 @@
package print
import (
"fmt"
"git-get/pkg/repo"
"strings"
"testing"
)
func TestTree(t *testing.T) {
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
`,
},
}
for i, test := range tests {
var repos []*repo.Repo
for _, path := range test.paths {
repos = append(repos, repo.New(nil, path)) //&Repo{path: path})
}
printer := SmartPrinter{}
// 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", printer.Print("root", repos))
// Rendered tree uses spaces for indentation but the test cases use tabs.
if got != strings.ReplaceAll(test.want, "\t", " ") {
t.Errorf("Failed test case %d, got: %+v; want: %+v", i, got, test.want)
}
}
}