6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-09 03:54:20 +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: rules:
- path: _test.go - path: _test.go
linters: 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. // Get executes the "git get" command.
func Get(c *GetCfg) error { func Get(conf *GetCfg) error {
if c.URL == "" && c.Dump == "" { if conf.URL == "" && conf.Dump == "" {
return fmt.Errorf("missing <REPO> argument or --dump flag") return fmt.Errorf("missing <REPO> argument or --dump flag")
} }
if c.URL != "" { if conf.URL != "" {
return cloneSingleRepo(c) return cloneSingleRepo(conf)
} }
if c.Dump != "" { if conf.Dump != "" {
return cloneDumpFile(c) return cloneDumpFile(conf)
} }
return nil return nil
} }
func cloneSingleRepo(c *GetCfg) error { func cloneSingleRepo(conf *GetCfg) error {
url, err := ParseURL(c.URL, c.DefHost, c.DefScheme) url, err := ParseURL(conf.URL, conf.DefHost, conf.DefScheme)
if err != nil { if err != nil {
return err return err
} }
opts := &git.CloneOpts{ opts := &git.CloneOpts{
URL: url, URL: url,
Path: filepath.Join(c.Root, URLToPath(*url, c.SkipHost)), Path: filepath.Join(conf.Root, URLToPath(*url, conf.SkipHost)),
Branch: c.Branch, Branch: conf.Branch,
} }
_, err = git.Clone(opts) _, err = git.Clone(opts)
@@ -51,21 +51,21 @@ func cloneSingleRepo(c *GetCfg) error {
return err return err
} }
func cloneDumpFile(c *GetCfg) error { func cloneDumpFile(conf *GetCfg) error {
parsedLines, err := parseDumpFile(c.Dump) parsedLines, err := parseDumpFile(conf.Dump)
if err != nil { if err != nil {
return err return err
} }
for _, line := range parsedLines { 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 { if err != nil {
return err return err
} }
opts := &git.CloneOpts{ opts := &git.CloneOpts{
URL: url, URL: url,
Path: filepath.Join(c.Root, URLToPath(*url, c.SkipHost)), Path: filepath.Join(conf.Root, URLToPath(*url, conf.SkipHost)),
Branch: line.branch, Branch: line.branch,
} }

View File

@@ -53,7 +53,7 @@ func (f *RepoFinder) Find() error {
return fmt.Errorf("failed to access root path: %w", err) 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 // Handle walk errors
if err != nil { if err != nil {
// Skip permission errors but continue walking // Skip permission errors but continue walking
@@ -65,12 +65,12 @@ func (f *RepoFinder) Find() error {
} }
// Only process directories // Only process directories
if !d.IsDir() { if !dir.IsDir() {
return nil return nil
} }
// Case 1: We're looking at a .git directory itself // Case 1: We're looking at a .git directory itself
if d.Name() == dotgit { if dir.Name() == dotgit {
parentPath := filepath.Dir(path) parentPath := filepath.Dir(path)
f.addIfOk(parentPath) 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. // 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. // 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 { func (f *RepoFinder) LoadAll(fetch bool) []*Status {
var ss []*Status var statuses []*Status
reposChan := make(chan *Repo, f.maxWorkers) reposChan := make(chan *Repo, f.maxWorkers)
statusChan := make(chan *Status, 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. // Read statuses from the statusChan and add then to the result slice.
// Close the channel when all repos are loaded. // Close the channel when all repos are loaded.
for status := range statusChan { for status := range statusChan {
ss = append(ss, status) statuses = append(statuses, status)
if len(ss) == len(f.repos) { if len(statuses) == len(f.repos) {
close(statusChan) close(statusChan)
} }
} }
// Sort the status slice by path // Sort the status slice by path
sort.Slice(ss, func(i, j int) bool { sort.Slice(statuses, func(i, j int) bool {
return strings.Compare(ss[i].path, ss[j].path) < 0 return strings.Compare(statuses[i].path, statuses[j].path) < 0
}) })
return ss return statuses
} }
func loadRepos(repos []*Repo, reposChan chan<- *Repo) { 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 // 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 { if err != nil {
return 0, 0, err return 0, 0, err
} }
behind, err := strconv.Atoi(lr[1]) behind, err := strconv.Atoi(count[1])
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }

View File

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

View File

@@ -18,18 +18,18 @@ func NewFlatPrinter() *FlatPrinter {
func (p *FlatPrinter) Print(repos []Printable) string { func (p *FlatPrinter) Print(repos []Printable) string {
var str strings.Builder var str strings.Builder
for _, r := range repos { for _, repo := range repos {
str.WriteString(strings.TrimSuffix(r.Path(), string(os.PathSeparator))) str.WriteString(strings.TrimSuffix(repo.Path(), string(os.PathSeparator)))
if len(r.Errors()) > 0 { if len(repo.Errors()) > 0 {
str.WriteString(" " + red("error") + "\n") str.WriteString(" " + red("error") + "\n")
continue continue
} }
str.WriteString(" " + blue(r.Current())) str.WriteString(" " + blue(repo.Current()))
current := r.BranchStatus(r.Current()) current := repo.BranchStatus(repo.Current())
worktree := r.WorkTreeStatus() worktree := repo.WorkTreeStatus()
if worktree != "" { if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", 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)}, " ")) str.WriteString(" " + strings.Join([]string{yellow(current), red(worktree)}, " "))
} }
for _, branch := range r.Branches() { for _, branch := range repo.Branches() {
status := r.BranchStatus(branch) status := repo.BranchStatus(branch)
if status == "" { if status == "" {
status = green("ok") 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))) 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 { func buildTree(root string, repos []Printable) *Node {
tree := Root(root) tree := Root(root)
for _, r := range repos { for _, repo := range repos {
path := strings.TrimPrefix(r.Path(), root) path := strings.TrimPrefix(repo.Path(), root)
path = strings.Trim(path, string(filepath.Separator)) path = strings.Trim(path, string(filepath.Separator))
subs := strings.Split(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 not, add it to node's children and move to next fragment.
// If it does, just move to the next fragment. // If it does, just move to the next fragment.
node := tree node := tree
for i, sub := range subs { for idx, sub := range subs {
child := node.GetChild(sub) child := node.GetChild(sub)
if child == nil { if child == nil {
node = node.Add(sub) node = node.Add(sub)
// If that's the last fragment, it's a tree leaf and needs a *Repo attached. // If that's the last fragment, it's a tree leaf and needs a *Repo attached.
if i == len(subs)-1 { if idx == len(subs)-1 {
node.repo = r node.repo = repo
} }
continue continue
@@ -120,28 +120,28 @@ func buildTree(root string, repos []Printable) *Node {
// printTree renders the repo tree by recursively traversing the tree nodes. // 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. // 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 { if node.children == nil {
tp.SetValue(printLeaf(node)) tree.SetValue(printLeaf(node))
} }
for _, child := range node.children { for _, child := range node.children {
branch := tp.AddBranch(child.val) branch := tree.AddBranch(child.val)
p.printTree(child, branch) p.printTree(child, branch)
} }
} }
func printLeaf(node *Node) string { 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. // 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. // 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")) return fmt.Sprintf("%s %s", node.val, red("error"))
} }
current := r.BranchStatus(r.Current()) current := repo.BranchStatus(repo.Current())
worktree := r.WorkTreeStatus() worktree := repo.WorkTreeStatus()
if worktree != "" { if worktree != "" {
worktree = fmt.Sprintf("[ %s ]", worktree) worktree = fmt.Sprintf("[ %s ]", worktree)
@@ -150,13 +150,13 @@ func printLeaf(node *Node) string {
var str strings.Builder var str strings.Builder
if worktree == "" && current == "" { 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 { } 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() { for _, branch := range repo.Branches() {
status := r.BranchStatus(branch) status := repo.BranchStatus(branch)
if status == "" { if status == "" {
status = green("ok") status = green("ok")
} }