1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/git/git_repo.go
Bryan Austin e2c1f793bf Fix newline in git module repo names breaking display
After setting up the git module with multiple repos and switching
between them, I observed some graphical wonkiness in the display:

https://i.imgur.com/R3e7eij.png

After adding some log statements, I tracked it down to the
`GitRepo.Repository` field having a newline in it after it's set
from a command execution's stdout. This change strips the
repository path of spaces when assigning to the `Repository` field,
which fixes the display issues.
2018-06-08 14:44:45 -07:00

93 lines
2.1 KiB
Go

package git
import (
"fmt"
"os/exec"
"strings"
"github.com/senorprogrammer/wtf/wtf"
)
type GitRepo struct {
Branch string
ChangedFiles []string
Commits []string
Repository string
Path string
}
func NewGitRepo(repoPath string) *GitRepo {
repo := GitRepo{Path: repoPath}
repo.Branch = repo.branch()
repo.ChangedFiles = repo.changedFiles()
repo.Commits = repo.commits()
repo.Repository = strings.TrimSpace(repo.repository())
return &repo
}
/* -------------------- Unexported Functions -------------------- */
func (repo *GitRepo) branch() string {
arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--abbrev-ref", "HEAD"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) changedFiles() []string {
arg := []string{repo.gitDir(), repo.workTree(), "status", "--porcelain"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (repo *GitRepo) commits() []string {
numStr := fmt.Sprintf("-n %d", Config.UInt("wtf.mods.git.commitCount", 10))
arg := []string{repo.gitDir(), repo.workTree(), "log", "--date=format:\"%b %d, %Y\"", numStr, "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (repo *GitRepo) repository() string {
arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--show-toplevel"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) pull() string {
arg := []string{repo.gitDir(), repo.workTree(), "pull"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) checkout(branch string) string {
arg := []string{repo.gitDir(), repo.workTree(), "checkout", branch}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) gitDir() string {
return fmt.Sprintf("--git-dir=%s/.git", repo.Path)
}
func (repo *GitRepo) workTree() string {
return fmt.Sprintf("--work-tree=%s", repo.Path)
}