6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-05 03:14:42 +00:00

Fix issues found by varnamelen linter

This commit is contained in:
Grzegorz Dlugoszewski
2025-08-24 15:44:22 +02:00
parent 288a642483
commit f60cfd1e40
7 changed files with 62 additions and 57 deletions

View File

@@ -22,5 +22,10 @@ linters:
rules:
- path: _test.go
linters:
- dupl # We don't mind duplicated code in tests. It helps with clarity
- dupl # We don't mind duplicated code in tests. It helps with clarity
- varnamelen # We don't mind short var names in tests.
- path: test/
linters:
- dupl
- varnamelen

View File

@@ -18,32 +18,32 @@ type GetCfg struct {
}
// Get executes the "git get" command.
func Get(c *GetCfg) error {
if c.URL == "" && c.Dump == "" {
func Get(conf *GetCfg) error {
if conf.URL == "" && conf.Dump == "" {
return fmt.Errorf("missing <REPO> argument or --dump flag")
}
if c.URL != "" {
return cloneSingleRepo(c)
if conf.URL != "" {
return cloneSingleRepo(conf)
}
if c.Dump != "" {
return cloneDumpFile(c)
if conf.Dump != "" {
return cloneDumpFile(conf)
}
return nil
}
func cloneSingleRepo(c *GetCfg) error {
url, err := ParseURL(c.URL, c.DefHost, c.DefScheme)
func cloneSingleRepo(conf *GetCfg) error {
url, err := ParseURL(conf.URL, conf.DefHost, conf.DefScheme)
if err != nil {
return err
}
opts := &git.CloneOpts{
URL: url,
Path: filepath.Join(c.Root, URLToPath(*url, c.SkipHost)),
Branch: c.Branch,
Path: filepath.Join(conf.Root, URLToPath(*url, conf.SkipHost)),
Branch: conf.Branch,
}
_, err = git.Clone(opts)
@@ -51,21 +51,21 @@ func cloneSingleRepo(c *GetCfg) error {
return err
}
func cloneDumpFile(c *GetCfg) error {
parsedLines, err := parseDumpFile(c.Dump)
func cloneDumpFile(conf *GetCfg) error {
parsedLines, err := parseDumpFile(conf.Dump)
if err != nil {
return err
}
for _, line := range parsedLines {
url, err := ParseURL(line.rawurl, c.DefHost, c.DefScheme)
url, err := ParseURL(line.rawurl, conf.DefHost, conf.DefScheme)
if err != nil {
return err
}
opts := &git.CloneOpts{
URL: url,
Path: filepath.Join(c.Root, URLToPath(*url, c.SkipHost)),
Path: filepath.Join(conf.Root, URLToPath(*url, conf.SkipHost)),
Branch: line.branch,
}

View File

@@ -53,7 +53,7 @@ func (f *RepoFinder) Find() error {
return fmt.Errorf("failed to access root path: %w", err)
}
err := filepath.WalkDir(f.root, func(path string, d fs.DirEntry, err error) error {
err := filepath.WalkDir(f.root, func(path string, dir fs.DirEntry, err error) error {
// Handle walk errors
if err != nil {
// Skip permission errors but continue walking
@@ -65,12 +65,12 @@ func (f *RepoFinder) Find() error {
}
// Only process directories
if !d.IsDir() {
if !dir.IsDir() {
return nil
}
// Case 1: We're looking at a .git directory itself
if d.Name() == dotgit {
if dir.Name() == dotgit {
parentPath := filepath.Dir(path)
f.addIfOk(parentPath)
@@ -101,7 +101,7 @@ func (f *RepoFinder) Find() error {
// If fetch equals true, it first fetches from the remote repo before loading the status.
// Each repo is loaded concurrently by a separate worker, with max 100 workers being active at the same time.
func (f *RepoFinder) LoadAll(fetch bool) []*Status {
var ss []*Status
var statuses []*Status
reposChan := make(chan *Repo, f.maxWorkers)
statusChan := make(chan *Status, f.maxWorkers)
@@ -118,18 +118,18 @@ func (f *RepoFinder) LoadAll(fetch bool) []*Status {
// Read statuses from the statusChan and add then to the result slice.
// Close the channel when all repos are loaded.
for status := range statusChan {
ss = append(ss, status)
if len(ss) == len(f.repos) {
statuses = append(statuses, status)
if len(statuses) == len(f.repos) {
close(statusChan)
}
}
// Sort the status slice by path
sort.Slice(ss, func(i, j int) bool {
return strings.Compare(ss[i].path, ss[j].path) < 0
sort.Slice(statuses, func(i, j int) bool {
return strings.Compare(statuses[i].path, statuses[j].path) < 0
})
return ss
return statuses
}
func loadRepos(repos []*Repo, reposChan chan<- *Repo) {

View File

@@ -170,14 +170,14 @@ func (r *Repo) AheadBehind(branch string, upstream string) (int, int, error) {
}
// rev-list --left-right --count output is separated by a tab
lr := strings.Split(out, "\t")
count := strings.Split(out, "\t")
ahead, err := strconv.Atoi(lr[0])
ahead, err := strconv.Atoi(count[0])
if err != nil {
return 0, 0, err
}
behind, err := strconv.Atoi(lr[1])
behind, err := strconv.Atoi(count[1])
if err != nil {
return 0, 0, err
}

View File

@@ -16,24 +16,24 @@ type ListCfg struct {
}
// List executes the "git list" command.
func List(c *ListCfg) error {
finder := git.NewRepoFinder(c.Root)
func List(conf *ListCfg) error {
finder := git.NewRepoFinder(conf.Root)
if err := finder.Find(); err != nil {
return err
}
statuses := finder.LoadAll(c.Fetch)
statuses := finder.LoadAll(conf.Fetch)
printables := make([]print.Printable, len(statuses))
for i := range statuses {
printables[i] = statuses[i]
}
switch c.Output {
switch conf.Output {
case cfg.OutFlat:
fmt.Print(print.NewFlatPrinter().Print(printables))
case cfg.OutTree:
fmt.Print(print.NewTreePrinter().Print(c.Root, printables))
fmt.Print(print.NewTreePrinter().Print(conf.Root, printables))
case cfg.OutDump:
fmt.Print(print.NewDumpPrinter().Print(printables))
default:

View File

@@ -18,18 +18,18 @@ func NewFlatPrinter() *FlatPrinter {
func (p *FlatPrinter) Print(repos []Printable) string {
var str strings.Builder
for _, r := range repos {
str.WriteString(strings.TrimSuffix(r.Path(), string(os.PathSeparator)))
for _, repo := range repos {
str.WriteString(strings.TrimSuffix(repo.Path(), string(os.PathSeparator)))
if len(r.Errors()) > 0 {
if len(repo.Errors()) > 0 {
str.WriteString(" " + red("error") + "\n")
continue
}
str.WriteString(" " + blue(r.Current()))
str.WriteString(" " + blue(repo.Current()))
current := r.BranchStatus(r.Current())
worktree := r.WorkTreeStatus()
current := repo.BranchStatus(repo.Current())
worktree := repo.WorkTreeStatus()
if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", worktree)
@@ -41,13 +41,13 @@ func (p *FlatPrinter) Print(repos []Printable) string {
str.WriteString(" " + strings.Join([]string{yellow(current), red(worktree)}, " "))
}
for _, branch := range r.Branches() {
status := r.BranchStatus(branch)
for _, branch := range repo.Branches() {
status := repo.BranchStatus(branch)
if status == "" {
status = green("ok")
}
indent := strings.Repeat(" ", len(r.Path())-1)
indent := strings.Repeat(" ", len(repo.Path())-1)
str.WriteString(fmt.Sprintf("\n%s %s %s", indent, blue(branch), yellow(status)))
}

View File

@@ -88,8 +88,8 @@ func (n *Node) GetChild(val string) *Node {
func buildTree(root string, repos []Printable) *Node {
tree := Root(root)
for _, r := range repos {
path := strings.TrimPrefix(r.Path(), root)
for _, repo := range repos {
path := strings.TrimPrefix(repo.Path(), root)
path = strings.Trim(path, string(filepath.Separator))
subs := strings.Split(path, string(filepath.Separator))
@@ -98,14 +98,14 @@ func buildTree(root string, repos []Printable) *Node {
// If not, add it to node's children and move to next fragment.
// If it does, just move to the next fragment.
node := tree
for i, sub := range subs {
for idx, sub := range subs {
child := node.GetChild(sub)
if child == nil {
node = node.Add(sub)
// If that's the last fragment, it's a tree leaf and needs a *Repo attached.
if i == len(subs)-1 {
node.repo = r
if idx == len(subs)-1 {
node.repo = repo
}
continue
@@ -120,28 +120,28 @@ func buildTree(root string, repos []Printable) *Node {
// printTree renders the repo tree by recursively traversing the tree nodes.
// If a node doesn't have any children, it's a leaf node containing the repo status.
func (p *TreePrinter) printTree(node *Node, tp treeprint.Tree) {
func (p *TreePrinter) printTree(node *Node, tree treeprint.Tree) {
if node.children == nil {
tp.SetValue(printLeaf(node))
tree.SetValue(printLeaf(node))
}
for _, child := range node.children {
branch := tp.AddBranch(child.val)
branch := tree.AddBranch(child.val)
p.printTree(child, branch)
}
}
func printLeaf(node *Node) string {
r := node.repo
repo := node.repo
// If any errors happened during status loading, don't print the status but "error" instead.
// Actual error messages are printed in bulk below the tree.
if len(r.Errors()) > 0 {
if len(repo.Errors()) > 0 {
return fmt.Sprintf("%s %s", node.val, red("error"))
}
current := r.BranchStatus(r.Current())
worktree := r.WorkTreeStatus()
current := repo.BranchStatus(repo.Current())
worktree := repo.WorkTreeStatus()
if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", worktree)
@@ -150,13 +150,13 @@ func printLeaf(node *Node) string {
var str strings.Builder
if worktree == "" && current == "" {
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(r.Current()), green("ok")))
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(repo.Current()), green("ok")))
} else {
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(r.Current()), strings.Join([]string{yellow(current), red(worktree)}, " ")))
str.WriteString(fmt.Sprintf("%s %s %s", node.val, blue(repo.Current()), strings.Join([]string{yellow(current), red(worktree)}, " ")))
}
for _, branch := range r.Branches() {
status := r.BranchStatus(branch)
for _, branch := range repo.Branches() {
status := repo.BranchStatus(branch)
if status == "" {
status = green("ok")
}