Skip to content

Instantly share code, notes, and snippets.

@strboul
Created July 8, 2024 11:08
Show Gist options
  • Save strboul/898a18c1e42e13286ddc1f3b3d9fe044 to your computer and use it in GitHub Desktop.
Save strboul/898a18c1e42e13286ddc1f3b3d9fe044 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
func main() {
cwd, cwdErr := os.Getwd()
if cwdErr != nil {
fmt.Printf("Error when getting cwd %s\n", cwdErr)
return
}
inputDir := flag.String("dir", cwd, "directory")
flag.Parse()
fmt.Println(*inputDir)
var directories []string
maxDepth := 1
root := *inputDir
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Calculate the current depth by counting separators in the path.
depth := strings.Count(path[len(root):], string(filepath.Separator))
if depth > maxDepth {
return filepath.SkipDir // Skip processing deeper levels
}
if info.IsDir() && depth == maxDepth {
directories = append(directories, path)
}
return nil
})
if err != nil {
fmt.Printf("Error directories %s\n", err)
return
}
//
// git status
//
var wg sync.WaitGroup
errorsCh := make(chan error)
resultsCh := make(chan string)
for _, dir := range directories {
wg.Add(1)
go func(dir string) {
defer wg.Done()
cmd := exec.Command("git", "-C", dir, "status", "--porcelain")
// cmd := exec.Command("git", "-C", dir, "fetch") // XXX fetch
output, err := cmd.CombinedOutput()
if err != nil {
errorsCh <- err
return
}
resultsCh <- fmt.Sprintf("%s %s", dir, strings.ReplaceAll(string(output), "\n", ""))
}(dir)
}
go func() {
for result := range resultsCh {
fmt.Println("*", result)
}
}()
go func() {
for err := range errorsCh {
fmt.Println("Err ::", err)
}
}()
wg.Wait()
fmt.Println("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment