Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,5 @@ A few possibilities listed below, none of this is promised or scheduled.
“why listed” overlay.
- **Submodules and worktrees** — scan or label linked worktrees and submodules explicitly instead of treating them
only as nested `.git` dirs.
- **Parallel scan** — configurable worker count for status/branch checks, plus clearer cancel behaviour while a
scan is running.
- **Configurable diff** — options such as ignore whitespace or word diff, driven from config, for the Diff pane.
- **Safer delete housekeeping** — dry-run delete, or move to Trash on macOS instead of only recursive delete.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/charmbracelet/bubbletea v1.3.10
github.com/charmbracelet/lipgloss v1.1.0
github.com/go-git/go-git/v5 v5.18.0
github.com/karrick/godirwalk v1.17.0
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/termenv v0.16.0
github.com/urfave/cli/v3 v3.8.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kevinburke/ssh_config v1.6.0 h1:J1FBfmuVosPHf5GRdltRLhPJtJpTlMdKTBjRgTaQBFY=
github.com/kevinburke/ssh_config v1.6.0/go.mod h1:q2RIzfka+BXARoNexmF9gkxEX7DmvbW9P4hIVx2Kg4M=
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
Expand Down
138 changes: 89 additions & 49 deletions scanner/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,120 @@ package scanner

import (
"context"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"slices"
"syscall"

"github.com/karrick/godirwalk"
"golang.org/x/sync/errgroup"
)

func skip(needle string, haystack []string) bool {
return slices.Contains(haystack, needle)
}

func isGitMetadataDir(path string, d fs.DirEntry) (bool, error) {
if d.Name() != ".git" {
return false, nil
}
if d.IsDir() {
return true, nil
}
if d.Type()&os.ModeSymlink != 0 {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.IsDir(), nil
}
return false, nil
}

// walkone descends a single directory tree looking for git repos.
// onRepoFound is called for each discovered repo (may be concurrent across roots); nil is safe.
func walkone(ctx context.Context, dir string, config *Config, results chan string, onRepoFound func(string)) error {
err := godirwalk.Walk(dir, &godirwalk.Options{
Unsorted: true,
ScratchBuffer: make([]byte, godirwalk.MinimumScratchBufferSize),
FollowSymbolicLinks: config.FollowSymlinks,
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
patherr, ok := err.(*os.PathError)
if ok {
switch patherr.Unwrap().Error() {
case "no such file or directory":
// might be symlink pointing to non-existent file
return godirwalk.SkipNode

case "too many levels of symbolic links":
// skip invalid symlinks
return godirwalk.SkipNode
}
var walkDirFn fs.WalkDirFunc
walkDirFn = func(path string, d fs.DirEntry, err error) error {
if err != nil {
var pathErr *os.PathError
if errors.As(err, &pathErr) && errors.Is(pathErr.Err, os.ErrNotExist) {
return nil
}
if errors.Is(err, os.ErrNotExist) {
return nil
}
if errors.Is(err, syscall.ELOOP) {
return nil
}
log.Printf("ERROR: %s: %v", path, err)
return godirwalk.Halt
},
Callback: func(path string, ent *godirwalk.Dirent) error {
return err
}

// early exit?
select {
case <-ctx.Done():
return filepath.SkipDir
default:
}

select {
case <-ctx.Done():
return filepath.SkipDir
default:
}

// process all the SkipThis rules first
if skip(path, config.ScanDirs.Exclude) {
return filepath.SkipDir
}

if skip(path, config.ScanDirs.Exclude) {
return godirwalk.SkipThis
}
if ent.IsSymlink() && !config.FollowSymlinks {
return godirwalk.SkipThis
}

// then process non-matching rules which still descend
if d.Type()&os.ModeSymlink != 0 && !config.FollowSymlinks {
return nil
}

if ent.Name() != ".git" {
return nil
}
isDir, _ := ent.IsDirOrSymlinkToDir()
if !isDir {
ok, metaErr := isGitMetadataDir(path, d)
if metaErr != nil {
if errors.Is(metaErr, os.ErrNotExist) || errors.Is(metaErr, syscall.ELOOP) {
return nil
}

log.Printf("ERROR: %s: %v", path, metaErr)
return metaErr
}
if ok {
repo := filepath.Dir(path)
if onRepoFound != nil {
onRepoFound(repo)
}
results <- repo
return godirwalk.SkipThis // don't descend further
},
})
return err
return filepath.SkipDir
}

if config.FollowSymlinks && d.Type()&os.ModeSymlink != 0 {
fi, statErr := os.Stat(path)
if statErr != nil {
if errors.Is(statErr, os.ErrNotExist) || errors.Is(statErr, syscall.ELOOP) {
return nil
}
log.Printf("ERROR: %s: %v", path, statErr)
return statErr
}
if fi.IsDir() {
entries, rdErr := os.ReadDir(path)
if rdErr != nil {
if errors.Is(rdErr, os.ErrNotExist) || errors.Is(rdErr, syscall.ELOOP) {
return nil
}
log.Printf("ERROR: %s: %v", path, rdErr)
return rdErr
}
for _, ent := range entries {
child := filepath.Join(path, ent.Name())
if werr := filepath.WalkDir(child, walkDirFn); werr != nil {
return werr
}
}
}
}

return nil
}

return filepath.WalkDir(dir, walkDirFn)
}

// Walk finds all git repositories in the directories specified in config.
Expand All @@ -84,10 +124,10 @@ func Walk(ctx context.Context, config *Config, results chan string, onRepoFound
ctx, cancel := context.WithCancel(ctx)
defer cancel()

var errors errgroup.Group
var eg errgroup.Group
for i := range config.ScanDirs.Include {
j := i // copy loop variable
errors.Go(func() error {
eg.Go(func() error {
err := walkone(ctx, config.ScanDirs.Include[j], config, results, onRepoFound)
if err == filepath.SkipDir {
cancel()
Expand All @@ -97,7 +137,7 @@ func Walk(ctx context.Context, config *Config, results chan string, onRepoFound
return nil
})
}
err := errors.Wait()
err := eg.Wait()
close(results)
return err
}
91 changes: 91 additions & 0 deletions scanner/multi_git_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package scanner

import (
"sort"
"sync"
)

// MultiGitStatus holds per-repository scan results. The zero value is usable:
// reads treat a nil receiver as empty; the first AddResult or Set allocates
// the inner map. Do not copy a non-zero MultiGitStatus (it contains a sync.Mutex).
type MultiGitStatus struct {
mu sync.Mutex
m map[string]RepoStatus
}

// NewMultiGitStatus returns an empty result set ready for concurrent AddResult calls.
func NewMultiGitStatus() *MultiGitStatus {
return &MultiGitStatus{m: make(map[string]RepoStatus)}
}

// AddResult records a dirty or diverged repository; safe for concurrent use.
func (m *MultiGitStatus) AddResult(path string, rs RepoStatus) {
if m == nil {
return
}
m.mu.Lock()
defer m.mu.Unlock()
if m.m == nil {
m.m = make(map[string]RepoStatus)
}
m.m[path] = rs
}

// Set replaces status for path (typically the UI thread after a scan completes).
func (m *MultiGitStatus) Set(path string, rs RepoStatus) {
if m == nil {
return
}
m.mu.Lock()
defer m.mu.Unlock()
if m.m == nil {
m.m = make(map[string]RepoStatus)
}
m.m[path] = rs
}

// Delete removes path from the set.
func (m *MultiGitStatus) Delete(path string) {
if m == nil {
return
}
m.mu.Lock()
defer m.mu.Unlock()
delete(m.m, path)
}

// Get returns status for path, if present.
func (m *MultiGitStatus) Get(path string) (RepoStatus, bool) {
if m == nil {
return RepoStatus{}, false
}
m.mu.Lock()
defer m.mu.Unlock()
rs, ok := m.m[path]
return rs, ok
}

// Len returns the number of repositories recorded.
func (m *MultiGitStatus) Len() int {
if m == nil {
return 0
}
m.mu.Lock()
defer m.mu.Unlock()
return len(m.m)
}

// SortedRepoPaths returns repository paths in stable alphabetical order.
func (m *MultiGitStatus) SortedRepoPaths() []string {
if m == nil {
return nil
}
m.mu.Lock()
paths := make([]string, 0, len(m.m))
for r := range m.m {
paths = append(paths, r)
}
m.mu.Unlock()
sort.Strings(paths)
return paths
}
Loading
Loading