gitpb/rill.go

102 lines
2.5 KiB
Go
Raw Normal View History

package gitpb
// runs git, parses output
// types faster than you can
import (
"errors"
2024-12-06 01:50:28 -06:00
sync "sync"
2024-12-06 01:50:28 -06:00
"github.com/destel/rill"
"github.com/go-cmd/cmd"
"go.wit.com/log"
)
var ErrorMissingGitConfig error = errors.New("missing .git/config")
var ErrorGitPullOnLocal error = errors.New("git pull on local only branch")
2024-12-06 01:50:28 -06:00
var ErrorGitPullOnDirty error = errors.New("cannot git pull on dirty repo")
2025-01-19 16:08:06 -06:00
func (repo *Repo) GitPull() (*cmd.Status, error) {
2024-12-06 01:50:28 -06:00
/*
2025-01-19 16:08:06 -06:00
currentName := repo.GetCurrentBranchName()
if repo.IsOnlyLocalTag(currentName) {
var result cmd.Status
result.Exit = 21
result.Error = ErrorGitPullOnLocal
// log.Info("git pull skipped on local only branch", repo.GetGoPath())
return result
2024-12-06 01:50:28 -06:00
}
*/
2025-01-19 16:08:06 -06:00
var cmd []string
cmd = append(cmd, "git", "pull")
return repo.RunVerbose(cmd)
2024-12-06 01:50:28 -06:00
}
// rill is awesome. long live rill
// attempt scan with rill
2025-01-19 16:08:06 -06:00
func (all *Repos) RillGitPull(part1 int, part2 int) map[*Repo]*cmd.Status {
2024-12-06 01:50:28 -06:00
var lock sync.Mutex
2025-01-19 16:08:06 -06:00
var allerr map[*Repo]*cmd.Status
allerr = make(map[*Repo]*cmd.Status)
2024-12-06 01:50:28 -06:00
// Convert a slice of user IDs into a channel
ids := rill.FromSlice(all.Repos, nil)
var counter int
// Read users from the API.
// Concurrency = 20
dirs := rill.Map(ids, part1, func(id *Repo) (*Repo, error) {
return id, nil
})
rill.ForEach(dirs, part2, func(repo *Repo) error {
counter += 1
if repo.CheckDirty() {
// log.Info("git pull skipped on dirty repo", repo.GoPath)
2025-01-19 16:08:06 -06:00
result := new(cmd.Status)
2024-12-06 01:50:28 -06:00
result.Error = ErrorGitPullOnDirty
lock.Lock()
defer lock.Unlock()
allerr[repo] = result
} else {
// todo: sort out what the hell is wrong with my code
// something seems to be trampling things
/*
var dur time.Duration
dur = time.Duration((1+rand.Intn(50))*20) * time.Millisecond
time.Sleep(dur)
*/
2025-01-19 16:08:06 -06:00
var result *cmd.Status
result, _ = repo.GitPull()
2025-01-17 06:04:36 -06:00
log.Info("git pull", repo.GetGoPath())
2025-01-19 16:08:06 -06:00
// log.Info(strings.Join(result.Stdout, "\n"))
2024-12-06 01:50:28 -06:00
lock.Lock()
defer lock.Unlock()
allerr[repo] = result
}
return nil
})
// for r, err := range allerr {
// log.Info("git pull error:", r.GoPath, err)
// }
return allerr
}
2024-12-17 00:00:49 -06:00
func (repo *Repo) GitPullRealtime() cmd.Status {
currentName := repo.GetCurrentBranchName()
if repo.IsOnlyLocalTag(currentName) {
var result cmd.Status
result.Exit = 21
result.Error = ErrorGitPullOnLocal
// log.Info("git pull skipped on local only branch", repo.GoPath)
return result
}
var cmd []string
cmd = append(cmd, "git", "pull")
r := repo.RunRealtime(cmd)
return r
}