6 Commits

Author SHA1 Message Date
cacdbf673f update to use /Users/tai 2025-08-10 15:15:50 -07:00
Tai Groot
991985ab4e update modeperm for directory creation on clone 2024-09-16 12:12:04 -07:00
417cf943fa fixup help text 2024-09-15 13:08:02 -07:00
1030a8f3a9 add cloning 2024-09-15 13:01:55 -07:00
f0c6f3906a fix print statements 2024-02-02 16:28:41 -08:00
01c736b54e fixup clone error 2024-02-02 16:24:06 -08:00
7 changed files with 61 additions and 32 deletions

1
cmd/mg/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
main

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"sync" "sync"
git "github.com/go-git/go-git/v5" git "github.com/go-git/go-git/v5"
@@ -37,17 +38,22 @@ var (
mutex := sync.Mutex{} mutex := sync.Mutex{}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(conf.Repos)) wg.Add(len(conf.Repos))
for i := 0; i < jobs; i++ { cloneFunc := func() {
go func() {
for repo := range repoChan { for repo := range repoChan {
_, err := git.PlainOpenWithOptions(repo.Remote, &(git.PlainOpenOptions{DetectDotGit: true})) _, err := git.PlainOpenWithOptions(repo.Path, &(git.PlainOpenOptions{DetectDotGit: true}))
if err == nil { if err == nil {
log.Printf("already cloned: %s\n", repo.Path) log.Printf("already cloned: %s\n", repo.Path)
mutex.Lock() mutex.Lock()
alreadyCloned++ alreadyCloned++
mutex.Unlock() mutex.Unlock()
wg.Done()
continue
} else if err == git.ErrRepositoryNotExists { } else if err == git.ErrRepositoryNotExists {
log.Printf("attempting clone: %s\n", repo) log.Printf("attempting clone: %s\n", repo.Path)
parentPath := filepath.Dir(repo.Path)
if _, err := os.Stat(parentPath); err != nil {
os.MkdirAll(parentPath, os.ModeDir|os.ModePerm)
}
_, err = git.PlainClone(repo.Path, false, &git.CloneOptions{ _, err = git.PlainClone(repo.Path, false, &git.CloneOptions{
URL: repo.Remote, URL: repo.Remote,
}) })
@@ -59,24 +65,28 @@ var (
wg.Done() wg.Done()
continue continue
} }
fmt.Printf("successfully cloned %s\n", repo) fmt.Printf("successfully cloned %s\n", repo.Path)
wg.Done() wg.Done()
continue continue
} else { } else {
mutex.Lock() mutex.Lock()
errs = append(errs, RepoError{Error: err, Repo: repo.Path}) errs = append(errs, RepoError{Error: err, Repo: repo.Path})
mutex.Unlock() mutex.Unlock()
log.Printf("clone failed for %s: %v\n", repo, err) log.Printf("clone failed for %s: %v\n", repo.Path, err)
wg.Done() wg.Done()
continue continue
} }
} }
}()
} }
for i := 0; i < jobs; i++ {
go cloneFunc()
}
fmt.Println(len(conf.Repos))
for _, repo := range conf.Repos { for _, repo := range conf.Repos {
repoChan <- repo repoChan <- repo
} }
close(repoChan) close(repoChan)
fmt.Println("waiting...")
wg.Wait() wg.Wait()
for _, err := range errs { for _, err := range errs {
log.Printf("error pulling %s: %s\n", err.Repo, err.Error) log.Printf("error pulling %s: %s\n", err.Repo, err.Error)

View File

@@ -3,6 +3,7 @@ package cmd
import ( import (
"log" "log"
"os" "os"
"strings"
"github.com/taigrr/mg/parse" "github.com/taigrr/mg/parse"
) )
@@ -27,5 +28,11 @@ func GetConfig() parse.MGConfig {
} }
} }
} }
homeDir, _ := os.UserHomeDir()
for i, repo := range conf.Repos {
if strings.HasPrefix(repo.Path, "$HOME") {
conf.Repos[i].Path = strings.Replace(repo.Path, "$HOME", homeDir, 1)
}
}
return conf return conf
} }

View File

@@ -15,7 +15,7 @@ var (
jobs int jobs int
pullCmd = &cobra.Command{ pullCmd = &cobra.Command{
Use: "pull", Use: "pull",
Short: "add current path to list of repos", Short: "update all git repos specified in config",
Run: func(_ *cobra.Command, args []string) { Run: func(_ *cobra.Command, args []string) {
type RepoError struct { type RepoError struct {
Error error Error error

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strings"
git "github.com/go-git/go-git/v5" git "github.com/go-git/go-git/v5"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -52,6 +53,16 @@ var registerCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
path = newPath.Filesystem.Root() path = newPath.Filesystem.Root()
homeDir, err := os.UserHomeDir()
if err != nil {
log.Println("Unable to get home directory")
os.Exit(1)
}
if strings.HasPrefix(path, homeDir) {
path = "$HOME" + path[len(homeDir):]
}
for _, v := range conf.Repos { for _, v := range conf.Repos {
if v.Path == path { if v.Path == path {
fmt.Printf("repo %s already registered\n", path) fmt.Printf("repo %s already registered\n", path)

2
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/taigrr/mg module github.com/taigrr/mg
go 1.21 go 1.23.1
require ( require (
github.com/go-git/go-git/v5 v5.11.0 github.com/go-git/go-git/v5 v5.11.0

View File

@@ -32,9 +32,9 @@ func (m MRConfig) ToMGConfig() MGConfig {
mgconf := MGConfig(m) mgconf := MGConfig(m)
for i, repo := range mgconf.Repos { for i, repo := range mgconf.Repos {
checkout := repo.Remote checkout := repo.Remote
if strings.HasPrefix(checkout, "git clone '") { if after, ok := strings.CutPrefix(checkout, "git clone '"); ok {
// git clone 'git@bitbucket.org:taigrr/mg.git' 'mg' // git clone 'git@bitbucket.org:taigrr/mg.git' 'mg'
remote := strings.TrimPrefix(checkout, "git clone '") remote := after
sp := strings.Split(remote, "' '") sp := strings.Split(remote, "' '")
remote = sp[0] remote = sp[0]
mgconf.Repos[i].Remote = remote mgconf.Repos[i].Remote = remote