|
| 1 | +package jobs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime" |
| 6 | + |
| 7 | + "charm.land/log/v2" |
| 8 | + "github.com/charmbracelet/soft-serve/git" |
| 9 | + "github.com/charmbracelet/soft-serve/pkg/backend" |
| 10 | + "github.com/charmbracelet/soft-serve/pkg/config" |
| 11 | + "github.com/charmbracelet/soft-serve/pkg/sync" |
| 12 | +) |
| 13 | + |
| 14 | +func init() { |
| 15 | + Register("git-gc", gitGC{}) |
| 16 | +} |
| 17 | + |
| 18 | +type gitGC struct{} |
| 19 | + |
| 20 | +func (g gitGC) Spec(ctx context.Context) string { |
| 21 | + cfg := config.FromContext(ctx) |
| 22 | + if cfg.Jobs.GitGC != "" { |
| 23 | + return cfg.Jobs.GitGC |
| 24 | + } |
| 25 | + return "" |
| 26 | +} |
| 27 | + |
| 28 | +func (g gitGC) Func(ctx context.Context) func() { |
| 29 | + b := backend.FromContext(ctx) |
| 30 | + logger := log.FromContext(ctx).WithPrefix("jobs.gitgc") |
| 31 | + return func() { |
| 32 | + repos, err := b.Repositories(ctx) |
| 33 | + if err != nil { |
| 34 | + logger.Error("error getting repositories", "err", err) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + wq := sync.NewWorkPool(ctx, runtime.GOMAXPROCS(0), |
| 39 | + sync.WithWorkPoolLogger(logger.Errorf), |
| 40 | + ) |
| 41 | + |
| 42 | + logger.Debug("cleaning git garbage") |
| 43 | + for _, repo := range repos { |
| 44 | + r, err := repo.Open() |
| 45 | + if err != nil { |
| 46 | + logger.Error("error opening repository", "repo", repo.Name(), "err", err) |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + name := repo.Name() |
| 51 | + wq.Add(name, func() { |
| 52 | + cmd := git.NewCommand("gc").WithContext(ctx) |
| 53 | + if _, err := cmd.RunInDir(r.Path); err != nil { |
| 54 | + logger.Error("error running git remote update", "repo", name, "err", err) |
| 55 | + } |
| 56 | + |
| 57 | + }) |
| 58 | + |
| 59 | + // TODO: clean up lfs |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | +} |
0 commit comments