feat(cmd): implement fetch and status commands, update deps to go1.26.1

- Implement fetch command: fetches all registered repos without merging,
  supports parallel execution via -j/--jobs flag
- Implement status command: shows uncommitted changes across all repos
  with per-file-type counts, supports parallel execution
- Update Go to 1.26.1
- Update go-git v5.16.5 -> v5.17.0, go-crypto v1.3.0 -> v1.4.0,
  go-billy v5.7.0 -> v5.8.0, x/net v0.50.0 -> v0.51.0
- Fix unregister command description (said 'add' instead of 'remove')
- Fix goimports formatting in mgconf_test.go
This commit is contained in:
2026-03-07 07:03:49 +00:00
parent 9d4ac7bc47
commit 348b87702d
6 changed files with 238 additions and 29 deletions

View File

@@ -2,18 +2,90 @@ package cmd
import (
"fmt"
"log"
"os"
"sync"
git "github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
)
var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "fetch all git repos without merging",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("fetch called")
Run: func(_ *cobra.Command, args []string) {
type RepoError struct {
Error error
Repo string
}
if jobs < 1 {
log.Println("jobs must be greater than 0")
os.Exit(1)
}
conf := GetConfig()
if len(args) > 0 {
log.Println("too many arguments")
os.Exit(1)
}
repoChan := make(chan string, len(conf.Repos))
var (
errs []RepoError
alreadyFetched int
mutex sync.Mutex
wg sync.WaitGroup
)
wg.Add(len(conf.Repos))
for i := 0; i < jobs; i++ {
go func() {
for repo := range repoChan {
log.Printf("attempting fetch: %s\n", repo)
r, err := git.PlainOpenWithOptions(repo, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo})
mutex.Unlock()
log.Printf("fetch failed for %s: %v\n", repo, err)
wg.Done()
continue
}
err = r.Fetch(&git.FetchOptions{})
if err == git.NoErrAlreadyUpToDate {
mutex.Lock()
alreadyFetched++
mutex.Unlock()
fmt.Printf("repo %s: already up to date\n", repo)
wg.Done()
continue
} else if err != nil {
mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo})
mutex.Unlock()
log.Printf("fetch failed for %s: %v\n", repo, err)
wg.Done()
continue
}
fmt.Printf("successfully fetched %s\n", repo)
wg.Done()
}
}()
}
for _, repo := range conf.Repos {
repoChan <- repo.Path
}
close(repoChan)
wg.Wait()
for _, err := range errs {
log.Printf("error fetching %s: %s\n", err.Repo, err.Error)
}
lenErrs := len(errs)
fmt.Println()
fmt.Printf("successfully fetched %d/%d repos\n", len(conf.Repos)-lenErrs, len(conf.Repos))
fmt.Printf("%d repos already up to date\n", alreadyFetched)
fmt.Printf("failed to fetch %d/%d repos\n", lenErrs, len(conf.Repos))
},
}
func init() {
rootCmd.AddCommand(fetchCmd)
fetchCmd.Flags().IntVarP(&jobs, "jobs", "j", 1, "number of jobs to run in parallel")
}

View File

@@ -2,19 +2,156 @@ package cmd
import (
"fmt"
"log"
"os"
"sort"
"sync"
git "github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
)
// statusCmd represents the status command
type repoStatus struct {
Path string
Modified int
Added int
Deleted int
Renamed int
Copied int
Untrack int
Clean bool
}
var statusCmd = &cobra.Command{
Use: "status",
Short: "get the combined git status for all git repos",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("status called")
Run: func(_ *cobra.Command, args []string) {
type RepoError struct {
Error error
Repo string
}
if jobs < 1 {
log.Println("jobs must be greater than 0")
os.Exit(1)
}
conf := GetConfig()
if len(args) > 0 {
log.Println("too many arguments")
os.Exit(1)
}
repoChan := make(chan string, len(conf.Repos))
var (
errs []RepoError
statuses []repoStatus
mutex sync.Mutex
wg sync.WaitGroup
)
wg.Add(len(conf.Repos))
for i := 0; i < jobs; i++ {
go func() {
for repo := range repoChan {
r, err := git.PlainOpenWithOptions(repo, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo})
mutex.Unlock()
wg.Done()
continue
}
w, err := r.Worktree()
if err != nil {
mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo})
mutex.Unlock()
wg.Done()
continue
}
st, err := w.Status()
if err != nil {
mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo})
mutex.Unlock()
wg.Done()
continue
}
rs := repoStatus{Path: repo, Clean: st.IsClean()}
for _, s := range st {
code := s.Worktree
if code == git.Unmodified {
code = s.Staging
}
switch code {
case git.Modified:
rs.Modified++
case git.Added:
rs.Added++
case git.Deleted:
rs.Deleted++
case git.Renamed:
rs.Renamed++
case git.Copied:
rs.Copied++
case git.Untracked:
rs.Untrack++
}
}
mutex.Lock()
statuses = append(statuses, rs)
mutex.Unlock()
wg.Done()
}
}()
}
for _, repo := range conf.Repos {
repoChan <- repo.Path
}
close(repoChan)
wg.Wait()
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].Path < statuses[j].Path
})
dirtyCount := 0
for _, rs := range statuses {
if rs.Clean {
continue
}
dirtyCount++
fmt.Printf("%s:\n", rs.Path)
if rs.Modified > 0 {
fmt.Printf(" modified: %d\n", rs.Modified)
}
if rs.Added > 0 {
fmt.Printf(" added: %d\n", rs.Added)
}
if rs.Deleted > 0 {
fmt.Printf(" deleted: %d\n", rs.Deleted)
}
if rs.Renamed > 0 {
fmt.Printf(" renamed: %d\n", rs.Renamed)
}
if rs.Copied > 0 {
fmt.Printf(" copied: %d\n", rs.Copied)
}
if rs.Untrack > 0 {
fmt.Printf(" untracked: %d\n", rs.Untrack)
}
}
for _, err := range errs {
log.Printf("error reading %s: %s\n", err.Repo, err.Error)
}
fmt.Println()
fmt.Printf("%d/%d repos have uncommitted changes\n", dirtyCount, len(conf.Repos))
if len(errs) > 0 {
fmt.Printf("failed to read %d/%d repos\n", len(errs), len(conf.Repos))
}
},
}
func init() {
rootCmd.AddCommand(statusCmd)
statusCmd.Flags().IntVarP(&jobs, "jobs", "j", 1, "number of jobs to run in parallel")
}

View File

@@ -11,7 +11,7 @@ import (
// unregisterCmd represents the unregister command
var unregisterCmd = &cobra.Command{
Use: "unregister",
Short: "add current path to list of repos",
Short: "remove current path from list of repos",
Run: func(_ *cobra.Command, args []string) {
conf := GetConfig()
path, err := os.Getwd()