git-repo-updater/internal/repositories/update.go
Oliver Davies d98e648dca
All checks were successful
/ check (push) Successful in 4s
Update module paths to Forgejo
2025-08-01 09:36:37 +01:00

60 lines
1.3 KiB
Go

package repositories
import (
"fmt"
"os"
"os/exec"
"slices"
"strings"
"code.oliverdavies.uk/opdavies/git-repo-updater/internal/config"
"code.oliverdavies.uk/opdavies/git-repo-updater/internal/utils"
)
func Update(repositoryPath string) error {
cfg, err := config.Load()
if err != nil {
// TODO
}
repositoryPath = strings.TrimSuffix(repositoryPath, "/.git")
expandedIgnored := make([]string, 0, len(cfg.IgnoredRepos))
for _, ignored := range cfg.IgnoredRepos {
if expanded, err := utils.ExpandPath(ignored); err == nil {
expandedIgnored = append(expandedIgnored, expanded)
}
}
if slices.Contains(expandedIgnored, repositoryPath) {
fmt.Printf("Skipping %s as it's ignored\n", repositoryPath)
return nil
}
err = os.Chdir(repositoryPath)
if err != nil {
return fmt.Errorf("failed to change directory to %s: %w", repositoryPath, err)
}
fmt.Printf("Updating %s\n", repositoryPath)
commands := [][]string{
{"git", "fetch", "--all", "--jobs=4", "--progress", "--prune"},
{"git", "pull", "--rebase"},
}
for _, args := range commands {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s failed: %w", strings.Join(args, " "), err)
}
}
return nil
}