6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 16:49:43 +00:00

Replace the godirwalk package with a native implementation, replace pathpkg with filepath for better cross-platform support

This commit is contained in:
Grzegorz Dlugoszewski
2025-08-24 12:39:27 +02:00
parent ec99227420
commit 8a9002e678
4 changed files with 43 additions and 60 deletions

1
go.mod
View File

@@ -3,7 +3,6 @@ module git-get
go 1.24
require (
github.com/karrick/godirwalk v1.17.0
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.10.0

2
go.sum
View File

@@ -12,8 +12,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

View File

@@ -1,26 +1,19 @@
package git
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"github.com/karrick/godirwalk"
)
// Max number of concurrently running status loading workers.
const maxWorkers = 100
// errSkipNode is used as an error indicating that .git directory has been found.
// It's handled by ErrorsCallback to tell the WalkCallback to skip this dir.
var errSkipNode = errors.New(".git directory found, skipping this node")
var errDirNoAccess = errors.New("directory can't be accessed")
var errDirNotExist = errors.New("directory doesn't exist")
var errDirNoAccess = fmt.Errorf("directory can't be accessed")
var errDirNotExist = fmt.Errorf("directory doesn't exist")
// Exists returns true if a directory exists. If it doesn't or the directory can't be accessed it returns an error.
func Exists(path string) (bool, error) {
@@ -30,10 +23,8 @@ func Exists(path string) (bool, error) {
return true, nil
}
if err != nil {
if os.IsNotExist(err) {
return false, fmt.Errorf("can't access %s: %w", path, errDirNotExist)
}
if os.IsNotExist(err) {
return false, fmt.Errorf("can't access %s: %w", path, errDirNotExist)
}
// Directory exists but can't be accessed
@@ -60,19 +51,43 @@ func NewRepoFinder(root string) *RepoFinder {
// Returns error if root repo path can't be found or accessed.
func (f *RepoFinder) Find() error {
if _, err := Exists(f.root); err != nil {
return err
return fmt.Errorf("failed to access root path: %w", err)
}
walkOpts := &godirwalk.Options{
ErrorCallback: f.errorCb,
Callback: f.walkCb,
// Use Unsorted to improve speed because repos will be processed by goroutines in a random order anyway.
Unsorted: true,
}
err := filepath.WalkDir(f.root, func(path string, d fs.DirEntry, err error) error {
// Handle walk errors
if err != nil {
// Skip permission errors but continue walking
if os.IsPermission(err) {
return nil // Skip this path but continue
}
return fmt.Errorf("failed to walk %s: %w", path, err)
}
// Only process directories
if !d.IsDir() {
return nil
}
// Case 1: We're looking at a .git directory itself
if d.Name() == dotgit {
parentPath := filepath.Dir(path)
f.addIfOk(parentPath)
return fs.SkipDir // Skip the .git directory contents
}
// Case 2: Check if this directory contains a .git subdirectory
gitPath := filepath.Join(path, dotgit)
if _, err := os.Stat(gitPath); err == nil {
f.addIfOk(path)
return fs.SkipDir // Skip this directory's contents since it's a repo
}
return nil // Continue walking
})
err := godirwalk.Walk(f.root, walkOpts)
if err != nil {
return err
return fmt.Errorf("failed to walk directory tree: %w", err)
}
if len(f.repos) == 0 {
@@ -131,41 +146,12 @@ func statusWorker(fetch bool, reposChan <-chan *Repo, statusChan chan<- *Status)
}
}
func (f *RepoFinder) walkCb(path string, ent *godirwalk.Dirent) error {
// Do not traverse .git directories
if ent.IsDir() && ent.Name() == dotgit {
f.addIfOk(path)
return errSkipNode
}
// Do not traverse directories containing a .git directory
if ent.IsDir() {
_, err := os.Stat(filepath.Join(path, dotgit))
if err == nil {
f.addIfOk(path)
return errSkipNode
}
}
return nil
}
// addIfOk adds the found repo to the repos slice if it can be opened.
func (f *RepoFinder) addIfOk(path string) {
// TODO: is the case below really correct? What if there's a race condition and the dir becomes unaccessible between finding it and opening?
// Open() should never return an error here. If a finder found a .git inside this dir, it means it could open and access it.
// If the dir was unaccessible, then it would have been skipped by the check in errorCb().
repo, err := Open(strings.TrimSuffix(path, dotgit))
// Open() should never return an error here since we already verified the .git directory exists.
// The path should already be the repository root (not the .git subdirectory).
repo, err := Open(path)
if err == nil {
f.repos = append(f.repos, repo)
}
}
func (f *RepoFinder) errorCb(_ string, err error) godirwalk.ErrorAction {
// Skip .git directory and directories we don't have permissions to access
// TODO: Will syscall.EACCES work on windows?
if errors.Is(err, errSkipNode) || errors.Is(err, syscall.EACCES) {
return godirwalk.SkipNode
}
return godirwalk.Halt
}

View File

@@ -6,7 +6,7 @@ import (
"fmt"
"os"
"os/exec"
pathpkg "path"
"path/filepath"
"strings"
)
@@ -45,7 +45,7 @@ func (c *Cmd) OnRepo(path string) *Cmd {
return c
}
insert := []string{"--work-tree", path, "--git-dir", pathpkg.Join(path, ".git")}
insert := []string{"--work-tree", path, "--git-dir", filepath.Join(path, ".git")}
// Insert into the args slice after the 1st element (https://github.com/golang/go/wiki/SliceTricks#insert)
c.cmd.Args = append(c.cmd.Args[:1], append(insert, c.cmd.Args[1:]...)...)