made directory search top level for efficiency
8f2f4161
1 file(s) · +16 −21
| 8 | 8 | "os/signal" |
|
| 9 | 9 | "path/filepath" |
|
| 10 | 10 | "syscall" |
|
| 11 | - | "time" |
|
| 12 | 11 | ||
| 13 | 12 | "github.com/AlecAivazis/survey/v2" |
|
| 14 | 13 | "github.com/fatih/color" |
|
| 33 | 32 | func findGitRepos(root string) []Repo { |
|
| 34 | 33 | var repos []Repo |
|
| 35 | 34 | var scannedDirs int |
|
| 36 | - | lastUpdateTime := time.Now() |
|
| 37 | 35 | ||
| 38 | - | err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
|
| 39 | - | if err != nil { |
|
| 40 | - | return err |
|
| 36 | + | entries, err := os.ReadDir(root) |
|
| 37 | + | if err != nil { |
|
| 38 | + | fmt.Printf("Error reading directory %v: %v\n", root, err) |
|
| 39 | + | return repos |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | for _, entry := range entries { |
|
| 43 | + | if !entry.IsDir() { |
|
| 44 | + | continue |
|
| 41 | 45 | } |
|
| 42 | 46 | ||
| 43 | 47 | scannedDirs++ |
|
| 48 | + | fmt.Printf("\rScanned %d directories...", scannedDirs) |
|
| 44 | 49 | ||
| 45 | - | // Update progress every second |
|
| 46 | - | if time.Since(lastUpdateTime) > time.Second { |
|
| 47 | - | fmt.Printf("\rScanned %d directories...", scannedDirs) |
|
| 48 | - | lastUpdateTime = time.Now() |
|
| 49 | - | } |
|
| 50 | + | dirPath := filepath.Join(root, entry.Name()) |
|
| 51 | + | gitDir := filepath.Join(dirPath, ".git") |
|
| 50 | 52 | ||
| 51 | - | if info.IsDir() && info.Name() == ".git" { |
|
| 52 | - | repoPath := filepath.Dir(path) |
|
| 53 | + | if _, err := os.Stat(gitDir); err == nil { |
|
| 53 | 54 | repos = append(repos, Repo{ |
|
| 54 | - | Path: repoPath, |
|
| 55 | - | Name: filepath.Base(repoPath), |
|
| 55 | + | Path: dirPath, |
|
| 56 | + | Name: entry.Name(), |
|
| 56 | 57 | }) |
|
| 57 | - | return filepath.SkipDir |
|
| 58 | 58 | } |
|
| 59 | - | return nil |
|
| 60 | - | }) |
|
| 59 | + | } |
|
| 61 | 60 | ||
| 62 | 61 | // Clear the progress line and print final count |
|
| 63 | 62 | fmt.Printf("\rScanned %d directories. Found %d Git repositories.\n", scannedDirs, len(repos)) |
|
| 64 | - | ||
| 65 | - | if err != nil { |
|
| 66 | - | fmt.Printf("Error walking the path %v: %v\n", root, err) |
|
| 67 | - | } |
|
| 68 | 63 | ||
| 69 | 64 | return repos |
|
| 70 | 65 | } |
|