6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-10 21:54:17 +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

@@ -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())
}